upload tizen1.0 source
[framework/multimedia/gst-plugins-good0.10.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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:element-gstrtpbin
22  * @see_also: gstrtpjitterbuffer, gstrtpsession, gstrtpptdemux, gstrtpssrcdemux
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_\%d pad. The session
32  * number must be specified in the pad name.
33  * Data received on the recv_rtp_sink_\%d 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_\%d_\%d_\%d on
39  * gstrtpbin 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_\%d 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_\%d 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_\%d pad, which will
51  * automatically create a send_rtp_src_\%d 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_\%d pad after updating its internal state.
55  *
56  * The session manager needs the clock-rate of the payload types it is handling
57  * and will signal the #GstRtpSession::request-pt-map signal when it needs such a
58  * mapping. One can clear the cached values with the #GstRtpSession::clear-pt-map
59  * signal.
60  *
61  * Access to the internal statistics of gstrtpbin is provided with the
62  * get-internal-session property. This action signal gives access to the
63  * RTPSession object which further provides action signals to retrieve the
64  * internal source and other sources.
65  *
66  * <refsect2>
67  * <title>Example pipelines</title>
68  * |[
69  * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink_0 \
70  *     gstrtpbin ! rtptheoradepay ! theoradec ! xvimagesink
71  * ]| Receive RTP data from port 5000 and send to the session 0 in gstrtpbin.
72  * |[
73  * gst-launch gstrtpbin name=rtpbin \
74  *         v4l2src ! ffmpegcolorspace ! ffenc_h263 ! rtph263ppay ! rtpbin.send_rtp_sink_0 \
75  *                   rtpbin.send_rtp_src_0 ! udpsink port=5000                            \
76  *                   rtpbin.send_rtcp_src_0 ! udpsink port=5001 sync=false async=false    \
77  *                   udpsrc port=5005 ! rtpbin.recv_rtcp_sink_0                           \
78  *         audiotestsrc ! amrnbenc ! rtpamrpay ! rtpbin.send_rtp_sink_1                   \
79  *                   rtpbin.send_rtp_src_1 ! udpsink port=5002                            \
80  *                   rtpbin.send_rtcp_src_1 ! udpsink port=5003 sync=false async=false    \
81  *                   udpsrc port=5007 ! rtpbin.recv_rtcp_sink_1
82  * ]| Encode and payload H263 video captured from a v4l2src. Encode and payload AMR
83  * audio generated from audiotestsrc. The video is sent to session 0 in rtpbin
84  * and the audio is sent to session 1. Video packets are sent on UDP port 5000
85  * and audio packets on port 5002. The video RTCP packets for session 0 are sent
86  * on port 5001 and the audio RTCP packets for session 0 are sent on port 5003.
87  * RTCP packets for session 0 are received on port 5005 and RTCP for session 1
88  * is received on port 5007. Since RTCP packets from the sender should be sent
89  * as soon as possible and do not participate in preroll, sync=false and
90  * async=false is configured on udpsink
91  * |[
92  * gst-launch -v gstrtpbin name=rtpbin                                          \
93  *     udpsrc caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H263-1998" \
94  *             port=5000 ! rtpbin.recv_rtp_sink_0                                \
95  *         rtpbin. ! rtph263pdepay ! ffdec_h263 ! xvimagesink                    \
96  *      udpsrc port=5001 ! rtpbin.recv_rtcp_sink_0                               \
97  *      rtpbin.send_rtcp_src_0 ! udpsink port=5005 sync=false async=false        \
98  *     udpsrc caps="application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)AMR,encoding-params=(string)1,octet-align=(string)1" \
99  *             port=5002 ! rtpbin.recv_rtp_sink_1                                \
100  *         rtpbin. ! rtpamrdepay ! amrnbdec ! alsasink                           \
101  *      udpsrc port=5003 ! rtpbin.recv_rtcp_sink_1                               \
102  *      rtpbin.send_rtcp_src_1 ! udpsink port=5007 sync=false async=false
103  * ]| Receive H263 on port 5000, send it through rtpbin in session 0, depayload,
104  * decode and display the video.
105  * Receive AMR on port 5002, send it through rtpbin in session 1, depayload,
106  * decode and play the audio.
107  * Receive server RTCP packets for session 0 on port 5001 and RTCP packets for
108  * session 1 on port 5003. These packets will be used for session management and
109  * synchronisation.
110  * Send RTCP reports for session 0 on port 5005 and RTCP reports for session 1
111  * on port 5007.
112  * </refsect2>
113  *
114  * Last reviewed on 2007-08-30 (0.10.6)
115  */
116
117 #ifdef HAVE_CONFIG_H
118 #include "config.h"
119 #endif
120 #include <stdio.h>
121 #include <string.h>
122
123 #include <gst/rtp/gstrtpbuffer.h>
124 #include <gst/rtp/gstrtcpbuffer.h>
125
126 #include "gstrtpbin-marshal.h"
127 #include "gstrtpbin.h"
128 #include "rtpsession.h"
129 #include "gstrtpsession.h"
130 #include "gstrtpjitterbuffer.h"
131
132 GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
133 #define GST_CAT_DEFAULT gst_rtp_bin_debug
134
135 /* sink pads */
136 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
137 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%d",
138     GST_PAD_SINK,
139     GST_PAD_REQUEST,
140     GST_STATIC_CAPS ("application/x-rtp")
141     );
142
143 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
144 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%d",
145     GST_PAD_SINK,
146     GST_PAD_REQUEST,
147     GST_STATIC_CAPS ("application/x-rtcp")
148     );
149
150 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
151 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%d",
152     GST_PAD_SINK,
153     GST_PAD_REQUEST,
154     GST_STATIC_CAPS ("application/x-rtp")
155     );
156
157 /* src pads */
158 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
159 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%d_%d_%d",
160     GST_PAD_SRC,
161     GST_PAD_SOMETIMES,
162     GST_STATIC_CAPS ("application/x-rtp")
163     );
164
165 static GstStaticPadTemplate rtpbin_send_rtcp_src_template =
166 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%d",
167     GST_PAD_SRC,
168     GST_PAD_REQUEST,
169     GST_STATIC_CAPS ("application/x-rtcp")
170     );
171
172 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
173 GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%d",
174     GST_PAD_SRC,
175     GST_PAD_SOMETIMES,
176     GST_STATIC_CAPS ("application/x-rtp")
177     );
178
179 #define GST_RTP_BIN_GET_PRIVATE(obj)  \
180    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_BIN, GstRtpBinPrivate))
181
182 #define GST_RTP_BIN_LOCK(bin)   g_mutex_lock ((bin)->priv->bin_lock)
183 #define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock ((bin)->priv->bin_lock)
184
185 /* lock to protect dynamic callbacks, like pad-added and new ssrc. */
186 #define GST_RTP_BIN_DYN_LOCK(bin)    g_mutex_lock ((bin)->priv->dyn_lock)
187 #define GST_RTP_BIN_DYN_UNLOCK(bin)  g_mutex_unlock ((bin)->priv->dyn_lock)
188
189 /* lock for shutdown */
190 #define GST_RTP_BIN_SHUTDOWN_LOCK(bin,label)     \
191 G_STMT_START {                                   \
192   if (g_atomic_int_get (&bin->priv->shutdown))   \
193     goto label;                                  \
194   GST_RTP_BIN_DYN_LOCK (bin);                    \
195   if (g_atomic_int_get (&bin->priv->shutdown)) { \
196     GST_RTP_BIN_DYN_UNLOCK (bin);                \
197     goto label;                                  \
198   }                                              \
199 } G_STMT_END
200
201 /* unlock for shutdown */
202 #define GST_RTP_BIN_SHUTDOWN_UNLOCK(bin)         \
203   GST_RTP_BIN_DYN_UNLOCK (bin);                  \
204
205 struct _GstRtpBinPrivate
206 {
207   GMutex *bin_lock;
208
209   /* lock protecting dynamic adding/removing */
210   GMutex *dyn_lock;
211
212   /* if we are shutting down or not */
213   gint shutdown;
214
215   gboolean autoremove;
216 };
217
218 /* signals and args */
219 enum
220 {
221   SIGNAL_REQUEST_PT_MAP,
222   SIGNAL_PAYLOAD_TYPE_CHANGE,
223   SIGNAL_CLEAR_PT_MAP,
224   SIGNAL_RESET_SYNC,
225   SIGNAL_GET_INTERNAL_SESSION,
226
227   SIGNAL_ON_NEW_SSRC,
228   SIGNAL_ON_SSRC_COLLISION,
229   SIGNAL_ON_SSRC_VALIDATED,
230   SIGNAL_ON_SSRC_ACTIVE,
231   SIGNAL_ON_SSRC_SDES,
232   SIGNAL_ON_BYE_SSRC,
233   SIGNAL_ON_BYE_TIMEOUT,
234   SIGNAL_ON_TIMEOUT,
235   SIGNAL_ON_SENDER_TIMEOUT,
236   SIGNAL_ON_NPT_STOP,
237   LAST_SIGNAL
238 };
239
240 #define DEFAULT_LATENCY_MS           200
241 #define DEFAULT_SDES                 NULL
242 #define DEFAULT_DO_LOST              FALSE
243 #define DEFAULT_IGNORE_PT            FALSE
244 #define DEFAULT_NTP_SYNC             FALSE
245 #define DEFAULT_AUTOREMOVE           FALSE
246 #define DEFAULT_BUFFER_MODE          RTP_JITTER_BUFFER_MODE_SLAVE
247 #define DEFAULT_USE_PIPELINE_CLOCK   FALSE
248
249 enum
250 {
251   PROP_0,
252   PROP_LATENCY,
253   PROP_SDES,
254   PROP_DO_LOST,
255   PROP_IGNORE_PT,
256   PROP_NTP_SYNC,
257   PROP_AUTOREMOVE,
258   PROP_BUFFER_MODE,
259   PROP_USE_PIPELINE_CLOCK,
260   PROP_LAST
261 };
262
263 /* helper objects */
264 typedef struct _GstRtpBinSession GstRtpBinSession;
265 typedef struct _GstRtpBinStream GstRtpBinStream;
266 typedef struct _GstRtpBinClient GstRtpBinClient;
267
268 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
269
270 static GstCaps *pt_map_requested (GstElement * element, guint pt,
271     GstRtpBinSession * session);
272 static void payload_type_change (GstElement * element, guint pt,
273     GstRtpBinSession * session);
274 static void free_client (GstRtpBinClient * client, GstRtpBin * bin);
275 static void free_stream (GstRtpBinStream * stream);
276
277 /* Manages the RTP stream for one SSRC.
278  *
279  * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
280  * If we see an SDES RTCP packet that links multiple SSRCs together based on a
281  * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
282  * together (see below).
283  */
284 struct _GstRtpBinStream
285 {
286   /* the SSRC of this stream */
287   guint32 ssrc;
288
289   /* parent bin */
290   GstRtpBin *bin;
291
292   /* the session this SSRC belongs to */
293   GstRtpBinSession *session;
294
295   /* the jitterbuffer of the SSRC */
296   GstElement *buffer;
297   gulong buffer_handlesync_sig;
298   gulong buffer_ptreq_sig;
299   gulong buffer_ntpstop_sig;
300   gint percent;
301
302   /* the PT demuxer of the SSRC */
303   GstElement *demux;
304   gulong demux_newpad_sig;
305   gulong demux_padremoved_sig;
306   gulong demux_ptreq_sig;
307   gulong demux_ptchange_sig;
308
309   /* if we have calculated a valid rt_delta for this stream */
310   gboolean have_sync;
311   /* mapping to local RTP and NTP time */
312   gint64 rt_delta;
313 };
314
315 #define GST_RTP_SESSION_LOCK(sess)   g_mutex_lock ((sess)->lock)
316 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock ((sess)->lock)
317
318 /* Manages the receiving end of the packets.
319  *
320  * There is one such structure for each RTP session (audio/video/...).
321  * We get the RTP/RTCP packets and stuff them into the session manager. From
322  * there they are pushed into an SSRC demuxer that splits the stream based on
323  * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
324  * the GstRtpBinStream above).
325  */
326 struct _GstRtpBinSession
327 {
328   /* session id */
329   gint id;
330   /* the parent bin */
331   GstRtpBin *bin;
332   /* the session element */
333   GstElement *session;
334   /* the SSRC demuxer */
335   GstElement *demux;
336   gulong demux_newpad_sig;
337   gulong demux_padremoved_sig;
338
339   GMutex *lock;
340
341   /* list of GstRtpBinStream */
342   GSList *streams;
343
344   /* mapping of payload type to caps */
345   GHashTable *ptmap;
346
347   /* the pads of the session */
348   GstPad *recv_rtp_sink;
349   GstPad *recv_rtp_sink_ghost;
350   GstPad *recv_rtp_src;
351   GstPad *recv_rtcp_sink;
352   GstPad *recv_rtcp_sink_ghost;
353   GstPad *sync_src;
354   GstPad *send_rtp_sink;
355   GstPad *send_rtp_sink_ghost;
356   GstPad *send_rtp_src;
357   GstPad *send_rtp_src_ghost;
358   GstPad *send_rtcp_src;
359   GstPad *send_rtcp_src_ghost;
360 };
361
362 /* Manages the RTP streams that come from one client and should therefore be
363  * synchronized.
364  */
365 struct _GstRtpBinClient
366 {
367   /* the common CNAME for the streams */
368   gchar *cname;
369   guint cname_len;
370
371   /* the streams */
372   guint nstreams;
373   GSList *streams;
374 };
375
376 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
377 static GstRtpBinSession *
378 find_session_by_id (GstRtpBin * rtpbin, gint id)
379 {
380   GSList *walk;
381
382   for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
383     GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
384
385     if (sess->id == id)
386       return sess;
387   }
388   return NULL;
389 }
390
391 /* find a session with the given request pad. Must be called with RTP_BIN_LOCK */
392 static GstRtpBinSession *
393 find_session_by_pad (GstRtpBin * rtpbin, GstPad * pad)
394 {
395   GSList *walk;
396
397   for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
398     GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
399
400     if ((sess->recv_rtp_sink_ghost == pad) ||
401         (sess->recv_rtcp_sink_ghost == pad) ||
402         (sess->send_rtp_sink_ghost == pad)
403         || (sess->send_rtcp_src_ghost == pad))
404       return sess;
405   }
406   return NULL;
407 }
408
409 static void
410 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
411 {
412   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
413       sess->id, ssrc);
414 }
415
416 static void
417 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
418 {
419   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
420       sess->id, ssrc);
421 }
422
423 static void
424 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
425 {
426   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
427       sess->id, ssrc);
428 }
429
430 static void
431 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
432 {
433   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
434       sess->id, ssrc);
435 }
436
437 static void
438 on_ssrc_sdes (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
439 {
440   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES], 0,
441       sess->id, ssrc);
442 }
443
444 static void
445 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
446 {
447   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
448       sess->id, ssrc);
449 }
450
451 static void
452 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
453 {
454   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
455       sess->id, ssrc);
456
457   if (sess->bin->priv->autoremove)
458     g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
459 }
460
461 static void
462 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
463 {
464   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
465       sess->id, ssrc);
466
467   if (sess->bin->priv->autoremove)
468     g_signal_emit_by_name (sess->demux, "clear-ssrc", ssrc, NULL);
469 }
470
471 static void
472 on_sender_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
473 {
474   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
475       sess->id, ssrc);
476 }
477
478 static void
479 on_npt_stop (GstElement * jbuf, GstRtpBinStream * stream)
480 {
481   g_signal_emit (stream->bin, gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP], 0,
482       stream->session->id, stream->ssrc);
483 }
484
485 /* must be called with the SESSION lock */
486 static GstRtpBinStream *
487 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
488 {
489   GSList *walk;
490
491   for (walk = session->streams; walk; walk = g_slist_next (walk)) {
492     GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
493
494     if (stream->ssrc == ssrc)
495       return stream;
496   }
497   return NULL;
498 }
499
500 static void
501 ssrc_demux_pad_removed (GstElement * element, guint ssrc, GstPad * pad,
502     GstRtpBinSession * session)
503 {
504   GstRtpBinStream *stream = NULL;
505
506   GST_RTP_SESSION_LOCK (session);
507   if ((stream = find_stream_by_ssrc (session, ssrc)))
508     session->streams = g_slist_remove (session->streams, stream);
509   GST_RTP_SESSION_UNLOCK (session);
510
511   if (stream)
512     free_stream (stream);
513 }
514
515 /* create a session with the given id.  Must be called with RTP_BIN_LOCK */
516 static GstRtpBinSession *
517 create_session (GstRtpBin * rtpbin, gint id)
518 {
519   GstRtpBinSession *sess;
520   GstElement *session, *demux;
521   GstState target;
522
523   if (!(session = gst_element_factory_make ("gstrtpsession", NULL)))
524     goto no_session;
525
526   if (!(demux = gst_element_factory_make ("gstrtpssrcdemux", NULL)))
527     goto no_demux;
528
529   sess = g_new0 (GstRtpBinSession, 1);
530   sess->lock = g_mutex_new ();
531   sess->id = id;
532   sess->bin = rtpbin;
533   sess->session = session;
534   sess->demux = demux;
535   sess->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
536       (GDestroyNotify) gst_caps_unref);
537   rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
538
539   /* configure SDES items */
540   GST_OBJECT_LOCK (rtpbin);
541   g_object_set (session, "sdes", rtpbin->sdes, "use-pipeline-clock",
542       rtpbin->use_pipeline_clock, NULL);
543   GST_OBJECT_UNLOCK (rtpbin);
544
545   /* provide clock_rate to the session manager when needed */
546   g_signal_connect (session, "request-pt-map",
547       (GCallback) pt_map_requested, sess);
548
549   g_signal_connect (sess->session, "on-new-ssrc",
550       (GCallback) on_new_ssrc, sess);
551   g_signal_connect (sess->session, "on-ssrc-collision",
552       (GCallback) on_ssrc_collision, sess);
553   g_signal_connect (sess->session, "on-ssrc-validated",
554       (GCallback) on_ssrc_validated, sess);
555   g_signal_connect (sess->session, "on-ssrc-active",
556       (GCallback) on_ssrc_active, sess);
557   g_signal_connect (sess->session, "on-ssrc-sdes",
558       (GCallback) on_ssrc_sdes, sess);
559   g_signal_connect (sess->session, "on-bye-ssrc",
560       (GCallback) on_bye_ssrc, sess);
561   g_signal_connect (sess->session, "on-bye-timeout",
562       (GCallback) on_bye_timeout, sess);
563   g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
564   g_signal_connect (sess->session, "on-sender-timeout",
565       (GCallback) on_sender_timeout, sess);
566
567   gst_bin_add (GST_BIN_CAST (rtpbin), session);
568   gst_bin_add (GST_BIN_CAST (rtpbin), demux);
569
570   GST_OBJECT_LOCK (rtpbin);
571   target = GST_STATE_TARGET (rtpbin);
572   GST_OBJECT_UNLOCK (rtpbin);
573
574   /* change state only to what's needed */
575   gst_element_set_state (demux, target);
576   gst_element_set_state (session, target);
577
578   return sess;
579
580   /* ERRORS */
581 no_session:
582   {
583     g_warning ("gstrtpbin: could not create gstrtpsession element");
584     return NULL;
585   }
586 no_demux:
587   {
588     gst_object_unref (session);
589     g_warning ("gstrtpbin: could not create gstrtpssrcdemux element");
590     return NULL;
591   }
592 }
593
594 static void
595 free_session (GstRtpBinSession * sess, GstRtpBin * bin)
596 {
597   GSList *client_walk;
598
599   GST_DEBUG_OBJECT (bin, "freeing session %p", sess);
600
601   gst_element_set_locked_state (sess->demux, TRUE);
602   gst_element_set_locked_state (sess->session, TRUE);
603
604   gst_element_set_state (sess->demux, GST_STATE_NULL);
605   gst_element_set_state (sess->session, GST_STATE_NULL);
606
607   if (sess->recv_rtp_sink != NULL) {
608     gst_element_release_request_pad (sess->session, sess->recv_rtp_sink);
609     gst_object_unref (sess->recv_rtp_sink);
610   }
611   if (sess->recv_rtp_src != NULL)
612     gst_object_unref (sess->recv_rtp_src);
613   if (sess->recv_rtcp_sink != NULL) {
614     gst_element_release_request_pad (sess->session, sess->recv_rtcp_sink);
615     gst_object_unref (sess->recv_rtcp_sink);
616   }
617   if (sess->sync_src != NULL)
618     gst_object_unref (sess->sync_src);
619   if (sess->send_rtp_sink != NULL) {
620     gst_element_release_request_pad (sess->session, sess->send_rtp_sink);
621     gst_object_unref (sess->send_rtp_sink);
622   }
623   if (sess->send_rtp_src != NULL)
624     gst_object_unref (sess->send_rtp_src);
625   if (sess->send_rtcp_src != NULL) {
626     gst_element_release_request_pad (sess->session, sess->send_rtcp_src);
627     gst_object_unref (sess->send_rtcp_src);
628   }
629
630   gst_bin_remove (GST_BIN_CAST (bin), sess->session);
631   gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
632
633   /* remove any references in bin->clients to the streams in sess->streams */
634   client_walk = bin->clients;
635   while (client_walk) {
636     GSList *client_node = client_walk;
637     GstRtpBinClient *client = (GstRtpBinClient *) client_node->data;
638     GSList *stream_walk = client->streams;
639
640     while (stream_walk) {
641       GSList *stream_node = stream_walk;
642       GstRtpBinStream *stream = (GstRtpBinStream *) stream_node->data;
643       GSList *inner_walk;
644
645       stream_walk = g_slist_next (stream_walk);
646
647       for (inner_walk = sess->streams; inner_walk;
648           inner_walk = g_slist_next (inner_walk)) {
649         if ((GstRtpBinStream *) inner_walk->data == stream) {
650           client->streams = g_slist_delete_link (client->streams, stream_node);
651           --client->nstreams;
652           break;
653         }
654       }
655     }
656     client_walk = g_slist_next (client_walk);
657
658     g_assert ((client->streams && client->nstreams > 0) || (!client->streams
659             && client->streams == 0));
660     if (client->nstreams == 0) {
661       free_client (client, bin);
662       bin->clients = g_slist_delete_link (bin->clients, client_node);
663     }
664   }
665
666   g_slist_foreach (sess->streams, (GFunc) free_stream, NULL);
667   g_slist_free (sess->streams);
668
669   g_mutex_free (sess->lock);
670   g_hash_table_destroy (sess->ptmap);
671
672   g_free (sess);
673 }
674
675 /* get the payload type caps for the specific payload @pt in @session */
676 static GstCaps *
677 get_pt_map (GstRtpBinSession * session, guint pt)
678 {
679   GstCaps *caps = NULL;
680   GstRtpBin *bin;
681   GValue ret = { 0 };
682   GValue args[3] = { {0}, {0}, {0} };
683
684   GST_DEBUG ("searching pt %d in cache", pt);
685
686   GST_RTP_SESSION_LOCK (session);
687
688   /* first look in the cache */
689   caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
690   if (caps) {
691     gst_caps_ref (caps);
692     goto done;
693   }
694
695   bin = session->bin;
696
697   GST_DEBUG ("emiting signal for pt %d in session %d", pt, session->id);
698
699   /* not in cache, send signal to request caps */
700   g_value_init (&args[0], GST_TYPE_ELEMENT);
701   g_value_set_object (&args[0], bin);
702   g_value_init (&args[1], G_TYPE_UINT);
703   g_value_set_uint (&args[1], session->id);
704   g_value_init (&args[2], G_TYPE_UINT);
705   g_value_set_uint (&args[2], pt);
706
707   g_value_init (&ret, GST_TYPE_CAPS);
708   g_value_set_boxed (&ret, NULL);
709
710   GST_RTP_SESSION_UNLOCK (session);
711
712   g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
713
714   GST_RTP_SESSION_LOCK (session);
715
716   g_value_unset (&args[0]);
717   g_value_unset (&args[1]);
718   g_value_unset (&args[2]);
719
720   /* look in the cache again because we let the lock go */
721   caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
722   if (caps) {
723     gst_caps_ref (caps);
724     g_value_unset (&ret);
725     goto done;
726   }
727
728   caps = (GstCaps *) g_value_dup_boxed (&ret);
729   g_value_unset (&ret);
730   if (!caps)
731     goto no_caps;
732
733   GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
734
735   /* store in cache, take additional ref */
736   g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt),
737       gst_caps_ref (caps));
738
739 done:
740   GST_RTP_SESSION_UNLOCK (session);
741
742   return caps;
743
744   /* ERRORS */
745 no_caps:
746   {
747     GST_RTP_SESSION_UNLOCK (session);
748     GST_DEBUG ("no pt map could be obtained");
749     return NULL;
750   }
751 }
752
753 static gboolean
754 return_true (gpointer key, gpointer value, gpointer user_data)
755 {
756   return TRUE;
757 }
758
759 static void
760 gst_rtp_bin_reset_sync (GstRtpBin * rtpbin)
761 {
762   GSList *clients, *streams;
763
764   GST_DEBUG_OBJECT (rtpbin, "Reset sync on all clients");
765
766   GST_RTP_BIN_LOCK (rtpbin);
767   for (clients = rtpbin->clients; clients; clients = g_slist_next (clients)) {
768     GstRtpBinClient *client = (GstRtpBinClient *) clients->data;
769
770     /* reset sync on all streams for this client */
771     for (streams = client->streams; streams; streams = g_slist_next (streams)) {
772       GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
773
774       /* make use require a new SR packet for this stream before we attempt new
775        * lip-sync */
776       stream->have_sync = FALSE;
777       stream->rt_delta = 0;
778     }
779   }
780   GST_RTP_BIN_UNLOCK (rtpbin);
781 }
782
783 static void
784 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
785 {
786   GSList *sessions, *streams;
787
788   GST_RTP_BIN_LOCK (bin);
789   GST_DEBUG_OBJECT (bin, "clearing pt map");
790   for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
791     GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
792
793     GST_DEBUG_OBJECT (bin, "clearing session %p", session);
794     g_signal_emit_by_name (session->session, "clear-pt-map", NULL);
795
796     GST_RTP_SESSION_LOCK (session);
797     g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
798
799     for (streams = session->streams; streams; streams = g_slist_next (streams)) {
800       GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
801
802       GST_DEBUG_OBJECT (bin, "clearing stream %p", stream);
803       g_signal_emit_by_name (stream->buffer, "clear-pt-map", NULL);
804       if (stream->demux)
805         g_signal_emit_by_name (stream->demux, "clear-pt-map", NULL);
806     }
807     GST_RTP_SESSION_UNLOCK (session);
808   }
809   GST_RTP_BIN_UNLOCK (bin);
810
811   /* reset sync too */
812   gst_rtp_bin_reset_sync (bin);
813 }
814
815 static RTPSession *
816 gst_rtp_bin_get_internal_session (GstRtpBin * bin, guint session_id)
817 {
818   RTPSession *internal_session = NULL;
819   GstRtpBinSession *session;
820
821   GST_RTP_BIN_LOCK (bin);
822   GST_DEBUG_OBJECT (bin, "retrieving internal RTPSession object, index: %d",
823       session_id);
824   session = find_session_by_id (bin, (gint) session_id);
825   if (session) {
826     g_object_get (session->session, "internal-session", &internal_session,
827         NULL);
828   }
829   GST_RTP_BIN_UNLOCK (bin);
830
831   return internal_session;
832 }
833
834 static void
835 gst_rtp_bin_propagate_property_to_jitterbuffer (GstRtpBin * bin,
836     const gchar * name, const GValue * value)
837 {
838   GSList *sessions, *streams;
839
840   GST_RTP_BIN_LOCK (bin);
841   for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
842     GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
843
844     GST_RTP_SESSION_LOCK (session);
845     for (streams = session->streams; streams; streams = g_slist_next (streams)) {
846       GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
847
848       g_object_set_property (G_OBJECT (stream->buffer), name, value);
849     }
850     GST_RTP_SESSION_UNLOCK (session);
851   }
852   GST_RTP_BIN_UNLOCK (bin);
853 }
854
855 /* get a client with the given SDES name. Must be called with RTP_BIN_LOCK */
856 static GstRtpBinClient *
857 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
858 {
859   GstRtpBinClient *result = NULL;
860   GSList *walk;
861
862   for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
863     GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
864
865     if (len != client->cname_len)
866       continue;
867
868     if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
869       GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
870           client->cname);
871       result = client;
872       break;
873     }
874   }
875
876   /* nothing found, create one */
877   if (result == NULL) {
878     result = g_new0 (GstRtpBinClient, 1);
879     result->cname = g_strndup ((gchar *) data, len);
880     result->cname_len = len;
881     bin->clients = g_slist_prepend (bin->clients, result);
882     GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
883         result->cname);
884   }
885   return result;
886 }
887
888 static void
889 free_client (GstRtpBinClient * client, GstRtpBin * bin)
890 {
891   GST_DEBUG_OBJECT (bin, "freeing client %p", client);
892   g_slist_free (client->streams);
893   g_free (client->cname);
894   g_free (client);
895 }
896
897 static void
898 get_current_times (GstRtpBin * bin, GstClockTime * running_time,
899     guint64 * ntpnstime)
900 {
901   guint64 ntpns;
902   GstClock *clock;
903   GstClockTime base_time, rt, clock_time;
904
905   GST_OBJECT_LOCK (bin);
906   if ((clock = GST_ELEMENT_CLOCK (bin))) {
907     base_time = GST_ELEMENT_CAST (bin)->base_time;
908     gst_object_ref (clock);
909     GST_OBJECT_UNLOCK (bin);
910
911     clock_time = gst_clock_get_time (clock);
912
913     if (bin->use_pipeline_clock) {
914       ntpns = clock_time;
915     } else {
916       GTimeVal current;
917
918       /* get current NTP time */
919       g_get_current_time (&current);
920       ntpns = GST_TIMEVAL_TO_TIME (current);
921     }
922
923     /* add constant to convert from 1970 based time to 1900 based time */
924     ntpns += (2208988800LL * GST_SECOND);
925
926     /* get current clock time and convert to running time */
927     rt = clock_time - base_time;
928
929     gst_object_unref (clock);
930   } else {
931     GST_OBJECT_UNLOCK (bin);
932     rt = -1;
933     ntpns = -1;
934   }
935   if (running_time)
936     *running_time = rt;
937   if (ntpnstime)
938     *ntpnstime = ntpns;
939 }
940
941 static void
942 stream_set_ts_offset (GstRtpBin * bin, GstRtpBinStream * stream,
943     gint64 ts_offset)
944 {
945   gint64 prev_ts_offset;
946
947   g_object_get (stream->buffer, "ts-offset", &prev_ts_offset, NULL);
948
949   /* delta changed, see how much */
950   if (prev_ts_offset != ts_offset) {
951     gint64 diff;
952
953     diff = prev_ts_offset - ts_offset;
954
955     GST_DEBUG_OBJECT (bin,
956         "ts-offset %" G_GINT64_FORMAT ", prev %" G_GINT64_FORMAT
957         ", diff: %" G_GINT64_FORMAT, ts_offset, prev_ts_offset, diff);
958
959     /* only change diff when it changed more than 4 milliseconds. This
960      * compensates for rounding errors in NTP to RTP timestamp
961      * conversions */
962     if (ABS (diff) > 4 * GST_MSECOND) {
963       if (ABS (diff) < (3 * GST_SECOND)) {
964         g_object_set (stream->buffer, "ts-offset", ts_offset, NULL);
965       } else {
966         GST_WARNING_OBJECT (bin, "offset unusually large, ignoring");
967       }
968     } else {
969       GST_DEBUG_OBJECT (bin, "offset too small, ignoring");
970     }
971   }
972   GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
973       stream->ssrc, ts_offset);
974 }
975
976 /* associate a stream to the given CNAME. This will make sure all streams for
977  * that CNAME are synchronized together.
978  * Must be called with GST_RTP_BIN_LOCK */
979 static void
980 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
981     guint8 * data, guint64 ntptime, guint64 last_extrtptime,
982     guint64 base_rtptime, guint64 base_time, guint clock_rate)
983 {
984   GstRtpBinClient *client;
985   gboolean created;
986   GSList *walk;
987   guint64 local_rt;
988   guint64 local_rtp;
989   GstClockTime running_time;
990   guint64 ntpnstime;
991   gint64 ntpdiff, rtdiff;
992   guint64 last_unix;
993
994   /* first find or create the CNAME */
995   client = get_client (bin, len, data, &created);
996
997   /* find stream in the client */
998   for (walk = client->streams; walk; walk = g_slist_next (walk)) {
999     GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1000
1001     if (ostream == stream)
1002       break;
1003   }
1004   /* not found, add it to the list */
1005   if (walk == NULL) {
1006     GST_DEBUG_OBJECT (bin,
1007         "new association of SSRC %08x with client %p with CNAME %s",
1008         stream->ssrc, client, client->cname);
1009     client->streams = g_slist_prepend (client->streams, stream);
1010     client->nstreams++;
1011   } else {
1012     GST_DEBUG_OBJECT (bin,
1013         "found association of SSRC %08x with client %p with CNAME %s",
1014         stream->ssrc, client, client->cname);
1015   }
1016
1017   /* Take the extended rtptime we found in the SR packet and map it to the
1018    * local rtptime. The local rtp time is used to construct timestamps on the
1019    * buffers so we will calculate what running_time corresponds to the RTP
1020    * timestamp in the SR packet. */
1021   local_rtp = last_extrtptime - base_rtptime;
1022
1023   GST_DEBUG_OBJECT (bin,
1024       "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
1025       ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d", base_rtptime,
1026       last_extrtptime, local_rtp, clock_rate);
1027
1028   /* calculate local RTP time in gstreamer timestamp, we essentially perform the
1029    * same conversion that a jitterbuffer would use to convert an rtp timestamp
1030    * into a corresponding gstreamer timestamp. Note that the base_time also
1031    * contains the drift between sender and receiver. */
1032   local_rt = gst_util_uint64_scale_int (local_rtp, GST_SECOND, clock_rate);
1033   local_rt += base_time;
1034
1035   /* convert ntptime to unix time since 1900 */
1036   last_unix = gst_util_uint64_scale (ntptime, GST_SECOND,
1037       (G_GINT64_CONSTANT (1) << 32));
1038
1039   stream->have_sync = TRUE;
1040
1041   GST_DEBUG_OBJECT (bin,
1042       "local UNIX %" G_GUINT64_FORMAT ", remote UNIX %" G_GUINT64_FORMAT,
1043       local_rt, last_unix);
1044
1045   /* recalc inter stream playout offset, but only if there is more than one
1046    * stream or we're doing NTP sync. */
1047   if (bin->ntp_sync) {
1048     /* For NTP sync we need to first get a snapshot of running_time and NTP
1049      * time. We know at what running_time we play a certain RTP time, we also
1050      * calculated when we would play the RTP time in the SR packet. Now we need
1051      * to know how the running_time and the NTP time relate to eachother. */
1052     get_current_times (bin, &running_time, &ntpnstime);
1053
1054     /* see how far away the NTP time is. This is the difference between the
1055      * current NTP time and the NTP time in the last SR packet. */
1056     ntpdiff = ntpnstime - last_unix;
1057     /* see how far away the running_time is. This is the difference between the
1058      * current running_time and the running_time of the RTP timestamp in the
1059      * last SR packet. */
1060     rtdiff = running_time - local_rt;
1061
1062     GST_DEBUG_OBJECT (bin,
1063         "NTP time %" G_GUINT64_FORMAT ", last unix %" G_GUINT64_FORMAT,
1064         ntpnstime, last_unix);
1065     GST_DEBUG_OBJECT (bin,
1066         "NTP diff %" G_GINT64_FORMAT ", RT diff %" G_GINT64_FORMAT, ntpdiff,
1067         rtdiff);
1068
1069     /* combine to get the final diff to apply to the running_time */
1070     stream->rt_delta = rtdiff - ntpdiff;
1071
1072     stream_set_ts_offset (bin, stream, stream->rt_delta);
1073   } else if (client->nstreams > 1) {
1074     gint64 min;
1075
1076     /* calculate delta between server and receiver. last_unix is created by
1077      * converting the ntptime in the last SR packet to a gstreamer timestamp. This
1078      * delta expresses the difference to our timeline and the server timeline. The
1079      * difference in itself doesn't mean much but we can combine the delta of
1080      * multiple streams to create a stream specific offset. */
1081     stream->rt_delta = last_unix - local_rt;
1082
1083     /* calculate the min of all deltas, ignoring streams that did not yet have a
1084      * valid rt_delta because we did not yet receive an SR packet for those
1085      * streams.
1086      * We calculate the mininum because we would like to only apply positive
1087      * offsets to streams, delaying their playback instead of trying to speed up
1088      * other streams (which might be imposible when we have to create negative
1089      * latencies).
1090      * The stream that has the smallest diff is selected as the reference stream,
1091      * all other streams will have a positive offset to this difference. */
1092     min = G_MAXINT64;
1093     for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1094       GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1095
1096       if (!ostream->have_sync)
1097         continue;
1098
1099       if (ostream->rt_delta < min)
1100         min = ostream->rt_delta;
1101     }
1102
1103     GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT, client,
1104         min);
1105
1106     /* calculate offsets for each stream */
1107     for (walk = client->streams; walk; walk = g_slist_next (walk)) {
1108       GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
1109       gint64 ts_offset;
1110
1111       /* ignore streams for which we didn't receive an SR packet yet, we
1112        * can't synchronize them yet. We can however sync other streams just
1113        * fine. */
1114       if (!ostream->have_sync)
1115         continue;
1116
1117       /* calculate offset to our reference stream, this should always give a
1118        * positive number. */
1119       ts_offset = ostream->rt_delta - min;
1120
1121       stream_set_ts_offset (bin, ostream, ts_offset);
1122     }
1123   }
1124   return;
1125 }
1126
1127 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
1128   for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
1129           (b) = gst_rtcp_packet_move_to_next ((packet)))
1130
1131 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
1132   for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
1133           (b) = gst_rtcp_packet_sdes_next_item ((packet)))
1134
1135 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
1136   for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
1137           (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
1138
1139 static void
1140 gst_rtp_bin_handle_sync (GstElement * jitterbuffer, GstStructure * s,
1141     GstRtpBinStream * stream)
1142 {
1143   GstRtpBin *bin;
1144   GstRTCPPacket packet;
1145   guint32 ssrc;
1146   guint64 ntptime;
1147   gboolean have_sr, have_sdes;
1148   gboolean more;
1149   guint64 base_rtptime;
1150   guint64 base_time;
1151   guint clock_rate;
1152   guint64 extrtptime;
1153   GstBuffer *buffer;
1154
1155   bin = stream->bin;
1156
1157   GST_DEBUG_OBJECT (bin, "sync handler called");
1158
1159   /* get the last relation between the rtp timestamps and the gstreamer
1160    * timestamps. We get this info directly from the jitterbuffer which
1161    * constructs gstreamer timestamps from rtp timestamps and so it know exactly
1162    * what the current situation is. */
1163   base_rtptime =
1164       g_value_get_uint64 (gst_structure_get_value (s, "base-rtptime"));
1165   base_time = g_value_get_uint64 (gst_structure_get_value (s, "base-time"));
1166   clock_rate = g_value_get_uint (gst_structure_get_value (s, "clock-rate"));
1167   extrtptime =
1168       g_value_get_uint64 (gst_structure_get_value (s, "sr-ext-rtptime"));
1169   buffer = gst_value_get_buffer (gst_structure_get_value (s, "sr-buffer"));
1170
1171   have_sr = FALSE;
1172   have_sdes = FALSE;
1173   GST_RTCP_BUFFER_FOR_PACKETS (more, buffer, &packet) {
1174     /* first packet must be SR or RR or else the validate would have failed */
1175     switch (gst_rtcp_packet_get_type (&packet)) {
1176       case GST_RTCP_TYPE_SR:
1177         /* only parse first. There is only supposed to be one SR in the packet
1178          * but we will deal with malformed packets gracefully */
1179         if (have_sr)
1180           break;
1181         /* get NTP and RTP times */
1182         gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, NULL,
1183             NULL, NULL);
1184
1185         GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
1186         /* ignore SR that is not ours */
1187         if (ssrc != stream->ssrc)
1188           continue;
1189
1190         have_sr = TRUE;
1191         break;
1192       case GST_RTCP_TYPE_SDES:
1193       {
1194         gboolean more_items, more_entries;
1195
1196         /* only deal with first SDES, there is only supposed to be one SDES in
1197          * the RTCP packet but we deal with bad packets gracefully. Also bail
1198          * out if we have not seen an SR item yet. */
1199         if (have_sdes || !have_sr)
1200           break;
1201
1202         GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
1203           /* skip items that are not about the SSRC of the sender */
1204           if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
1205             continue;
1206
1207           /* find the CNAME entry */
1208           GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
1209             GstRTCPSDESType type;
1210             guint8 len;
1211             guint8 *data;
1212
1213             gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
1214
1215             if (type == GST_RTCP_SDES_CNAME) {
1216               GST_RTP_BIN_LOCK (bin);
1217               /* associate the stream to CNAME */
1218               gst_rtp_bin_associate (bin, stream, len, data,
1219                   ntptime, extrtptime, base_rtptime, base_time, clock_rate);
1220               GST_RTP_BIN_UNLOCK (bin);
1221             }
1222           }
1223         }
1224         have_sdes = TRUE;
1225         break;
1226       }
1227       default:
1228         /* we can ignore these packets */
1229         break;
1230     }
1231   }
1232 }
1233
1234 /* create a new stream with @ssrc in @session. Must be called with
1235  * RTP_SESSION_LOCK. */
1236 static GstRtpBinStream *
1237 create_stream (GstRtpBinSession * session, guint32 ssrc)
1238 {
1239   GstElement *buffer, *demux = NULL;
1240   GstRtpBinStream *stream;
1241   GstRtpBin *rtpbin;
1242   GstState target;
1243
1244   rtpbin = session->bin;
1245
1246   if (!(buffer = gst_element_factory_make ("gstrtpjitterbuffer", NULL)))
1247     goto no_jitterbuffer;
1248
1249   if (!rtpbin->ignore_pt)
1250     if (!(demux = gst_element_factory_make ("gstrtpptdemux", NULL)))
1251       goto no_demux;
1252
1253
1254   stream = g_new0 (GstRtpBinStream, 1);
1255   stream->ssrc = ssrc;
1256   stream->bin = rtpbin;
1257   stream->session = session;
1258   stream->buffer = buffer;
1259   stream->demux = demux;
1260
1261   stream->have_sync = FALSE;
1262   stream->rt_delta = 0;
1263   stream->percent = 100;
1264   session->streams = g_slist_prepend (session->streams, stream);
1265
1266   /* provide clock_rate to the jitterbuffer when needed */
1267   stream->buffer_ptreq_sig = g_signal_connect (buffer, "request-pt-map",
1268       (GCallback) pt_map_requested, session);
1269   stream->buffer_ntpstop_sig = g_signal_connect (buffer, "on-npt-stop",
1270       (GCallback) on_npt_stop, stream);
1271
1272   g_object_set_data (G_OBJECT (buffer), "GstRTPBin.session", session);
1273   g_object_set_data (G_OBJECT (buffer), "GstRTPBin.stream", stream);
1274
1275   /* configure latency and packet lost */
1276   g_object_set (buffer, "latency", rtpbin->latency_ms, NULL);
1277   g_object_set (buffer, "do-lost", rtpbin->do_lost, NULL);
1278   g_object_set (buffer, "mode", rtpbin->buffer_mode, NULL);
1279
1280   if (!rtpbin->ignore_pt)
1281     gst_bin_add (GST_BIN_CAST (rtpbin), demux);
1282   gst_bin_add (GST_BIN_CAST (rtpbin), buffer);
1283
1284   /* link stuff */
1285   if (demux)
1286     gst_element_link (buffer, demux);
1287
1288   if (rtpbin->buffering) {
1289     guint64 last_out;
1290
1291     GST_INFO_OBJECT (rtpbin,
1292         "bin is buffering, set jitterbuffer as not active");
1293     g_signal_emit_by_name (buffer, "set-active", FALSE, (gint64) 0, &last_out);
1294   }
1295
1296
1297   GST_OBJECT_LOCK (rtpbin);
1298   target = GST_STATE_TARGET (rtpbin);
1299   GST_OBJECT_UNLOCK (rtpbin);
1300
1301   /* from sink to source */
1302   if (demux)
1303     gst_element_set_state (demux, target);
1304
1305   gst_element_set_state (buffer, target);
1306
1307   return stream;
1308
1309   /* ERRORS */
1310 no_jitterbuffer:
1311   {
1312     g_warning ("gstrtpbin: could not create gstrtpjitterbuffer element");
1313     return NULL;
1314   }
1315 no_demux:
1316   {
1317     gst_object_unref (buffer);
1318     g_warning ("gstrtpbin: could not create gstrtpptdemux element");
1319     return NULL;
1320   }
1321 }
1322
1323 static void
1324 free_stream (GstRtpBinStream * stream)
1325 {
1326   GstRtpBinSession *session;
1327
1328   session = stream->session;
1329
1330   if (stream->demux) {
1331     g_signal_handler_disconnect (stream->demux, stream->demux_newpad_sig);
1332     g_signal_handler_disconnect (stream->demux, stream->demux_ptreq_sig);
1333     g_signal_handler_disconnect (stream->demux, stream->demux_ptchange_sig);
1334   }
1335   g_signal_handler_disconnect (stream->buffer, stream->buffer_handlesync_sig);
1336   g_signal_handler_disconnect (stream->buffer, stream->buffer_ptreq_sig);
1337   g_signal_handler_disconnect (stream->buffer, stream->buffer_ntpstop_sig);
1338
1339   if (stream->demux)
1340     gst_element_set_locked_state (stream->demux, TRUE);
1341   gst_element_set_locked_state (stream->buffer, TRUE);
1342
1343   if (stream->demux)
1344     gst_element_set_state (stream->demux, GST_STATE_NULL);
1345   gst_element_set_state (stream->buffer, GST_STATE_NULL);
1346
1347   /* now remove this signal, we need this while going to NULL because it to
1348    * do some cleanups */
1349   if (stream->demux)
1350     g_signal_handler_disconnect (stream->demux, stream->demux_padremoved_sig);
1351
1352   gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
1353   if (stream->demux)
1354     gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
1355
1356   g_free (stream);
1357 }
1358
1359 /* GObject vmethods */
1360 static void gst_rtp_bin_dispose (GObject * object);
1361 static void gst_rtp_bin_finalize (GObject * object);
1362 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
1363     const GValue * value, GParamSpec * pspec);
1364 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
1365     GValue * value, GParamSpec * pspec);
1366
1367 /* GstElement vmethods */
1368 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
1369     GstStateChange transition);
1370 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
1371     GstPadTemplate * templ, const gchar * name);
1372 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
1373 static void gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message);
1374
1375 GST_BOILERPLATE (GstRtpBin, gst_rtp_bin, GstBin, GST_TYPE_BIN);
1376
1377 static void
1378 gst_rtp_bin_base_init (gpointer klass)
1379 {
1380   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
1381
1382   /* sink pads */
1383   gst_element_class_add_pad_template (element_class,
1384       gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
1385   gst_element_class_add_pad_template (element_class,
1386       gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
1387   gst_element_class_add_pad_template (element_class,
1388       gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
1389
1390   /* src pads */
1391   gst_element_class_add_pad_template (element_class,
1392       gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
1393   gst_element_class_add_pad_template (element_class,
1394       gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
1395   gst_element_class_add_pad_template (element_class,
1396       gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
1397
1398   gst_element_class_set_details_simple (element_class, "RTP Bin",
1399       "Filter/Network/RTP",
1400       "Real-Time Transport Protocol bin",
1401       "Wim Taymans <wim.taymans@gmail.com>");
1402 }
1403
1404 static void
1405 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1406 {
1407   GObjectClass *gobject_class;
1408   GstElementClass *gstelement_class;
1409   GstBinClass *gstbin_class;
1410
1411   gobject_class = (GObjectClass *) klass;
1412   gstelement_class = (GstElementClass *) klass;
1413   gstbin_class = (GstBinClass *) klass;
1414
1415   g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1416
1417   gobject_class->dispose = gst_rtp_bin_dispose;
1418   gobject_class->finalize = gst_rtp_bin_finalize;
1419   gobject_class->set_property = gst_rtp_bin_set_property;
1420   gobject_class->get_property = gst_rtp_bin_get_property;
1421
1422   g_object_class_install_property (gobject_class, PROP_LATENCY,
1423       g_param_spec_uint ("latency", "Buffer latency in ms",
1424           "Default amount of ms to buffer in the jitterbuffers", 0,
1425           G_MAXUINT, DEFAULT_LATENCY_MS,
1426           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1427
1428   /**
1429    * GstRtpBin::request-pt-map:
1430    * @rtpbin: the object which received the signal
1431    * @session: the session
1432    * @pt: the pt
1433    *
1434    * Request the payload type as #GstCaps for @pt in @session.
1435    */
1436   gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1437       g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1438       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1439       NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1440       G_TYPE_UINT, G_TYPE_UINT);
1441
1442     /**
1443    * GstRtpBin::payload-type-change:
1444    * @rtpbin: the object which received the signal
1445    * @session: the session
1446    * @pt: the pt
1447    *
1448    * Signal that the current payload type changed to @pt in @session.
1449    *
1450    * Since: 0.10.17
1451    */
1452   gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
1453       g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
1454       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, payload_type_change),
1455       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1456       G_TYPE_UINT, G_TYPE_UINT);
1457
1458   /**
1459    * GstRtpBin::clear-pt-map:
1460    * @rtpbin: the object which received the signal
1461    *
1462    * Clear all previously cached pt-mapping obtained with
1463    * #GstRtpBin::request-pt-map.
1464    */
1465   gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1466       g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1467       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1468           clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1469       0, G_TYPE_NONE);
1470
1471   /**
1472    * GstRtpBin::reset-sync:
1473    * @rtpbin: the object which received the signal
1474    *
1475    * Reset all currently configured lip-sync parameters and require new SR
1476    * packets for all streams before lip-sync is attempted again.
1477    */
1478   gst_rtp_bin_signals[SIGNAL_RESET_SYNC] =
1479       g_signal_new ("reset-sync", G_TYPE_FROM_CLASS (klass),
1480       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1481           reset_sync), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1482       0, G_TYPE_NONE);
1483
1484   /**
1485    * GstRtpBin::get-internal-session:
1486    * @rtpbin: the object which received the signal
1487    * @id: the session id
1488    *
1489    * Request the internal RTPSession object as #GObject in session @id.
1490    */
1491   gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_SESSION] =
1492       g_signal_new ("get-internal-session", G_TYPE_FROM_CLASS (klass),
1493       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1494           get_internal_session), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
1495       RTP_TYPE_SESSION, 1, G_TYPE_UINT);
1496
1497   /**
1498    * GstRtpBin::on-new-ssrc:
1499    * @rtpbin: the object which received the signal
1500    * @session: the session
1501    * @ssrc: the SSRC
1502    *
1503    * Notify of a new SSRC that entered @session.
1504    */
1505   gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1506       g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1507       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1508       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1509       G_TYPE_UINT, G_TYPE_UINT);
1510   /**
1511    * GstRtpBin::on-ssrc-collision:
1512    * @rtpbin: the object which received the signal
1513    * @session: the session
1514    * @ssrc: the SSRC
1515    *
1516    * Notify when we have an SSRC collision
1517    */
1518   gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1519       g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1520       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1521       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1522       G_TYPE_UINT, G_TYPE_UINT);
1523   /**
1524    * GstRtpBin::on-ssrc-validated:
1525    * @rtpbin: the object which received the signal
1526    * @session: the session
1527    * @ssrc: the SSRC
1528    *
1529    * Notify of a new SSRC that became validated.
1530    */
1531   gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1532       g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1533       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1534       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1535       G_TYPE_UINT, G_TYPE_UINT);
1536   /**
1537    * GstRtpBin::on-ssrc-active:
1538    * @rtpbin: the object which received the signal
1539    * @session: the session
1540    * @ssrc: the SSRC
1541    *
1542    * Notify of a SSRC that is active, i.e., sending RTCP.
1543    */
1544   gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1545       g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1546       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
1547       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1548       G_TYPE_UINT, G_TYPE_UINT);
1549   /**
1550    * GstRtpBin::on-ssrc-sdes:
1551    * @rtpbin: the object which received the signal
1552    * @session: the session
1553    * @ssrc: the SSRC
1554    *
1555    * Notify of a SSRC that is active, i.e., sending RTCP.
1556    */
1557   gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES] =
1558       g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
1559       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
1560       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1561       G_TYPE_UINT, G_TYPE_UINT);
1562
1563   /**
1564    * GstRtpBin::on-bye-ssrc:
1565    * @rtpbin: the object which received the signal
1566    * @session: the session
1567    * @ssrc: the SSRC
1568    *
1569    * Notify of an SSRC that became inactive because of a BYE packet.
1570    */
1571   gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1572       g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1573       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1574       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1575       G_TYPE_UINT, G_TYPE_UINT);
1576   /**
1577    * GstRtpBin::on-bye-timeout:
1578    * @rtpbin: the object which received the signal
1579    * @session: the session
1580    * @ssrc: the SSRC
1581    *
1582    * Notify of an SSRC that has timed out because of BYE
1583    */
1584   gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1585       g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1586       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1587       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1588       G_TYPE_UINT, G_TYPE_UINT);
1589   /**
1590    * GstRtpBin::on-timeout:
1591    * @rtpbin: the object which received the signal
1592    * @session: the session
1593    * @ssrc: the SSRC
1594    *
1595    * Notify of an SSRC that has timed out
1596    */
1597   gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1598       g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1599       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1600       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1601       G_TYPE_UINT, G_TYPE_UINT);
1602   /**
1603    * GstRtpBin::on-sender-timeout:
1604    * @rtpbin: the object which received the signal
1605    * @session: the session
1606    * @ssrc: the SSRC
1607    *
1608    * Notify of a sender SSRC that has timed out and became a receiver
1609    */
1610   gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT] =
1611       g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
1612       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_sender_timeout),
1613       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1614       G_TYPE_UINT, G_TYPE_UINT);
1615
1616   /**
1617    * GstRtpBin::on-npt-stop:
1618    * @rtpbin: the object which received the signal
1619    * @session: the session
1620    * @ssrc: the SSRC
1621    *
1622    * Notify that SSRC sender has sent data up to the configured NPT stop time.
1623    */
1624   gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP] =
1625       g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
1626       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_npt_stop),
1627       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1628       G_TYPE_UINT, G_TYPE_UINT);
1629
1630   g_object_class_install_property (gobject_class, PROP_SDES,
1631       g_param_spec_boxed ("sdes", "SDES",
1632           "The SDES items of this session",
1633           GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1634
1635   g_object_class_install_property (gobject_class, PROP_DO_LOST,
1636       g_param_spec_boolean ("do-lost", "Do Lost",
1637           "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
1638           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1639
1640   g_object_class_install_property (gobject_class, PROP_AUTOREMOVE,
1641       g_param_spec_boolean ("autoremove", "Auto Remove",
1642           "Automatically remove timed out sources", DEFAULT_AUTOREMOVE,
1643           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1644
1645   g_object_class_install_property (gobject_class, PROP_IGNORE_PT,
1646       g_param_spec_boolean ("ignore-pt", "Ignore PT",
1647           "Do not demultiplex based on PT values", DEFAULT_IGNORE_PT,
1648           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1649
1650   g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
1651       g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
1652           "Use the pipeline clock to set the NTP time in the RTCP SR messages",
1653           DEFAULT_USE_PIPELINE_CLOCK,
1654           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1655   /**
1656    * GstRtpBin::buffer-mode:
1657    *
1658    * Control the buffering and timestamping mode used by the jitterbuffer.
1659    *
1660    * Since: 0.10.17
1661    */
1662   g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
1663       g_param_spec_enum ("buffer-mode", "Buffer Mode",
1664           "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
1665           DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1666   /**
1667    * GstRtpBin::ntp-sync:
1668    *
1669    * Synchronize received streams to the NTP clock. When the NTP clock is shared
1670    * between the receivers and the senders (such as when using ntpd) this option
1671    * can be used to synchronize receivers on multiple machines.
1672    *
1673    * Since: 0.10.21
1674    */
1675   g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
1676       g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
1677           "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
1678           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1679
1680   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1681   gstelement_class->request_new_pad =
1682       GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1683   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1684
1685   gstbin_class->handle_message = GST_DEBUG_FUNCPTR (gst_rtp_bin_handle_message);
1686
1687   klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1688   klass->reset_sync = GST_DEBUG_FUNCPTR (gst_rtp_bin_reset_sync);
1689   klass->get_internal_session =
1690       GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_session);
1691
1692   GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1693 }
1694
1695 static void
1696 gst_rtp_bin_init (GstRtpBin * rtpbin, GstRtpBinClass * klass)
1697 {
1698   gchar *str;
1699
1700   rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1701   rtpbin->priv->bin_lock = g_mutex_new ();
1702   rtpbin->priv->dyn_lock = g_mutex_new ();
1703
1704   rtpbin->latency_ms = DEFAULT_LATENCY_MS;
1705   rtpbin->latency_ns = DEFAULT_LATENCY_MS * GST_MSECOND;
1706   rtpbin->do_lost = DEFAULT_DO_LOST;
1707   rtpbin->ignore_pt = DEFAULT_IGNORE_PT;
1708   rtpbin->ntp_sync = DEFAULT_NTP_SYNC;
1709   rtpbin->priv->autoremove = DEFAULT_AUTOREMOVE;
1710   rtpbin->buffer_mode = DEFAULT_BUFFER_MODE;
1711   rtpbin->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
1712
1713   /* some default SDES entries */
1714   str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
1715   rtpbin->sdes = gst_structure_new ("application/x-rtp-source-sdes",
1716       "cname", G_TYPE_STRING, str,
1717       "name", G_TYPE_STRING, g_get_real_name (),
1718       "tool", G_TYPE_STRING, "GStreamer", NULL);
1719   g_free (str);
1720 }
1721
1722 static void
1723 gst_rtp_bin_dispose (GObject * object)
1724 {
1725   GstRtpBin *rtpbin;
1726
1727   rtpbin = GST_RTP_BIN (object);
1728
1729   GST_DEBUG_OBJECT (object, "freeing sessions");
1730   g_slist_foreach (rtpbin->sessions, (GFunc) free_session, rtpbin);
1731   g_slist_free (rtpbin->sessions);
1732   rtpbin->sessions = NULL;
1733   GST_DEBUG_OBJECT (object, "freeing clients");
1734   g_slist_foreach (rtpbin->clients, (GFunc) free_client, rtpbin);
1735   g_slist_free (rtpbin->clients);
1736   rtpbin->clients = NULL;
1737
1738   G_OBJECT_CLASS (parent_class)->dispose (object);
1739 }
1740
1741 static void
1742 gst_rtp_bin_finalize (GObject * object)
1743 {
1744   GstRtpBin *rtpbin;
1745
1746   rtpbin = GST_RTP_BIN (object);
1747
1748   if (rtpbin->sdes)
1749     gst_structure_free (rtpbin->sdes);
1750
1751   g_mutex_free (rtpbin->priv->bin_lock);
1752   g_mutex_free (rtpbin->priv->dyn_lock);
1753
1754   G_OBJECT_CLASS (parent_class)->finalize (object);
1755 }
1756
1757
1758 static void
1759 gst_rtp_bin_set_sdes_struct (GstRtpBin * bin, const GstStructure * sdes)
1760 {
1761   GSList *item;
1762
1763   if (sdes == NULL)
1764     return;
1765
1766   GST_RTP_BIN_LOCK (bin);
1767
1768   GST_OBJECT_LOCK (bin);
1769   if (bin->sdes)
1770     gst_structure_free (bin->sdes);
1771   bin->sdes = gst_structure_copy (sdes);
1772   GST_OBJECT_UNLOCK (bin);
1773
1774   /* store in all sessions */
1775   for (item = bin->sessions; item; item = g_slist_next (item)) {
1776     GstRtpBinSession *session = item->data;
1777     g_object_set (session->session, "sdes", sdes, NULL);
1778   }
1779
1780   GST_RTP_BIN_UNLOCK (bin);
1781 }
1782
1783 static GstStructure *
1784 gst_rtp_bin_get_sdes_struct (GstRtpBin * bin)
1785 {
1786   GstStructure *result;
1787
1788   GST_OBJECT_LOCK (bin);
1789   result = gst_structure_copy (bin->sdes);
1790   GST_OBJECT_UNLOCK (bin);
1791
1792   return result;
1793 }
1794
1795 static void
1796 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1797     const GValue * value, GParamSpec * pspec)
1798 {
1799   GstRtpBin *rtpbin;
1800
1801   rtpbin = GST_RTP_BIN (object);
1802
1803   switch (prop_id) {
1804     case PROP_LATENCY:
1805       GST_RTP_BIN_LOCK (rtpbin);
1806       rtpbin->latency_ms = g_value_get_uint (value);
1807       rtpbin->latency_ns = rtpbin->latency_ms * GST_MSECOND;
1808       GST_RTP_BIN_UNLOCK (rtpbin);
1809       /* propagate the property down to the jitterbuffer */
1810       gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "latency", value);
1811       break;
1812     case PROP_SDES:
1813       gst_rtp_bin_set_sdes_struct (rtpbin, g_value_get_boxed (value));
1814       break;
1815     case PROP_DO_LOST:
1816       GST_RTP_BIN_LOCK (rtpbin);
1817       rtpbin->do_lost = g_value_get_boolean (value);
1818       GST_RTP_BIN_UNLOCK (rtpbin);
1819       gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "do-lost", value);
1820       break;
1821     case PROP_NTP_SYNC:
1822       rtpbin->ntp_sync = g_value_get_boolean (value);
1823       break;
1824     case PROP_IGNORE_PT:
1825       rtpbin->ignore_pt = g_value_get_boolean (value);
1826       break;
1827     case PROP_AUTOREMOVE:
1828       rtpbin->priv->autoremove = g_value_get_boolean (value);
1829       break;
1830     case PROP_USE_PIPELINE_CLOCK:
1831     {
1832       GSList *sessions;
1833       GST_RTP_BIN_LOCK (rtpbin);
1834       rtpbin->use_pipeline_clock = g_value_get_boolean (value);
1835       for (sessions = rtpbin->sessions; sessions;
1836           sessions = g_slist_next (sessions)) {
1837         GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
1838
1839         g_object_set (G_OBJECT (session->session),
1840             "use-pipeline-clock", rtpbin->use_pipeline_clock, NULL);
1841       }
1842       GST_RTP_BIN_UNLOCK (rtpbin);
1843     }
1844       break;
1845     case PROP_BUFFER_MODE:
1846       GST_RTP_BIN_LOCK (rtpbin);
1847       rtpbin->buffer_mode = g_value_get_enum (value);
1848       GST_RTP_BIN_UNLOCK (rtpbin);
1849       /* propagate the property down to the jitterbuffer */
1850       gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "mode", value);
1851       break;
1852     default:
1853       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1854       break;
1855   }
1856 }
1857
1858 static void
1859 gst_rtp_bin_get_property (GObject * object, guint prop_id,
1860     GValue * value, GParamSpec * pspec)
1861 {
1862   GstRtpBin *rtpbin;
1863
1864   rtpbin = GST_RTP_BIN (object);
1865
1866   switch (prop_id) {
1867     case PROP_LATENCY:
1868       GST_RTP_BIN_LOCK (rtpbin);
1869       g_value_set_uint (value, rtpbin->latency_ms);
1870       GST_RTP_BIN_UNLOCK (rtpbin);
1871       break;
1872     case PROP_SDES:
1873       g_value_take_boxed (value, gst_rtp_bin_get_sdes_struct (rtpbin));
1874       break;
1875     case PROP_DO_LOST:
1876       GST_RTP_BIN_LOCK (rtpbin);
1877       g_value_set_boolean (value, rtpbin->do_lost);
1878       GST_RTP_BIN_UNLOCK (rtpbin);
1879       break;
1880     case PROP_IGNORE_PT:
1881       g_value_set_boolean (value, rtpbin->ignore_pt);
1882       break;
1883     case PROP_NTP_SYNC:
1884       g_value_set_boolean (value, rtpbin->ntp_sync);
1885       break;
1886     case PROP_AUTOREMOVE:
1887       g_value_set_boolean (value, rtpbin->priv->autoremove);
1888       break;
1889     case PROP_BUFFER_MODE:
1890       g_value_set_enum (value, rtpbin->buffer_mode);
1891       break;
1892     case PROP_USE_PIPELINE_CLOCK:
1893       g_value_set_boolean (value, rtpbin->use_pipeline_clock);
1894       break;
1895     default:
1896       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1897       break;
1898   }
1899 }
1900
1901 static void
1902 gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
1903 {
1904   GstRtpBin *rtpbin;
1905
1906   rtpbin = GST_RTP_BIN (bin);
1907
1908   switch (GST_MESSAGE_TYPE (message)) {
1909     case GST_MESSAGE_ELEMENT:
1910     {
1911       const GstStructure *s = gst_message_get_structure (message);
1912
1913       /* we change the structure name and add the session ID to it */
1914       if (gst_structure_has_name (s, "application/x-rtp-source-sdes")) {
1915         GstRtpBinSession *sess;
1916
1917         /* find the session we set it as object data */
1918         sess = g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
1919             "GstRTPBin.session");
1920
1921         if (G_LIKELY (sess)) {
1922           message = gst_message_make_writable (message);
1923           s = gst_message_get_structure (message);
1924           gst_structure_set ((GstStructure *) s, "session", G_TYPE_UINT,
1925               sess->id, NULL);
1926         }
1927       }
1928       GST_BIN_CLASS (parent_class)->handle_message (bin, message);
1929       break;
1930     }
1931     case GST_MESSAGE_BUFFERING:
1932     {
1933       gint percent;
1934       gint min_percent = 100;
1935       GSList *sessions, *streams;
1936       GstRtpBinStream *stream;
1937       gboolean change = FALSE, active = FALSE;
1938       GstClockTime min_out_time;
1939       GstBufferingMode mode;
1940       gint avg_in, avg_out;
1941       gint64 buffering_left;
1942
1943       gst_message_parse_buffering (message, &percent);
1944       gst_message_parse_buffering_stats (message, &mode, &avg_in, &avg_out,
1945           &buffering_left);
1946
1947       stream =
1948           g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
1949           "GstRTPBin.stream");
1950
1951       GST_DEBUG_OBJECT (bin, "got percent %d from stream %p", percent, stream);
1952
1953       /* get the stream */
1954       if (G_LIKELY (stream)) {
1955         GST_RTP_BIN_LOCK (rtpbin);
1956         /* fill in the percent */
1957         stream->percent = percent;
1958
1959         /* calculate the min value for all streams */
1960         for (sessions = rtpbin->sessions; sessions;
1961             sessions = g_slist_next (sessions)) {
1962           GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
1963
1964           GST_RTP_SESSION_LOCK (session);
1965           if (session->streams) {
1966             for (streams = session->streams; streams;
1967                 streams = g_slist_next (streams)) {
1968               GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
1969
1970               GST_DEBUG_OBJECT (bin, "stream %p percent %d", stream,
1971                   stream->percent);
1972
1973               /* find min percent */
1974               if (min_percent > stream->percent)
1975                 min_percent = stream->percent;
1976             }
1977           } else {
1978             GST_INFO_OBJECT (bin,
1979                 "session has no streams, setting min_percent to 0");
1980             min_percent = 0;
1981           }
1982           GST_RTP_SESSION_UNLOCK (session);
1983         }
1984         GST_DEBUG_OBJECT (bin, "min percent %d", min_percent);
1985
1986         if (rtpbin->buffering) {
1987           if (min_percent == 100) {
1988             rtpbin->buffering = FALSE;
1989             active = TRUE;
1990             change = TRUE;
1991           }
1992         } else {
1993           if (min_percent < 100) {
1994             /* pause the streams */
1995             rtpbin->buffering = TRUE;
1996             active = FALSE;
1997             change = TRUE;
1998           }
1999         }
2000         GST_RTP_BIN_UNLOCK (rtpbin);
2001
2002         gst_message_unref (message);
2003
2004         /* make a new buffering message with the min value */
2005         message =
2006             gst_message_new_buffering (GST_OBJECT_CAST (bin), min_percent);
2007         gst_message_set_buffering_stats (message, mode, avg_in, avg_out,
2008             buffering_left);
2009
2010         if (G_UNLIKELY (change)) {
2011           GstClock *clock;
2012           guint64 running_time = 0;
2013           guint64 offset = 0;
2014
2015           /* figure out the running time when we have a clock */
2016           if (G_LIKELY ((clock =
2017                       gst_element_get_clock (GST_ELEMENT_CAST (bin))))) {
2018             guint64 now, base_time;
2019
2020             now = gst_clock_get_time (clock);
2021             base_time = gst_element_get_base_time (GST_ELEMENT_CAST (bin));
2022             running_time = now - base_time;
2023           }
2024           GST_DEBUG_OBJECT (bin,
2025               "running time now %" GST_TIME_FORMAT,
2026               GST_TIME_ARGS (running_time));
2027
2028           GST_RTP_BIN_LOCK (rtpbin);
2029
2030           /* when we reactivate, calculate the offsets so that all streams have
2031            * an output time that is at least as big as the running_time */
2032           offset = 0;
2033           if (active) {
2034             if (running_time > rtpbin->buffer_start) {
2035               offset = running_time - rtpbin->buffer_start;
2036               if (offset >= rtpbin->latency_ns)
2037                 offset -= rtpbin->latency_ns;
2038               else
2039                 offset = 0;
2040             }
2041           }
2042
2043           /* pause all streams */
2044           min_out_time = -1;
2045           for (sessions = rtpbin->sessions; sessions;
2046               sessions = g_slist_next (sessions)) {
2047             GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2048
2049             GST_RTP_SESSION_LOCK (session);
2050             for (streams = session->streams; streams;
2051                 streams = g_slist_next (streams)) {
2052               GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
2053               GstElement *element = stream->buffer;
2054               guint64 last_out;
2055
2056               g_signal_emit_by_name (element, "set-active", active, offset,
2057                   &last_out);
2058
2059               if (!active) {
2060                 g_object_get (element, "percent", &stream->percent, NULL);
2061
2062                 if (last_out == -1)
2063                   last_out = 0;
2064                 if (min_out_time == -1 || last_out < min_out_time)
2065                   min_out_time = last_out;
2066               }
2067
2068               GST_DEBUG_OBJECT (bin,
2069                   "setting %p to %d, offset %" GST_TIME_FORMAT ", last %"
2070                   GST_TIME_FORMAT ", percent %d", element, active,
2071                   GST_TIME_ARGS (offset), GST_TIME_ARGS (last_out),
2072                   stream->percent);
2073             }
2074             GST_RTP_SESSION_UNLOCK (session);
2075           }
2076           GST_DEBUG_OBJECT (bin,
2077               "min out time %" GST_TIME_FORMAT, GST_TIME_ARGS (min_out_time));
2078
2079           /* the buffer_start is the min out time of all paused jitterbuffers */
2080           if (!active)
2081             rtpbin->buffer_start = min_out_time;
2082
2083           GST_RTP_BIN_UNLOCK (rtpbin);
2084         }
2085       }
2086       GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2087       break;
2088     }
2089     default:
2090     {
2091       GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2092       break;
2093     }
2094   }
2095 }
2096
2097 static GstStateChangeReturn
2098 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
2099 {
2100   GstStateChangeReturn res;
2101   GstRtpBin *rtpbin;
2102   GstRtpBinPrivate *priv;
2103
2104   rtpbin = GST_RTP_BIN (element);
2105   priv = rtpbin->priv;
2106
2107   switch (transition) {
2108     case GST_STATE_CHANGE_NULL_TO_READY:
2109       break;
2110     case GST_STATE_CHANGE_READY_TO_PAUSED:
2111       GST_LOG_OBJECT (rtpbin, "clearing shutdown flag");
2112       g_atomic_int_set (&priv->shutdown, 0);
2113       break;
2114     case GST_STATE_CHANGE_PAUSED_TO_READY:
2115       GST_LOG_OBJECT (rtpbin, "setting shutdown flag");
2116       g_atomic_int_set (&priv->shutdown, 1);
2117       /* wait for all callbacks to end by taking the lock. No new callbacks will
2118        * be able to happen as we set the shutdown flag. */
2119       GST_RTP_BIN_DYN_LOCK (rtpbin);
2120       GST_LOG_OBJECT (rtpbin, "dynamic lock taken, we can continue shutdown");
2121       GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2122       break;
2123     default:
2124       break;
2125   }
2126
2127   res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2128
2129   switch (transition) {
2130     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2131       break;
2132     case GST_STATE_CHANGE_PAUSED_TO_READY:
2133       break;
2134     case GST_STATE_CHANGE_READY_TO_NULL:
2135       break;
2136     default:
2137       break;
2138   }
2139   return res;
2140 }
2141
2142 /* a new pad (SSRC) was created in @session. This signal is emited from the
2143  * payload demuxer. */
2144 static void
2145 new_payload_found (GstElement * element, guint pt, GstPad * pad,
2146     GstRtpBinStream * stream)
2147 {
2148   GstRtpBin *rtpbin;
2149   GstElementClass *klass;
2150   GstPadTemplate *templ;
2151   gchar *padname;
2152   GstPad *gpad;
2153
2154   rtpbin = stream->bin;
2155
2156   GST_DEBUG ("new payload pad %d", pt);
2157
2158   GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2159
2160   /* ghost the pad to the parent */
2161   klass = GST_ELEMENT_GET_CLASS (rtpbin);
2162   templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
2163   padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
2164       stream->session->id, stream->ssrc, pt);
2165   gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2166   g_free (padname);
2167   g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", gpad);
2168
2169   gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
2170   gst_pad_set_active (gpad, TRUE);
2171   GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2172
2173   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2174
2175   return;
2176
2177 shutdown:
2178   {
2179     GST_DEBUG ("ignoring, we are shutting down");
2180     return;
2181   }
2182 }
2183
2184 static void
2185 payload_pad_removed (GstElement * element, GstPad * pad,
2186     GstRtpBinStream * stream)
2187 {
2188   GstRtpBin *rtpbin;
2189   GstPad *gpad;
2190
2191   rtpbin = stream->bin;
2192
2193   GST_DEBUG ("payload pad removed");
2194
2195   GST_RTP_BIN_DYN_LOCK (rtpbin);
2196   if ((gpad = g_object_get_data (G_OBJECT (pad), "GstRTPBin.ghostpad"))) {
2197     g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", NULL);
2198
2199     gst_pad_set_active (gpad, FALSE);
2200     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2201   }
2202   GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2203 }
2204
2205 static GstCaps *
2206 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
2207 {
2208   GstRtpBin *rtpbin;
2209   GstCaps *caps;
2210
2211   rtpbin = session->bin;
2212
2213   GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
2214       session->id);
2215
2216   caps = get_pt_map (session, pt);
2217   if (!caps)
2218     goto no_caps;
2219
2220   return caps;
2221
2222   /* ERRORS */
2223 no_caps:
2224   {
2225     GST_DEBUG_OBJECT (rtpbin, "could not get caps");
2226     return NULL;
2227   }
2228 }
2229
2230 static void
2231 payload_type_change (GstElement * element, guint pt, GstRtpBinSession * session)
2232 {
2233   GST_DEBUG_OBJECT (session->bin,
2234       "emiting signal for pt type changed to %d in session %d", pt,
2235       session->id);
2236
2237   g_signal_emit (session->bin, gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE],
2238       0, session->id, pt);
2239 }
2240
2241 /* emited when caps changed for the session */
2242 static void
2243 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
2244 {
2245   GstRtpBin *bin;
2246   GstCaps *caps;
2247   gint payload;
2248   const GstStructure *s;
2249
2250   bin = session->bin;
2251
2252   g_object_get (pad, "caps", &caps, NULL);
2253
2254   if (caps == NULL)
2255     return;
2256
2257   GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
2258
2259   s = gst_caps_get_structure (caps, 0);
2260
2261   /* get payload, finish when it's not there */
2262   if (!gst_structure_get_int (s, "payload", &payload))
2263     return;
2264
2265   GST_RTP_SESSION_LOCK (session);
2266   GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
2267   g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
2268   GST_RTP_SESSION_UNLOCK (session);
2269 }
2270
2271 /* a new pad (SSRC) was created in @session */
2272 static void
2273 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
2274     GstRtpBinSession * session)
2275 {
2276   GstRtpBin *rtpbin;
2277   GstRtpBinStream *stream;
2278   GstPad *sinkpad, *srcpad;
2279   gchar *padname;
2280
2281   rtpbin = session->bin;
2282
2283   GST_DEBUG_OBJECT (rtpbin, "new SSRC pad %08x, %s:%s", ssrc,
2284       GST_DEBUG_PAD_NAME (pad));
2285
2286   GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2287
2288   GST_RTP_SESSION_LOCK (session);
2289
2290   /* create new stream */
2291   stream = create_stream (session, ssrc);
2292   if (!stream)
2293     goto no_stream;
2294
2295   /* get pad and link */
2296   GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTP");
2297   padname = g_strdup_printf ("src_%d", ssrc);
2298   srcpad = gst_element_get_static_pad (element, padname);
2299   g_free (padname);
2300   sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
2301   gst_pad_link (srcpad, sinkpad);
2302   gst_object_unref (sinkpad);
2303   gst_object_unref (srcpad);
2304
2305   GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTCP");
2306   padname = g_strdup_printf ("rtcp_src_%d", ssrc);
2307   srcpad = gst_element_get_static_pad (element, padname);
2308   g_free (padname);
2309   sinkpad = gst_element_get_request_pad (stream->buffer, "sink_rtcp");
2310   gst_pad_link (srcpad, sinkpad);
2311   gst_object_unref (sinkpad);
2312   gst_object_unref (srcpad);
2313
2314   /* connect to the RTCP sync signal from the jitterbuffer */
2315   GST_DEBUG_OBJECT (rtpbin, "connecting sync signal");
2316   stream->buffer_handlesync_sig = g_signal_connect (stream->buffer,
2317       "handle-sync", (GCallback) gst_rtp_bin_handle_sync, stream);
2318
2319   if (stream->demux) {
2320     /* connect to the new-pad signal of the payload demuxer, this will expose the
2321      * new pad by ghosting it. */
2322     stream->demux_newpad_sig = g_signal_connect (stream->demux,
2323         "new-payload-type", (GCallback) new_payload_found, stream);
2324     stream->demux_padremoved_sig = g_signal_connect (stream->demux,
2325         "pad-removed", (GCallback) payload_pad_removed, stream);
2326
2327     /* connect to the request-pt-map signal. This signal will be emited by the
2328      * demuxer so that it can apply a proper caps on the buffers for the
2329      * depayloaders. */
2330     stream->demux_ptreq_sig = g_signal_connect (stream->demux,
2331         "request-pt-map", (GCallback) pt_map_requested, session);
2332     /* connect to the  signal so it can be forwarded. */
2333     stream->demux_ptchange_sig = g_signal_connect (stream->demux,
2334         "payload-type-change", (GCallback) payload_type_change, session);
2335   } else {
2336     /* add gstrtpjitterbuffer src pad to pads */
2337     GstElementClass *klass;
2338     GstPadTemplate *templ;
2339     gchar *padname;
2340     GstPad *gpad, *pad;
2341
2342     pad = gst_element_get_static_pad (stream->buffer, "src");
2343
2344     /* ghost the pad to the parent */
2345     klass = GST_ELEMENT_GET_CLASS (rtpbin);
2346     templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
2347     padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
2348         stream->session->id, stream->ssrc, 255);
2349     gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2350     g_free (padname);
2351
2352     gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
2353     gst_pad_set_active (gpad, TRUE);
2354     gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2355
2356     gst_object_unref (pad);
2357   }
2358
2359   GST_RTP_SESSION_UNLOCK (session);
2360   GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2361
2362   return;
2363
2364   /* ERRORS */
2365 shutdown:
2366   {
2367     GST_DEBUG_OBJECT (rtpbin, "we are shutting down");
2368     return;
2369   }
2370 no_stream:
2371   {
2372     GST_RTP_SESSION_UNLOCK (session);
2373     GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2374     GST_DEBUG_OBJECT (rtpbin, "could not create stream");
2375     return;
2376   }
2377 }
2378
2379 /* Create a pad for receiving RTP for the session in @name. Must be called with
2380  * RTP_BIN_LOCK.
2381  */
2382 static GstPad *
2383 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2384 {
2385   GstPad *sinkdpad;
2386   guint sessid;
2387   GstRtpBinSession *session;
2388   GstPadLinkReturn lres;
2389
2390   /* first get the session number */
2391   if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
2392     goto no_name;
2393
2394   GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2395
2396   /* get or create session */
2397   session = find_session_by_id (rtpbin, sessid);
2398   if (!session) {
2399     GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2400     /* create session now */
2401     session = create_session (rtpbin, sessid);
2402     if (session == NULL)
2403       goto create_error;
2404   }
2405
2406   /* check if pad was requested */
2407   if (session->recv_rtp_sink_ghost != NULL)
2408     return session->recv_rtp_sink_ghost;
2409
2410   GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
2411   /* get recv_rtp pad and store */
2412   session->recv_rtp_sink =
2413       gst_element_get_request_pad (session->session, "recv_rtp_sink");
2414   if (session->recv_rtp_sink == NULL)
2415     goto pad_failed;
2416
2417   g_signal_connect (session->recv_rtp_sink, "notify::caps",
2418       (GCallback) caps_changed, session);
2419
2420   GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
2421   /* get srcpad, link to SSRCDemux */
2422   session->recv_rtp_src =
2423       gst_element_get_static_pad (session->session, "recv_rtp_src");
2424   if (session->recv_rtp_src == NULL)
2425     goto pad_failed;
2426
2427   GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
2428   sinkdpad = gst_element_get_static_pad (session->demux, "sink");
2429   GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
2430   lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
2431   gst_object_unref (sinkdpad);
2432   if (lres != GST_PAD_LINK_OK)
2433     goto link_failed;
2434
2435   /* connect to the new-ssrc-pad signal of the SSRC demuxer */
2436   session->demux_newpad_sig = g_signal_connect (session->demux,
2437       "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
2438   session->demux_padremoved_sig = g_signal_connect (session->demux,
2439       "removed-ssrc-pad", (GCallback) ssrc_demux_pad_removed, session);
2440
2441   GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
2442   session->recv_rtp_sink_ghost =
2443       gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
2444   gst_pad_set_active (session->recv_rtp_sink_ghost, TRUE);
2445   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->recv_rtp_sink_ghost);
2446
2447   return session->recv_rtp_sink_ghost;
2448
2449   /* ERRORS */
2450 no_name:
2451   {
2452     g_warning ("gstrtpbin: invalid name given");
2453     return NULL;
2454   }
2455 create_error:
2456   {
2457     /* create_session already warned */
2458     return NULL;
2459   }
2460 pad_failed:
2461   {
2462     g_warning ("gstrtpbin: failed to get session pad");
2463     return NULL;
2464   }
2465 link_failed:
2466   {
2467     g_warning ("gstrtpbin: failed to link pads");
2468     return NULL;
2469   }
2470 }
2471
2472 static void
2473 remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2474 {
2475   if (session->demux_newpad_sig) {
2476     g_signal_handler_disconnect (session->demux, session->demux_newpad_sig);
2477     session->demux_newpad_sig = 0;
2478   }
2479   if (session->demux_padremoved_sig) {
2480     g_signal_handler_disconnect (session->demux, session->demux_padremoved_sig);
2481     session->demux_padremoved_sig = 0;
2482   }
2483   if (session->recv_rtp_src) {
2484     gst_object_unref (session->recv_rtp_src);
2485     session->recv_rtp_src = NULL;
2486   }
2487   if (session->recv_rtp_sink) {
2488     gst_element_release_request_pad (session->session, session->recv_rtp_sink);
2489     gst_object_unref (session->recv_rtp_sink);
2490     session->recv_rtp_sink = NULL;
2491   }
2492   if (session->recv_rtp_sink_ghost) {
2493     gst_pad_set_active (session->recv_rtp_sink_ghost, FALSE);
2494     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2495         session->recv_rtp_sink_ghost);
2496     session->recv_rtp_sink_ghost = NULL;
2497   }
2498 }
2499
2500 /* Create a pad for receiving RTCP for the session in @name. Must be called with
2501  * RTP_BIN_LOCK.
2502  */
2503 static GstPad *
2504 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
2505     const gchar * name)
2506 {
2507   guint sessid;
2508   GstRtpBinSession *session;
2509   GstPad *sinkdpad;
2510   GstPadLinkReturn lres;
2511
2512   /* first get the session number */
2513   if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
2514     goto no_name;
2515
2516   GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2517
2518   /* get or create the session */
2519   session = find_session_by_id (rtpbin, sessid);
2520   if (!session) {
2521     GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2522     /* create session now */
2523     session = create_session (rtpbin, sessid);
2524     if (session == NULL)
2525       goto create_error;
2526   }
2527
2528   /* check if pad was requested */
2529   if (session->recv_rtcp_sink_ghost != NULL)
2530     return session->recv_rtcp_sink_ghost;
2531
2532   /* get recv_rtp pad and store */
2533   GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
2534   session->recv_rtcp_sink =
2535       gst_element_get_request_pad (session->session, "recv_rtcp_sink");
2536   if (session->recv_rtcp_sink == NULL)
2537     goto pad_failed;
2538
2539   /* get srcpad, link to SSRCDemux */
2540   GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
2541   session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
2542   if (session->sync_src == NULL)
2543     goto pad_failed;
2544
2545   GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
2546   sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
2547   lres = gst_pad_link (session->sync_src, sinkdpad);
2548   gst_object_unref (sinkdpad);
2549   if (lres != GST_PAD_LINK_OK)
2550     goto link_failed;
2551
2552   session->recv_rtcp_sink_ghost =
2553       gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
2554   gst_pad_set_active (session->recv_rtcp_sink_ghost, TRUE);
2555   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin),
2556       session->recv_rtcp_sink_ghost);
2557
2558   return session->recv_rtcp_sink_ghost;
2559
2560   /* ERRORS */
2561 no_name:
2562   {
2563     g_warning ("gstrtpbin: invalid name given");
2564     return NULL;
2565   }
2566 create_error:
2567   {
2568     /* create_session already warned */
2569     return NULL;
2570   }
2571 pad_failed:
2572   {
2573     g_warning ("gstrtpbin: failed to get session pad");
2574     return NULL;
2575   }
2576 link_failed:
2577   {
2578     g_warning ("gstrtpbin: failed to link pads");
2579     return NULL;
2580   }
2581 }
2582
2583 static void
2584 remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2585 {
2586   if (session->recv_rtcp_sink_ghost) {
2587     gst_pad_set_active (session->recv_rtcp_sink_ghost, FALSE);
2588     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2589         session->recv_rtcp_sink_ghost);
2590     session->recv_rtcp_sink_ghost = NULL;
2591   }
2592   if (session->sync_src) {
2593     /* releasing the request pad should also unref the sync pad */
2594     gst_object_unref (session->sync_src);
2595     session->sync_src = NULL;
2596   }
2597   if (session->recv_rtcp_sink) {
2598     gst_element_release_request_pad (session->session, session->recv_rtcp_sink);
2599     gst_object_unref (session->recv_rtcp_sink);
2600     session->recv_rtcp_sink = NULL;
2601   }
2602 }
2603
2604 /* Create a pad for sending RTP for the session in @name. Must be called with
2605  * RTP_BIN_LOCK.
2606  */
2607 static GstPad *
2608 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2609 {
2610   gchar *gname;
2611   guint sessid;
2612   GstRtpBinSession *session;
2613   GstElementClass *klass;
2614
2615   /* first get the session number */
2616   if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
2617     goto no_name;
2618
2619   /* get or create session */
2620   session = find_session_by_id (rtpbin, sessid);
2621   if (!session) {
2622     /* create session now */
2623     session = create_session (rtpbin, sessid);
2624     if (session == NULL)
2625       goto create_error;
2626   }
2627
2628   /* check if pad was requested */
2629   if (session->send_rtp_sink_ghost != NULL)
2630     return session->send_rtp_sink_ghost;
2631
2632   /* get send_rtp pad and store */
2633   session->send_rtp_sink =
2634       gst_element_get_request_pad (session->session, "send_rtp_sink");
2635   if (session->send_rtp_sink == NULL)
2636     goto pad_failed;
2637
2638   session->send_rtp_sink_ghost =
2639       gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
2640   gst_pad_set_active (session->send_rtp_sink_ghost, TRUE);
2641   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_sink_ghost);
2642
2643   /* get srcpad */
2644   session->send_rtp_src =
2645       gst_element_get_static_pad (session->session, "send_rtp_src");
2646   if (session->send_rtp_src == NULL)
2647     goto no_srcpad;
2648
2649   /* ghost the new source pad */
2650   klass = GST_ELEMENT_GET_CLASS (rtpbin);
2651   gname = g_strdup_printf ("send_rtp_src_%d", sessid);
2652   templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
2653   session->send_rtp_src_ghost =
2654       gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
2655   gst_pad_set_active (session->send_rtp_src_ghost, TRUE);
2656   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_src_ghost);
2657   g_free (gname);
2658
2659   return session->send_rtp_sink_ghost;
2660
2661   /* ERRORS */
2662 no_name:
2663   {
2664     g_warning ("gstrtpbin: invalid name given");
2665     return NULL;
2666   }
2667 create_error:
2668   {
2669     /* create_session already warned */
2670     return NULL;
2671   }
2672 pad_failed:
2673   {
2674     g_warning ("gstrtpbin: failed to get session pad for session %d", sessid);
2675     return NULL;
2676   }
2677 no_srcpad:
2678   {
2679     g_warning ("gstrtpbin: failed to get rtp source pad for session %d",
2680         sessid);
2681     return NULL;
2682   }
2683 }
2684
2685 static void
2686 remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2687 {
2688   if (session->send_rtp_src_ghost) {
2689     gst_pad_set_active (session->send_rtp_src_ghost, FALSE);
2690     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2691         session->send_rtp_src_ghost);
2692     session->send_rtp_src_ghost = NULL;
2693   }
2694   if (session->send_rtp_src) {
2695     gst_object_unref (session->send_rtp_src);
2696     session->send_rtp_src = NULL;
2697   }
2698   if (session->send_rtp_sink) {
2699     gst_element_release_request_pad (GST_ELEMENT_CAST (session->session),
2700         session->send_rtp_sink);
2701     gst_object_unref (session->send_rtp_sink);
2702     session->send_rtp_sink = NULL;
2703   }
2704   if (session->send_rtp_sink_ghost) {
2705     gst_pad_set_active (session->send_rtp_sink_ghost, FALSE);
2706     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2707         session->send_rtp_sink_ghost);
2708     session->send_rtp_sink_ghost = NULL;
2709   }
2710 }
2711
2712 /* Create a pad for sending RTCP for the session in @name. Must be called with
2713  * RTP_BIN_LOCK.
2714  */
2715 static GstPad *
2716 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2717 {
2718   guint sessid;
2719   GstRtpBinSession *session;
2720
2721   /* first get the session number */
2722   if (name == NULL || sscanf (name, "send_rtcp_src_%d", &sessid) != 1)
2723     goto no_name;
2724
2725   /* get or create session */
2726   session = find_session_by_id (rtpbin, sessid);
2727   if (!session)
2728     goto no_session;
2729
2730   /* check if pad was requested */
2731   if (session->send_rtcp_src_ghost != NULL)
2732     return session->send_rtcp_src_ghost;
2733
2734   /* get rtcp_src pad and store */
2735   session->send_rtcp_src =
2736       gst_element_get_request_pad (session->session, "send_rtcp_src");
2737   if (session->send_rtcp_src == NULL)
2738     goto pad_failed;
2739
2740   session->send_rtcp_src_ghost =
2741       gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
2742   gst_pad_set_active (session->send_rtcp_src_ghost, TRUE);
2743   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtcp_src_ghost);
2744
2745   return session->send_rtcp_src_ghost;
2746
2747   /* ERRORS */
2748 no_name:
2749   {
2750     g_warning ("gstrtpbin: invalid name given");
2751     return NULL;
2752   }
2753 no_session:
2754   {
2755     g_warning ("gstrtpbin: session with id %d does not exist", sessid);
2756     return NULL;
2757   }
2758 pad_failed:
2759   {
2760     g_warning ("gstrtpbin: failed to get rtcp pad for session %d", sessid);
2761     return NULL;
2762   }
2763 }
2764
2765 static void
2766 remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2767 {
2768   if (session->send_rtcp_src_ghost) {
2769     gst_pad_set_active (session->send_rtcp_src_ghost, FALSE);
2770     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2771         session->send_rtcp_src_ghost);
2772     session->send_rtcp_src_ghost = NULL;
2773   }
2774   if (session->send_rtcp_src) {
2775     gst_element_release_request_pad (session->session, session->send_rtcp_src);
2776     gst_object_unref (session->send_rtcp_src);
2777     session->send_rtcp_src = NULL;
2778   }
2779 }
2780
2781 /* If the requested name is NULL we should create a name with
2782  * the session number assuming we want the lowest posible session
2783  * with a free pad like the template */
2784 static gchar *
2785 gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
2786 {
2787   gboolean name_found = FALSE;
2788   gint session = 0;
2789   GstIterator *pad_it = NULL;
2790   gchar *pad_name = NULL;
2791
2792   GST_DEBUG_OBJECT (element, "find a free pad name for template");
2793   while (!name_found) {
2794     gboolean done = FALSE;
2795     g_free (pad_name);
2796     pad_name = g_strdup_printf (templ->name_template, session++);
2797     pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
2798     name_found = TRUE;
2799     while (!done) {
2800       gpointer data;
2801
2802       switch (gst_iterator_next (pad_it, &data)) {
2803         case GST_ITERATOR_OK:
2804         {
2805           GstPad *pad;
2806           gchar *name;
2807
2808           pad = GST_PAD_CAST (data);
2809           name = gst_pad_get_name (pad);
2810
2811           if (strcmp (name, pad_name) == 0) {
2812             done = TRUE;
2813             name_found = FALSE;
2814           }
2815           g_free (name);
2816           gst_object_unref (pad);
2817           break;
2818         }
2819         case GST_ITERATOR_ERROR:
2820         case GST_ITERATOR_RESYNC:
2821           /* restart iteration */
2822           done = TRUE;
2823           name_found = FALSE;
2824           session = 0;
2825           break;
2826         case GST_ITERATOR_DONE:
2827           done = TRUE;
2828           break;
2829       }
2830     }
2831     gst_iterator_free (pad_it);
2832   }
2833
2834   GST_DEBUG_OBJECT (element, "free pad name found: '%s'", pad_name);
2835   return pad_name;
2836 }
2837
2838 /*
2839  */
2840 static GstPad *
2841 gst_rtp_bin_request_new_pad (GstElement * element,
2842     GstPadTemplate * templ, const gchar * name)
2843 {
2844   GstRtpBin *rtpbin;
2845   GstElementClass *klass;
2846   GstPad *result;
2847
2848   gchar *pad_name = NULL;
2849
2850   g_return_val_if_fail (templ != NULL, NULL);
2851   g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
2852
2853   rtpbin = GST_RTP_BIN (element);
2854   klass = GST_ELEMENT_GET_CLASS (element);
2855
2856   GST_RTP_BIN_LOCK (rtpbin);
2857
2858   if (name == NULL) {
2859     /* use a free pad name */
2860     pad_name = gst_rtp_bin_get_free_pad_name (element, templ);
2861   } else {
2862     /* use the provided name */
2863     pad_name = g_strdup (name);
2864   }
2865
2866   GST_DEBUG_OBJECT (rtpbin, "Trying to request a pad with name %s", pad_name);
2867
2868   /* figure out the template */
2869   if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
2870     result = create_recv_rtp (rtpbin, templ, pad_name);
2871   } else if (templ == gst_element_class_get_pad_template (klass,
2872           "recv_rtcp_sink_%d")) {
2873     result = create_recv_rtcp (rtpbin, templ, pad_name);
2874   } else if (templ == gst_element_class_get_pad_template (klass,
2875           "send_rtp_sink_%d")) {
2876     result = create_send_rtp (rtpbin, templ, pad_name);
2877   } else if (templ == gst_element_class_get_pad_template (klass,
2878           "send_rtcp_src_%d")) {
2879     result = create_rtcp (rtpbin, templ, pad_name);
2880   } else
2881     goto wrong_template;
2882
2883   g_free (pad_name);
2884   GST_RTP_BIN_UNLOCK (rtpbin);
2885
2886   return result;
2887
2888   /* ERRORS */
2889 wrong_template:
2890   {
2891     g_free (pad_name);
2892     GST_RTP_BIN_UNLOCK (rtpbin);
2893     g_warning ("gstrtpbin: this is not our template");
2894     return NULL;
2895   }
2896 }
2897
2898 static void
2899 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
2900 {
2901   GstRtpBinSession *session;
2902   GstRtpBin *rtpbin;
2903
2904   g_return_if_fail (GST_IS_GHOST_PAD (pad));
2905   g_return_if_fail (GST_IS_RTP_BIN (element));
2906
2907   rtpbin = GST_RTP_BIN (element);
2908
2909   GST_RTP_BIN_LOCK (rtpbin);
2910   GST_DEBUG_OBJECT (rtpbin, "Trying to release pad %s:%s",
2911       GST_DEBUG_PAD_NAME (pad));
2912
2913   if (!(session = find_session_by_pad (rtpbin, pad)))
2914     goto unknown_pad;
2915
2916   if (session->recv_rtp_sink_ghost == pad) {
2917     remove_recv_rtp (rtpbin, session);
2918   } else if (session->recv_rtcp_sink_ghost == pad) {
2919     remove_recv_rtcp (rtpbin, session);
2920   } else if (session->send_rtp_sink_ghost == pad) {
2921     remove_send_rtp (rtpbin, session);
2922   } else if (session->send_rtcp_src_ghost == pad) {
2923     remove_rtcp (rtpbin, session);
2924   }
2925
2926   /* no more request pads, free the complete session */
2927   if (session->recv_rtp_sink_ghost == NULL
2928       && session->recv_rtcp_sink_ghost == NULL
2929       && session->send_rtp_sink_ghost == NULL
2930       && session->send_rtcp_src_ghost == NULL) {
2931     GST_DEBUG_OBJECT (rtpbin, "no more pads for session %p", session);
2932     rtpbin->sessions = g_slist_remove (rtpbin->sessions, session);
2933     free_session (session, rtpbin);
2934   }
2935   GST_RTP_BIN_UNLOCK (rtpbin);
2936
2937   return;
2938
2939   /* ERROR */
2940 unknown_pad:
2941   {
2942     GST_RTP_BIN_UNLOCK (rtpbin);
2943     g_warning ("gstrtpbin: %s:%s is not one of our request pads",
2944         GST_DEBUG_PAD_NAME (pad));
2945     return;
2946   }
2947 }