Update for g_type_class_add_private() deprecation in recent GLib
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / gstrtpbin.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@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 /**
21  * SECTION:element-rtpbin
22  * @see_also: rtpjitterbuffer, rtpsession, rtpptdemux, rtpssrcdemux
23  *
24  * RTP bin combines the functions of #GstRtpSession, #GstRtpSsrcDemux,
25  * #GstRtpJitterBuffer and #GstRtpPtDemux in one element. It allows for multiple
26  * RTP sessions that will be synchronized together using RTCP SR packets.
27  *
28  * #GstRtpBin is configured with a number of request pads that define the
29  * functionality that is activated, similar to the #GstRtpSession element.
30  *
31  * To use #GstRtpBin as an RTP receiver, request a recv_rtp_sink_\%u pad. The session
32  * number must be specified in the pad name.
33  * Data received on the recv_rtp_sink_\%u pad will be processed in the #GstRtpSession
34  * manager and after being validated forwarded on #GstRtpSsrcDemux element. Each
35  * RTP stream is demuxed based on the SSRC and send to a #GstRtpJitterBuffer. After
36  * the packets are released from the jitterbuffer, they will be forwarded to a
37  * #GstRtpPtDemux element. The #GstRtpPtDemux element will demux the packets based
38  * on the payload type and will create a unique pad recv_rtp_src_\%u_\%u_\%u on
39  * rtpbin with the session number, SSRC and payload type respectively as the pad
40  * name.
41  *
42  * To also use #GstRtpBin as an RTCP receiver, request a recv_rtcp_sink_\%u pad. The
43  * session number must be specified in the pad name.
44  *
45  * If you want the session manager to generate and send RTCP packets, request
46  * the send_rtcp_src_\%u pad with the session number in the pad name. Packet pushed
47  * on this pad contain SR/RR RTCP reports that should be sent to all participants
48  * in the session.
49  *
50  * To use #GstRtpBin as a sender, request a send_rtp_sink_\%u pad, which will
51  * automatically create a send_rtp_src_\%u pad. If the session number is not provided,
52  * the pad from the lowest available session will be returned. The session manager will modify the
53  * SSRC in the RTP packets to its own SSRC and wil forward the packets on the
54  * send_rtp_src_\%u pad after updating its internal state.
55  *
56  * #GstRtpBin can also demultiplex incoming bundled streams. The first
57  * #GstRtpSession will have a #GstRtpSsrcDemux element splitting the streams
58  * based on their SSRC and potentially dispatched to a different #GstRtpSession.
59  * Because retransmission SSRCs need to be merged with the corresponding media
60  * stream the #GstRtpBin::on-bundled-ssrc signal is emitted so that the
61  * application can find out to which session the SSRC belongs.
62  *
63  * The session manager needs the clock-rate of the payload types it is handling
64  * and will signal the #GstRtpSession::request-pt-map signal when it needs such a
65  * mapping. One can clear the cached values with the #GstRtpSession::clear-pt-map
66  * signal.
67  *
68  * Access to the internal statistics of rtpbin is provided with the
69  * get-internal-session property. This action signal gives access to the
70  * RTPSession object which further provides action signals to retrieve the
71  * internal source and other sources.
72  *
73  * #GstRtpBin also has signals (#GstRtpBin::request-rtp-encoder,
74  * #GstRtpBin::request-rtp-decoder, #GstRtpBin::request-rtcp-encoder and
75  * #GstRtpBin::request-rtp-decoder) to dynamically request for RTP and RTCP encoders
76  * and decoders in order to support SRTP. The encoders must provide the pads
77  * rtp_sink_\%u and rtp_src_\%u for RTP and rtcp_sink_\%u and rtcp_src_\%u for
78  * RTCP. The session number will be used in the pad name. The decoders must provide
79  * rtp_sink and rtp_src for RTP and rtcp_sink and rtcp_src for RTCP. The decoders will
80  * be placed before the #GstRtpSession element, thus they must support SSRC demuxing
81  * internally.
82  *
83  * #GstRtpBin has signals (#GstRtpBin::request-aux-sender and
84  * #GstRtpBin::request-aux-receiver to dynamically request an element that can be
85  * used to create or merge additional RTP streams. AUX elements are needed to
86  * implement FEC or retransmission (such as RFC 4588). An AUX sender must have one
87  * sink_\%u pad that matches the sessionid in the signal and it should have 1 or
88  * more src_\%u pads. For each src_%\u pad, a session will be made (if needed)
89  * and the pad will be linked to the session send_rtp_sink pad. Each session will
90  * then expose its source pad as send_rtp_src_\%u on #GstRtpBin.
91  * An AUX receiver has 1 src_\%u pad that much match the sessionid in the signal
92  * and 1 or more sink_\%u pads. A session will be made for each sink_\%u pad
93  * when the corresponding recv_rtp_sink_\%u pad is requested on #GstRtpBin.
94  *
95  * <refsect2>
96  * <title>Example pipelines</title>
97  * |[
98  * gst-launch-1.0 udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink_0 \
99  *     rtpbin ! rtptheoradepay ! theoradec ! xvimagesink
100  * ]| Receive RTP data from port 5000 and send to the session 0 in rtpbin.
101  * |[
102  * gst-launch-1.0 rtpbin name=rtpbin \
103  *         v4l2src ! videoconvert ! ffenc_h263 ! rtph263ppay ! rtpbin.send_rtp_sink_0 \
104  *                   rtpbin.send_rtp_src_0 ! udpsink port=5000                            \
105  *                   rtpbin.send_rtcp_src_0 ! udpsink port=5001 sync=false async=false    \
106  *                   udpsrc port=5005 ! rtpbin.recv_rtcp_sink_0                           \
107  *         audiotestsrc ! amrnbenc ! rtpamrpay ! rtpbin.send_rtp_sink_1                   \
108  *                   rtpbin.send_rtp_src_1 ! udpsink port=5002                            \
109  *                   rtpbin.send_rtcp_src_1 ! udpsink port=5003 sync=false async=false    \
110  *                   udpsrc port=5007 ! rtpbin.recv_rtcp_sink_1
111  * ]| Encode and payload H263 video captured from a v4l2src. Encode and payload AMR
112  * audio generated from audiotestsrc. The video is sent to session 0 in rtpbin
113  * and the audio is sent to session 1. Video packets are sent on UDP port 5000
114  * and audio packets on port 5002. The video RTCP packets for session 0 are sent
115  * on port 5001 and the audio RTCP packets for session 0 are sent on port 5003.
116  * RTCP packets for session 0 are received on port 5005 and RTCP for session 1
117  * is received on port 5007. Since RTCP packets from the sender should be sent
118  * as soon as possible and do not participate in preroll, sync=false and
119  * async=false is configured on udpsink
120  * |[
121  * gst-launch-1.0 -v rtpbin name=rtpbin                                          \
122  *     udpsrc caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H263-1998" \
123  *             port=5000 ! rtpbin.recv_rtp_sink_0                                \
124  *         rtpbin. ! rtph263pdepay ! ffdec_h263 ! xvimagesink                    \
125  *      udpsrc port=5001 ! rtpbin.recv_rtcp_sink_0                               \
126  *      rtpbin.send_rtcp_src_0 ! udpsink port=5005 sync=false async=false        \
127  *     udpsrc caps="application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)AMR,encoding-params=(string)1,octet-align=(string)1" \
128  *             port=5002 ! rtpbin.recv_rtp_sink_1                                \
129  *         rtpbin. ! rtpamrdepay ! amrnbdec ! alsasink                           \
130  *      udpsrc port=5003 ! rtpbin.recv_rtcp_sink_1                               \
131  *      rtpbin.send_rtcp_src_1 ! udpsink port=5007 sync=false async=false
132  * ]| Receive H263 on port 5000, send it through rtpbin in session 0, depayload,
133  * decode and display the video.
134  * Receive AMR on port 5002, send it through rtpbin in session 1, depayload,
135  * decode and play the audio.
136  * Receive server RTCP packets for session 0 on port 5001 and RTCP packets for
137  * session 1 on port 5003. These packets will be used for session management and
138  * synchronisation.
139  * Send RTCP reports for session 0 on port 5005 and RTCP reports for session 1
140  * on port 5007.
141  * </refsect2>
142  */
143
144 #ifdef HAVE_CONFIG_H
145 #include "config.h"
146 #endif
147 #include <stdio.h>
148 #include <string.h>
149
150 #include <gst/rtp/gstrtpbuffer.h>
151 #include <gst/rtp/gstrtcpbuffer.h>
152
153 #include "gstrtpbin.h"
154 #include "rtpsession.h"
155 #include "gstrtpsession.h"
156 #include "gstrtpjitterbuffer.h"
157
158 #include <gst/glib-compat-private.h>
159
160 GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
161 #define GST_CAT_DEFAULT gst_rtp_bin_debug
162
163 /* sink pads */
164 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
165     GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%u",
166     GST_PAD_SINK,
167     GST_PAD_REQUEST,
168     GST_STATIC_CAPS ("application/x-rtp;application/x-srtp")
169     );
170
171 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
172     GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%u",
173     GST_PAD_SINK,
174     GST_PAD_REQUEST,
175     GST_STATIC_CAPS ("application/x-rtcp;application/x-srtcp")
176     );
177
178 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
179 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%u",
180     GST_PAD_SINK,
181     GST_PAD_REQUEST,
182     GST_STATIC_CAPS ("application/x-rtp")
183     );
184
185 /* src pads */
186 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
187 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%u_%u_%u",
188     GST_PAD_SRC,
189     GST_PAD_SOMETIMES,
190     GST_STATIC_CAPS ("application/x-rtp")
191     );
192
193 static GstStaticPadTemplate rtpbin_send_rtcp_src_template =
194     GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%u",
195     GST_PAD_SRC,
196     GST_PAD_REQUEST,
197     GST_STATIC_CAPS ("application/x-rtcp;application/x-srtcp")
198     );
199
200 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
201     GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%u",
202     GST_PAD_SRC,
203     GST_PAD_SOMETIMES,
204     GST_STATIC_CAPS ("application/x-rtp;application/x-srtp")
205     );
206
207 #define GST_RTP_BIN_LOCK(bin)   g_mutex_lock (&(bin)->priv->bin_lock)
208 #define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock (&(bin)->priv->bin_lock)
209
210 /* lock to protect dynamic callbacks, like pad-added and new ssrc. */
211 #define GST_RTP_BIN_DYN_LOCK(bin)    g_mutex_lock (&(bin)->priv->dyn_lock)
212 #define GST_RTP_BIN_DYN_UNLOCK(bin)  g_mutex_unlock (&(bin)->priv->dyn_lock)
213
214 /* lock for shutdown */
215 #define GST_RTP_BIN_SHUTDOWN_LOCK(bin,label)     \
216 G_STMT_START {                                   \
217   if (g_atomic_int_get (&bin->priv->shutdown))   \
218     goto label;                                  \
219   GST_RTP_BIN_DYN_LOCK (bin);                    \
220   if (g_atomic_int_get (&bin->priv->shutdown)) { \
221     GST_RTP_BIN_DYN_UNLOCK (bin);                \
222     goto label;                                  \
223   }                                              \
224 } G_STMT_END
225
226 /* unlock for shutdown */
227 #define GST_RTP_BIN_SHUTDOWN_UNLOCK(bin)         \
228   GST_RTP_BIN_DYN_UNLOCK (bin);                  \
229
230 /* Minimum time offset to apply. This compensates for rounding errors in NTP to
231  * RTP timestamp conversions */
232 #define MIN_TS_OFFSET (4 * GST_MSECOND)
233
234 struct _GstRtpBinPrivate
235 {
236   GMutex bin_lock;
237
238   /* lock protecting dynamic adding/removing */
239   GMutex dyn_lock;
240
241   /* if we are shutting down or not */
242   gint shutdown;
243
244   gboolean autoremove;
245
246   /* NTP time in ns of last SR sync used */
247   guint64 last_ntpnstime;
248
249   /* list of extra elements */
250   GList *elements;
251 };
252
253 /* signals and args */
254 enum
255 {
256   SIGNAL_REQUEST_PT_MAP,
257   SIGNAL_PAYLOAD_TYPE_CHANGE,
258   SIGNAL_CLEAR_PT_MAP,
259   SIGNAL_RESET_SYNC,
260   SIGNAL_GET_SESSION,
261   SIGNAL_GET_INTERNAL_SESSION,
262   SIGNAL_GET_STORAGE,
263   SIGNAL_GET_INTERNAL_STORAGE,
264
265   SIGNAL_ON_NEW_SSRC,
266   SIGNAL_ON_SSRC_COLLISION,
267   SIGNAL_ON_SSRC_VALIDATED,
268   SIGNAL_ON_SSRC_ACTIVE,
269   SIGNAL_ON_SSRC_SDES,
270   SIGNAL_ON_BYE_SSRC,
271   SIGNAL_ON_BYE_TIMEOUT,
272   SIGNAL_ON_TIMEOUT,
273   SIGNAL_ON_SENDER_TIMEOUT,
274   SIGNAL_ON_NPT_STOP,
275
276   SIGNAL_REQUEST_RTP_ENCODER,
277   SIGNAL_REQUEST_RTP_DECODER,
278   SIGNAL_REQUEST_RTCP_ENCODER,
279   SIGNAL_REQUEST_RTCP_DECODER,
280
281   SIGNAL_REQUEST_FEC_DECODER,
282   SIGNAL_REQUEST_FEC_ENCODER,
283
284   SIGNAL_NEW_JITTERBUFFER,
285   SIGNAL_NEW_STORAGE,
286
287   SIGNAL_REQUEST_AUX_SENDER,
288   SIGNAL_REQUEST_AUX_RECEIVER,
289
290   SIGNAL_ON_NEW_SENDER_SSRC,
291   SIGNAL_ON_SENDER_SSRC_ACTIVE,
292
293   SIGNAL_ON_BUNDLED_SSRC,
294
295   LAST_SIGNAL
296 };
297
298 #define DEFAULT_LATENCY_MS           200
299 #define DEFAULT_DROP_ON_LATENCY      FALSE
300 #define DEFAULT_SDES                 NULL
301 #define DEFAULT_DO_LOST              FALSE
302 #define DEFAULT_IGNORE_PT            FALSE
303 #define DEFAULT_NTP_SYNC             FALSE
304 #define DEFAULT_AUTOREMOVE           FALSE
305 #define DEFAULT_BUFFER_MODE          RTP_JITTER_BUFFER_MODE_SLAVE
306 #define DEFAULT_USE_PIPELINE_CLOCK   FALSE
307 #define DEFAULT_RTCP_SYNC            GST_RTP_BIN_RTCP_SYNC_ALWAYS
308 #define DEFAULT_RTCP_SYNC_INTERVAL   0
309 #define DEFAULT_DO_SYNC_EVENT        FALSE
310 #define DEFAULT_DO_RETRANSMISSION    FALSE
311 #define DEFAULT_RTP_PROFILE          GST_RTP_PROFILE_AVP
312 #define DEFAULT_NTP_TIME_SOURCE      GST_RTP_NTP_TIME_SOURCE_NTP
313 #define DEFAULT_RTCP_SYNC_SEND_TIME  TRUE
314 #define DEFAULT_MAX_RTCP_RTP_TIME_DIFF 1000
315 #define DEFAULT_MAX_DROPOUT_TIME     60000
316 #define DEFAULT_MAX_MISORDER_TIME    2000
317 #define DEFAULT_RFC7273_SYNC         FALSE
318 #define DEFAULT_MAX_STREAMS          G_MAXUINT
319 #define DEFAULT_MAX_TS_OFFSET_ADJUSTMENT G_GUINT64_CONSTANT(0)
320 #define DEFAULT_MAX_TS_OFFSET        G_GINT64_CONSTANT(3000000000)
321
322 enum
323 {
324   PROP_0,
325   PROP_LATENCY,
326   PROP_DROP_ON_LATENCY,
327   PROP_SDES,
328   PROP_DO_LOST,
329   PROP_IGNORE_PT,
330   PROP_NTP_SYNC,
331   PROP_RTCP_SYNC,
332   PROP_RTCP_SYNC_INTERVAL,
333   PROP_AUTOREMOVE,
334   PROP_BUFFER_MODE,
335   PROP_USE_PIPELINE_CLOCK,
336   PROP_DO_SYNC_EVENT,
337   PROP_DO_RETRANSMISSION,
338   PROP_RTP_PROFILE,
339   PROP_NTP_TIME_SOURCE,
340   PROP_RTCP_SYNC_SEND_TIME,
341   PROP_MAX_RTCP_RTP_TIME_DIFF,
342   PROP_MAX_DROPOUT_TIME,
343   PROP_MAX_MISORDER_TIME,
344   PROP_RFC7273_SYNC,
345   PROP_MAX_STREAMS,
346   PROP_MAX_TS_OFFSET_ADJUSTMENT,
347   PROP_MAX_TS_OFFSET,
348 };
349
350 #define GST_RTP_BIN_RTCP_SYNC_TYPE (gst_rtp_bin_rtcp_sync_get_type())
351 static GType
352 gst_rtp_bin_rtcp_sync_get_type (void)
353 {
354   static GType rtcp_sync_type = 0;
355   static const GEnumValue rtcp_sync_types[] = {
356     {GST_RTP_BIN_RTCP_SYNC_ALWAYS, "always", "always"},
357     {GST_RTP_BIN_RTCP_SYNC_INITIAL, "initial", "initial"},
358     {GST_RTP_BIN_RTCP_SYNC_RTP, "rtp-info", "rtp-info"},
359     {0, NULL, NULL},
360   };
361
362   if (!rtcp_sync_type) {
363     rtcp_sync_type = g_enum_register_static ("GstRTCPSync", rtcp_sync_types);
364   }
365   return rtcp_sync_type;
366 }
367
368 /* helper objects */
369 typedef struct _GstRtpBinSession GstRtpBinSession;
370 typedef struct _GstRtpBinStream GstRtpBinStream;
371 typedef struct _GstRtpBinClient GstRtpBinClient;
372
373 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
374
375 static GstCaps *pt_map_requested (GstElement * element, guint pt,
376     GstRtpBinSession * session);
377 static void payload_type_change (GstElement * element, guint pt,
378     GstRtpBinSession * session);
379 static void remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session);
380 static void remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session);
381 static void remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session);
382 static void remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session);
383 static void free_client (GstRtpBinClient * client, GstRtpBin * bin);
384 static void free_stream (GstRtpBinStream * stream, GstRtpBin * bin);
385 static GstRtpBinSession *create_session (GstRtpBin * rtpbin, gint id);
386 static GstPad *complete_session_sink (GstRtpBin * rtpbin,
387     GstRtpBinSession * session, gboolean bundle_demuxer_needed);
388 static void
389 complete_session_receiver (GstRtpBin * rtpbin, GstRtpBinSession * session,
390     guint sessid);
391 static GstPad *complete_session_rtcp (GstRtpBin * rtpbin,
392     GstRtpBinSession * session, guint sessid, gboolean bundle_demuxer_needed);
393
394 /* Manages the RTP stream for one SSRC.
395  *
396  * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
397  * If we see an SDES RTCP packet that links multiple SSRCs together based on a
398  * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
399  * together (see below).
400  */
401 struct _GstRtpBinStream
402 {
403   /* the SSRC of this stream */
404   guint32 ssrc;
405
406   /* parent bin */
407   GstRtpBin *bin;
408
409   /* the session this SSRC belongs to */
410   GstRtpBinSession *session;
411
412   /* the jitterbuffer of the SSRC */
413   GstElement *buffer;
414   gulong buffer_handlesync_sig;
415   gulong buffer_ptreq_sig;
416   gulong buffer_ntpstop_sig;
417   gint percent;
418
419   /* the PT demuxer of the SSRC */
420   GstElement *demux;
421   gulong demux_newpad_sig;
422   gulong demux_padremoved_sig;
423   gulong demux_ptreq_sig;
424   gulong demux_ptchange_sig;
425
426   /* if we have calculated a valid rt_delta for this stream */
427   gboolean have_sync;
428   /* mapping to local RTP and NTP time */
429   gint64 rt_delta;
430   gint64 rtp_delta;
431   /* base rtptime in gst time */
432   gint64 clock_base;
433 };
434
435 #define GST_RTP_SESSION_LOCK(sess)   g_mutex_lock (&(sess)->lock)
436 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock (&(sess)->lock)
437
438 /* Manages the receiving end of the packets.
439  *
440  * There is one such structure for each RTP session (audio/video/...).
441  * We get the RTP/RTCP packets and stuff them into the session manager. From
442  * there they are pushed into an SSRC demuxer that splits the stream based on
443  * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
444  * the GstRtpBinStream above).
445  *
446  * Before the SSRC demuxer, a storage element may be inserted for the purpose
447  * of Forward Error Correction.
448  */
449 struct _GstRtpBinSession
450 {
451   /* session id */
452   gint id;
453   /* the parent bin */
454   GstRtpBin *bin;
455   /* the session element */
456   GstElement *session;
457   /* the SSRC demuxer */
458   GstElement *demux;
459   gulong demux_newpad_sig;
460   gulong demux_padremoved_sig;
461
462   /* Fec support */
463   GstElement *storage;
464
465   /* Bundling support */
466   GstElement *rtp_funnel;
467   GstElement *rtcp_funnel;
468   GstElement *bundle_demux;
469   gulong bundle_demux_newpad_sig;
470
471   GMutex lock;
472
473   /* list of GstRtpBinStream */
474   GSList *streams;
475
476   /* list of elements */
477   GSList *elements;
478
479   /* mapping of payload type to caps */
480   GHashTable *ptmap;
481
482   /* the pads of the session */
483   GstPad *recv_rtp_sink;
484   GstPad *recv_rtp_sink_ghost;
485   GstPad *recv_rtp_src;
486   GstPad *recv_rtcp_sink;
487   GstPad *recv_rtcp_sink_ghost;
488   GstPad *sync_src;
489   GstPad *send_rtp_sink;
490   GstPad *send_rtp_sink_ghost;
491   GstPad *send_rtp_src_ghost;
492   GstPad *send_rtcp_src;
493   GstPad *send_rtcp_src_ghost;
494 };
495
496 /* Manages the RTP streams that come from one client and should therefore be
497  * synchronized.
498  */
499 struct _GstRtpBinClient
500 {
501   /* the common CNAME for the streams */
502   gchar *cname;
503   guint cname_len;
504
505   /* the streams */
506   guint nstreams;
507   GSList *streams;
508 };
509
510 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
511 static GstRtpBinSession *
512 find_session_by_id (GstRtpBin * rtpbin, gint id)
513 {
514   GSList *walk;
515
516   for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
517     GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
518
519     if (sess->id == id)
520       return sess;
521   }
522   return NULL;
523 }
524
525 /* find a session with the given request pad. Must be called with RTP_BIN_LOCK */
526 static GstRtpBinSession *
527 find_session_by_pad (GstRtpBin * rtpbin, GstPad * pad)
528 {
529   GSList *walk;
530
531   for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
532     GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
533
534     if ((sess->recv_rtp_sink_ghost == pad) ||
535         (sess->recv_rtcp_sink_ghost == pad) ||
536         (sess->send_rtp_sink_ghost == pad)
537         || (sess->send_rtcp_src_ghost == pad))
538       return sess;
539   }
540   return NULL;
541 }
542
543 static void
544 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
545 {
546   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
547       sess->id, ssrc);
548 }
549
550 static void
551 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
552 {
553   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
554       sess->id, ssrc);
555 }
556
557 static void
558 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
559 {
560   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
561       sess->id, ssrc);
562 }
563
564 static void
565 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
566 {
567   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
568       sess->id, ssrc);
569 }
570
571 static void
572 on_ssrc_sdes (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
573 {
574   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES], 0,
575       sess->id, ssrc);
576 }
577
578 static void
579 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
580 {
581   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
582       sess->id, ssrc);
583 }
584
585 static void
586 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
587 {
588   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
589       sess->id, ssrc);
590
591   if (sess->bin->priv->autoremove)
592     g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
593 }
594
595 static void
596 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
597 {
598   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
599       sess->id, ssrc);
600
601   if (sess->bin->priv->autoremove)
602     g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
603 }
604
605 static void
606 on_sender_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
607 {
608   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
609       sess->id, ssrc);
610 }
611
612 static void
613 on_npt_stop (GstElement * jbuf, GstRtpBinStream * stream)
614 {
615   g_signal_emit (stream->bin, gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP], 0,
616       stream->session->id, stream->ssrc);
617 }
618
619 static void
620 on_new_sender_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
621 {
622   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SENDER_SSRC], 0,
623       sess->id, ssrc);
624 }
625
626 static void
627 on_sender_ssrc_active (GstElement * session, guint32 ssrc,
628     GstRtpBinSession * sess)
629 {
630   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE],
631       0, sess->id, ssrc);
632 }
633
634 /* must be called with the SESSION lock */
635 static GstRtpBinStream *
636 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
637 {
638   GSList *walk;
639
640   for (walk = session->streams; walk; walk = g_slist_next (walk)) {
641     GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
642
643     if (stream->ssrc == ssrc)
644       return stream;
645   }
646   return NULL;
647 }
648
649 static void
650 ssrc_demux_pad_removed (GstElement * element, guint ssrc, GstPad * pad,
651     GstRtpBinSession * session)
652 {
653   GstRtpBinStream *stream = NULL;
654   GstRtpBin *rtpbin;
655
656   rtpbin = session->bin;
657
658   GST_RTP_BIN_LOCK (rtpbin);
659
660   GST_RTP_SESSION_LOCK (session);
661   if ((stream = find_stream_by_ssrc (session, ssrc)))
662     session->streams = g_slist_remove (session->streams, stream);
663   GST_RTP_SESSION_UNLOCK (session);
664
665   if (stream)
666     free_stream (stream, rtpbin);
667
668   GST_RTP_BIN_UNLOCK (rtpbin);
669 }
670
671 static void
672 new_bundled_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
673     GstRtpBinSession * session)
674 {
675   GValue result = G_VALUE_INIT;
676   GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
677   guint session_id = 0;
678   GstRtpBinSession *target_session = NULL;
679   GstRtpBin *rtpbin = session->bin;
680   gchar *name;
681   GstPad *src_pad;
682   GstPad *recv_rtp_sink = NULL;
683   GstPad *recv_rtcp_sink = NULL;
684   GstPadLinkReturn ret;
685
686   GST_RTP_BIN_DYN_LOCK (rtpbin);
687   GST_DEBUG_OBJECT (rtpbin, "new bundled SSRC pad %08x, %s:%s", ssrc,
688       GST_DEBUG_PAD_NAME (pad));
689
690   g_value_init (&result, G_TYPE_UINT);
691   g_value_init (&params[0], GST_TYPE_ELEMENT);
692   g_value_set_object (&params[0], rtpbin);
693   g_value_init (&params[1], G_TYPE_UINT);
694   g_value_set_uint (&params[1], ssrc);
695
696   g_signal_emitv (params,
697       gst_rtp_bin_signals[SIGNAL_ON_BUNDLED_SSRC], 0, &result);
698   g_value_unset (&params[0]);
699
700   session_id = g_value_get_uint (&result);
701   if (session_id == 0) {
702     target_session = session;
703   } else {
704     target_session = find_session_by_id (rtpbin, (gint) session_id);
705     if (!target_session) {
706       target_session = create_session (rtpbin, session_id);
707     }
708     if (!target_session) {
709       /* create_session() warned already */
710       GST_RTP_BIN_DYN_UNLOCK (rtpbin);
711       return;
712     }
713
714     if (!target_session->recv_rtp_sink) {
715       recv_rtp_sink = complete_session_sink (rtpbin, target_session, FALSE);
716     }
717
718     if (!target_session->recv_rtp_src)
719       complete_session_receiver (rtpbin, target_session, session_id);
720
721     if (!target_session->recv_rtcp_sink) {
722       recv_rtcp_sink =
723           complete_session_rtcp (rtpbin, target_session, session_id, FALSE);
724     }
725   }
726
727   GST_DEBUG_OBJECT (rtpbin, "Assigning bundled ssrc %u to session %u", ssrc,
728       session_id);
729
730   if (!recv_rtp_sink) {
731     recv_rtp_sink =
732         gst_element_get_request_pad (target_session->rtp_funnel, "sink_%u");
733   }
734
735   if (!recv_rtcp_sink) {
736     recv_rtcp_sink =
737         gst_element_get_request_pad (target_session->rtcp_funnel, "sink_%u");
738   }
739
740   name = g_strdup_printf ("src_%u", ssrc);
741   src_pad = gst_element_get_static_pad (element, name);
742   ret = gst_pad_link (src_pad, recv_rtp_sink);
743   g_free (name);
744   gst_object_unref (src_pad);
745   gst_object_unref (recv_rtp_sink);
746   if (ret != GST_PAD_LINK_OK) {
747     g_warning
748         ("rtpbin: failed to link bundle demuxer to receive rtp funnel for session %u",
749         session_id);
750   }
751
752   name = g_strdup_printf ("rtcp_src_%u", ssrc);
753   src_pad = gst_element_get_static_pad (element, name);
754   gst_pad_link (src_pad, recv_rtcp_sink);
755   g_free (name);
756   gst_object_unref (src_pad);
757   gst_object_unref (recv_rtcp_sink);
758   if (ret != GST_PAD_LINK_OK) {
759     g_warning
760         ("rtpbin: failed to link bundle demuxer to receive rtcp sink pad for session %u",
761         session_id);
762   }
763
764   GST_RTP_BIN_DYN_UNLOCK (rtpbin);
765 }
766
767 /* create a session with the given id.  Must be called with RTP_BIN_LOCK */
768 static GstRtpBinSession *
769 create_session (GstRtpBin * rtpbin, gint id)
770 {
771   GstRtpBinSession *sess;
772   GstElement *session, *demux;
773   GstElement *storage = NULL;
774   GstState target;
775
776   if (!(session = gst_element_factory_make ("rtpsession", NULL)))
777     goto no_session;
778
779   if (!(demux = gst_element_factory_make ("rtpssrcdemux", NULL)))
780     goto no_demux;
781
782   if (!(storage = gst_element_factory_make ("rtpstorage", NULL)))
783     goto no_storage;
784
785   g_signal_emit (rtpbin, gst_rtp_bin_signals[SIGNAL_NEW_STORAGE], 0, storage,
786       id);
787
788   sess = g_new0 (GstRtpBinSession, 1);
789   g_mutex_init (&sess->lock);
790   sess->id = id;
791   sess->bin = rtpbin;
792   sess->session = session;
793   sess->demux = demux;
794   sess->storage = storage;
795
796   sess->rtp_funnel = gst_element_factory_make ("funnel", NULL);
797   sess->rtcp_funnel = gst_element_factory_make ("funnel", NULL);
798
799   sess->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
800       (GDestroyNotify) gst_caps_unref);
801   rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
802
803   /* configure SDES items */
804   GST_OBJECT_LOCK (rtpbin);
805   g_object_set (session, "sdes", rtpbin->sdes, "rtp-profile",
806       rtpbin->rtp_profile, "rtcp-sync-send-time", rtpbin->rtcp_sync_send_time,
807       NULL);
808   if (rtpbin->use_pipeline_clock)
809     g_object_set (session, "use-pipeline-clock", rtpbin->use_pipeline_clock,
810         NULL);
811   else
812     g_object_set (session, "ntp-time-source", rtpbin->ntp_time_source, NULL);
813
814   g_object_set (session, "max-dropout-time", rtpbin->max_dropout_time,
815       "max-misorder-time", rtpbin->max_misorder_time, NULL);
816   GST_OBJECT_UNLOCK (rtpbin);
817
818   /* provide clock_rate to the session manager when needed */
819   g_signal_connect (session, "request-pt-map",
820       (GCallback) pt_map_requested, sess);
821
822   g_signal_connect (sess->session, "on-new-ssrc",
823       (GCallback) on_new_ssrc, sess);
824   g_signal_connect (sess->session, "on-ssrc-collision",
825       (GCallback) on_ssrc_collision, sess);
826   g_signal_connect (sess->session, "on-ssrc-validated",
827       (GCallback) on_ssrc_validated, sess);
828   g_signal_connect (sess->session, "on-ssrc-active",
829       (GCallback) on_ssrc_active, sess);
830   g_signal_connect (sess->session, "on-ssrc-sdes",
831       (GCallback) on_ssrc_sdes, sess);
832   g_signal_connect (sess->session, "on-bye-ssrc",
833       (GCallback) on_bye_ssrc, sess);
834   g_signal_connect (sess->session, "on-bye-timeout",
835       (GCallback) on_bye_timeout, sess);
836   g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
837   g_signal_connect (sess->session, "on-sender-timeout",
838       (GCallback) on_sender_timeout, sess);
839   g_signal_connect (sess->session, "on-new-sender-ssrc",
840       (GCallback) on_new_sender_ssrc, sess);
841   g_signal_connect (sess->session, "on-sender-ssrc-active",
842       (GCallback) on_sender_ssrc_active, sess);
843
844   gst_bin_add (GST_BIN_CAST (rtpbin), session);
845   gst_bin_add (GST_BIN_CAST (rtpbin), demux);
846   gst_bin_add (GST_BIN_CAST (rtpbin), sess->rtp_funnel);
847   gst_bin_add (GST_BIN_CAST (rtpbin), sess->rtcp_funnel);
848   gst_bin_add (GST_BIN_CAST (rtpbin), storage);
849
850   GST_OBJECT_LOCK (rtpbin);
851   target = GST_STATE_TARGET (rtpbin);
852   GST_OBJECT_UNLOCK (rtpbin);
853
854   /* change state only to what's needed */
855   gst_element_set_state (demux, target);
856   gst_element_set_state (session, target);
857   gst_element_set_state (sess->rtp_funnel, target);
858   gst_element_set_state (sess->rtcp_funnel, target);
859   gst_element_set_state (storage, target);
860
861   return sess;
862
863   /* ERRORS */
864 no_session:
865   {
866     g_warning ("rtpbin: could not create rtpsession element");
867     return NULL;
868   }
869 no_demux:
870   {
871     gst_object_unref (session);
872     g_warning ("rtpbin: could not create rtpssrcdemux element");
873     return NULL;
874   }
875 no_storage:
876   {
877     gst_object_unref (session);
878     gst_object_unref (demux);
879     g_warning ("rtpbin: could not create rtpstorage element");
880     return NULL;
881   }
882 }
883
884 static gboolean
885 bin_manage_element (GstRtpBin * bin, GstElement * element)
886 {
887   GstRtpBinPrivate *priv = bin->priv;
888
889   if (g_list_find (priv->elements, element)) {
890     GST_DEBUG_OBJECT (bin, "requested element %p already in bin", element);
891   } else {
892     GST_DEBUG_OBJECT (bin, "adding requested element %p", element);
893
894     if (g_object_is_floating (element))
895       element = gst_object_ref_sink (element);
896
897     if (!gst_bin_add (GST_BIN_CAST (bin), element))
898       goto add_failed;
899     if (!gst_element_sync_state_with_parent (element))
900       GST_WARNING_OBJECT (bin, "unable to sync element state with rtpbin");
901   }
902   /* we add the element multiple times, each we need an equal number of
903    * removes to really remove the element from the bin */
904   priv->elements = g_list_prepend (priv->elements, element);
905
906   return TRUE;
907
908   /* ERRORS */
909 add_failed:
910   {
911     GST_WARNING_OBJECT (bin, "unable to add element");
912     gst_object_unref (element);
913     return FALSE;
914   }
915 }
916
917 static void
918 remove_bin_element (GstElement * element, GstRtpBin * bin)
919 {
920   GstRtpBinPrivate *priv = bin->priv;
921   GList *find;
922
923   find = g_list_find (priv->elements, element);
924   if (find) {
925     priv->elements = g_list_delete_link (priv->elements, find);
926
927     if (!g_list_find (priv->elements, element)) {
928       gst_element_set_locked_state (element, TRUE);
929       gst_bin_remove (GST_BIN_CAST (bin), element);
930       gst_element_set_state (element, GST_STATE_NULL);
931     }
932
933     gst_object_unref (element);
934   }
935 }
936
937 /* called with RTP_BIN_LOCK */
938 static void
939 free_session (GstRtpBinSession * sess, GstRtpBin * bin)
940 {
941   GST_DEBUG_OBJECT (bin, "freeing session %p", sess);
942
943   gst_element_set_locked_state (sess->demux, TRUE);
944   gst_element_set_locked_state (sess->session, TRUE);
945
946   gst_element_set_state (sess->demux, GST_STATE_NULL);
947   gst_element_set_state (sess->session, GST_STATE_NULL);
948
949   remove_recv_rtp (bin, sess);
950   remove_recv_rtcp (bin, sess);
951   remove_send_rtp (bin, sess);
952   remove_rtcp (bin, sess);
953
954   gst_bin_remove (GST_BIN_CAST (bin), sess->session);
955   gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
956
957   g_slist_foreach (sess->elements, (GFunc) remove_bin_element, bin);
958   g_slist_free (sess->elements);
959
960   g_slist_foreach (sess->streams, (GFunc) free_stream, bin);
961   g_slist_free (sess->streams);
962
963   g_mutex_clear (&sess->lock);
964   g_hash_table_destroy (sess->ptmap);
965
966   g_free (sess);
967 }
968
969 /* get the payload type caps for the specific payload @pt in @session */
970 static GstCaps *
971 get_pt_map (GstRtpBinSession * session, guint pt)
972 {
973   GstCaps *caps = NULL;
974   GstRtpBin *bin;
975   GValue ret = { 0 };
976   GValue args[3] = { {0}, {0}, {0} };
977
978   GST_DEBUG ("searching pt %u in cache", pt);
979
980   GST_RTP_SESSION_LOCK (session);
981
982   /* first look in the cache */
983   caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
984   if (caps) {
985     gst_caps_ref (caps);
986     goto done;
987   }
988
989   bin = session->bin;
990
991   GST_DEBUG ("emiting signal for pt %u in session %u", pt, session->id);
992
993   /* not in cache, send signal to request caps */
994   g_value_init (&args[0], GST_TYPE_ELEMENT);
995   g_value_set_object (&args[0], bin);
996   g_value_init (&args[1], G_TYPE_UINT);
997   g_value_set_uint (&args[1], session->id);
998   g_value_init (&args[2], G_TYPE_UINT);
999   g_value_set_uint (&args[2], pt);
1000
1001   g_value_init (&ret, GST_TYPE_CAPS);
1002   g_value_set_boxed (&ret, NULL);
1003
1004   GST_RTP_SESSION_UNLOCK (session);
1005
1006   g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
1007
1008   GST_RTP_SESSION_LOCK (session);
1009
1010   g_value_unset (&args[0]);
1011   g_value_unset (&args[1]);
1012   g_value_unset (&args[2]);
1013
1014   /* look in the cache again because we let the lock go */
1015   caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
1016   if (caps) {
1017     gst_caps_ref (caps);
1018     g_value_unset (&ret);
1019     goto done;
1020   }
1021
1022   caps = (GstCaps *) g_value_dup_boxed (&ret);
1023   g_value_unset (&ret);
1024   if (!caps)
1025     goto no_caps;
1026
1027   GST_DEBUG ("caching pt %u as %" GST_PTR_FORMAT, pt, caps);
1028
1029   /* store in cache, take additional ref */
1030   g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt),
1031       gst_caps_ref (caps));
1032
1033 done:
1034   GST_RTP_SESSION_UNLOCK (session);
1035
1036   return caps;
1037
1038   /* ERRORS */
1039 no_caps:
1040   {
1041     GST_RTP_SESSION_UNLOCK (session);
1042     GST_DEBUG ("no pt map could be obtained");
1043     return NULL;
1044   }
1045 }
1046
1047 static gboolean
1048 return_true (gpointer key, gpointer value, gpointer user_data)
1049 {
1050   return TRUE;
1051 }
1052
1053 static void
1054 gst_rtp_bin_reset_sync (GstRtpBin * rtpbin)
1055 {
1056   GSList *clients, *streams;
1057
1058   GST_DEBUG_OBJECT (rtpbin, "Reset sync on all clients");
1059
1060   GST_RTP_BIN_LOCK (rtpbin);
1061   for (clients = rtpbin->clients; clients; clients = g_slist_next (clients)) {
1062     GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
1063
1064     /* reset sync on all streams for this client */
1065     for (streams = client->streams; streams; streams = g_slist_next (streams)) {
1066       GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
1067
1068       /* make use require a new SR packet for this stream before we attempt new
1069        * lip-sync */
1070       stream->have_sync = FALSE;
1071       stream->rt_delta = 0;
1072       stream->rtp_delta = 0;
1073       stream->clock_base = -100 * GST_SECOND;
1074     }
1075   }
1076   GST_RTP_BIN_UNLOCK (rtpbin);
1077 }
1078
1079 static void
1080 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
1081 {
1082   GSList *sessions, *streams;
1083
1084   GST_RTP_BIN_LOCK (bin);
1085   GST_DEBUG_OBJECT (bin, "clearing pt map");
1086   for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
1087     GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
1088
1089     GST_DEBUG_OBJECT (bin, "clearing session %p", session);
1090     g_signal_emit_by_name (session->session, "clear-pt-map", NULL);
1091
1092     GST_RTP_SESSION_LOCK (session);
1093     g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
1094
1095     for (streams = session->streams; streams; streams = g_slist_next (streams)) {
1096       GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
1097
1098       GST_DEBUG_OBJECT (bin, "clearing stream %p", stream);
1099       g_signal_emit_by_name (stream->buffer, "clear-pt-map", NULL);
1100       if (stream->demux)
1101         g_signal_emit_by_name (stream->demux, "clear-pt-map", NULL);
1102     }
1103     GST_RTP_SESSION_UNLOCK (session);
1104   }
1105   GST_RTP_BIN_UNLOCK (bin);
1106
1107   /* reset sync too */
1108   gst_rtp_bin_reset_sync (bin);
1109 }
1110
1111 static GstElement *
1112 gst_rtp_bin_get_session (GstRtpBin * bin, guint session_id)
1113 {
1114   GstRtpBinSession *session;
1115   GstElement *ret = NULL;
1116
1117   GST_RTP_BIN_LOCK (bin);
1118   GST_DEBUG_OBJECT (bin, "retrieving GstRtpSession, index: %u", session_id);
1119   session = find_session_by_id (bin, (gint) session_id);
1120   if (session) {
1121     ret = gst_object_ref (session->session);
1122   }
1123   GST_RTP_BIN_UNLOCK (bin);
1124
1125   return ret;
1126 }
1127
1128 static RTPSession *
1129 gst_rtp_bin_get_internal_session (GstRtpBin * bin, guint session_id)
1130 {
1131   RTPSession *internal_session = NULL;
1132   GstRtpBinSession *session;
1133
1134   GST_RTP_BIN_LOCK (bin);
1135   GST_DEBUG_OBJECT (bin, "retrieving internal RTPSession object, index: %u",
1136       session_id);
1137   session = find_session_by_id (bin, (gint) session_id);
1138   if (session) {
1139     g_object_get (session->session, "internal-session", &internal_session,
1140         NULL);
1141   }
1142   GST_RTP_BIN_UNLOCK (bin);
1143
1144   return internal_session;
1145 }
1146
1147 static GstElement *
1148 gst_rtp_bin_get_storage (GstRtpBin * bin, guint session_id)
1149 {
1150   GstRtpBinSession *session;
1151   GstElement *res = NULL;
1152
1153   GST_RTP_BIN_LOCK (bin);
1154   GST_DEBUG_OBJECT (bin, "retrieving internal storage object, index: %u",
1155       session_id);
1156   session = find_session_by_id (bin, (gint) session_id);
1157   if (session && session->storage) {
1158     res = gst_object_ref (session->storage);
1159   }
1160   GST_RTP_BIN_UNLOCK (bin);
1161
1162   return res;
1163 }
1164
1165 static GObject *
1166 gst_rtp_bin_get_internal_storage (GstRtpBin * bin, guint session_id)
1167 {
1168   GObject *internal_storage = NULL;
1169   GstRtpBinSession *session;
1170
1171   GST_RTP_BIN_LOCK (bin);
1172   GST_DEBUG_OBJECT (bin, "retrieving internal storage object, index: %u",
1173       session_id);
1174   session = find_session_by_id (bin, (gint) session_id);
1175   if (session && session->storage) {
1176     g_object_get (session->storage, "internal-storage", &internal_storage,
1177         NULL);
1178   }
1179   GST_RTP_BIN_UNLOCK (bin);
1180
1181   return internal_storage;
1182 }
1183
1184 static GstElement *
1185 gst_rtp_bin_request_encoder (GstRtpBin * bin, guint session_id)
1186 {
1187   GST_DEBUG_OBJECT (bin, "return NULL encoder");
1188   return NULL;
1189 }
1190
1191 static GstElement *
1192 gst_rtp_bin_request_decoder (GstRtpBin * bin, guint session_id)
1193 {
1194   GST_DEBUG_OBJECT (bin, "return NULL decoder");
1195   return NULL;
1196 }
1197
1198 static void
1199 gst_rtp_bin_propagate_property_to_jitterbuffer (GstRtpBin * bin,
1200     const gchar * name, const GValue * value)
1201 {
1202   GSList *sessions, *streams;
1203
1204   GST_RTP_BIN_LOCK (bin);
1205   for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
1206     GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
1207
1208     GST_RTP_SESSION_LOCK (session);
1209     for (streams = session->streams; streams; streams = g_slist_next (streams)) {
1210       GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
1211
1212       g_object_set_property (G_OBJECT (stream->buffer), name, value);
1213     }
1214     GST_RTP_SESSION_UNLOCK (session);
1215   }
1216   GST_RTP_BIN_UNLOCK (bin);
1217 }
1218
1219 static void
1220 gst_rtp_bin_propagate_property_to_session (GstRtpBin * bin,
1221     const gchar * name, const GValue * value)
1222 {
1223   GSList *sessions;
1224
1225   GST_RTP_BIN_LOCK (bin);
1226   for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
1227     GstRtpBinSession *sess = (GstRtpBinSession *) sessions->data;
1228
1229     g_object_set_property (G_OBJECT (sess->session), name, value);
1230   }
1231   GST_RTP_BIN_UNLOCK (bin);
1232 }
1233
1234 /* get a client with the given SDES name. Must be called with RTP_BIN_LOCK */
1235 static GstRtpBinClient *
1236 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
1237 {
1238   GstRtpBinClient *result = NULL;
1239   GSList *walk;
1240
1241   for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
1242     GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
1243
1244     if (len != client->cname_len)
1245       continue;
1246
1247     if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
1248       GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
1249           client->cname);
1250       result = client;
1251       break;
1252     }
1253   }
1254
1255   /* nothing found, create one */
1256   if (result == NULL) {
1257     result = g_new0 (GstRtpBinClient, 1);
1258     result->cname = g_strndup ((gchar *) data, len);
1259     result->cname_len = len;
1260     bin->clients = g_slist_prepend (bin->clients, result);
1261     GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
1262         result->cname);
1263   }
1264   return result;
1265 }
1266
1267 static void
1268 free_client (GstRtpBinClient * client, GstRtpBin * bin)
1269 {
1270   GST_DEBUG_OBJECT (bin, "freeing client %p", client);
1271   g_slist_free (client->streams);
1272   g_free (client->cname);
1273   g_free (client);
1274 }
1275
1276 static void
1277 get_current_times (GstRtpBin * bin, GstClockTime * running_time,
1278     guint64 * ntpnstime)
1279 {
1280   guint64 ntpns = -1;
1281   GstClock *clock;
1282   GstClockTime base_time, rt, clock_time;
1283
1284   GST_OBJECT_LOCK (bin);
1285   if ((clock = GST_ELEMENT_CLOCK (bin))) {
1286     base_time = GST_ELEMENT_CAST (bin)->base_time;
1287     gst_object_ref (clock);
1288     GST_OBJECT_UNLOCK (bin);
1289
1290     /* get current clock time and convert to running time */
1291     clock_time = gst_clock_get_time (clock);
1292     rt = clock_time - base_time;
1293
1294     if (bin->use_pipeline_clock) {
1295       ntpns = rt;
1296       /* add constant to convert from 1970 based time to 1900 based time */
1297       ntpns += (2208988800LL * GST_SECOND);
1298     } else {
1299       switch (bin->ntp_time_source) {
1300         case GST_RTP_NTP_TIME_SOURCE_NTP:
1301         case GST_RTP_NTP_TIME_SOURCE_UNIX:{
1302           GTimeVal current;
1303
1304           /* get current NTP time */
1305           g_get_current_time (&current);
1306           ntpns = GST_TIMEVAL_TO_TIME (current);
1307
1308           /* add constant to convert from 1970 based time to 1900 based time */
1309           if (bin->ntp_time_source == GST_RTP_NTP_TIME_SOURCE_NTP)
1310             ntpns += (2208988800LL * GST_SECOND);
1311           break;
1312         }
1313         case GST_RTP_NTP_TIME_SOURCE_RUNNING_TIME:
1314           ntpns = rt;
1315           break;
1316         case GST_RTP_NTP_TIME_SOURCE_CLOCK_TIME:
1317           ntpns = clock_time;
1318           break;
1319         default:
1320           ntpns = -1;           /* Fix uninited compiler warning */
1321           g_assert_not_reached ();
1322           break;
1323       }
1324     }
1325
1326     gst_object_unref (clock);
1327   } else {
1328     GST_OBJECT_UNLOCK (bin);
1329     rt = -1;
1330     ntpns = -1;
1331   }
1332   if (running_time)
1333     *running_time = rt;
1334   if (ntpnstime)
1335     *ntpnstime = ntpns;
1336 }
1337
1338 static void
1339 stream_set_ts_offset (GstRtpBin * bin, GstRtpBinStream * stream,
1340     gint64 ts_offset, gint64 max_ts_offset, gint64 min_ts_offset,
1341     gboolean allow_positive_ts_offset)
1342 {
1343   gint64 prev_ts_offset;
1344
1345   g_object_get (stream->buffer, "ts-offset", &prev_ts_offset, NULL);
1346
1347   /* delta changed, see how much */
1348   if (prev_ts_offset != ts_offset) {
1349     gint64 diff;
1350
1351     diff = prev_ts_offset - ts_offset;
1352
1353     GST_DEBUG_OBJECT (bin,
1354         "ts-offset %" G_GINT64_FORMAT ", prev %" G_GINT64_FORMAT
1355         ", diff: %" G_GINT64_FORMAT, ts_offset, prev_ts_offset, diff);
1356
1357     /* ignore minor offsets */
1358     if (ABS (diff) < min_ts_offset) {
1359       GST_DEBUG_OBJECT (bin, "offset too small, ignoring");
1360       return;
1361     }
1362
1363     /* sanity check offset */
1364     if (max_ts_offset > 0) {
1365       if (ts_offset > 0 && !allow_positive_ts_offset) {
1366         GST_DEBUG_OBJECT (bin,
1367             "offset is positive (clocks are out of sync), ignoring");
1368         return;
1369       }
1370       if (ABS (ts_offset) > max_ts_offset) {
1371         GST_DEBUG_OBJECT (bin, "offset too large, ignoring");
1372         return;
1373       }
1374     }
1375
1376     g_object_set (stream->buffer, "ts-offset", ts_offset, NULL);
1377   }
1378   GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
1379       stream->ssrc, ts_offset);
1380 }
1381
1382 static void
1383 gst_rtp_bin_send_sync_event (GstRtpBinStream * stream)
1384 {
1385   if (stream->bin->send_sync_event) {
1386     GstEvent *event;
1387     GstPad *srcpad;
1388
1389     GST_DEBUG_OBJECT (stream->bin,
1390         "sending GstRTCPSRReceived event downstream");
1391
1392     event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
1393         gst_structure_new_empty ("GstRTCPSRReceived"));
1394
1395     srcpad = gst_element_get_static_pad (stream->buffer, "src");
1396     gst_pad_push_event (srcpad, event);
1397     gst_object_unref (srcpad);
1398   }
1399 }
1400
1401 /* associate a stream to the given CNAME. This will make sure all streams for
1402  * that CNAME are synchronized together.
1403  * Must be called with GST_RTP_BIN_LOCK */
1404 static void
1405 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
1406     guint8 * data, guint64 ntptime, guint64 last_extrtptime,
1407     guint64 base_rtptime, guint64 base_time, guint clock_rate,
1408     gint64 rtp_clock_base)
1409 {
1410   GstRtpBinClient *client;
1411   gboolean created;
1412   GSList *walk;
1413   GstClockTime running_time, running_time_rtp;
1414   guint64 ntpnstime;
1415
1416   /* first find or create the CNAME */
1417   client = get_client (bin, len, data, &created);
1418
1419   /* find stream in the client */
1420   for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1421     GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1422
1423     if (ostream == stream)
1424       break;
1425   }
1426   /* not found, add it to the list */
1427   if (walk == NULL) {
1428     GST_DEBUG_OBJECT (bin,
1429         "new association of SSRC %08x with client %p with CNAME %s",
1430         stream->ssrc, client, client->cname);
1431     client->streams = g_slist_prepend (client->streams, stream);
1432     client->nstreams++;
1433   } else {
1434     GST_DEBUG_OBJECT (bin,
1435         "found association of SSRC %08x with client %p with CNAME %s",
1436         stream->ssrc, client, client->cname);
1437   }
1438
1439   if (!GST_CLOCK_TIME_IS_VALID (last_extrtptime)) {
1440     GST_DEBUG_OBJECT (bin, "invalidated sync data");
1441     if (bin->rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1442       /* we don't need that data, so carry on,
1443        * but make some values look saner */
1444       last_extrtptime = base_rtptime;
1445     } else {
1446       /* nothing we can do with this data in this case */
1447       GST_DEBUG_OBJECT (bin, "bailing out");
1448       return;
1449     }
1450   }
1451
1452   /* Take the extended rtptime we found in the SR packet and map it to the
1453    * local rtptime. The local rtp time is used to construct timestamps on the
1454    * buffers so we will calculate what running_time corresponds to the RTP
1455    * timestamp in the SR packet. */
1456   running_time_rtp = last_extrtptime - base_rtptime;
1457
1458   GST_DEBUG_OBJECT (bin,
1459       "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
1460       ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d, "
1461       "clock-base %" G_GINT64_FORMAT, base_rtptime,
1462       last_extrtptime, running_time_rtp, clock_rate, rtp_clock_base);
1463
1464   /* calculate local RTP time in gstreamer timestamp, we essentially perform the
1465    * same conversion that a jitterbuffer would use to convert an rtp timestamp
1466    * into a corresponding gstreamer timestamp. Note that the base_time also
1467    * contains the drift between sender and receiver. */
1468   running_time =
1469       gst_util_uint64_scale_int (running_time_rtp, GST_SECOND, clock_rate);
1470   running_time += base_time;
1471
1472   /* convert ntptime to nanoseconds */
1473   ntpnstime = gst_util_uint64_scale (ntptime, GST_SECOND,
1474       (G_GINT64_CONSTANT (1) << 32));
1475
1476   stream->have_sync = TRUE;
1477
1478   GST_DEBUG_OBJECT (bin,
1479       "SR RTP running time %" G_GUINT64_FORMAT ", SR NTP %" G_GUINT64_FORMAT,
1480       running_time, ntpnstime);
1481
1482   /* recalc inter stream playout offset, but only if there is more than one
1483    * stream or we're doing NTP sync. */
1484   if (bin->ntp_sync) {
1485     gint64 ntpdiff, rtdiff;
1486     guint64 local_ntpnstime;
1487     GstClockTime local_running_time;
1488
1489     /* For NTP sync we need to first get a snapshot of running_time and NTP
1490      * time. We know at what running_time we play a certain RTP time, we also
1491      * calculated when we would play the RTP time in the SR packet. Now we need
1492      * to know how the running_time and the NTP time relate to eachother. */
1493     get_current_times (bin, &local_running_time, &local_ntpnstime);
1494
1495     /* see how far away the NTP time is. This is the difference between the
1496      * current NTP time and the NTP time in the last SR packet. */
1497     ntpdiff = local_ntpnstime - ntpnstime;
1498     /* see how far away the running_time is. This is the difference between the
1499      * current running_time and the running_time of the RTP timestamp in the
1500      * last SR packet. */
1501     rtdiff = local_running_time - running_time;
1502
1503     GST_DEBUG_OBJECT (bin,
1504         "local NTP time %" G_GUINT64_FORMAT ", SR NTP time %" G_GUINT64_FORMAT,
1505         local_ntpnstime, ntpnstime);
1506     GST_DEBUG_OBJECT (bin,
1507         "local running time %" G_GUINT64_FORMAT ", SR RTP running time %"
1508         G_GUINT64_FORMAT, local_running_time, running_time);
1509     GST_DEBUG_OBJECT (bin,
1510         "NTP diff %" G_GINT64_FORMAT ", RT diff %" G_GINT64_FORMAT, ntpdiff,
1511         rtdiff);
1512
1513     /* combine to get the final diff to apply to the running_time */
1514     stream->rt_delta = rtdiff - ntpdiff;
1515
1516     stream_set_ts_offset (bin, stream, stream->rt_delta, bin->max_ts_offset,
1517         0, FALSE);
1518   } else {
1519     gint64 min, rtp_min, clock_base = stream->clock_base;
1520     gboolean all_sync, use_rtp;
1521     gboolean rtcp_sync = g_atomic_int_get (&bin->rtcp_sync);
1522
1523     /* calculate delta between server and receiver. ntpnstime is created by
1524      * converting the ntptime in the last SR packet to a gstreamer timestamp. This
1525      * delta expresses the difference to our timeline and the server timeline. The
1526      * difference in itself doesn't mean much but we can combine the delta of
1527      * multiple streams to create a stream specific offset. */
1528     stream->rt_delta = ntpnstime - running_time;
1529
1530     /* calculate the min of all deltas, ignoring streams that did not yet have a
1531      * valid rt_delta because we did not yet receive an SR packet for those
1532      * streams.
1533      * We calculate the mininum because we would like to only apply positive
1534      * offsets to streams, delaying their playback instead of trying to speed up
1535      * other streams (which might be imposible when we have to create negative
1536      * latencies).
1537      * The stream that has the smallest diff is selected as the reference stream,
1538      * all other streams will have a positive offset to this difference. */
1539
1540     /* some alternative setting allow ignoring RTCP as much as possible,
1541      * for servers generating bogus ntp timeline */
1542     min = rtp_min = G_MAXINT64;
1543     use_rtp = FALSE;
1544     if (rtcp_sync == GST_RTP_BIN_RTCP_SYNC_RTP) {
1545       guint64 ext_base;
1546
1547       use_rtp = TRUE;
1548       /* signed version for convienience */
1549       clock_base = base_rtptime;
1550       /* deal with possible wrap-around */
1551       ext_base = base_rtptime;
1552       rtp_clock_base = gst_rtp_buffer_ext_timestamp (&ext_base, rtp_clock_base);
1553       /* sanity check; base rtp and provided clock_base should be close */
1554       if (rtp_clock_base >= clock_base) {
1555         if (rtp_clock_base - clock_base < 10 * clock_rate) {
1556           rtp_clock_base = base_time +
1557               gst_util_uint64_scale_int (rtp_clock_base - clock_base,
1558               GST_SECOND, clock_rate);
1559         } else {
1560           use_rtp = FALSE;
1561         }
1562       } else {
1563         if (clock_base - rtp_clock_base < 10 * clock_rate) {
1564           rtp_clock_base = base_time -
1565               gst_util_uint64_scale_int (clock_base - rtp_clock_base,
1566               GST_SECOND, clock_rate);
1567         } else {
1568           use_rtp = FALSE;
1569         }
1570       }
1571       /* warn and bail for clarity out if no sane values */
1572       if (!use_rtp) {
1573         GST_WARNING_OBJECT (bin, "unable to sync to provided rtptime");
1574         return;
1575       }
1576       /* store to track changes */
1577       clock_base = rtp_clock_base;
1578       /* generate a fake as before,
1579        * now equating rtptime obtained from RTP-Info,
1580        * where the large time represent the otherwise irrelevant npt/ntp time */
1581       stream->rtp_delta = (GST_SECOND << 28) - rtp_clock_base;
1582     } else {
1583       clock_base = rtp_clock_base;
1584     }
1585
1586     all_sync = TRUE;
1587     for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1588       GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1589
1590       if (!ostream->have_sync) {
1591         all_sync = FALSE;
1592         continue;
1593       }
1594
1595       /* change in current stream's base from previously init'ed value
1596        * leads to reset of all stream's base */
1597       if (stream != ostream && stream->clock_base >= 0 &&
1598           (stream->clock_base != clock_base)) {
1599         GST_DEBUG_OBJECT (bin, "reset upon clock base change");
1600         ostream->clock_base = -100 * GST_SECOND;
1601         ostream->rtp_delta = 0;
1602       }
1603
1604       if (ostream->rt_delta < min)
1605         min = ostream->rt_delta;
1606       if (ostream->rtp_delta < rtp_min)
1607         rtp_min = ostream->rtp_delta;
1608     }
1609
1610     /* arrange to re-sync for each stream upon significant change,
1611      * e.g. post-seek */
1612     all_sync = all_sync && (stream->clock_base == clock_base);
1613     stream->clock_base = clock_base;
1614
1615     /* may need init performed above later on, but nothing more to do now */
1616     if (client->nstreams <= 1)
1617       return;
1618
1619     GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT
1620         " all sync %d", client, min, all_sync);
1621     GST_DEBUG_OBJECT (bin, "rtcp sync mode %d, use_rtp %d", rtcp_sync, use_rtp);
1622
1623     switch (rtcp_sync) {
1624       case GST_RTP_BIN_RTCP_SYNC_RTP:
1625         if (!use_rtp)
1626           break;
1627         GST_DEBUG_OBJECT (bin, "using rtp generated reports; "
1628             "client %p min rtp delta %" G_GINT64_FORMAT, client, rtp_min);
1629         /* fall-through */
1630       case GST_RTP_BIN_RTCP_SYNC_INITIAL:
1631         /* if all have been synced already, do not bother further */
1632         if (all_sync) {
1633           GST_DEBUG_OBJECT (bin, "all streams already synced; done");
1634           return;
1635         }
1636         break;
1637       default:
1638         break;
1639     }
1640
1641     /* bail out if we adjusted recently enough */
1642     if (all_sync && (ntpnstime - bin->priv->last_ntpnstime) <
1643         bin->rtcp_sync_interval * GST_MSECOND) {
1644       GST_DEBUG_OBJECT (bin, "discarding RTCP sender packet for sync; "
1645           "previous sender info too recent "
1646           "(previous NTP %" G_GUINT64_FORMAT ")", bin->priv->last_ntpnstime);
1647       return;
1648     }
1649     bin->priv->last_ntpnstime = ntpnstime;
1650
1651     /* calculate offsets for each stream */
1652     for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1653       GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1654       gint64 ts_offset;
1655
1656       /* ignore streams for which we didn't receive an SR packet yet, we
1657        * can't synchronize them yet. We can however sync other streams just
1658        * fine. */
1659       if (!ostream->have_sync)
1660         continue;
1661
1662       /* calculate offset to our reference stream, this should always give a
1663        * positive number. */
1664       if (use_rtp)
1665         ts_offset = ostream->rtp_delta - rtp_min;
1666       else
1667         ts_offset = ostream->rt_delta - min;
1668
1669       stream_set_ts_offset (bin, ostream, ts_offset, bin->max_ts_offset,
1670           MIN_TS_OFFSET, TRUE);
1671     }
1672   }
1673   gst_rtp_bin_send_sync_event (stream);
1674
1675   return;
1676 }
1677
1678 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
1679   for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
1680           (b) = gst_rtcp_packet_move_to_next ((packet)))
1681
1682 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
1683   for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
1684           (b) = gst_rtcp_packet_sdes_next_item ((packet)))
1685
1686 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
1687   for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
1688           (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
1689
1690 static void
1691 gst_rtp_bin_handle_sync (GstElement * jitterbuffer, GstStructure * s,
1692     GstRtpBinStream * stream)
1693 {
1694   GstRtpBin *bin;
1695   GstRTCPPacket packet;
1696   guint32 ssrc;
1697   guint64 ntptime;
1698   gboolean have_sr, have_sdes;
1699   gboolean more;
1700   guint64 base_rtptime;
1701   guint64 base_time;
1702   guint clock_rate;
1703   guint64 clock_base;
1704   guint64 extrtptime;
1705   GstBuffer *buffer;
1706   GstRTCPBuffer rtcp = { NULL, };
1707
1708   bin = stream->bin;
1709
1710   GST_DEBUG_OBJECT (bin, "sync handler called");
1711
1712   /* get the last relation between the rtp timestamps and the gstreamer
1713    * timestamps. We get this info directly from the jitterbuffer which
1714    * constructs gstreamer timestamps from rtp timestamps and so it know exactly
1715    * what the current situation is. */
1716   base_rtptime =
1717       g_value_get_uint64 (gst_structure_get_value (s, "base-rtptime"));
1718   base_time = g_value_get_uint64 (gst_structure_get_value (s, "base-time"));
1719   clock_rate = g_value_get_uint (gst_structure_get_value (s, "clock-rate"));
1720   clock_base = g_value_get_uint64 (gst_structure_get_value (s, "clock-base"));
1721   extrtptime =
1722       g_value_get_uint64 (gst_structure_get_value (s, "sr-ext-rtptime"));
1723   buffer = gst_value_get_buffer (gst_structure_get_value (s, "sr-buffer"));
1724
1725   have_sr = FALSE;
1726   have_sdes = FALSE;
1727
1728   gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
1729
1730   GST_RTCP_BUFFER_FOR_PACKETS (more, &rtcp, &packet) {
1731     /* first packet must be SR or RR or else the validate would have failed */
1732     switch (gst_rtcp_packet_get_type (&packet)) {
1733       case GST_RTCP_TYPE_SR:
1734         /* only parse first. There is only supposed to be one SR in the packet
1735          * but we will deal with malformed packets gracefully */
1736         if (have_sr)
1737           break;
1738         /* get NTP and RTP times */
1739         gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, NULL,
1740             NULL, NULL);
1741
1742         GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
1743         /* ignore SR that is not ours */
1744         if (ssrc != stream->ssrc)
1745           continue;
1746
1747         have_sr = TRUE;
1748         break;
1749       case GST_RTCP_TYPE_SDES:
1750       {
1751         gboolean more_items, more_entries;
1752
1753         /* only deal with first SDES, there is only supposed to be one SDES in
1754          * the RTCP packet but we deal with bad packets gracefully. Also bail
1755          * out if we have not seen an SR item yet. */
1756         if (have_sdes || !have_sr)
1757           break;
1758
1759         GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
1760           /* skip items that are not about the SSRC of the sender */
1761           if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
1762             continue;
1763
1764           /* find the CNAME entry */
1765           GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
1766             GstRTCPSDESType type;
1767             guint8 len;
1768             guint8 *data;
1769
1770             gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
1771
1772             if (type == GST_RTCP_SDES_CNAME) {
1773               GST_RTP_BIN_LOCK (bin);
1774               /* associate the stream to CNAME */
1775               gst_rtp_bin_associate (bin, stream, len, data,
1776                   ntptime, extrtptime, base_rtptime, base_time, clock_rate,
1777                   clock_base);
1778               GST_RTP_BIN_UNLOCK (bin);
1779             }
1780           }
1781         }
1782         have_sdes = TRUE;
1783         break;
1784       }
1785       default:
1786         /* we can ignore these packets */
1787         break;
1788     }
1789   }
1790   gst_rtcp_buffer_unmap (&rtcp);
1791 }
1792
1793 /* create a new stream with @ssrc in @session. Must be called with
1794  * RTP_SESSION_LOCK. */
1795 static GstRtpBinStream *
1796 create_stream (GstRtpBinSession * session, guint32 ssrc)
1797 {
1798   GstElement *buffer, *demux = NULL;
1799   GstRtpBinStream *stream;
1800   GstRtpBin *rtpbin;
1801   GstState target;
1802
1803   rtpbin = session->bin;
1804
1805   if (g_slist_length (session->streams) >= rtpbin->max_streams)
1806     goto max_streams;
1807
1808   if (!(buffer = gst_element_factory_make ("rtpjitterbuffer", NULL)))
1809     goto no_jitterbuffer;
1810
1811   if (!rtpbin->ignore_pt) {
1812     if (!(demux = gst_element_factory_make ("rtpptdemux", NULL)))
1813       goto no_demux;
1814   }
1815
1816   stream = g_new0 (GstRtpBinStream, 1);
1817   stream->ssrc = ssrc;
1818   stream->bin = rtpbin;
1819   stream->session = session;
1820   stream->buffer = buffer;
1821   stream->demux = demux;
1822
1823   stream->have_sync = FALSE;
1824   stream->rt_delta = 0;
1825   stream->rtp_delta = 0;
1826   stream->percent = 100;
1827   stream->clock_base = -100 * GST_SECOND;
1828   session->streams = g_slist_prepend (session->streams, stream);
1829
1830   /* provide clock_rate to the jitterbuffer when needed */
1831   stream->buffer_ptreq_sig = g_signal_connect (buffer, "request-pt-map",
1832       (GCallback) pt_map_requested, session);
1833   stream->buffer_ntpstop_sig = g_signal_connect (buffer, "on-npt-stop",
1834       (GCallback) on_npt_stop, stream);
1835
1836   g_object_set_data (G_OBJECT (buffer), "GstRTPBin.session", session);
1837   g_object_set_data (G_OBJECT (buffer), "GstRTPBin.stream", stream);
1838
1839   /* configure latency and packet lost */
1840   g_object_set (buffer, "latency", rtpbin->latency_ms, NULL);
1841   g_object_set (buffer, "drop-on-latency", rtpbin->drop_on_latency, NULL);
1842   g_object_set (buffer, "do-lost", rtpbin->do_lost, NULL);
1843   g_object_set (buffer, "mode", rtpbin->buffer_mode, NULL);
1844   g_object_set (buffer, "do-retransmission", rtpbin->do_retransmission, NULL);
1845   g_object_set (buffer, "max-rtcp-rtp-time-diff",
1846       rtpbin->max_rtcp_rtp_time_diff, NULL);
1847   g_object_set (buffer, "max-dropout-time", rtpbin->max_dropout_time,
1848       "max-misorder-time", rtpbin->max_misorder_time, NULL);
1849   g_object_set (buffer, "rfc7273-sync", rtpbin->rfc7273_sync, NULL);
1850   g_object_set (buffer, "max-ts-offset-adjustment",
1851       rtpbin->max_ts_offset_adjustment, NULL);
1852
1853   g_signal_emit (rtpbin, gst_rtp_bin_signals[SIGNAL_NEW_JITTERBUFFER], 0,
1854       buffer, session->id, ssrc);
1855
1856   if (!rtpbin->ignore_pt)
1857     gst_bin_add (GST_BIN_CAST (rtpbin), demux);
1858   gst_bin_add (GST_BIN_CAST (rtpbin), buffer);
1859
1860   /* link stuff */
1861   if (demux)
1862     gst_element_link_pads_full (buffer, "src", demux, "sink",
1863         GST_PAD_LINK_CHECK_NOTHING);
1864
1865   if (rtpbin->buffering) {
1866     guint64 last_out;
1867
1868     GST_INFO_OBJECT (rtpbin,
1869         "bin is buffering, set jitterbuffer as not active");
1870     g_signal_emit_by_name (buffer, "set-active", FALSE, (gint64) 0, &last_out);
1871   }
1872
1873
1874   GST_OBJECT_LOCK (rtpbin);
1875   target = GST_STATE_TARGET (rtpbin);
1876   GST_OBJECT_UNLOCK (rtpbin);
1877
1878   /* from sink to source */
1879   if (demux)
1880     gst_element_set_state (demux, target);
1881
1882   gst_element_set_state (buffer, target);
1883
1884   return stream;
1885
1886   /* ERRORS */
1887 max_streams:
1888   {
1889     GST_WARNING_OBJECT (rtpbin, "stream exeeds maximum (%d)",
1890         rtpbin->max_streams);
1891     return NULL;
1892   }
1893 no_jitterbuffer:
1894   {
1895     g_warning ("rtpbin: could not create rtpjitterbuffer element");
1896     return NULL;
1897   }
1898 no_demux:
1899   {
1900     gst_object_unref (buffer);
1901     g_warning ("rtpbin: could not create rtpptdemux element");
1902     return NULL;
1903   }
1904 }
1905
1906 /* called with RTP_BIN_LOCK */
1907 static void
1908 free_stream (GstRtpBinStream * stream, GstRtpBin * bin)
1909 {
1910   GSList *clients, *next_client;
1911
1912   GST_DEBUG_OBJECT (bin, "freeing stream %p", stream);
1913
1914   if (stream->demux) {
1915     g_signal_handler_disconnect (stream->demux, stream->demux_newpad_sig);
1916     g_signal_handler_disconnect (stream->demux, stream->demux_ptreq_sig);
1917     g_signal_handler_disconnect (stream->demux, stream->demux_ptchange_sig);
1918   }
1919   g_signal_handler_disconnect (stream->buffer, stream->buffer_handlesync_sig);
1920   g_signal_handler_disconnect (stream->buffer, stream->buffer_ptreq_sig);
1921   g_signal_handler_disconnect (stream->buffer, stream->buffer_ntpstop_sig);
1922
1923   if (stream->demux)
1924     gst_element_set_locked_state (stream->demux, TRUE);
1925   gst_element_set_locked_state (stream->buffer, TRUE);
1926
1927   if (stream->demux)
1928     gst_element_set_state (stream->demux, GST_STATE_NULL);
1929   gst_element_set_state (stream->buffer, GST_STATE_NULL);
1930
1931   /* now remove this signal, we need this while going to NULL because it to
1932    * do some cleanups */
1933   if (stream->demux)
1934     g_signal_handler_disconnect (stream->demux, stream->demux_padremoved_sig);
1935
1936   gst_bin_remove (GST_BIN_CAST (bin), stream->buffer);
1937   if (stream->demux)
1938     gst_bin_remove (GST_BIN_CAST (bin), stream->demux);
1939
1940   for (clients = bin->clients; clients; clients = next_client) {
1941     GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
1942     GSList *streams, *next_stream;
1943
1944     next_client = g_slist_next (clients);
1945
1946     for (streams = client->streams; streams; streams = next_stream) {
1947       GstRtpBinStream *ostream = (GstRtpBinStream *) streams->data;
1948
1949       next_stream = g_slist_next (streams);
1950
1951       if (ostream == stream) {
1952         client->streams = g_slist_delete_link (client->streams, streams);
1953         /* If this was the last stream belonging to this client,
1954          * clean up the client. */
1955         if (--client->nstreams == 0) {
1956           bin->clients = g_slist_delete_link (bin->clients, clients);
1957           free_client (client, bin);
1958           break;
1959         }
1960       }
1961     }
1962   }
1963   g_free (stream);
1964 }
1965
1966 /* GObject vmethods */
1967 static void gst_rtp_bin_dispose (GObject * object);
1968 static void gst_rtp_bin_finalize (GObject * object);
1969 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
1970     const GValue * value, GParamSpec * pspec);
1971 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
1972     GValue * value, GParamSpec * pspec);
1973
1974 /* GstElement vmethods */
1975 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
1976     GstStateChange transition);
1977 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
1978     GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
1979 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
1980 static void gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message);
1981
1982 #define gst_rtp_bin_parent_class parent_class
1983 G_DEFINE_TYPE_WITH_PRIVATE (GstRtpBin, gst_rtp_bin, GST_TYPE_BIN);
1984
1985 static gboolean
1986 _gst_element_accumulator (GSignalInvocationHint * ihint,
1987     GValue * return_accu, const GValue * handler_return, gpointer dummy)
1988 {
1989   GstElement *element;
1990
1991   element = g_value_get_object (handler_return);
1992   GST_DEBUG ("got element %" GST_PTR_FORMAT, element);
1993
1994   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
1995     g_value_set_object (return_accu, element);
1996
1997   /* stop emission if we have an element */
1998   return (element == NULL);
1999 }
2000
2001 static gboolean
2002 _gst_caps_accumulator (GSignalInvocationHint * ihint,
2003     GValue * return_accu, const GValue * handler_return, gpointer dummy)
2004 {
2005   GstCaps *caps;
2006
2007   caps = g_value_get_boxed (handler_return);
2008   GST_DEBUG ("got caps %" GST_PTR_FORMAT, caps);
2009
2010   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
2011     g_value_set_boxed (return_accu, caps);
2012
2013   /* stop emission if we have a caps */
2014   return (caps == NULL);
2015 }
2016
2017 static void
2018 gst_rtp_bin_class_init (GstRtpBinClass * klass)
2019 {
2020   GObjectClass *gobject_class;
2021   GstElementClass *gstelement_class;
2022   GstBinClass *gstbin_class;
2023
2024   gobject_class = (GObjectClass *) klass;
2025   gstelement_class = (GstElementClass *) klass;
2026   gstbin_class = (GstBinClass *) klass;
2027
2028   gobject_class->dispose = gst_rtp_bin_dispose;
2029   gobject_class->finalize = gst_rtp_bin_finalize;
2030   gobject_class->set_property = gst_rtp_bin_set_property;
2031   gobject_class->get_property = gst_rtp_bin_get_property;
2032
2033   g_object_class_install_property (gobject_class, PROP_LATENCY,
2034       g_param_spec_uint ("latency", "Buffer latency in ms",
2035           "Default amount of ms to buffer in the jitterbuffers", 0,
2036           G_MAXUINT, DEFAULT_LATENCY_MS,
2037           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2038
2039   g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
2040       g_param_spec_boolean ("drop-on-latency",
2041           "Drop buffers when maximum latency is reached",
2042           "Tells the jitterbuffer to never exceed the given latency in size",
2043           DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2044
2045   /**
2046    * GstRtpBin::request-pt-map:
2047    * @rtpbin: the object which received the signal
2048    * @session: the session
2049    * @pt: the pt
2050    *
2051    * Request the payload type as #GstCaps for @pt in @session.
2052    */
2053   gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
2054       g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
2055       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
2056       _gst_caps_accumulator, NULL, g_cclosure_marshal_generic, GST_TYPE_CAPS,
2057       2, G_TYPE_UINT, G_TYPE_UINT);
2058
2059     /**
2060    * GstRtpBin::payload-type-change:
2061    * @rtpbin: the object which received the signal
2062    * @session: the session
2063    * @pt: the pt
2064    *
2065    * Signal that the current payload type changed to @pt in @session.
2066    */
2067   gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
2068       g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
2069       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, payload_type_change),
2070       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2071       G_TYPE_UINT);
2072
2073   /**
2074    * GstRtpBin::clear-pt-map:
2075    * @rtpbin: the object which received the signal
2076    *
2077    * Clear all previously cached pt-mapping obtained with
2078    * #GstRtpBin::request-pt-map.
2079    */
2080   gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
2081       g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
2082       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2083           clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
2084       0, G_TYPE_NONE);
2085
2086   /**
2087    * GstRtpBin::reset-sync:
2088    * @rtpbin: the object which received the signal
2089    *
2090    * Reset all currently configured lip-sync parameters and require new SR
2091    * packets for all streams before lip-sync is attempted again.
2092    */
2093   gst_rtp_bin_signals[SIGNAL_RESET_SYNC] =
2094       g_signal_new ("reset-sync", G_TYPE_FROM_CLASS (klass),
2095       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2096           reset_sync), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
2097       0, G_TYPE_NONE);
2098
2099   /**
2100    * GstRtpBin::get-session:
2101    * @rtpbin: the object which received the signal
2102    * @id: the session id
2103    *
2104    * Request the related GstRtpSession as #GstElement related with session @id.
2105    *
2106    * Since: 1.8
2107    */
2108   gst_rtp_bin_signals[SIGNAL_GET_SESSION] =
2109       g_signal_new ("get-session", G_TYPE_FROM_CLASS (klass),
2110       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2111           get_session), NULL, NULL, g_cclosure_marshal_generic,
2112       GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2113
2114   /**
2115    * GstRtpBin::get-internal-session:
2116    * @rtpbin: the object which received the signal
2117    * @id: the session id
2118    *
2119    * Request the internal RTPSession object as #GObject in session @id.
2120    */
2121   gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_SESSION] =
2122       g_signal_new ("get-internal-session", G_TYPE_FROM_CLASS (klass),
2123       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2124           get_internal_session), NULL, NULL, g_cclosure_marshal_generic,
2125       RTP_TYPE_SESSION, 1, G_TYPE_UINT);
2126
2127   /**
2128    * GstRtpBin::get-internal-storage:
2129    * @rtpbin: the object which received the signal
2130    * @id: the session id
2131    *
2132    * Request the internal RTPStorage object as #GObject in session @id.
2133    *
2134    * Since: 1.14
2135    */
2136   gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_STORAGE] =
2137       g_signal_new ("get-internal-storage", G_TYPE_FROM_CLASS (klass),
2138       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2139           get_internal_storage), NULL, NULL, g_cclosure_marshal_generic,
2140       G_TYPE_OBJECT, 1, G_TYPE_UINT);
2141
2142   /**
2143    * GstRtpBin::get-storage:
2144    * @rtpbin: the object which received the signal
2145    * @id: the session id
2146    *
2147    * Request the RTPStorage element as #GObject in session @id.
2148    *
2149    * Since: 1.16
2150    */
2151   gst_rtp_bin_signals[SIGNAL_GET_STORAGE] =
2152       g_signal_new ("get-storage", G_TYPE_FROM_CLASS (klass),
2153       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
2154           get_storage), NULL, NULL, g_cclosure_marshal_generic,
2155       GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2156
2157   /**
2158    * GstRtpBin::on-new-ssrc:
2159    * @rtpbin: the object which received the signal
2160    * @session: the session
2161    * @ssrc: the SSRC
2162    *
2163    * Notify of a new SSRC that entered @session.
2164    */
2165   gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
2166       g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
2167       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
2168       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2169       G_TYPE_UINT);
2170   /**
2171    * GstRtpBin::on-ssrc-collision:
2172    * @rtpbin: the object which received the signal
2173    * @session: the session
2174    * @ssrc: the SSRC
2175    *
2176    * Notify when we have an SSRC collision
2177    */
2178   gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
2179       g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
2180       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
2181       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2182       G_TYPE_UINT);
2183   /**
2184    * GstRtpBin::on-ssrc-validated:
2185    * @rtpbin: the object which received the signal
2186    * @session: the session
2187    * @ssrc: the SSRC
2188    *
2189    * Notify of a new SSRC that became validated.
2190    */
2191   gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
2192       g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
2193       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
2194       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2195       G_TYPE_UINT);
2196   /**
2197    * GstRtpBin::on-ssrc-active:
2198    * @rtpbin: the object which received the signal
2199    * @session: the session
2200    * @ssrc: the SSRC
2201    *
2202    * Notify of a SSRC that is active, i.e., sending RTCP.
2203    */
2204   gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
2205       g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
2206       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
2207       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2208       G_TYPE_UINT);
2209   /**
2210    * GstRtpBin::on-ssrc-sdes:
2211    * @rtpbin: the object which received the signal
2212    * @session: the session
2213    * @ssrc: the SSRC
2214    *
2215    * Notify of a SSRC that is active, i.e., sending RTCP.
2216    */
2217   gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES] =
2218       g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
2219       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
2220       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2221       G_TYPE_UINT);
2222
2223   /**
2224    * GstRtpBin::on-bye-ssrc:
2225    * @rtpbin: the object which received the signal
2226    * @session: the session
2227    * @ssrc: the SSRC
2228    *
2229    * Notify of an SSRC that became inactive because of a BYE packet.
2230    */
2231   gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
2232       g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
2233       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
2234       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2235       G_TYPE_UINT);
2236   /**
2237    * GstRtpBin::on-bye-timeout:
2238    * @rtpbin: the object which received the signal
2239    * @session: the session
2240    * @ssrc: the SSRC
2241    *
2242    * Notify of an SSRC that has timed out because of BYE
2243    */
2244   gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
2245       g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
2246       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
2247       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2248       G_TYPE_UINT);
2249   /**
2250    * GstRtpBin::on-timeout:
2251    * @rtpbin: the object which received the signal
2252    * @session: the session
2253    * @ssrc: the SSRC
2254    *
2255    * Notify of an SSRC that has timed out
2256    */
2257   gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
2258       g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
2259       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
2260       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2261       G_TYPE_UINT);
2262   /**
2263    * GstRtpBin::on-sender-timeout:
2264    * @rtpbin: the object which received the signal
2265    * @session: the session
2266    * @ssrc: the SSRC
2267    *
2268    * Notify of a sender SSRC that has timed out and became a receiver
2269    */
2270   gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT] =
2271       g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
2272       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_sender_timeout),
2273       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2274       G_TYPE_UINT);
2275
2276   /**
2277    * GstRtpBin::on-npt-stop:
2278    * @rtpbin: the object which received the signal
2279    * @session: the session
2280    * @ssrc: the SSRC
2281    *
2282    * Notify that SSRC sender has sent data up to the configured NPT stop time.
2283    */
2284   gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP] =
2285       g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
2286       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_npt_stop),
2287       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2288       G_TYPE_UINT);
2289
2290   /**
2291    * GstRtpBin::request-rtp-encoder:
2292    * @rtpbin: the object which received the signal
2293    * @session: the session
2294    *
2295    * Request an RTP encoder element for the given @session. The encoder
2296    * element will be added to the bin if not previously added.
2297    *
2298    * If no handler is connected, no encoder will be used.
2299    *
2300    * Since: 1.4
2301    */
2302   gst_rtp_bin_signals[SIGNAL_REQUEST_RTP_ENCODER] =
2303       g_signal_new ("request-rtp-encoder", G_TYPE_FROM_CLASS (klass),
2304       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2305           request_rtp_encoder), _gst_element_accumulator, NULL,
2306       g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2307
2308   /**
2309    * GstRtpBin::request-rtp-decoder:
2310    * @rtpbin: the object which received the signal
2311    * @session: the session
2312    *
2313    * Request an RTP decoder element for the given @session. The decoder
2314    * element will be added to the bin if not previously added.
2315    *
2316    * If no handler is connected, no encoder will be used.
2317    *
2318    * Since: 1.4
2319    */
2320   gst_rtp_bin_signals[SIGNAL_REQUEST_RTP_DECODER] =
2321       g_signal_new ("request-rtp-decoder", G_TYPE_FROM_CLASS (klass),
2322       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2323           request_rtp_decoder), _gst_element_accumulator, NULL,
2324       g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2325
2326   /**
2327    * GstRtpBin::request-rtcp-encoder:
2328    * @rtpbin: the object which received the signal
2329    * @session: the session
2330    *
2331    * Request an RTCP encoder element for the given @session. The encoder
2332    * element will be added to the bin if not previously added.
2333    *
2334    * If no handler is connected, no encoder will be used.
2335    *
2336    * Since: 1.4
2337    */
2338   gst_rtp_bin_signals[SIGNAL_REQUEST_RTCP_ENCODER] =
2339       g_signal_new ("request-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
2340       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2341           request_rtcp_encoder), _gst_element_accumulator, NULL,
2342       g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2343
2344   /**
2345    * GstRtpBin::request-rtcp-decoder:
2346    * @rtpbin: the object which received the signal
2347    * @session: the session
2348    *
2349    * Request an RTCP decoder element for the given @session. The decoder
2350    * element will be added to the bin if not previously added.
2351    *
2352    * If no handler is connected, no encoder will be used.
2353    *
2354    * Since: 1.4
2355    */
2356   gst_rtp_bin_signals[SIGNAL_REQUEST_RTCP_DECODER] =
2357       g_signal_new ("request-rtcp-decoder", G_TYPE_FROM_CLASS (klass),
2358       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2359           request_rtcp_decoder), _gst_element_accumulator, NULL,
2360       g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2361
2362   /**
2363    * GstRtpBin::new-jitterbuffer:
2364    * @rtpbin: the object which received the signal
2365    * @jitterbuffer: the new jitterbuffer
2366    * @session: the session
2367    * @ssrc: the SSRC
2368    *
2369    * Notify that a new @jitterbuffer was created for @session and @ssrc.
2370    * This signal can, for example, be used to configure @jitterbuffer.
2371    *
2372    * Since: 1.4
2373    */
2374   gst_rtp_bin_signals[SIGNAL_NEW_JITTERBUFFER] =
2375       g_signal_new ("new-jitterbuffer", G_TYPE_FROM_CLASS (klass),
2376       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2377           new_jitterbuffer), NULL, NULL, g_cclosure_marshal_generic,
2378       G_TYPE_NONE, 3, GST_TYPE_ELEMENT, G_TYPE_UINT, G_TYPE_UINT);
2379
2380   /**
2381    * GstRtpBin::new-storage:
2382    * @rtpbin: the object which received the signal
2383    * @storage: the new storage
2384    * @session: the session
2385    *
2386    * Notify that a new @storage was created for @session.
2387    * This signal can, for example, be used to configure @storage.
2388    *
2389    * Since: 1.14
2390    */
2391   gst_rtp_bin_signals[SIGNAL_NEW_STORAGE] =
2392       g_signal_new ("new-storage", G_TYPE_FROM_CLASS (klass),
2393       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2394           new_storage), NULL, NULL, g_cclosure_marshal_generic,
2395       G_TYPE_NONE, 2, GST_TYPE_ELEMENT, G_TYPE_UINT);
2396
2397   /**
2398    * GstRtpBin::request-aux-sender:
2399    * @rtpbin: the object which received the signal
2400    * @session: the session
2401    *
2402    * Request an AUX sender element for the given @session. The AUX
2403    * element will be added to the bin.
2404    *
2405    * If no handler is connected, no AUX element will be used.
2406    *
2407    * Since: 1.4
2408    */
2409   gst_rtp_bin_signals[SIGNAL_REQUEST_AUX_SENDER] =
2410       g_signal_new ("request-aux-sender", G_TYPE_FROM_CLASS (klass),
2411       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2412           request_aux_sender), _gst_element_accumulator, NULL,
2413       g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2414
2415   /**
2416    * GstRtpBin::request-aux-receiver:
2417    * @rtpbin: the object which received the signal
2418    * @session: the session
2419    *
2420    * Request an AUX receiver element for the given @session. The AUX
2421    * element will be added to the bin.
2422    *
2423    * If no handler is connected, no AUX element will be used.
2424    *
2425    * Since: 1.4
2426    */
2427   gst_rtp_bin_signals[SIGNAL_REQUEST_AUX_RECEIVER] =
2428       g_signal_new ("request-aux-receiver", G_TYPE_FROM_CLASS (klass),
2429       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2430           request_aux_receiver), _gst_element_accumulator, NULL,
2431       g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2432
2433   /**
2434    * GstRtpBin::request-fec-decoder:
2435    * @rtpbin: the object which received the signal
2436    * @session: the session index
2437    *
2438    * Request a FEC decoder element for the given @session. The element
2439    * will be added to the bin after the pt demuxer.
2440    *
2441    * If no handler is connected, no FEC decoder will be used.
2442    *
2443    * Since: 1.14
2444    */
2445   gst_rtp_bin_signals[SIGNAL_REQUEST_FEC_DECODER] =
2446       g_signal_new ("request-fec-decoder", G_TYPE_FROM_CLASS (klass),
2447       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2448           request_fec_decoder), _gst_element_accumulator, NULL,
2449       g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2450
2451   /**
2452    * GstRtpBin::request-fec-encoder:
2453    * @rtpbin: the object which received the signal
2454    * @session: the session index
2455    *
2456    * Request a FEC encoder element for the given @session. The element
2457    * will be added to the bin after the RTPSession.
2458    *
2459    * If no handler is connected, no FEC encoder will be used.
2460    *
2461    * Since: 1.14
2462    */
2463   gst_rtp_bin_signals[SIGNAL_REQUEST_FEC_ENCODER] =
2464       g_signal_new ("request-fec-encoder", G_TYPE_FROM_CLASS (klass),
2465       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2466           request_fec_encoder), _gst_element_accumulator, NULL,
2467       g_cclosure_marshal_generic, GST_TYPE_ELEMENT, 1, G_TYPE_UINT);
2468
2469   /**
2470    * GstRtpBin::on-new-sender-ssrc:
2471    * @rtpbin: the object which received the signal
2472    * @session: the session
2473    * @ssrc: the sender SSRC
2474    *
2475    * Notify of a new sender SSRC that entered @session.
2476    *
2477    * Since: 1.8
2478    */
2479   gst_rtp_bin_signals[SIGNAL_ON_NEW_SENDER_SSRC] =
2480       g_signal_new ("on-new-sender-ssrc", G_TYPE_FROM_CLASS (klass),
2481       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_sender_ssrc),
2482       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
2483       G_TYPE_UINT);
2484   /**
2485    * GstRtpBin::on-sender-ssrc-active:
2486    * @rtpbin: the object which received the signal
2487    * @session: the session
2488    * @ssrc: the sender SSRC
2489    *
2490    * Notify of a sender SSRC that is active, i.e., sending RTCP.
2491    *
2492    * Since: 1.8
2493    */
2494   gst_rtp_bin_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE] =
2495       g_signal_new ("on-sender-ssrc-active", G_TYPE_FROM_CLASS (klass),
2496       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2497           on_sender_ssrc_active), NULL, NULL, g_cclosure_marshal_generic,
2498       G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
2499
2500
2501   /**
2502    * GstRtpBin::on-bundled-ssrc:
2503    * @rtpbin: the object which received the signal
2504    * @ssrc: the bundled SSRC
2505    *
2506    * Notify of a new incoming bundled SSRC. If no handler is connected to the
2507    * signal then the #GstRtpSession created for the recv_rtp_sink_\%u
2508    * request pad will be managing this new SSRC. However if there is a handler
2509    * connected then the application can decided to dispatch this new stream to
2510    * another session by providing its ID as return value of the handler. This
2511    * can be particularly useful to keep retransmission SSRCs grouped with the
2512    * session for which they handle retransmission.
2513    *
2514    * Since: 1.12
2515    */
2516   gst_rtp_bin_signals[SIGNAL_ON_BUNDLED_SSRC] =
2517       g_signal_new ("on-bundled-ssrc", G_TYPE_FROM_CLASS (klass),
2518       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass,
2519           on_bundled_ssrc), NULL, NULL,
2520       g_cclosure_marshal_generic, G_TYPE_UINT, 1, G_TYPE_UINT);
2521
2522
2523   g_object_class_install_property (gobject_class, PROP_SDES,
2524       g_param_spec_boxed ("sdes", "SDES",
2525           "The SDES items of this session",
2526           GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2527
2528   g_object_class_install_property (gobject_class, PROP_DO_LOST,
2529       g_param_spec_boolean ("do-lost", "Do Lost",
2530           "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
2531           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2532
2533   g_object_class_install_property (gobject_class, PROP_AUTOREMOVE,
2534       g_param_spec_boolean ("autoremove", "Auto Remove",
2535           "Automatically remove timed out sources", DEFAULT_AUTOREMOVE,
2536           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2537
2538   g_object_class_install_property (gobject_class, PROP_IGNORE_PT,
2539       g_param_spec_boolean ("ignore-pt", "Ignore PT",
2540           "Do not demultiplex based on PT values", DEFAULT_IGNORE_PT,
2541           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2542
2543   g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
2544       g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
2545           "Use the pipeline running-time to set the NTP time in the RTCP SR messages "
2546           "(DEPRECATED: Use ntp-time-source property)",
2547           DEFAULT_USE_PIPELINE_CLOCK,
2548           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
2549   /**
2550    * GstRtpBin:buffer-mode:
2551    *
2552    * Control the buffering and timestamping mode used by the jitterbuffer.
2553    */
2554   g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
2555       g_param_spec_enum ("buffer-mode", "Buffer Mode",
2556           "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
2557           DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2558   /**
2559    * GstRtpBin:ntp-sync:
2560    *
2561    * Set the NTP time from the sender reports as the running-time on the
2562    * buffers. When both the sender and receiver have sychronized
2563    * running-time, i.e. when the clock and base-time is shared
2564    * between the receivers and the and the senders, this option can be
2565    * used to synchronize receivers on multiple machines.
2566    */
2567   g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
2568       g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
2569           "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
2570           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2571
2572   /**
2573    * GstRtpBin:rtcp-sync:
2574    *
2575    * If not synchronizing (directly) to the NTP clock, determines how to sync
2576    * the various streams.
2577    */
2578   g_object_class_install_property (gobject_class, PROP_RTCP_SYNC,
2579       g_param_spec_enum ("rtcp-sync", "RTCP Sync",
2580           "Use of RTCP SR in synchronization", GST_RTP_BIN_RTCP_SYNC_TYPE,
2581           DEFAULT_RTCP_SYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2582
2583   /**
2584    * GstRtpBin:rtcp-sync-interval:
2585    *
2586    * Determines how often to sync streams using RTCP data.
2587    */
2588   g_object_class_install_property (gobject_class, PROP_RTCP_SYNC_INTERVAL,
2589       g_param_spec_uint ("rtcp-sync-interval", "RTCP Sync Interval",
2590           "RTCP SR interval synchronization (ms) (0 = always)",
2591           0, G_MAXUINT, DEFAULT_RTCP_SYNC_INTERVAL,
2592           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2593
2594   g_object_class_install_property (gobject_class, PROP_DO_SYNC_EVENT,
2595       g_param_spec_boolean ("do-sync-event", "Do Sync Event",
2596           "Send event downstream when a stream is synchronized to the sender",
2597           DEFAULT_DO_SYNC_EVENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2598
2599   /**
2600    * GstRtpBin:do-retransmission:
2601    *
2602    * Enables RTP retransmission on all streams. To control retransmission on
2603    * a per-SSRC basis, connect to the #GstRtpBin::new-jitterbuffer signal and
2604    * set the #GstRtpJitterBuffer::do-retransmission property on the
2605    * #GstRtpJitterBuffer object instead.
2606    */
2607   g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
2608       g_param_spec_boolean ("do-retransmission", "Do retransmission",
2609           "Enable retransmission on all streams",
2610           DEFAULT_DO_RETRANSMISSION,
2611           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2612
2613   /**
2614    * GstRtpBin:rtp-profile:
2615    *
2616    * Sets the default RTP profile of newly created RTP sessions. The
2617    * profile can be changed afterwards on a per-session basis.
2618    */
2619   g_object_class_install_property (gobject_class, PROP_RTP_PROFILE,
2620       g_param_spec_enum ("rtp-profile", "RTP Profile",
2621           "Default RTP profile of newly created sessions",
2622           GST_TYPE_RTP_PROFILE, DEFAULT_RTP_PROFILE,
2623           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2624
2625   g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
2626       g_param_spec_enum ("ntp-time-source", "NTP Time Source",
2627           "NTP time source for RTCP packets",
2628           gst_rtp_ntp_time_source_get_type (), DEFAULT_NTP_TIME_SOURCE,
2629           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2630
2631   g_object_class_install_property (gobject_class, PROP_RTCP_SYNC_SEND_TIME,
2632       g_param_spec_boolean ("rtcp-sync-send-time", "RTCP Sync Send Time",
2633           "Use send time or capture time for RTCP sync "
2634           "(TRUE = send time, FALSE = capture time)",
2635           DEFAULT_RTCP_SYNC_SEND_TIME,
2636           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2637
2638   g_object_class_install_property (gobject_class, PROP_MAX_RTCP_RTP_TIME_DIFF,
2639       g_param_spec_int ("max-rtcp-rtp-time-diff", "Max RTCP RTP Time Diff",
2640           "Maximum amount of time in ms that the RTP time in RTCP SRs "
2641           "is allowed to be ahead (-1 disabled)", -1, G_MAXINT,
2642           DEFAULT_MAX_RTCP_RTP_TIME_DIFF,
2643           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2644
2645   g_object_class_install_property (gobject_class, PROP_MAX_DROPOUT_TIME,
2646       g_param_spec_uint ("max-dropout-time", "Max dropout time",
2647           "The maximum time (milliseconds) of missing packets tolerated.",
2648           0, G_MAXUINT, DEFAULT_MAX_DROPOUT_TIME,
2649           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2650
2651   g_object_class_install_property (gobject_class, PROP_MAX_MISORDER_TIME,
2652       g_param_spec_uint ("max-misorder-time", "Max misorder time",
2653           "The maximum time (milliseconds) of misordered packets tolerated.",
2654           0, G_MAXUINT, DEFAULT_MAX_MISORDER_TIME,
2655           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2656
2657   g_object_class_install_property (gobject_class, PROP_RFC7273_SYNC,
2658       g_param_spec_boolean ("rfc7273-sync", "Sync on RFC7273 clock",
2659           "Synchronize received streams to the RFC7273 clock "
2660           "(requires clock and offset to be provided)", DEFAULT_RFC7273_SYNC,
2661           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2662
2663   g_object_class_install_property (gobject_class, PROP_MAX_STREAMS,
2664       g_param_spec_uint ("max-streams", "Max Streams",
2665           "The maximum number of streams to create for one session",
2666           0, G_MAXUINT, DEFAULT_MAX_STREAMS,
2667           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2668
2669   /**
2670    * GstRtpBin:max-ts-offset-adjustment:
2671    *
2672    * Syncing time stamps to NTP time adds a time offset. This parameter
2673    * specifies the maximum number of nanoseconds per frame that this time offset
2674    * may be adjusted with. This is used to avoid sudden large changes to time
2675    * stamps.
2676    *
2677    * Since: 1.14
2678    */
2679   g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET_ADJUSTMENT,
2680       g_param_spec_uint64 ("max-ts-offset-adjustment",
2681           "Max Timestamp Offset Adjustment",
2682           "The maximum number of nanoseconds per frame that time stamp offsets "
2683           "may be adjusted (0 = no limit).", 0, G_MAXUINT64,
2684           DEFAULT_MAX_TS_OFFSET_ADJUSTMENT, G_PARAM_READWRITE |
2685           G_PARAM_STATIC_STRINGS));
2686
2687   /**
2688    * GstRtpBin:max-ts-offset:
2689    *
2690    * Used to set an upper limit of how large a time offset may be. This
2691    * is used to protect against unrealistic values as a result of either
2692    * client,server or clock issues.
2693    *
2694    * Since: 1.14
2695    */
2696   g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET,
2697       g_param_spec_int64 ("max-ts-offset", "Max TS Offset",
2698           "The maximum absolute value of the time offset in (nanoseconds). "
2699           "Note, if the ntp-sync parameter is set the default value is "
2700           "changed to 0 (no limit)", 0, G_MAXINT64, DEFAULT_MAX_TS_OFFSET,
2701           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2702
2703   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
2704   gstelement_class->request_new_pad =
2705       GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
2706   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
2707
2708   /* sink pads */
2709   gst_element_class_add_static_pad_template (gstelement_class,
2710       &rtpbin_recv_rtp_sink_template);
2711   gst_element_class_add_static_pad_template (gstelement_class,
2712       &rtpbin_recv_rtcp_sink_template);
2713   gst_element_class_add_static_pad_template (gstelement_class,
2714       &rtpbin_send_rtp_sink_template);
2715
2716   /* src pads */
2717   gst_element_class_add_static_pad_template (gstelement_class,
2718       &rtpbin_recv_rtp_src_template);
2719   gst_element_class_add_static_pad_template (gstelement_class,
2720       &rtpbin_send_rtcp_src_template);
2721   gst_element_class_add_static_pad_template (gstelement_class,
2722       &rtpbin_send_rtp_src_template);
2723
2724   gst_element_class_set_static_metadata (gstelement_class, "RTP Bin",
2725       "Filter/Network/RTP",
2726       "Real-Time Transport Protocol bin",
2727       "Wim Taymans <wim.taymans@gmail.com>");
2728
2729   gstbin_class->handle_message = GST_DEBUG_FUNCPTR (gst_rtp_bin_handle_message);
2730
2731   klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
2732   klass->reset_sync = GST_DEBUG_FUNCPTR (gst_rtp_bin_reset_sync);
2733   klass->get_session = GST_DEBUG_FUNCPTR (gst_rtp_bin_get_session);
2734   klass->get_internal_session =
2735       GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_session);
2736   klass->get_storage = GST_DEBUG_FUNCPTR (gst_rtp_bin_get_storage);
2737   klass->get_internal_storage =
2738       GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_storage);
2739   klass->request_rtp_encoder = GST_DEBUG_FUNCPTR (gst_rtp_bin_request_encoder);
2740   klass->request_rtp_decoder = GST_DEBUG_FUNCPTR (gst_rtp_bin_request_decoder);
2741   klass->request_rtcp_encoder = GST_DEBUG_FUNCPTR (gst_rtp_bin_request_encoder);
2742   klass->request_rtcp_decoder = GST_DEBUG_FUNCPTR (gst_rtp_bin_request_decoder);
2743
2744   GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
2745 }
2746
2747 static void
2748 gst_rtp_bin_init (GstRtpBin * rtpbin)
2749 {
2750   gchar *cname;
2751
2752   rtpbin->priv = gst_rtp_bin_get_instance_private (rtpbin);
2753   g_mutex_init (&rtpbin->priv->bin_lock);
2754   g_mutex_init (&rtpbin->priv->dyn_lock);
2755
2756   rtpbin->latency_ms = DEFAULT_LATENCY_MS;
2757   rtpbin->latency_ns = DEFAULT_LATENCY_MS * GST_MSECOND;
2758   rtpbin->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
2759   rtpbin->do_lost = DEFAULT_DO_LOST;
2760   rtpbin->ignore_pt = DEFAULT_IGNORE_PT;
2761   rtpbin->ntp_sync = DEFAULT_NTP_SYNC;
2762   rtpbin->rtcp_sync = DEFAULT_RTCP_SYNC;
2763   rtpbin->rtcp_sync_interval = DEFAULT_RTCP_SYNC_INTERVAL;
2764   rtpbin->priv->autoremove = DEFAULT_AUTOREMOVE;
2765   rtpbin->buffer_mode = DEFAULT_BUFFER_MODE;
2766   rtpbin->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
2767   rtpbin->send_sync_event = DEFAULT_DO_SYNC_EVENT;
2768   rtpbin->do_retransmission = DEFAULT_DO_RETRANSMISSION;
2769   rtpbin->rtp_profile = DEFAULT_RTP_PROFILE;
2770   rtpbin->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
2771   rtpbin->rtcp_sync_send_time = DEFAULT_RTCP_SYNC_SEND_TIME;
2772   rtpbin->max_rtcp_rtp_time_diff = DEFAULT_MAX_RTCP_RTP_TIME_DIFF;
2773   rtpbin->max_dropout_time = DEFAULT_MAX_DROPOUT_TIME;
2774   rtpbin->max_misorder_time = DEFAULT_MAX_MISORDER_TIME;
2775   rtpbin->rfc7273_sync = DEFAULT_RFC7273_SYNC;
2776   rtpbin->max_streams = DEFAULT_MAX_STREAMS;
2777   rtpbin->max_ts_offset_adjustment = DEFAULT_MAX_TS_OFFSET_ADJUSTMENT;
2778   rtpbin->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
2779   rtpbin->max_ts_offset_is_set = FALSE;
2780
2781   /* some default SDES entries */
2782   cname = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
2783   rtpbin->sdes = gst_structure_new ("application/x-rtp-source-sdes",
2784       "cname", G_TYPE_STRING, cname, "tool", G_TYPE_STRING, "GStreamer", NULL);
2785   g_free (cname);
2786 }
2787
2788 static void
2789 gst_rtp_bin_dispose (GObject * object)
2790 {
2791   GstRtpBin *rtpbin;
2792
2793   rtpbin = GST_RTP_BIN (object);
2794
2795   GST_RTP_BIN_LOCK (rtpbin);
2796   GST_DEBUG_OBJECT (object, "freeing sessions");
2797   g_slist_foreach (rtpbin->sessions, (GFunc) free_session, rtpbin);
2798   g_slist_free (rtpbin->sessions);
2799   rtpbin->sessions = NULL;
2800   GST_RTP_BIN_UNLOCK (rtpbin);
2801
2802   G_OBJECT_CLASS (parent_class)->dispose (object);
2803 }
2804
2805 static void
2806 gst_rtp_bin_finalize (GObject * object)
2807 {
2808   GstRtpBin *rtpbin;
2809
2810   rtpbin = GST_RTP_BIN (object);
2811
2812   if (rtpbin->sdes)
2813     gst_structure_free (rtpbin->sdes);
2814
2815   g_mutex_clear (&rtpbin->priv->bin_lock);
2816   g_mutex_clear (&rtpbin->priv->dyn_lock);
2817
2818   G_OBJECT_CLASS (parent_class)->finalize (object);
2819 }
2820
2821
2822 static void
2823 gst_rtp_bin_set_sdes_struct (GstRtpBin * bin, const GstStructure * sdes)
2824 {
2825   GSList *item;
2826
2827   if (sdes == NULL)
2828     return;
2829
2830   GST_RTP_BIN_LOCK (bin);
2831
2832   GST_OBJECT_LOCK (bin);
2833   if (bin->sdes)
2834     gst_structure_free (bin->sdes);
2835   bin->sdes = gst_structure_copy (sdes);
2836   GST_OBJECT_UNLOCK (bin);
2837
2838   /* store in all sessions */
2839   for (item = bin->sessions; item; item = g_slist_next (item)) {
2840     GstRtpBinSession *session = item->data;
2841     g_object_set (session->session, "sdes", sdes, NULL);
2842   }
2843
2844   GST_RTP_BIN_UNLOCK (bin);
2845 }
2846
2847 static GstStructure *
2848 gst_rtp_bin_get_sdes_struct (GstRtpBin * bin)
2849 {
2850   GstStructure *result;
2851
2852   GST_OBJECT_LOCK (bin);
2853   result = gst_structure_copy (bin->sdes);
2854   GST_OBJECT_UNLOCK (bin);
2855
2856   return result;
2857 }
2858
2859 static void
2860 gst_rtp_bin_set_property (GObject * object, guint prop_id,
2861     const GValue * value, GParamSpec * pspec)
2862 {
2863   GstRtpBin *rtpbin;
2864
2865   rtpbin = GST_RTP_BIN (object);
2866
2867   switch (prop_id) {
2868     case PROP_LATENCY:
2869       GST_RTP_BIN_LOCK (rtpbin);
2870       rtpbin->latency_ms = g_value_get_uint (value);
2871       rtpbin->latency_ns = rtpbin->latency_ms * GST_MSECOND;
2872       GST_RTP_BIN_UNLOCK (rtpbin);
2873       /* propagate the property down to the jitterbuffer */
2874       gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "latency", value);
2875       break;
2876     case PROP_DROP_ON_LATENCY:
2877       GST_RTP_BIN_LOCK (rtpbin);
2878       rtpbin->drop_on_latency = g_value_get_boolean (value);
2879       GST_RTP_BIN_UNLOCK (rtpbin);
2880       /* propagate the property down to the jitterbuffer */
2881       gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2882           "drop-on-latency", value);
2883       break;
2884     case PROP_SDES:
2885       gst_rtp_bin_set_sdes_struct (rtpbin, g_value_get_boxed (value));
2886       break;
2887     case PROP_DO_LOST:
2888       GST_RTP_BIN_LOCK (rtpbin);
2889       rtpbin->do_lost = g_value_get_boolean (value);
2890       GST_RTP_BIN_UNLOCK (rtpbin);
2891       gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "do-lost", value);
2892       break;
2893     case PROP_NTP_SYNC:
2894       rtpbin->ntp_sync = g_value_get_boolean (value);
2895       /* The default value of max_ts_offset depends on ntp_sync. If user
2896        * hasn't set it then change default value */
2897       if (!rtpbin->max_ts_offset_is_set) {
2898         if (rtpbin->ntp_sync) {
2899           rtpbin->max_ts_offset = 0;
2900         } else {
2901           rtpbin->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
2902         }
2903       }
2904       break;
2905     case PROP_RTCP_SYNC:
2906       g_atomic_int_set (&rtpbin->rtcp_sync, g_value_get_enum (value));
2907       break;
2908     case PROP_RTCP_SYNC_INTERVAL:
2909       rtpbin->rtcp_sync_interval = g_value_get_uint (value);
2910       break;
2911     case PROP_IGNORE_PT:
2912       rtpbin->ignore_pt = g_value_get_boolean (value);
2913       break;
2914     case PROP_AUTOREMOVE:
2915       rtpbin->priv->autoremove = g_value_get_boolean (value);
2916       break;
2917     case PROP_USE_PIPELINE_CLOCK:
2918     {
2919       GSList *sessions;
2920       GST_RTP_BIN_LOCK (rtpbin);
2921       rtpbin->use_pipeline_clock = g_value_get_boolean (value);
2922       for (sessions = rtpbin->sessions; sessions;
2923           sessions = g_slist_next (sessions)) {
2924         GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2925
2926         g_object_set (G_OBJECT (session->session),
2927             "use-pipeline-clock", rtpbin->use_pipeline_clock, NULL);
2928       }
2929       GST_RTP_BIN_UNLOCK (rtpbin);
2930     }
2931       break;
2932     case PROP_DO_SYNC_EVENT:
2933       rtpbin->send_sync_event = g_value_get_boolean (value);
2934       break;
2935     case PROP_BUFFER_MODE:
2936       GST_RTP_BIN_LOCK (rtpbin);
2937       rtpbin->buffer_mode = g_value_get_enum (value);
2938       GST_RTP_BIN_UNLOCK (rtpbin);
2939       /* propagate the property down to the jitterbuffer */
2940       gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "mode", value);
2941       break;
2942     case PROP_DO_RETRANSMISSION:
2943       GST_RTP_BIN_LOCK (rtpbin);
2944       rtpbin->do_retransmission = g_value_get_boolean (value);
2945       GST_RTP_BIN_UNLOCK (rtpbin);
2946       gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2947           "do-retransmission", value);
2948       break;
2949     case PROP_RTP_PROFILE:
2950       rtpbin->rtp_profile = g_value_get_enum (value);
2951       break;
2952     case PROP_NTP_TIME_SOURCE:{
2953       GSList *sessions;
2954       GST_RTP_BIN_LOCK (rtpbin);
2955       rtpbin->ntp_time_source = g_value_get_enum (value);
2956       for (sessions = rtpbin->sessions; sessions;
2957           sessions = g_slist_next (sessions)) {
2958         GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2959
2960         g_object_set (G_OBJECT (session->session),
2961             "ntp-time-source", rtpbin->ntp_time_source, NULL);
2962       }
2963       GST_RTP_BIN_UNLOCK (rtpbin);
2964       break;
2965     }
2966     case PROP_RTCP_SYNC_SEND_TIME:{
2967       GSList *sessions;
2968       GST_RTP_BIN_LOCK (rtpbin);
2969       rtpbin->rtcp_sync_send_time = g_value_get_boolean (value);
2970       for (sessions = rtpbin->sessions; sessions;
2971           sessions = g_slist_next (sessions)) {
2972         GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2973
2974         g_object_set (G_OBJECT (session->session),
2975             "rtcp-sync-send-time", rtpbin->rtcp_sync_send_time, NULL);
2976       }
2977       GST_RTP_BIN_UNLOCK (rtpbin);
2978       break;
2979     }
2980     case PROP_MAX_RTCP_RTP_TIME_DIFF:
2981       GST_RTP_BIN_LOCK (rtpbin);
2982       rtpbin->max_rtcp_rtp_time_diff = g_value_get_int (value);
2983       GST_RTP_BIN_UNLOCK (rtpbin);
2984       gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2985           "max-rtcp-rtp-time-diff", value);
2986       break;
2987     case PROP_MAX_DROPOUT_TIME:
2988       GST_RTP_BIN_LOCK (rtpbin);
2989       rtpbin->max_dropout_time = g_value_get_uint (value);
2990       GST_RTP_BIN_UNLOCK (rtpbin);
2991       gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
2992           "max-dropout-time", value);
2993       gst_rtp_bin_propagate_property_to_session (rtpbin, "max-dropout-time",
2994           value);
2995       break;
2996     case PROP_MAX_MISORDER_TIME:
2997       GST_RTP_BIN_LOCK (rtpbin);
2998       rtpbin->max_misorder_time = g_value_get_uint (value);
2999       GST_RTP_BIN_UNLOCK (rtpbin);
3000       gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
3001           "max-misorder-time", value);
3002       gst_rtp_bin_propagate_property_to_session (rtpbin, "max-misorder-time",
3003           value);
3004       break;
3005     case PROP_RFC7273_SYNC:
3006       rtpbin->rfc7273_sync = g_value_get_boolean (value);
3007       gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
3008           "rfc7273-sync", value);
3009       break;
3010     case PROP_MAX_STREAMS:
3011       rtpbin->max_streams = g_value_get_uint (value);
3012       break;
3013     case PROP_MAX_TS_OFFSET_ADJUSTMENT:
3014       rtpbin->max_ts_offset_adjustment = g_value_get_uint64 (value);
3015       gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin,
3016           "max-ts-offset-adjustment", value);
3017       break;
3018     case PROP_MAX_TS_OFFSET:
3019       rtpbin->max_ts_offset = g_value_get_int64 (value);
3020       rtpbin->max_ts_offset_is_set = TRUE;
3021       break;
3022     default:
3023       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3024       break;
3025   }
3026 }
3027
3028 static void
3029 gst_rtp_bin_get_property (GObject * object, guint prop_id,
3030     GValue * value, GParamSpec * pspec)
3031 {
3032   GstRtpBin *rtpbin;
3033
3034   rtpbin = GST_RTP_BIN (object);
3035
3036   switch (prop_id) {
3037     case PROP_LATENCY:
3038       GST_RTP_BIN_LOCK (rtpbin);
3039       g_value_set_uint (value, rtpbin->latency_ms);
3040       GST_RTP_BIN_UNLOCK (rtpbin);
3041       break;
3042     case PROP_DROP_ON_LATENCY:
3043       GST_RTP_BIN_LOCK (rtpbin);
3044       g_value_set_boolean (value, rtpbin->drop_on_latency);
3045       GST_RTP_BIN_UNLOCK (rtpbin);
3046       break;
3047     case PROP_SDES:
3048       g_value_take_boxed (value, gst_rtp_bin_get_sdes_struct (rtpbin));
3049       break;
3050     case PROP_DO_LOST:
3051       GST_RTP_BIN_LOCK (rtpbin);
3052       g_value_set_boolean (value, rtpbin->do_lost);
3053       GST_RTP_BIN_UNLOCK (rtpbin);
3054       break;
3055     case PROP_IGNORE_PT:
3056       g_value_set_boolean (value, rtpbin->ignore_pt);
3057       break;
3058     case PROP_NTP_SYNC:
3059       g_value_set_boolean (value, rtpbin->ntp_sync);
3060       break;
3061     case PROP_RTCP_SYNC:
3062       g_value_set_enum (value, g_atomic_int_get (&rtpbin->rtcp_sync));
3063       break;
3064     case PROP_RTCP_SYNC_INTERVAL:
3065       g_value_set_uint (value, rtpbin->rtcp_sync_interval);
3066       break;
3067     case PROP_AUTOREMOVE:
3068       g_value_set_boolean (value, rtpbin->priv->autoremove);
3069       break;
3070     case PROP_BUFFER_MODE:
3071       g_value_set_enum (value, rtpbin->buffer_mode);
3072       break;
3073     case PROP_USE_PIPELINE_CLOCK:
3074       g_value_set_boolean (value, rtpbin->use_pipeline_clock);
3075       break;
3076     case PROP_DO_SYNC_EVENT:
3077       g_value_set_boolean (value, rtpbin->send_sync_event);
3078       break;
3079     case PROP_DO_RETRANSMISSION:
3080       GST_RTP_BIN_LOCK (rtpbin);
3081       g_value_set_boolean (value, rtpbin->do_retransmission);
3082       GST_RTP_BIN_UNLOCK (rtpbin);
3083       break;
3084     case PROP_RTP_PROFILE:
3085       g_value_set_enum (value, rtpbin->rtp_profile);
3086       break;
3087     case PROP_NTP_TIME_SOURCE:
3088       g_value_set_enum (value, rtpbin->ntp_time_source);
3089       break;
3090     case PROP_RTCP_SYNC_SEND_TIME:
3091       g_value_set_boolean (value, rtpbin->rtcp_sync_send_time);
3092       break;
3093     case PROP_MAX_RTCP_RTP_TIME_DIFF:
3094       GST_RTP_BIN_LOCK (rtpbin);
3095       g_value_set_int (value, rtpbin->max_rtcp_rtp_time_diff);
3096       GST_RTP_BIN_UNLOCK (rtpbin);
3097       break;
3098     case PROP_MAX_DROPOUT_TIME:
3099       g_value_set_uint (value, rtpbin->max_dropout_time);
3100       break;
3101     case PROP_MAX_MISORDER_TIME:
3102       g_value_set_uint (value, rtpbin->max_misorder_time);
3103       break;
3104     case PROP_RFC7273_SYNC:
3105       g_value_set_boolean (value, rtpbin->rfc7273_sync);
3106       break;
3107     case PROP_MAX_STREAMS:
3108       g_value_set_uint (value, rtpbin->max_streams);
3109       break;
3110     case PROP_MAX_TS_OFFSET_ADJUSTMENT:
3111       g_value_set_uint64 (value, rtpbin->max_ts_offset_adjustment);
3112       break;
3113     case PROP_MAX_TS_OFFSET:
3114       g_value_set_int64 (value, rtpbin->max_ts_offset);
3115       break;
3116     default:
3117       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3118       break;
3119   }
3120 }
3121
3122 static void
3123 gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
3124 {
3125   GstRtpBin *rtpbin;
3126
3127   rtpbin = GST_RTP_BIN (bin);
3128
3129   switch (GST_MESSAGE_TYPE (message)) {
3130     case GST_MESSAGE_ELEMENT:
3131     {
3132       const GstStructure *s = gst_message_get_structure (message);
3133
3134       /* we change the structure name and add the session ID to it */
3135       if (gst_structure_has_name (s, "application/x-rtp-source-sdes")) {
3136         GstRtpBinSession *sess;
3137
3138         /* find the session we set it as object data */
3139         sess = g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
3140             "GstRTPBin.session");
3141
3142         if (G_LIKELY (sess)) {
3143           message = gst_message_make_writable (message);
3144           s = gst_message_get_structure (message);
3145           gst_structure_set ((GstStructure *) s, "session", G_TYPE_UINT,
3146               sess->id, NULL);
3147         }
3148       }
3149       GST_BIN_CLASS (parent_class)->handle_message (bin, message);
3150       break;
3151     }
3152     case GST_MESSAGE_BUFFERING:
3153     {
3154       gint percent;
3155       gint min_percent = 100;
3156       GSList *sessions, *streams;
3157       GstRtpBinStream *stream;
3158       gboolean change = FALSE, active = FALSE;
3159       GstClockTime min_out_time;
3160       GstBufferingMode mode;
3161       gint avg_in, avg_out;
3162       gint64 buffering_left;
3163
3164       gst_message_parse_buffering (message, &percent);
3165       gst_message_parse_buffering_stats (message, &mode, &avg_in, &avg_out,
3166           &buffering_left);
3167
3168       stream =
3169           g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
3170           "GstRTPBin.stream");
3171
3172       GST_DEBUG_OBJECT (bin, "got percent %d from stream %p", percent, stream);
3173
3174       /* get the stream */
3175       if (G_LIKELY (stream)) {
3176         GST_RTP_BIN_LOCK (rtpbin);
3177         /* fill in the percent */
3178         stream->percent = percent;
3179
3180         /* calculate the min value for all streams */
3181         for (sessions = rtpbin->sessions; sessions;
3182             sessions = g_slist_next (sessions)) {
3183           GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
3184
3185           GST_RTP_SESSION_LOCK (session);
3186           if (session->streams) {
3187             for (streams = session->streams; streams;
3188                 streams = g_slist_next (streams)) {
3189               GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
3190
3191               GST_DEBUG_OBJECT (bin, "stream %p percent %d", stream,
3192                   stream->percent);
3193
3194               /* find min percent */
3195               if (min_percent > stream->percent)
3196                 min_percent = stream->percent;
3197             }
3198           } else {
3199             GST_INFO_OBJECT (bin,
3200                 "session has no streams, setting min_percent to 0");
3201             min_percent = 0;
3202           }
3203           GST_RTP_SESSION_UNLOCK (session);
3204         }
3205         GST_DEBUG_OBJECT (bin, "min percent %d", min_percent);
3206
3207         if (rtpbin->buffering) {
3208           if (min_percent == 100) {
3209             rtpbin->buffering = FALSE;
3210             active = TRUE;
3211             change = TRUE;
3212           }
3213         } else {
3214           if (min_percent < 100) {
3215             /* pause the streams */
3216             rtpbin->buffering = TRUE;
3217             active = FALSE;
3218             change = TRUE;
3219           }
3220         }
3221         GST_RTP_BIN_UNLOCK (rtpbin);
3222
3223         gst_message_unref (message);
3224
3225         /* make a new buffering message with the min value */
3226         message =
3227             gst_message_new_buffering (GST_OBJECT_CAST (bin), min_percent);
3228         gst_message_set_buffering_stats (message, mode, avg_in, avg_out,
3229             buffering_left);
3230
3231         if (G_UNLIKELY (change)) {
3232           GstClock *clock;
3233           guint64 running_time = 0;
3234           guint64 offset = 0;
3235
3236           /* figure out the running time when we have a clock */
3237           if (G_LIKELY ((clock =
3238                       gst_element_get_clock (GST_ELEMENT_CAST (bin))))) {
3239             guint64 now, base_time;
3240
3241             now = gst_clock_get_time (clock);
3242             base_time = gst_element_get_base_time (GST_ELEMENT_CAST (bin));
3243             running_time = now - base_time;
3244             gst_object_unref (clock);
3245           }
3246           GST_DEBUG_OBJECT (bin,
3247               "running time now %" GST_TIME_FORMAT,
3248               GST_TIME_ARGS (running_time));
3249
3250           GST_RTP_BIN_LOCK (rtpbin);
3251
3252           /* when we reactivate, calculate the offsets so that all streams have
3253            * an output time that is at least as big as the running_time */
3254           offset = 0;
3255           if (active) {
3256             if (running_time > rtpbin->buffer_start) {
3257               offset = running_time - rtpbin->buffer_start;
3258               if (offset >= rtpbin->latency_ns)
3259                 offset -= rtpbin->latency_ns;
3260               else
3261                 offset = 0;
3262             }
3263           }
3264
3265           /* pause all streams */
3266           min_out_time = -1;
3267           for (sessions = rtpbin->sessions; sessions;
3268               sessions = g_slist_next (sessions)) {
3269             GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
3270
3271             GST_RTP_SESSION_LOCK (session);
3272             for (streams = session->streams; streams;
3273                 streams = g_slist_next (streams)) {
3274               GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
3275               GstElement *element = stream->buffer;
3276               guint64 last_out;
3277
3278               g_signal_emit_by_name (element, "set-active", active, offset,
3279                   &last_out);
3280
3281               if (!active) {
3282                 g_object_get (element, "percent", &stream->percent, NULL);
3283
3284                 if (last_out == -1)
3285                   last_out = 0;
3286                 if (min_out_time == -1 || last_out < min_out_time)
3287                   min_out_time = last_out;
3288               }
3289
3290               GST_DEBUG_OBJECT (bin,
3291                   "setting %p to %d, offset %" GST_TIME_FORMAT ", last %"
3292                   GST_TIME_FORMAT ", percent %d", element, active,
3293                   GST_TIME_ARGS (offset), GST_TIME_ARGS (last_out),
3294                   stream->percent);
3295             }
3296             GST_RTP_SESSION_UNLOCK (session);
3297           }
3298           GST_DEBUG_OBJECT (bin,
3299               "min out time %" GST_TIME_FORMAT, GST_TIME_ARGS (min_out_time));
3300
3301           /* the buffer_start is the min out time of all paused jitterbuffers */
3302           if (!active)
3303             rtpbin->buffer_start = min_out_time;
3304
3305           GST_RTP_BIN_UNLOCK (rtpbin);
3306         }
3307       }
3308       GST_BIN_CLASS (parent_class)->handle_message (bin, message);
3309       break;
3310     }
3311     default:
3312     {
3313       GST_BIN_CLASS (parent_class)->handle_message (bin, message);
3314       break;
3315     }
3316   }
3317 }
3318
3319 static GstStateChangeReturn
3320 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
3321 {
3322   GstStateChangeReturn res;
3323   GstRtpBin *rtpbin;
3324   GstRtpBinPrivate *priv;
3325
3326   rtpbin = GST_RTP_BIN (element);
3327   priv = rtpbin->priv;
3328
3329   switch (transition) {
3330     case GST_STATE_CHANGE_NULL_TO_READY:
3331       break;
3332     case GST_STATE_CHANGE_READY_TO_PAUSED:
3333       priv->last_ntpnstime = 0;
3334       GST_LOG_OBJECT (rtpbin, "clearing shutdown flag");
3335       g_atomic_int_set (&priv->shutdown, 0);
3336       break;
3337     case GST_STATE_CHANGE_PAUSED_TO_READY:
3338       GST_LOG_OBJECT (rtpbin, "setting shutdown flag");
3339       g_atomic_int_set (&priv->shutdown, 1);
3340       /* wait for all callbacks to end by taking the lock. No new callbacks will
3341        * be able to happen as we set the shutdown flag. */
3342       GST_RTP_BIN_DYN_LOCK (rtpbin);
3343       GST_LOG_OBJECT (rtpbin, "dynamic lock taken, we can continue shutdown");
3344       GST_RTP_BIN_DYN_UNLOCK (rtpbin);
3345       break;
3346     default:
3347       break;
3348   }
3349
3350   res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3351
3352   switch (transition) {
3353     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3354       break;
3355     case GST_STATE_CHANGE_PAUSED_TO_READY:
3356       break;
3357     case GST_STATE_CHANGE_READY_TO_NULL:
3358       break;
3359     default:
3360       break;
3361   }
3362   return res;
3363 }
3364
3365 static GstElement *
3366 session_request_element (GstRtpBinSession * session, guint signal)
3367 {
3368   GstElement *element = NULL;
3369   GstRtpBin *bin = session->bin;
3370
3371   g_signal_emit (bin, gst_rtp_bin_signals[signal], 0, session->id, &element);
3372
3373   if (element) {
3374     if (!bin_manage_element (bin, element))
3375       goto manage_failed;
3376     session->elements = g_slist_prepend (session->elements, element);
3377   }
3378   return element;
3379
3380   /* ERRORS */
3381 manage_failed:
3382   {
3383     GST_WARNING_OBJECT (bin, "unable to manage element");
3384     gst_object_unref (element);
3385     return NULL;
3386   }
3387 }
3388
3389 static gboolean
3390 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
3391 {
3392   GstPad *gpad = GST_PAD_CAST (user_data);
3393
3394   GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
3395   gst_pad_store_sticky_event (gpad, *event);
3396
3397   return TRUE;
3398 }
3399
3400 /* a new pad (SSRC) was created in @session. This signal is emitted from the
3401  * payload demuxer. */
3402 static void
3403 new_payload_found (GstElement * element, guint pt, GstPad * pad,
3404     GstRtpBinStream * stream)
3405 {
3406   GstRtpBin *rtpbin;
3407   GstElementClass *klass;
3408   GstPadTemplate *templ;
3409   gchar *padname;
3410   GstPad *gpad;
3411
3412   rtpbin = stream->bin;
3413
3414   GST_DEBUG_OBJECT (rtpbin, "new payload pad %u", pt);
3415
3416   pad = gst_object_ref (pad);
3417
3418   if (stream->session->storage) {
3419     GstElement *fec_decoder =
3420         session_request_element (stream->session, SIGNAL_REQUEST_FEC_DECODER);
3421
3422     if (fec_decoder) {
3423       GstPad *sinkpad, *srcpad;
3424       GstPadLinkReturn ret;
3425
3426       sinkpad = gst_element_get_static_pad (fec_decoder, "sink");
3427
3428       if (!sinkpad)
3429         goto fec_decoder_sink_failed;
3430
3431       ret = gst_pad_link (pad, sinkpad);
3432       gst_object_unref (sinkpad);
3433
3434       if (ret != GST_PAD_LINK_OK)
3435         goto fec_decoder_link_failed;
3436
3437       srcpad = gst_element_get_static_pad (fec_decoder, "src");
3438
3439       if (!srcpad)
3440         goto fec_decoder_src_failed;
3441
3442       gst_pad_sticky_events_foreach (pad, copy_sticky_events, srcpad);
3443       gst_object_unref (pad);
3444       pad = srcpad;
3445     }
3446   }
3447
3448   GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
3449
3450   /* ghost the pad to the parent */
3451   klass = GST_ELEMENT_GET_CLASS (rtpbin);
3452   templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
3453   padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
3454       stream->session->id, stream->ssrc, pt);
3455   gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
3456   g_free (padname);
3457   g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", gpad);
3458
3459   gst_pad_set_active (gpad, TRUE);
3460   GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
3461
3462   gst_pad_sticky_events_foreach (pad, copy_sticky_events, gpad);
3463   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
3464
3465 done:
3466   gst_object_unref (pad);
3467
3468   return;
3469
3470 shutdown:
3471   {
3472     GST_DEBUG ("ignoring, we are shutting down");
3473     goto done;
3474   }
3475 fec_decoder_sink_failed:
3476   {
3477     g_warning ("rtpbin: failed to get fec encoder sink pad for session %u",
3478         stream->session->id);
3479     goto done;
3480   }
3481 fec_decoder_src_failed:
3482   {
3483     g_warning ("rtpbin: failed to get fec encoder src pad for session %u",
3484         stream->session->id);
3485     goto done;
3486   }
3487 fec_decoder_link_failed:
3488   {
3489     g_warning ("rtpbin: failed to link fec decoder for session %u",
3490         stream->session->id);
3491     goto done;
3492   }
3493 }
3494
3495 static void
3496 payload_pad_removed (GstElement * element, GstPad * pad,
3497     GstRtpBinStream * stream)
3498 {
3499   GstRtpBin *rtpbin;
3500   GstPad *gpad;
3501
3502   rtpbin = stream->bin;
3503
3504   GST_DEBUG ("payload pad removed");
3505
3506   GST_RTP_BIN_DYN_LOCK (rtpbin);
3507   if ((gpad = g_object_get_data (G_OBJECT (pad), "GstRTPBin.ghostpad"))) {
3508     g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", NULL);
3509
3510     gst_pad_set_active (gpad, FALSE);
3511     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), gpad);
3512   }
3513   GST_RTP_BIN_DYN_UNLOCK (rtpbin);
3514 }
3515
3516 static GstCaps *
3517 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
3518 {
3519   GstRtpBin *rtpbin;
3520   GstCaps *caps;
3521
3522   rtpbin = session->bin;
3523
3524   GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %u in session %u", pt,
3525       session->id);
3526
3527   caps = get_pt_map (session, pt);
3528   if (!caps)
3529     goto no_caps;
3530
3531   return caps;
3532
3533   /* ERRORS */
3534 no_caps:
3535   {
3536     GST_DEBUG_OBJECT (rtpbin, "could not get caps");
3537     return NULL;
3538   }
3539 }
3540
3541 static GstCaps *
3542 ptdemux_pt_map_requested (GstElement * element, guint pt,
3543     GstRtpBinSession * session)
3544 {
3545   GstCaps *ret = pt_map_requested (element, pt, session);
3546
3547   if (ret && gst_caps_get_size (ret) == 1) {
3548     const GstStructure *s = gst_caps_get_structure (ret, 0);
3549     gboolean is_fec;
3550
3551     if (gst_structure_get_boolean (s, "is-fec", &is_fec) && is_fec) {
3552       GValue v = G_VALUE_INIT;
3553       GValue v2 = G_VALUE_INIT;
3554
3555       GST_INFO_OBJECT (session->bin, "Will ignore FEC pt %u in session %u", pt,
3556           session->id);
3557       g_value_init (&v, GST_TYPE_ARRAY);
3558       g_value_init (&v2, G_TYPE_INT);
3559       g_object_get_property (G_OBJECT (element), "ignored-payload-types", &v);
3560       g_value_set_int (&v2, pt);
3561       gst_value_array_append_value (&v, &v2);
3562       g_value_unset (&v2);
3563       g_object_set_property (G_OBJECT (element), "ignored-payload-types", &v);
3564       g_value_unset (&v);
3565     }
3566   }
3567
3568   return ret;
3569 }
3570
3571 static void
3572 payload_type_change (GstElement * element, guint pt, GstRtpBinSession * session)
3573 {
3574   GST_DEBUG_OBJECT (session->bin,
3575       "emiting signal for pt type changed to %u in session %u", pt,
3576       session->id);
3577
3578   g_signal_emit (session->bin, gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE],
3579       0, session->id, pt);
3580 }
3581
3582 /* emitted when caps changed for the session */
3583 static void
3584 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
3585 {
3586   GstRtpBin *bin;
3587   GstCaps *caps;
3588   gint payload;
3589   const GstStructure *s;
3590
3591   bin = session->bin;
3592
3593   g_object_get (pad, "caps", &caps, NULL);
3594
3595   if (caps == NULL)
3596     return;
3597
3598   GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
3599
3600   s = gst_caps_get_structure (caps, 0);
3601
3602   /* get payload, finish when it's not there */
3603   if (!gst_structure_get_int (s, "payload", &payload)) {
3604     gst_caps_unref (caps);
3605     return;
3606   }
3607
3608   GST_RTP_SESSION_LOCK (session);
3609   GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
3610   g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
3611   GST_RTP_SESSION_UNLOCK (session);
3612 }
3613
3614 /* a new pad (SSRC) was created in @session */
3615 static void
3616 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
3617     GstRtpBinSession * session)
3618 {
3619   GstRtpBin *rtpbin;
3620   GstRtpBinStream *stream;
3621   GstPad *sinkpad, *srcpad;
3622   gchar *padname;
3623
3624   rtpbin = session->bin;
3625
3626   GST_DEBUG_OBJECT (rtpbin, "new SSRC pad %08x, %s:%s", ssrc,
3627       GST_DEBUG_PAD_NAME (pad));
3628
3629   GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
3630
3631   GST_RTP_SESSION_LOCK (session);
3632
3633   /* create new stream */
3634   stream = create_stream (session, ssrc);
3635   if (!stream)
3636     goto no_stream;
3637
3638   /* get pad and link */
3639   GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTP");
3640   padname = g_strdup_printf ("src_%u", ssrc);
3641   srcpad = gst_element_get_static_pad (element, padname);
3642   g_free (padname);
3643   sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
3644   gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_NOTHING);
3645   gst_object_unref (sinkpad);
3646   gst_object_unref (srcpad);
3647
3648   GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTCP");
3649   padname = g_strdup_printf ("rtcp_src_%u", ssrc);
3650   srcpad = gst_element_get_static_pad (element, padname);
3651   g_free (padname);
3652   sinkpad = gst_element_get_request_pad (stream->buffer, "sink_rtcp");
3653   gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_NOTHING);
3654   gst_object_unref (sinkpad);
3655   gst_object_unref (srcpad);
3656
3657   /* connect to the RTCP sync signal from the jitterbuffer */
3658   GST_DEBUG_OBJECT (rtpbin, "connecting sync signal");
3659   stream->buffer_handlesync_sig = g_signal_connect (stream->buffer,
3660       "handle-sync", (GCallback) gst_rtp_bin_handle_sync, stream);
3661
3662   if (stream->demux) {
3663     /* connect to the new-pad signal of the payload demuxer, this will expose the
3664      * new pad by ghosting it. */
3665     stream->demux_newpad_sig = g_signal_connect (stream->demux,
3666         "new-payload-type", (GCallback) new_payload_found, stream);
3667     stream->demux_padremoved_sig = g_signal_connect (stream->demux,
3668         "pad-removed", (GCallback) payload_pad_removed, stream);
3669
3670     /* connect to the request-pt-map signal. This signal will be emitted by the
3671      * demuxer so that it can apply a proper caps on the buffers for the
3672      * depayloaders. */
3673     stream->demux_ptreq_sig = g_signal_connect (stream->demux,
3674         "request-pt-map", (GCallback) ptdemux_pt_map_requested, session);
3675     /* connect to the  signal so it can be forwarded. */
3676     stream->demux_ptchange_sig = g_signal_connect (stream->demux,
3677         "payload-type-change", (GCallback) payload_type_change, session);
3678   } else {
3679     /* add rtpjitterbuffer src pad to pads */
3680     GstElementClass *klass;
3681     GstPadTemplate *templ;
3682     gchar *padname;
3683     GstPad *gpad, *pad;
3684
3685     pad = gst_element_get_static_pad (stream->buffer, "src");
3686
3687     /* ghost the pad to the parent */
3688     klass = GST_ELEMENT_GET_CLASS (rtpbin);
3689     templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%u_%u_%u");
3690     padname = g_strdup_printf ("recv_rtp_src_%u_%u_%u",
3691         stream->session->id, stream->ssrc, 255);
3692     gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
3693     g_free (padname);
3694
3695     gst_pad_set_active (gpad, TRUE);
3696     gst_pad_sticky_events_foreach (pad, copy_sticky_events, gpad);
3697     gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
3698
3699     gst_object_unref (pad);
3700   }
3701
3702   GST_RTP_SESSION_UNLOCK (session);
3703   GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
3704
3705   return;
3706
3707   /* ERRORS */
3708 shutdown:
3709   {
3710     GST_DEBUG_OBJECT (rtpbin, "we are shutting down");
3711     return;
3712   }
3713 no_stream:
3714   {
3715     GST_RTP_SESSION_UNLOCK (session);
3716     GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
3717     GST_DEBUG_OBJECT (rtpbin, "could not create stream");
3718     return;
3719   }
3720 }
3721
3722 static void
3723 session_maybe_create_bundle_demuxer (GstRtpBinSession * session)
3724 {
3725   GstRtpBin *rtpbin;
3726
3727   if (session->bundle_demux)
3728     return;
3729
3730   rtpbin = session->bin;
3731   if (g_signal_has_handler_pending (rtpbin,
3732           gst_rtp_bin_signals[SIGNAL_ON_BUNDLED_SSRC], 0, TRUE)) {
3733     GST_DEBUG_OBJECT (rtpbin, "Adding a bundle SSRC demuxer to session %u",
3734         session->id);
3735     session->bundle_demux = gst_element_factory_make ("rtpssrcdemux", NULL);
3736     session->bundle_demux_newpad_sig = g_signal_connect (session->bundle_demux,
3737         "new-ssrc-pad", (GCallback) new_bundled_ssrc_pad_found, session);
3738
3739     gst_bin_add (GST_BIN_CAST (rtpbin), session->bundle_demux);
3740     gst_element_sync_state_with_parent (session->bundle_demux);
3741   } else {
3742     GST_DEBUG_OBJECT (rtpbin,
3743         "No handler for the on-bundled-ssrc signal so no need for a bundle SSRC demuxer in session %u",
3744         session->id);
3745   }
3746 }
3747
3748 static GstPad *
3749 complete_session_sink (GstRtpBin * rtpbin, GstRtpBinSession * session,
3750     gboolean bundle_demuxer_needed)
3751 {
3752   guint sessid = session->id;
3753   GstPad *recv_rtp_sink;
3754   GstPad *funnel_src;
3755   GstElement *decoder;
3756
3757   g_assert (!session->recv_rtp_sink);
3758
3759   /* get recv_rtp pad and store */
3760   session->recv_rtp_sink =
3761       gst_element_get_request_pad (session->session, "recv_rtp_sink");
3762   if (session->recv_rtp_sink == NULL)
3763     goto pad_failed;
3764
3765   g_signal_connect (session->recv_rtp_sink, "notify::caps",
3766       (GCallback) caps_changed, session);
3767
3768   if (bundle_demuxer_needed)
3769     session_maybe_create_bundle_demuxer (session);
3770
3771   GST_DEBUG_OBJECT (rtpbin, "requesting RTP decoder");
3772   decoder = session_request_element (session, SIGNAL_REQUEST_RTP_DECODER);
3773   if (decoder) {
3774     GstPad *decsrc, *decsink;
3775     GstPadLinkReturn ret;
3776
3777     GST_DEBUG_OBJECT (rtpbin, "linking RTP decoder");
3778     decsink = gst_element_get_static_pad (decoder, "rtp_sink");
3779     if (decsink == NULL)
3780       goto dec_sink_failed;
3781
3782     recv_rtp_sink = decsink;
3783
3784     decsrc = gst_element_get_static_pad (decoder, "rtp_src");
3785     if (decsrc == NULL)
3786       goto dec_src_failed;
3787
3788     if (session->bundle_demux) {
3789       GstPad *demux_sink;
3790       demux_sink = gst_element_get_static_pad (session->bundle_demux, "sink");
3791       ret = gst_pad_link (decsrc, demux_sink);
3792       gst_object_unref (demux_sink);
3793     } else {
3794       ret = gst_pad_link (decsrc, session->recv_rtp_sink);
3795     }
3796     gst_object_unref (decsrc);
3797
3798     if (ret != GST_PAD_LINK_OK)
3799       goto dec_link_failed;
3800
3801   } else {
3802     GST_DEBUG_OBJECT (rtpbin, "no RTP decoder given");
3803     if (session->bundle_demux) {
3804       recv_rtp_sink =
3805           gst_element_get_static_pad (session->bundle_demux, "sink");
3806     } else {
3807       recv_rtp_sink =
3808           gst_element_get_request_pad (session->rtp_funnel, "sink_%u");
3809     }
3810   }
3811
3812   funnel_src = gst_element_get_static_pad (session->rtp_funnel, "src");
3813   gst_pad_link (funnel_src, session->recv_rtp_sink);
3814   gst_object_unref (funnel_src);
3815
3816   return recv_rtp_sink;
3817
3818   /* ERRORS */
3819 pad_failed:
3820   {
3821     g_warning ("rtpbin: failed to get session recv_rtp_sink pad");
3822     return NULL;
3823   }
3824 dec_sink_failed:
3825   {
3826     g_warning ("rtpbin: failed to get decoder sink pad for session %u", sessid);
3827     return NULL;
3828   }
3829 dec_src_failed:
3830   {
3831     g_warning ("rtpbin: failed to get decoder src pad for session %u", sessid);
3832     gst_object_unref (recv_rtp_sink);
3833     return NULL;
3834   }
3835 dec_link_failed:
3836   {
3837     g_warning ("rtpbin: failed to link rtp decoder for session %u", sessid);
3838     gst_object_unref (recv_rtp_sink);
3839     return NULL;
3840   }
3841 }
3842
3843 static void
3844 complete_session_receiver (GstRtpBin * rtpbin, GstRtpBinSession * session,
3845     guint sessid)
3846 {
3847   GstElement *aux;
3848   GstPad *recv_rtp_src;
3849
3850   g_assert (!session->recv_rtp_src);
3851
3852   session->recv_rtp_src =
3853       gst_element_get_static_pad (session->session, "recv_rtp_src");
3854   if (session->recv_rtp_src == NULL)
3855     goto pad_failed;
3856
3857   /* find out if we need AUX elements */
3858   aux = session_request_element (session, SIGNAL_REQUEST_AUX_RECEIVER);
3859   if (aux) {
3860     gchar *pname;
3861     GstPad *auxsink;
3862     GstPadLinkReturn ret;
3863
3864     GST_DEBUG_OBJECT (rtpbin, "linking AUX receiver");
3865
3866     pname = g_strdup_printf ("sink_%u", sessid);
3867     auxsink = gst_element_get_static_pad (aux, pname);
3868     g_free (pname);
3869     if (auxsink == NULL)
3870       goto aux_sink_failed;
3871
3872     ret = gst_pad_link (session->recv_rtp_src, auxsink);
3873     gst_object_unref (auxsink);
3874     if (ret != GST_PAD_LINK_OK)
3875       goto aux_link_failed;
3876
3877     /* this can be NULL when this AUX element is not to be linked any further */
3878     pname = g_strdup_printf ("src_%u", sessid);
3879     recv_rtp_src = gst_element_get_static_pad (aux, pname);
3880     g_free (pname);
3881   } else {
3882     recv_rtp_src = gst_object_ref (session->recv_rtp_src);
3883   }
3884
3885   /* Add a storage element if needed */
3886   if (recv_rtp_src && session->storage) {
3887     GstPadLinkReturn ret;
3888     GstPad *sinkpad = gst_element_get_static_pad (session->storage, "sink");
3889
3890     ret = gst_pad_link (recv_rtp_src, sinkpad);
3891
3892     gst_object_unref (sinkpad);
3893     gst_object_unref (recv_rtp_src);
3894
3895     if (ret != GST_PAD_LINK_OK)
3896       goto storage_link_failed;
3897
3898     recv_rtp_src = gst_element_get_static_pad (session->storage, "src");
3899   }
3900
3901   if (recv_rtp_src) {
3902     GstPad *sinkdpad;
3903
3904     GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
3905     sinkdpad = gst_element_get_static_pad (session->demux, "sink");
3906     GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
3907     gst_pad_link_full (recv_rtp_src, sinkdpad, GST_PAD_LINK_CHECK_NOTHING);
3908     gst_object_unref (sinkdpad);
3909     gst_object_unref (recv_rtp_src);
3910
3911     /* connect to the new-ssrc-pad signal of the SSRC demuxer */
3912     session->demux_newpad_sig = g_signal_connect (session->demux,
3913         "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
3914     session->demux_padremoved_sig = g_signal_connect (session->demux,
3915         "removed-ssrc-pad", (GCallback) ssrc_demux_pad_removed, session);
3916   }
3917
3918   return;
3919
3920 pad_failed:
3921   {
3922     g_warning ("rtpbin: failed to get session recv_rtp_src pad");
3923     return;
3924   }
3925 aux_sink_failed:
3926   {
3927     g_warning ("rtpbin: failed to get AUX sink pad for session %u", sessid);
3928     return;
3929   }
3930 aux_link_failed:
3931   {
3932     g_warning ("rtpbin: failed to link AUX pad to session %u", sessid);
3933     return;
3934   }
3935 storage_link_failed:
3936   {
3937     g_warning ("rtpbin: failed to link storage");
3938     return;
3939   }
3940 }
3941
3942 /* Create a pad for receiving RTP for the session in @name. Must be called with
3943  * RTP_BIN_LOCK.
3944  */
3945 static GstPad *
3946 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
3947 {
3948   guint sessid;
3949   GstRtpBinSession *session;
3950   GstPad *recv_rtp_sink;
3951
3952   /* first get the session number */
3953   if (name == NULL || sscanf (name, "recv_rtp_sink_%u", &sessid) != 1)
3954     goto no_name;
3955
3956   GST_DEBUG_OBJECT (rtpbin, "finding session %u", sessid);
3957
3958   /* get or create session */
3959   session = find_session_by_id (rtpbin, sessid);
3960   if (!session) {
3961     GST_DEBUG_OBJECT (rtpbin, "creating session %u", sessid);
3962     /* create session now */
3963     session = create_session (rtpbin, sessid);
3964     if (session == NULL)
3965       goto create_error;
3966   }
3967
3968   /* check if pad was requested */
3969   if (session->recv_rtp_sink_ghost != NULL)
3970     return session->recv_rtp_sink_ghost;
3971
3972   /* setup the session sink pad */
3973   recv_rtp_sink = complete_session_sink (rtpbin, session, TRUE);
3974   if (!recv_rtp_sink)
3975     goto session_sink_failed;
3976
3977
3978   GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
3979   session->recv_rtp_sink_ghost =
3980       gst_ghost_pad_new_from_template (name, recv_rtp_sink, templ);
3981   gst_object_unref (recv_rtp_sink);
3982   gst_pad_set_active (session->recv_rtp_sink_ghost, TRUE);
3983   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->recv_rtp_sink_ghost);
3984
3985   complete_session_receiver (rtpbin, session, sessid);
3986
3987   return session->recv_rtp_sink_ghost;
3988
3989   /* ERRORS */
3990 no_name:
3991   {
3992     g_warning ("rtpbin: invalid name given");
3993     return NULL;
3994   }
3995 create_error:
3996   {
3997     /* create_session already warned */
3998     return NULL;
3999   }
4000 session_sink_failed:
4001   {
4002     /* warning already done */
4003     return NULL;
4004   }
4005 }
4006
4007 static void
4008 remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
4009 {
4010   if (session->demux_newpad_sig) {
4011     g_signal_handler_disconnect (session->demux, session->demux_newpad_sig);
4012     session->demux_newpad_sig = 0;
4013   }
4014   if (session->demux_padremoved_sig) {
4015     g_signal_handler_disconnect (session->demux, session->demux_padremoved_sig);
4016     session->demux_padremoved_sig = 0;
4017   }
4018   if (session->bundle_demux_newpad_sig) {
4019     g_signal_handler_disconnect (session->bundle_demux,
4020         session->bundle_demux_newpad_sig);
4021     session->bundle_demux_newpad_sig = 0;
4022   }
4023   if (session->recv_rtp_src) {
4024     gst_object_unref (session->recv_rtp_src);
4025     session->recv_rtp_src = NULL;
4026   }
4027   if (session->recv_rtp_sink) {
4028     gst_element_release_request_pad (session->session, session->recv_rtp_sink);
4029     gst_object_unref (session->recv_rtp_sink);
4030     session->recv_rtp_sink = NULL;
4031   }
4032   if (session->recv_rtp_sink_ghost) {
4033     gst_pad_set_active (session->recv_rtp_sink_ghost, FALSE);
4034     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
4035         session->recv_rtp_sink_ghost);
4036     session->recv_rtp_sink_ghost = NULL;
4037   }
4038 }
4039
4040 static GstPad *
4041 complete_session_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session,
4042     guint sessid, gboolean bundle_demuxer_needed)
4043 {
4044   GstElement *decoder;
4045   GstPad *sinkdpad;
4046   GstPad *decsink = NULL;
4047   GstPad *funnel_src;
4048
4049   /* get recv_rtp pad and store */
4050   GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
4051   session->recv_rtcp_sink =
4052       gst_element_get_request_pad (session->session, "recv_rtcp_sink");
4053   if (session->recv_rtcp_sink == NULL)
4054     goto pad_failed;
4055
4056   if (bundle_demuxer_needed)
4057     session_maybe_create_bundle_demuxer (session);
4058
4059   GST_DEBUG_OBJECT (rtpbin, "getting RTCP decoder");
4060   decoder = session_request_element (session, SIGNAL_REQUEST_RTCP_DECODER);
4061   if (decoder) {
4062     GstPad *decsrc;
4063     GstPadLinkReturn ret;
4064
4065     GST_DEBUG_OBJECT (rtpbin, "linking RTCP decoder");
4066     decsink = gst_element_get_static_pad (decoder, "rtcp_sink");
4067     decsrc = gst_element_get_static_pad (decoder, "rtcp_src");
4068
4069     if (decsink == NULL)
4070       goto dec_sink_failed;
4071
4072     if (decsrc == NULL)
4073       goto dec_src_failed;
4074
4075     if (session->bundle_demux) {
4076       GstPad *demux_sink;
4077       demux_sink =
4078           gst_element_get_static_pad (session->bundle_demux, "rtcp_sink");
4079       ret = gst_pad_link (decsrc, demux_sink);
4080       gst_object_unref (demux_sink);
4081     } else {
4082       ret = gst_pad_link (decsrc, session->recv_rtcp_sink);
4083     }
4084     gst_object_unref (decsrc);
4085
4086     if (ret != GST_PAD_LINK_OK)
4087       goto dec_link_failed;
4088   } else {
4089     GST_DEBUG_OBJECT (rtpbin, "no RTCP decoder given");
4090     if (session->bundle_demux) {
4091       decsink = gst_element_get_static_pad (session->bundle_demux, "rtcp_sink");
4092     } else {
4093       decsink = gst_element_get_request_pad (session->rtcp_funnel, "sink_%u");
4094     }
4095   }
4096
4097   /* get srcpad, link to SSRCDemux */
4098   GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
4099   session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
4100   if (session->sync_src == NULL)
4101     goto src_pad_failed;
4102
4103   GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
4104   sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
4105   gst_pad_link_full (session->sync_src, sinkdpad, GST_PAD_LINK_CHECK_NOTHING);
4106   gst_object_unref (sinkdpad);
4107
4108   funnel_src = gst_element_get_static_pad (session->rtcp_funnel, "src");
4109   gst_pad_link (funnel_src, session->recv_rtcp_sink);
4110   gst_object_unref (funnel_src);
4111
4112   return decsink;
4113
4114 pad_failed:
4115   {
4116     g_warning ("rtpbin: failed to get session rtcp_sink pad");
4117     return NULL;
4118   }
4119 dec_sink_failed:
4120   {
4121     g_warning ("rtpbin: failed to get decoder sink pad for session %u", sessid);
4122     return NULL;
4123   }
4124 dec_src_failed:
4125   {
4126     g_warning ("rtpbin: failed to get decoder src pad for session %u", sessid);
4127     goto cleanup;
4128   }
4129 dec_link_failed:
4130   {
4131     g_warning ("rtpbin: failed to link rtcp decoder for session %u", sessid);
4132     goto cleanup;
4133   }
4134 src_pad_failed:
4135   {
4136     g_warning ("rtpbin: failed to get session sync_src pad");
4137   }
4138
4139 cleanup:
4140   gst_object_unref (decsink);
4141   return NULL;
4142 }
4143
4144 /* Create a pad for receiving RTCP for the session in @name. Must be called with
4145  * RTP_BIN_LOCK.
4146  */
4147 static GstPad *
4148 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
4149     const gchar * name)
4150 {
4151   guint sessid;
4152   GstRtpBinSession *session;
4153   GstPad *decsink = NULL;
4154
4155   /* first get the session number */
4156   if (name == NULL || sscanf (name, "recv_rtcp_sink_%u", &sessid) != 1)
4157     goto no_name;
4158
4159   GST_DEBUG_OBJECT (rtpbin, "finding session %u", sessid);
4160
4161   /* get or create the session */
4162   session = find_session_by_id (rtpbin, sessid);
4163   if (!session) {
4164     GST_DEBUG_OBJECT (rtpbin, "creating session %u", sessid);
4165     /* create session now */
4166     session = create_session (rtpbin, sessid);
4167     if (session == NULL)
4168       goto create_error;
4169   }
4170
4171   /* check if pad was requested */
4172   if (session->recv_rtcp_sink_ghost != NULL)
4173     return session->recv_rtcp_sink_ghost;
4174
4175   decsink = complete_session_rtcp (rtpbin, session, sessid, TRUE);
4176   if (!decsink)
4177     goto create_error;
4178
4179   session->recv_rtcp_sink_ghost =
4180       gst_ghost_pad_new_from_template (name, decsink, templ);
4181   gst_object_unref (decsink);
4182   gst_pad_set_active (session->recv_rtcp_sink_ghost, TRUE);
4183   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin),
4184       session->recv_rtcp_sink_ghost);
4185
4186   return session->recv_rtcp_sink_ghost;
4187
4188   /* ERRORS */
4189 no_name:
4190   {
4191     g_warning ("rtpbin: invalid name given");
4192     return NULL;
4193   }
4194 create_error:
4195   {
4196     /* create_session already warned */
4197     return NULL;
4198   }
4199 }
4200
4201 static void
4202 remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
4203 {
4204   if (session->recv_rtcp_sink_ghost) {
4205     gst_pad_set_active (session->recv_rtcp_sink_ghost, FALSE);
4206     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
4207         session->recv_rtcp_sink_ghost);
4208     session->recv_rtcp_sink_ghost = NULL;
4209   }
4210   if (session->sync_src) {
4211     /* releasing the request pad should also unref the sync pad */
4212     gst_object_unref (session->sync_src);
4213     session->sync_src = NULL;
4214   }
4215   if (session->recv_rtcp_sink) {
4216     gst_element_release_request_pad (session->session, session->recv_rtcp_sink);
4217     gst_object_unref (session->recv_rtcp_sink);
4218     session->recv_rtcp_sink = NULL;
4219   }
4220 }
4221
4222 static gboolean
4223 complete_session_src (GstRtpBin * rtpbin, GstRtpBinSession * session)
4224 {
4225   gchar *gname;
4226   guint sessid = session->id;
4227   GstPad *send_rtp_src;
4228   GstElement *encoder;
4229   GstElementClass *klass;
4230   GstPadTemplate *templ;
4231   gboolean ret = FALSE;
4232
4233   /* get srcpad */
4234   send_rtp_src = gst_element_get_static_pad (session->session, "send_rtp_src");
4235
4236   if (send_rtp_src == NULL)
4237     goto no_srcpad;
4238
4239   GST_DEBUG_OBJECT (rtpbin, "getting RTP encoder");
4240   encoder = session_request_element (session, SIGNAL_REQUEST_RTP_ENCODER);
4241   if (encoder) {
4242     gchar *ename;
4243     GstPad *encsrc, *encsink;
4244     GstPadLinkReturn ret;
4245
4246     GST_DEBUG_OBJECT (rtpbin, "linking RTP encoder");
4247     ename = g_strdup_printf ("rtp_src_%u", sessid);
4248     encsrc = gst_element_get_static_pad (encoder, ename);
4249     g_free (ename);
4250
4251     if (encsrc == NULL)
4252       goto enc_src_failed;
4253
4254     ename = g_strdup_printf ("rtp_sink_%u", sessid);
4255     encsink = gst_element_get_static_pad (encoder, ename);
4256     g_free (ename);
4257     if (encsink == NULL)
4258       goto enc_sink_failed;
4259
4260     ret = gst_pad_link (send_rtp_src, encsink);
4261     gst_object_unref (encsink);
4262     gst_object_unref (send_rtp_src);
4263
4264     send_rtp_src = encsrc;
4265
4266     if (ret != GST_PAD_LINK_OK)
4267       goto enc_link_failed;
4268   } else {
4269     GST_DEBUG_OBJECT (rtpbin, "no RTP encoder given");
4270   }
4271
4272   /* ghost the new source pad */
4273   klass = GST_ELEMENT_GET_CLASS (rtpbin);
4274   gname = g_strdup_printf ("send_rtp_src_%u", sessid);
4275   templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%u");
4276   session->send_rtp_src_ghost =
4277       gst_ghost_pad_new_from_template (gname, send_rtp_src, templ);
4278   gst_pad_set_active (session->send_rtp_src_ghost, TRUE);
4279   gst_pad_sticky_events_foreach (send_rtp_src, copy_sticky_events,
4280       session->send_rtp_src_ghost);
4281   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_src_ghost);
4282   g_free (gname);
4283
4284   ret = TRUE;
4285
4286 done:
4287   if (send_rtp_src)
4288     gst_object_unref (send_rtp_src);
4289
4290   return ret;
4291
4292   /* ERRORS */
4293 no_srcpad:
4294   {
4295     g_warning ("rtpbin: failed to get rtp source pad for session %u", sessid);
4296     goto done;
4297   }
4298 enc_src_failed:
4299   {
4300     g_warning ("rtpbin: failed to get %" GST_PTR_FORMAT
4301         " src pad for session %u", encoder, sessid);
4302     goto done;
4303   }
4304 enc_sink_failed:
4305   {
4306     g_warning ("rtpbin: failed to get %" GST_PTR_FORMAT
4307         " sink pad for session %u", encoder, sessid);
4308     goto done;
4309   }
4310 enc_link_failed:
4311   {
4312     g_warning ("rtpbin: failed to link %" GST_PTR_FORMAT " for session %u",
4313         encoder, sessid);
4314     goto done;
4315   }
4316 }
4317
4318 static gboolean
4319 setup_aux_sender_fold (const GValue * item, GValue * result, gpointer user_data)
4320 {
4321   GstPad *pad;
4322   gchar *name;
4323   guint sessid;
4324   GstRtpBinSession *session = user_data, *newsess;
4325   GstRtpBin *rtpbin = session->bin;
4326   GstPadLinkReturn ret;
4327
4328   pad = g_value_get_object (item);
4329   name = gst_pad_get_name (pad);
4330
4331   if (name == NULL || sscanf (name, "src_%u", &sessid) != 1)
4332     goto no_name;
4333
4334   g_free (name);
4335
4336   newsess = find_session_by_id (rtpbin, sessid);
4337   if (newsess == NULL) {
4338     /* create new session */
4339     newsess = create_session (rtpbin, sessid);
4340     if (newsess == NULL)
4341       goto create_error;
4342   } else if (newsess->send_rtp_sink != NULL)
4343     goto existing_session;
4344
4345   /* get send_rtp pad and store */
4346   newsess->send_rtp_sink =
4347       gst_element_get_request_pad (newsess->session, "send_rtp_sink");
4348   if (newsess->send_rtp_sink == NULL)
4349     goto pad_failed;
4350
4351   ret = gst_pad_link (pad, newsess->send_rtp_sink);
4352   if (ret != GST_PAD_LINK_OK)
4353     goto aux_link_failed;
4354
4355   if (!complete_session_src (rtpbin, newsess))
4356     goto session_src_failed;
4357
4358   return TRUE;
4359
4360   /* ERRORS */
4361 no_name:
4362   {
4363     GST_WARNING ("ignoring invalid pad name %s", GST_STR_NULL (name));
4364     g_free (name);
4365     return TRUE;
4366   }
4367 create_error:
4368   {
4369     /* create_session already warned */
4370     return FALSE;
4371   }
4372 existing_session:
4373   {
4374     g_warning ("rtpbin: session %u is already a sender", sessid);
4375     return FALSE;
4376   }
4377 pad_failed:
4378   {
4379     g_warning ("rtpbin: failed to get session pad for session %u", sessid);
4380     return FALSE;
4381   }
4382 aux_link_failed:
4383   {
4384     g_warning ("rtpbin: failed to link AUX for session %u", sessid);
4385     return FALSE;
4386   }
4387 session_src_failed:
4388   {
4389     g_warning ("rtpbin: failed to complete AUX for session %u", sessid);
4390     return FALSE;
4391   }
4392 }
4393
4394 static gboolean
4395 setup_aux_sender (GstRtpBin * rtpbin, GstRtpBinSession * session,
4396     GstElement * aux)
4397 {
4398   GstIterator *it;
4399   GValue result = { 0, };
4400   GstIteratorResult res;
4401
4402   it = gst_element_iterate_src_pads (aux);
4403   res = gst_iterator_fold (it, setup_aux_sender_fold, &result, session);
4404   gst_iterator_free (it);
4405
4406   return res == GST_ITERATOR_DONE;
4407 }
4408
4409 /* Create a pad for sending RTP for the session in @name. Must be called with
4410  * RTP_BIN_LOCK.
4411  */
4412 static GstPad *
4413 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
4414 {
4415   gchar *pname;
4416   guint sessid;
4417   GstPad *send_rtp_sink;
4418   GstElement *aux;
4419   GstElement *encoder;
4420   GstElement *prev = NULL;
4421   GstRtpBinSession *session;
4422
4423   /* first get the session number */
4424   if (name == NULL || sscanf (name, "send_rtp_sink_%u", &sessid) != 1)
4425     goto no_name;
4426
4427   /* get or create session */
4428   session = find_session_by_id (rtpbin, sessid);
4429   if (!session) {
4430     /* create session now */
4431     session = create_session (rtpbin, sessid);
4432     if (session == NULL)
4433       goto create_error;
4434   }
4435
4436   /* check if pad was requested */
4437   if (session->send_rtp_sink_ghost != NULL)
4438     return session->send_rtp_sink_ghost;
4439
4440   /* check if we are already using this session as a sender */
4441   if (session->send_rtp_sink != NULL)
4442     goto existing_session;
4443
4444   encoder = session_request_element (session, SIGNAL_REQUEST_FEC_ENCODER);
4445
4446   if (encoder) {
4447     GST_DEBUG_OBJECT (rtpbin, "Linking FEC encoder");
4448
4449     send_rtp_sink = gst_element_get_static_pad (encoder, "sink");
4450
4451     if (!send_rtp_sink)
4452       goto enc_sink_failed;
4453
4454     prev = encoder;
4455   }
4456
4457   GST_DEBUG_OBJECT (rtpbin, "getting RTP AUX sender");
4458   aux = session_request_element (session, SIGNAL_REQUEST_AUX_SENDER);
4459   if (aux) {
4460     GstPad *sinkpad;
4461     GST_DEBUG_OBJECT (rtpbin, "linking AUX sender");
4462     if (!setup_aux_sender (rtpbin, session, aux))
4463       goto aux_session_failed;
4464
4465     pname = g_strdup_printf ("sink_%u", sessid);
4466     sinkpad = gst_element_get_static_pad (aux, pname);
4467     g_free (pname);
4468
4469     if (sinkpad == NULL)
4470       goto aux_sink_failed;
4471
4472     if (!prev) {
4473       send_rtp_sink = sinkpad;
4474     } else {
4475       GstPad *srcpad = gst_element_get_static_pad (prev, "src");
4476       GstPadLinkReturn ret;
4477
4478       ret = gst_pad_link (srcpad, sinkpad);
4479       gst_object_unref (srcpad);
4480       if (ret != GST_PAD_LINK_OK) {
4481         goto aux_link_failed;
4482       }
4483     }
4484     prev = aux;
4485   } else {
4486     /* get send_rtp pad and store */
4487     session->send_rtp_sink =
4488         gst_element_get_request_pad (session->session, "send_rtp_sink");
4489     if (session->send_rtp_sink == NULL)
4490       goto pad_failed;
4491
4492     if (!complete_session_src (rtpbin, session))
4493       goto session_src_failed;
4494
4495     if (!prev) {
4496       send_rtp_sink = gst_object_ref (session->send_rtp_sink);
4497     } else {
4498       GstPad *srcpad = gst_element_get_static_pad (prev, "src");
4499       GstPadLinkReturn ret;
4500
4501       ret = gst_pad_link (srcpad, session->send_rtp_sink);
4502       gst_object_unref (srcpad);
4503       if (ret != GST_PAD_LINK_OK)
4504         goto session_link_failed;
4505     }
4506   }
4507
4508   session->send_rtp_sink_ghost =
4509       gst_ghost_pad_new_from_template (name, send_rtp_sink, templ);
4510   gst_object_unref (send_rtp_sink);
4511   gst_pad_set_active (session->send_rtp_sink_ghost, TRUE);
4512   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_sink_ghost);
4513
4514   return session->send_rtp_sink_ghost;
4515
4516   /* ERRORS */
4517 no_name:
4518   {
4519     g_warning ("rtpbin: invalid name given");
4520     return NULL;
4521   }
4522 create_error:
4523   {
4524     /* create_session already warned */
4525     return NULL;
4526   }
4527 existing_session:
4528   {
4529     g_warning ("rtpbin: session %u is already in use", sessid);
4530     return NULL;
4531   }
4532 aux_session_failed:
4533   {
4534     g_warning ("rtpbin: failed to get AUX sink pad for session %u", sessid);
4535     return NULL;
4536   }
4537 aux_sink_failed:
4538   {
4539     g_warning ("rtpbin: failed to get AUX sink pad for session %u", sessid);
4540     return NULL;
4541   }
4542 aux_link_failed:
4543   {
4544     g_warning ("rtpbin: failed to link %" GST_PTR_FORMAT " for session %u",
4545         aux, sessid);
4546     return NULL;
4547   }
4548 pad_failed:
4549   {
4550     g_warning ("rtpbin: failed to get session pad for session %u", sessid);
4551     return NULL;
4552   }
4553 session_src_failed:
4554   {
4555     g_warning ("rtpbin: failed to setup source pads for session %u", sessid);
4556     return NULL;
4557   }
4558 session_link_failed:
4559   {
4560     g_warning ("rtpbin: failed to link %" GST_PTR_FORMAT " for session %u",
4561         session, sessid);
4562     return NULL;
4563   }
4564 enc_sink_failed:
4565   {
4566     g_warning ("rtpbin: failed to get %" GST_PTR_FORMAT
4567         " sink pad for session %u", encoder, sessid);
4568     return NULL;
4569   }
4570 }
4571
4572 static void
4573 remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
4574 {
4575   if (session->send_rtp_src_ghost) {
4576     gst_pad_set_active (session->send_rtp_src_ghost, FALSE);
4577     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
4578         session->send_rtp_src_ghost);
4579     session->send_rtp_src_ghost = NULL;
4580   }
4581   if (session->send_rtp_sink) {
4582     gst_element_release_request_pad (GST_ELEMENT_CAST (session->session),
4583         session->send_rtp_sink);
4584     gst_object_unref (session->send_rtp_sink);
4585     session->send_rtp_sink = NULL;
4586   }
4587   if (session->send_rtp_sink_ghost) {
4588     gst_pad_set_active (session->send_rtp_sink_ghost, FALSE);
4589     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
4590         session->send_rtp_sink_ghost);
4591     session->send_rtp_sink_ghost = NULL;
4592   }
4593 }
4594
4595 /* Create a pad for sending RTCP for the session in @name. Must be called with
4596  * RTP_BIN_LOCK.
4597  */
4598 static GstPad *
4599 create_send_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
4600     const gchar * name)
4601 {
4602   guint sessid;
4603   GstPad *encsrc;
4604   GstElement *encoder;
4605   GstRtpBinSession *session;
4606
4607   /* first get the session number */
4608   if (name == NULL || sscanf (name, "send_rtcp_src_%u", &sessid) != 1)
4609     goto no_name;
4610
4611   /* get or create session */
4612   session = find_session_by_id (rtpbin, sessid);
4613   if (!session) {
4614     GST_DEBUG_OBJECT (rtpbin, "creating session %u", sessid);
4615     /* create session now */
4616     session = create_session (rtpbin, sessid);
4617     if (session == NULL)
4618       goto create_error;
4619   }
4620
4621   /* check if pad was requested */
4622   if (session->send_rtcp_src_ghost != NULL)
4623     return session->send_rtcp_src_ghost;
4624
4625   /* get rtcp_src pad and store */
4626   session->send_rtcp_src =
4627       gst_element_get_request_pad (session->session, "send_rtcp_src");
4628   if (session->send_rtcp_src == NULL)
4629     goto pad_failed;
4630
4631   GST_DEBUG_OBJECT (rtpbin, "getting RTCP encoder");
4632   encoder = session_request_element (session, SIGNAL_REQUEST_RTCP_ENCODER);
4633   if (encoder) {
4634     gchar *ename;
4635     GstPad *encsink;
4636     GstPadLinkReturn ret;
4637
4638     GST_DEBUG_OBJECT (rtpbin, "linking RTCP encoder");
4639
4640     ename = g_strdup_printf ("rtcp_src_%u", sessid);
4641     encsrc = gst_element_get_static_pad (encoder, ename);
4642     g_free (ename);
4643     if (encsrc == NULL)
4644       goto enc_src_failed;
4645
4646     ename = g_strdup_printf ("rtcp_sink_%u", sessid);
4647     encsink = gst_element_get_static_pad (encoder, ename);
4648     g_free (ename);
4649     if (encsink == NULL)
4650       goto enc_sink_failed;
4651
4652     ret = gst_pad_link (session->send_rtcp_src, encsink);
4653     gst_object_unref (encsink);
4654
4655     if (ret != GST_PAD_LINK_OK)
4656       goto enc_link_failed;
4657   } else {
4658     GST_DEBUG_OBJECT (rtpbin, "no RTCP encoder given");
4659     encsrc = gst_object_ref (session->send_rtcp_src);
4660   }
4661
4662   session->send_rtcp_src_ghost =
4663       gst_ghost_pad_new_from_template (name, encsrc, templ);
4664   gst_object_unref (encsrc);
4665   gst_pad_set_active (session->send_rtcp_src_ghost, TRUE);
4666   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtcp_src_ghost);
4667
4668   return session->send_rtcp_src_ghost;
4669
4670   /* ERRORS */
4671 no_name:
4672   {
4673     g_warning ("rtpbin: invalid name given");
4674     return NULL;
4675   }
4676 create_error:
4677   {
4678     /* create_session already warned */
4679     return NULL;
4680   }
4681 pad_failed:
4682   {
4683     g_warning ("rtpbin: failed to get rtcp pad for session %u", sessid);
4684     return NULL;
4685   }
4686 enc_src_failed:
4687   {
4688     g_warning ("rtpbin: failed to get encoder src pad for session %u", sessid);
4689     return NULL;
4690   }
4691 enc_sink_failed:
4692   {
4693     g_warning ("rtpbin: failed to get encoder sink pad for session %u", sessid);
4694     gst_object_unref (encsrc);
4695     return NULL;
4696   }
4697 enc_link_failed:
4698   {
4699     g_warning ("rtpbin: failed to link rtcp encoder for session %u", sessid);
4700     gst_object_unref (encsrc);
4701     return NULL;
4702   }
4703 }
4704
4705 static void
4706 remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
4707 {
4708   if (session->send_rtcp_src_ghost) {
4709     gst_pad_set_active (session->send_rtcp_src_ghost, FALSE);
4710     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
4711         session->send_rtcp_src_ghost);
4712     session->send_rtcp_src_ghost = NULL;
4713   }
4714   if (session->send_rtcp_src) {
4715     gst_element_release_request_pad (session->session, session->send_rtcp_src);
4716     gst_object_unref (session->send_rtcp_src);
4717     session->send_rtcp_src = NULL;
4718   }
4719 }
4720
4721 /* If the requested name is NULL we should create a name with
4722  * the session number assuming we want the lowest posible session
4723  * with a free pad like the template */
4724 static gchar *
4725 gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
4726 {
4727   gboolean name_found = FALSE;
4728   gint session = 0;
4729   GstIterator *pad_it = NULL;
4730   gchar *pad_name = NULL;
4731   GValue data = { 0, };
4732
4733   GST_DEBUG_OBJECT (element, "find a free pad name for template");
4734   while (!name_found) {
4735     gboolean done = FALSE;
4736
4737     g_free (pad_name);
4738     pad_name = g_strdup_printf (templ->name_template, session++);
4739     pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
4740     name_found = TRUE;
4741     while (!done) {
4742       switch (gst_iterator_next (pad_it, &data)) {
4743         case GST_ITERATOR_OK:
4744         {
4745           GstPad *pad;
4746           gchar *name;
4747
4748           pad = g_value_get_object (&data);
4749           name = gst_pad_get_name (pad);
4750
4751           if (strcmp (name, pad_name) == 0) {
4752             done = TRUE;
4753             name_found = FALSE;
4754           }
4755           g_free (name);
4756           g_value_reset (&data);
4757           break;
4758         }
4759         case GST_ITERATOR_ERROR:
4760         case GST_ITERATOR_RESYNC:
4761           /* restart iteration */
4762           done = TRUE;
4763           name_found = FALSE;
4764           session = 0;
4765           break;
4766         case GST_ITERATOR_DONE:
4767           done = TRUE;
4768           break;
4769       }
4770     }
4771     g_value_unset (&data);
4772     gst_iterator_free (pad_it);
4773   }
4774
4775   GST_DEBUG_OBJECT (element, "free pad name found: '%s'", pad_name);
4776   return pad_name;
4777 }
4778
4779 /*
4780  */
4781 static GstPad *
4782 gst_rtp_bin_request_new_pad (GstElement * element,
4783     GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
4784 {
4785   GstRtpBin *rtpbin;
4786   GstElementClass *klass;
4787   GstPad *result;
4788
4789   gchar *pad_name = NULL;
4790
4791   g_return_val_if_fail (templ != NULL, NULL);
4792   g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
4793
4794   rtpbin = GST_RTP_BIN (element);
4795   klass = GST_ELEMENT_GET_CLASS (element);
4796
4797   GST_RTP_BIN_LOCK (rtpbin);
4798
4799   if (name == NULL) {
4800     /* use a free pad name */
4801     pad_name = gst_rtp_bin_get_free_pad_name (element, templ);
4802   } else {
4803     /* use the provided name */
4804     pad_name = g_strdup (name);
4805   }
4806
4807   GST_DEBUG_OBJECT (rtpbin, "Trying to request a pad with name %s", pad_name);
4808
4809   /* figure out the template */
4810   if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%u")) {
4811     result = create_recv_rtp (rtpbin, templ, pad_name);
4812   } else if (templ == gst_element_class_get_pad_template (klass,
4813           "recv_rtcp_sink_%u")) {
4814     result = create_recv_rtcp (rtpbin, templ, pad_name);
4815   } else if (templ == gst_element_class_get_pad_template (klass,
4816           "send_rtp_sink_%u")) {
4817     result = create_send_rtp (rtpbin, templ, pad_name);
4818   } else if (templ == gst_element_class_get_pad_template (klass,
4819           "send_rtcp_src_%u")) {
4820     result = create_send_rtcp (rtpbin, templ, pad_name);
4821   } else
4822     goto wrong_template;
4823
4824   g_free (pad_name);
4825   GST_RTP_BIN_UNLOCK (rtpbin);
4826
4827   return result;
4828
4829   /* ERRORS */
4830 wrong_template:
4831   {
4832     g_free (pad_name);
4833     GST_RTP_BIN_UNLOCK (rtpbin);
4834     g_warning ("rtpbin: this is not our template");
4835     return NULL;
4836   }
4837 }
4838
4839 static void
4840 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
4841 {
4842   GstRtpBinSession *session;
4843   GstRtpBin *rtpbin;
4844
4845   g_return_if_fail (GST_IS_GHOST_PAD (pad));
4846   g_return_if_fail (GST_IS_RTP_BIN (element));
4847
4848   rtpbin = GST_RTP_BIN (element);
4849
4850   GST_RTP_BIN_LOCK (rtpbin);
4851   GST_DEBUG_OBJECT (rtpbin, "Trying to release pad %s:%s",
4852       GST_DEBUG_PAD_NAME (pad));
4853
4854   if (!(session = find_session_by_pad (rtpbin, pad)))
4855     goto unknown_pad;
4856
4857   if (session->recv_rtp_sink_ghost == pad) {
4858     remove_recv_rtp (rtpbin, session);
4859   } else if (session->recv_rtcp_sink_ghost == pad) {
4860     remove_recv_rtcp (rtpbin, session);
4861   } else if (session->send_rtp_sink_ghost == pad) {
4862     remove_send_rtp (rtpbin, session);
4863   } else if (session->send_rtcp_src_ghost == pad) {
4864     remove_rtcp (rtpbin, session);
4865   }
4866
4867   /* no more request pads, free the complete session */
4868   if (session->recv_rtp_sink_ghost == NULL
4869       && session->recv_rtcp_sink_ghost == NULL
4870       && session->send_rtp_sink_ghost == NULL
4871       && session->send_rtcp_src_ghost == NULL) {
4872     GST_DEBUG_OBJECT (rtpbin, "no more pads for session %p", session);
4873     rtpbin->sessions = g_slist_remove (rtpbin->sessions, session);
4874     free_session (session, rtpbin);
4875   }
4876   GST_RTP_BIN_UNLOCK (rtpbin);
4877
4878   return;
4879
4880   /* ERROR */
4881 unknown_pad:
4882   {
4883     GST_RTP_BIN_UNLOCK (rtpbin);
4884     g_warning ("rtpbin: %s:%s is not one of our request pads",
4885         GST_DEBUG_PAD_NAME (pad));
4886     return;
4887   }
4888 }