rtpbin: fix setting the SDES property
[platform/upstream/gstreamer.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   gst_element_set_locked_state (stream->demux, TRUE);
1340   gst_element_set_locked_state (stream->buffer, TRUE);
1341
1342   gst_element_set_state (stream->demux, GST_STATE_NULL);
1343   gst_element_set_state (stream->buffer, GST_STATE_NULL);
1344
1345   /* now remove this signal, we need this while going to NULL because it to
1346    * do some cleanups */
1347   if (stream->demux)
1348     g_signal_handler_disconnect (stream->demux, stream->demux_padremoved_sig);
1349
1350   gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
1351   if (stream->demux)
1352     gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
1353
1354   g_free (stream);
1355 }
1356
1357 /* GObject vmethods */
1358 static void gst_rtp_bin_dispose (GObject * object);
1359 static void gst_rtp_bin_finalize (GObject * object);
1360 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
1361     const GValue * value, GParamSpec * pspec);
1362 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
1363     GValue * value, GParamSpec * pspec);
1364
1365 /* GstElement vmethods */
1366 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
1367     GstStateChange transition);
1368 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
1369     GstPadTemplate * templ, const gchar * name);
1370 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
1371 static void gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message);
1372
1373 GST_BOILERPLATE (GstRtpBin, gst_rtp_bin, GstBin, GST_TYPE_BIN);
1374
1375 static void
1376 gst_rtp_bin_base_init (gpointer klass)
1377 {
1378   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
1379
1380   /* sink pads */
1381   gst_element_class_add_pad_template (element_class,
1382       gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
1383   gst_element_class_add_pad_template (element_class,
1384       gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
1385   gst_element_class_add_pad_template (element_class,
1386       gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
1387
1388   /* src pads */
1389   gst_element_class_add_pad_template (element_class,
1390       gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
1391   gst_element_class_add_pad_template (element_class,
1392       gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
1393   gst_element_class_add_pad_template (element_class,
1394       gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
1395
1396   gst_element_class_set_details_simple (element_class, "RTP Bin",
1397       "Filter/Network/RTP",
1398       "Real-Time Transport Protocol bin",
1399       "Wim Taymans <wim.taymans@gmail.com>");
1400 }
1401
1402 static void
1403 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1404 {
1405   GObjectClass *gobject_class;
1406   GstElementClass *gstelement_class;
1407   GstBinClass *gstbin_class;
1408
1409   gobject_class = (GObjectClass *) klass;
1410   gstelement_class = (GstElementClass *) klass;
1411   gstbin_class = (GstBinClass *) klass;
1412
1413   g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1414
1415   gobject_class->dispose = gst_rtp_bin_dispose;
1416   gobject_class->finalize = gst_rtp_bin_finalize;
1417   gobject_class->set_property = gst_rtp_bin_set_property;
1418   gobject_class->get_property = gst_rtp_bin_get_property;
1419
1420   g_object_class_install_property (gobject_class, PROP_LATENCY,
1421       g_param_spec_uint ("latency", "Buffer latency in ms",
1422           "Default amount of ms to buffer in the jitterbuffers", 0,
1423           G_MAXUINT, DEFAULT_LATENCY_MS,
1424           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1425
1426   /**
1427    * GstRtpBin::request-pt-map:
1428    * @rtpbin: the object which received the signal
1429    * @session: the session
1430    * @pt: the pt
1431    *
1432    * Request the payload type as #GstCaps for @pt in @session.
1433    */
1434   gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1435       g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1436       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1437       NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1438       G_TYPE_UINT, G_TYPE_UINT);
1439
1440     /**
1441    * GstRtpBin::payload-type-change:
1442    * @rtpbin: the object which received the signal
1443    * @session: the session
1444    * @pt: the pt
1445    *
1446    * Signal that the current payload type changed to @pt in @session.
1447    *
1448    * Since: 0.10.17
1449    */
1450   gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
1451       g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
1452       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, payload_type_change),
1453       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1454       G_TYPE_UINT, G_TYPE_UINT);
1455
1456   /**
1457    * GstRtpBin::clear-pt-map:
1458    * @rtpbin: the object which received the signal
1459    *
1460    * Clear all previously cached pt-mapping obtained with
1461    * #GstRtpBin::request-pt-map.
1462    */
1463   gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1464       g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1465       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1466           clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1467       0, G_TYPE_NONE);
1468
1469   /**
1470    * GstRtpBin::reset-sync:
1471    * @rtpbin: the object which received the signal
1472    *
1473    * Reset all currently configured lip-sync parameters and require new SR
1474    * packets for all streams before lip-sync is attempted again.
1475    */
1476   gst_rtp_bin_signals[SIGNAL_RESET_SYNC] =
1477       g_signal_new ("reset-sync", G_TYPE_FROM_CLASS (klass),
1478       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1479           reset_sync), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1480       0, G_TYPE_NONE);
1481
1482   /**
1483    * GstRtpBin::get-internal-session:
1484    * @rtpbin: the object which received the signal
1485    * @id: the session id
1486    *
1487    * Request the internal RTPSession object as #GObject in session @id.
1488    */
1489   gst_rtp_bin_signals[SIGNAL_GET_INTERNAL_SESSION] =
1490       g_signal_new ("get-internal-session", G_TYPE_FROM_CLASS (klass),
1491       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1492           get_internal_session), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
1493       RTP_TYPE_SESSION, 1, G_TYPE_UINT);
1494
1495   /**
1496    * GstRtpBin::on-new-ssrc:
1497    * @rtpbin: the object which received the signal
1498    * @session: the session
1499    * @ssrc: the SSRC
1500    *
1501    * Notify of a new SSRC that entered @session.
1502    */
1503   gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1504       g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1505       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1506       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1507       G_TYPE_UINT, G_TYPE_UINT);
1508   /**
1509    * GstRtpBin::on-ssrc-collision:
1510    * @rtpbin: the object which received the signal
1511    * @session: the session
1512    * @ssrc: the SSRC
1513    *
1514    * Notify when we have an SSRC collision
1515    */
1516   gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1517       g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1518       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1519       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1520       G_TYPE_UINT, G_TYPE_UINT);
1521   /**
1522    * GstRtpBin::on-ssrc-validated:
1523    * @rtpbin: the object which received the signal
1524    * @session: the session
1525    * @ssrc: the SSRC
1526    *
1527    * Notify of a new SSRC that became validated.
1528    */
1529   gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1530       g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1531       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1532       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1533       G_TYPE_UINT, G_TYPE_UINT);
1534   /**
1535    * GstRtpBin::on-ssrc-active:
1536    * @rtpbin: the object which received the signal
1537    * @session: the session
1538    * @ssrc: the SSRC
1539    *
1540    * Notify of a SSRC that is active, i.e., sending RTCP.
1541    */
1542   gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1543       g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1544       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
1545       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1546       G_TYPE_UINT, G_TYPE_UINT);
1547   /**
1548    * GstRtpBin::on-ssrc-sdes:
1549    * @rtpbin: the object which received the signal
1550    * @session: the session
1551    * @ssrc: the SSRC
1552    *
1553    * Notify of a SSRC that is active, i.e., sending RTCP.
1554    */
1555   gst_rtp_bin_signals[SIGNAL_ON_SSRC_SDES] =
1556       g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
1557       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_sdes),
1558       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1559       G_TYPE_UINT, G_TYPE_UINT);
1560
1561   /**
1562    * GstRtpBin::on-bye-ssrc:
1563    * @rtpbin: the object which received the signal
1564    * @session: the session
1565    * @ssrc: the SSRC
1566    *
1567    * Notify of an SSRC that became inactive because of a BYE packet.
1568    */
1569   gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1570       g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1571       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1572       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1573       G_TYPE_UINT, G_TYPE_UINT);
1574   /**
1575    * GstRtpBin::on-bye-timeout:
1576    * @rtpbin: the object which received the signal
1577    * @session: the session
1578    * @ssrc: the SSRC
1579    *
1580    * Notify of an SSRC that has timed out because of BYE
1581    */
1582   gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1583       g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1584       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1585       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1586       G_TYPE_UINT, G_TYPE_UINT);
1587   /**
1588    * GstRtpBin::on-timeout:
1589    * @rtpbin: the object which received the signal
1590    * @session: the session
1591    * @ssrc: the SSRC
1592    *
1593    * Notify of an SSRC that has timed out
1594    */
1595   gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1596       g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1597       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1598       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1599       G_TYPE_UINT, G_TYPE_UINT);
1600   /**
1601    * GstRtpBin::on-sender-timeout:
1602    * @rtpbin: the object which received the signal
1603    * @session: the session
1604    * @ssrc: the SSRC
1605    *
1606    * Notify of a sender SSRC that has timed out and became a receiver
1607    */
1608   gst_rtp_bin_signals[SIGNAL_ON_SENDER_TIMEOUT] =
1609       g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
1610       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_sender_timeout),
1611       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1612       G_TYPE_UINT, G_TYPE_UINT);
1613
1614   /**
1615    * GstRtpBin::on-npt-stop:
1616    * @rtpbin: the object which received the signal
1617    * @session: the session
1618    * @ssrc: the SSRC
1619    *
1620    * Notify that SSRC sender has sent data up to the configured NPT stop time.
1621    */
1622   gst_rtp_bin_signals[SIGNAL_ON_NPT_STOP] =
1623       g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
1624       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_npt_stop),
1625       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1626       G_TYPE_UINT, G_TYPE_UINT);
1627
1628   g_object_class_install_property (gobject_class, PROP_SDES,
1629       g_param_spec_boxed ("sdes", "SDES",
1630           "The SDES items of this session",
1631           GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1632
1633   g_object_class_install_property (gobject_class, PROP_DO_LOST,
1634       g_param_spec_boolean ("do-lost", "Do Lost",
1635           "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
1636           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1637
1638   g_object_class_install_property (gobject_class, PROP_AUTOREMOVE,
1639       g_param_spec_boolean ("autoremove", "Auto Remove",
1640           "Automatically remove timed out sources", DEFAULT_AUTOREMOVE,
1641           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1642
1643   g_object_class_install_property (gobject_class, PROP_IGNORE_PT,
1644       g_param_spec_boolean ("ignore-pt", "Ignore PT",
1645           "Do not demultiplex based on PT values", DEFAULT_IGNORE_PT,
1646           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1647
1648   g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
1649       g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
1650           "Use the pipeline clock to set the NTP time in the RTCP SR messages",
1651           DEFAULT_USE_PIPELINE_CLOCK,
1652           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1653   /**
1654    * GstRtpBin::buffer-mode:
1655    *
1656    * Control the buffering and timestamping mode used by the jitterbuffer.
1657    *
1658    * Since: 0.10.17
1659    */
1660   g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
1661       g_param_spec_enum ("buffer-mode", "Buffer Mode",
1662           "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
1663           DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1664   /**
1665    * GstRtpBin::ntp-sync:
1666    *
1667    * Synchronize received streams to the NTP clock. When the NTP clock is shared
1668    * between the receivers and the senders (such as when using ntpd) this option
1669    * can be used to synchronize receivers on multiple machines.
1670    *
1671    * Since: 0.10.21
1672    */
1673   g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
1674       g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
1675           "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
1676           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1677
1678   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1679   gstelement_class->request_new_pad =
1680       GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1681   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1682
1683   gstbin_class->handle_message = GST_DEBUG_FUNCPTR (gst_rtp_bin_handle_message);
1684
1685   klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1686   klass->reset_sync = GST_DEBUG_FUNCPTR (gst_rtp_bin_reset_sync);
1687   klass->get_internal_session =
1688       GST_DEBUG_FUNCPTR (gst_rtp_bin_get_internal_session);
1689
1690   GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1691 }
1692
1693 static void
1694 gst_rtp_bin_init (GstRtpBin * rtpbin, GstRtpBinClass * klass)
1695 {
1696   gchar *str;
1697
1698   rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1699   rtpbin->priv->bin_lock = g_mutex_new ();
1700   rtpbin->priv->dyn_lock = g_mutex_new ();
1701
1702   rtpbin->latency_ms = DEFAULT_LATENCY_MS;
1703   rtpbin->latency_ns = DEFAULT_LATENCY_MS * GST_MSECOND;
1704   rtpbin->do_lost = DEFAULT_DO_LOST;
1705   rtpbin->ignore_pt = DEFAULT_IGNORE_PT;
1706   rtpbin->ntp_sync = DEFAULT_NTP_SYNC;
1707   rtpbin->priv->autoremove = DEFAULT_AUTOREMOVE;
1708   rtpbin->buffer_mode = DEFAULT_BUFFER_MODE;
1709   rtpbin->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
1710
1711   /* some default SDES entries */
1712   str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
1713   rtpbin->sdes = gst_structure_new ("application/x-rtp-source-sdes",
1714       "cname", G_TYPE_STRING, str,
1715       "name", G_TYPE_STRING, g_get_real_name (),
1716       "tool", G_TYPE_STRING, "GStreamer", NULL);
1717   g_free (str);
1718 }
1719
1720 static void
1721 gst_rtp_bin_dispose (GObject * object)
1722 {
1723   GstRtpBin *rtpbin;
1724
1725   rtpbin = GST_RTP_BIN (object);
1726
1727   GST_DEBUG_OBJECT (object, "freeing sessions");
1728   g_slist_foreach (rtpbin->sessions, (GFunc) free_session, rtpbin);
1729   g_slist_free (rtpbin->sessions);
1730   rtpbin->sessions = NULL;
1731   GST_DEBUG_OBJECT (object, "freeing clients");
1732   g_slist_foreach (rtpbin->clients, (GFunc) free_client, rtpbin);
1733   g_slist_free (rtpbin->clients);
1734   rtpbin->clients = NULL;
1735
1736   G_OBJECT_CLASS (parent_class)->dispose (object);
1737 }
1738
1739 static void
1740 gst_rtp_bin_finalize (GObject * object)
1741 {
1742   GstRtpBin *rtpbin;
1743
1744   rtpbin = GST_RTP_BIN (object);
1745
1746   if (rtpbin->sdes)
1747     gst_structure_free (rtpbin->sdes);
1748
1749   g_mutex_free (rtpbin->priv->bin_lock);
1750   g_mutex_free (rtpbin->priv->dyn_lock);
1751
1752   G_OBJECT_CLASS (parent_class)->finalize (object);
1753 }
1754
1755
1756 static void
1757 gst_rtp_bin_set_sdes_struct (GstRtpBin * bin, const GstStructure * sdes)
1758 {
1759   GSList *item;
1760
1761   if (sdes == NULL)
1762     return;
1763
1764   GST_RTP_BIN_LOCK (bin);
1765
1766   GST_OBJECT_LOCK (bin);
1767   if (bin->sdes)
1768     gst_structure_free (bin->sdes);
1769   bin->sdes = gst_structure_copy (sdes);
1770   GST_OBJECT_UNLOCK (bin);
1771
1772   /* store in all sessions */
1773   for (item = bin->sessions; item; item = g_slist_next (item)) {
1774     GstRtpBinSession *session = item->data;
1775     g_object_set (session->session, "sdes", sdes, NULL);
1776   }
1777
1778   GST_RTP_BIN_UNLOCK (bin);
1779 }
1780
1781 static GstStructure *
1782 gst_rtp_bin_get_sdes_struct (GstRtpBin * bin)
1783 {
1784   GstStructure *result;
1785
1786   GST_OBJECT_LOCK (bin);
1787   result = gst_structure_copy (bin->sdes);
1788   GST_OBJECT_UNLOCK (bin);
1789
1790   return result;
1791 }
1792
1793 static void
1794 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1795     const GValue * value, GParamSpec * pspec)
1796 {
1797   GstRtpBin *rtpbin;
1798
1799   rtpbin = GST_RTP_BIN (object);
1800
1801   switch (prop_id) {
1802     case PROP_LATENCY:
1803       GST_RTP_BIN_LOCK (rtpbin);
1804       rtpbin->latency_ms = g_value_get_uint (value);
1805       rtpbin->latency_ns = rtpbin->latency_ms * GST_MSECOND;
1806       GST_RTP_BIN_UNLOCK (rtpbin);
1807       /* propagate the property down to the jitterbuffer */
1808       gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "latency", value);
1809       break;
1810     case PROP_SDES:
1811       gst_rtp_bin_set_sdes_struct (rtpbin, g_value_get_boxed (value));
1812       break;
1813     case PROP_DO_LOST:
1814       GST_RTP_BIN_LOCK (rtpbin);
1815       rtpbin->do_lost = g_value_get_boolean (value);
1816       GST_RTP_BIN_UNLOCK (rtpbin);
1817       gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "do-lost", value);
1818       break;
1819     case PROP_NTP_SYNC:
1820       rtpbin->ntp_sync = g_value_get_boolean (value);
1821       break;
1822     case PROP_IGNORE_PT:
1823       rtpbin->ignore_pt = g_value_get_boolean (value);
1824       break;
1825     case PROP_AUTOREMOVE:
1826       rtpbin->priv->autoremove = g_value_get_boolean (value);
1827       break;
1828     case PROP_USE_PIPELINE_CLOCK:
1829     {
1830       GSList *sessions;
1831       GST_RTP_BIN_LOCK (rtpbin);
1832       rtpbin->use_pipeline_clock = g_value_get_boolean (value);
1833       for (sessions = rtpbin->sessions; sessions;
1834           sessions = g_slist_next (sessions)) {
1835         GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
1836
1837         g_object_set (G_OBJECT (session->session),
1838             "use-pipeline-clock", rtpbin->use_pipeline_clock, NULL);
1839       }
1840       GST_RTP_BIN_UNLOCK (rtpbin);
1841     }
1842       break;
1843     case PROP_BUFFER_MODE:
1844       GST_RTP_BIN_LOCK (rtpbin);
1845       rtpbin->buffer_mode = g_value_get_enum (value);
1846       GST_RTP_BIN_UNLOCK (rtpbin);
1847       /* propagate the property down to the jitterbuffer */
1848       gst_rtp_bin_propagate_property_to_jitterbuffer (rtpbin, "mode", value);
1849       break;
1850     default:
1851       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1852       break;
1853   }
1854 }
1855
1856 static void
1857 gst_rtp_bin_get_property (GObject * object, guint prop_id,
1858     GValue * value, GParamSpec * pspec)
1859 {
1860   GstRtpBin *rtpbin;
1861
1862   rtpbin = GST_RTP_BIN (object);
1863
1864   switch (prop_id) {
1865     case PROP_LATENCY:
1866       GST_RTP_BIN_LOCK (rtpbin);
1867       g_value_set_uint (value, rtpbin->latency_ms);
1868       GST_RTP_BIN_UNLOCK (rtpbin);
1869       break;
1870     case PROP_SDES:
1871       g_value_take_boxed (value, gst_rtp_bin_get_sdes_struct (rtpbin));
1872       break;
1873     case PROP_DO_LOST:
1874       GST_RTP_BIN_LOCK (rtpbin);
1875       g_value_set_boolean (value, rtpbin->do_lost);
1876       GST_RTP_BIN_UNLOCK (rtpbin);
1877       break;
1878     case PROP_IGNORE_PT:
1879       g_value_set_boolean (value, rtpbin->ignore_pt);
1880       break;
1881     case PROP_NTP_SYNC:
1882       g_value_set_boolean (value, rtpbin->ntp_sync);
1883       break;
1884     case PROP_AUTOREMOVE:
1885       g_value_set_boolean (value, rtpbin->priv->autoremove);
1886       break;
1887     case PROP_BUFFER_MODE:
1888       g_value_set_enum (value, rtpbin->buffer_mode);
1889       break;
1890     case PROP_USE_PIPELINE_CLOCK:
1891       g_value_set_boolean (value, rtpbin->use_pipeline_clock);
1892       break;
1893     default:
1894       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1895       break;
1896   }
1897 }
1898
1899 static void
1900 gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
1901 {
1902   GstRtpBin *rtpbin;
1903
1904   rtpbin = GST_RTP_BIN (bin);
1905
1906   switch (GST_MESSAGE_TYPE (message)) {
1907     case GST_MESSAGE_ELEMENT:
1908     {
1909       const GstStructure *s = gst_message_get_structure (message);
1910
1911       /* we change the structure name and add the session ID to it */
1912       if (gst_structure_has_name (s, "application/x-rtp-source-sdes")) {
1913         GstRtpBinSession *sess;
1914
1915         /* find the session we set it as object data */
1916         sess = g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
1917             "GstRTPBin.session");
1918
1919         if (G_LIKELY (sess)) {
1920           message = gst_message_make_writable (message);
1921           s = gst_message_get_structure (message);
1922           gst_structure_set ((GstStructure *) s, "session", G_TYPE_UINT,
1923               sess->id, NULL);
1924         }
1925       }
1926       GST_BIN_CLASS (parent_class)->handle_message (bin, message);
1927       break;
1928     }
1929     case GST_MESSAGE_BUFFERING:
1930     {
1931       gint percent;
1932       gint min_percent = 100;
1933       GSList *sessions, *streams;
1934       GstRtpBinStream *stream;
1935       gboolean change = FALSE, active = FALSE;
1936       GstClockTime min_out_time;
1937       GstBufferingMode mode;
1938       gint avg_in, avg_out;
1939       gint64 buffering_left;
1940
1941       gst_message_parse_buffering (message, &percent);
1942       gst_message_parse_buffering_stats (message, &mode, &avg_in, &avg_out,
1943           &buffering_left);
1944
1945       stream =
1946           g_object_get_data (G_OBJECT (GST_MESSAGE_SRC (message)),
1947           "GstRTPBin.stream");
1948
1949       GST_DEBUG_OBJECT (bin, "got percent %d from stream %p", percent, stream);
1950
1951       /* get the stream */
1952       if (G_LIKELY (stream)) {
1953         GST_RTP_BIN_LOCK (rtpbin);
1954         /* fill in the percent */
1955         stream->percent = percent;
1956
1957         /* calculate the min value for all streams */
1958         for (sessions = rtpbin->sessions; sessions;
1959             sessions = g_slist_next (sessions)) {
1960           GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
1961
1962           GST_RTP_SESSION_LOCK (session);
1963           if (session->streams) {
1964             for (streams = session->streams; streams;
1965                 streams = g_slist_next (streams)) {
1966               GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
1967
1968               GST_DEBUG_OBJECT (bin, "stream %p percent %d", stream,
1969                   stream->percent);
1970
1971               /* find min percent */
1972               if (min_percent > stream->percent)
1973                 min_percent = stream->percent;
1974             }
1975           } else {
1976             GST_INFO_OBJECT (bin,
1977                 "session has no streams, setting min_percent to 0");
1978             min_percent = 0;
1979           }
1980           GST_RTP_SESSION_UNLOCK (session);
1981         }
1982         GST_DEBUG_OBJECT (bin, "min percent %d", min_percent);
1983
1984         if (rtpbin->buffering) {
1985           if (min_percent == 100) {
1986             rtpbin->buffering = FALSE;
1987             active = TRUE;
1988             change = TRUE;
1989           }
1990         } else {
1991           if (min_percent < 100) {
1992             /* pause the streams */
1993             rtpbin->buffering = TRUE;
1994             active = FALSE;
1995             change = TRUE;
1996           }
1997         }
1998         GST_RTP_BIN_UNLOCK (rtpbin);
1999
2000         gst_message_unref (message);
2001
2002         /* make a new buffering message with the min value */
2003         message =
2004             gst_message_new_buffering (GST_OBJECT_CAST (bin), min_percent);
2005         gst_message_set_buffering_stats (message, mode, avg_in, avg_out,
2006             buffering_left);
2007
2008         if (G_UNLIKELY (change)) {
2009           GstClock *clock;
2010           guint64 running_time = 0;
2011           guint64 offset = 0;
2012
2013           /* figure out the running time when we have a clock */
2014           if (G_LIKELY ((clock =
2015                       gst_element_get_clock (GST_ELEMENT_CAST (bin))))) {
2016             guint64 now, base_time;
2017
2018             now = gst_clock_get_time (clock);
2019             base_time = gst_element_get_base_time (GST_ELEMENT_CAST (bin));
2020             running_time = now - base_time;
2021           }
2022           GST_DEBUG_OBJECT (bin,
2023               "running time now %" GST_TIME_FORMAT,
2024               GST_TIME_ARGS (running_time));
2025
2026           GST_RTP_BIN_LOCK (rtpbin);
2027
2028           /* when we reactivate, calculate the offsets so that all streams have
2029            * an output time that is at least as big as the running_time */
2030           offset = 0;
2031           if (active) {
2032             if (running_time > rtpbin->buffer_start) {
2033               offset = running_time - rtpbin->buffer_start;
2034               if (offset >= rtpbin->latency_ns)
2035                 offset -= rtpbin->latency_ns;
2036               else
2037                 offset = 0;
2038             }
2039           }
2040
2041           /* pause all streams */
2042           min_out_time = -1;
2043           for (sessions = rtpbin->sessions; sessions;
2044               sessions = g_slist_next (sessions)) {
2045             GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
2046
2047             GST_RTP_SESSION_LOCK (session);
2048             for (streams = session->streams; streams;
2049                 streams = g_slist_next (streams)) {
2050               GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
2051               GstElement *element = stream->buffer;
2052               guint64 last_out;
2053
2054               g_signal_emit_by_name (element, "set-active", active, offset,
2055                   &last_out);
2056
2057               if (!active) {
2058                 g_object_get (element, "percent", &stream->percent, NULL);
2059
2060                 if (last_out == -1)
2061                   last_out = 0;
2062                 if (min_out_time == -1 || last_out < min_out_time)
2063                   min_out_time = last_out;
2064               }
2065
2066               GST_DEBUG_OBJECT (bin,
2067                   "setting %p to %d, offset %" GST_TIME_FORMAT ", last %"
2068                   GST_TIME_FORMAT ", percent %d", element, active,
2069                   GST_TIME_ARGS (offset), GST_TIME_ARGS (last_out),
2070                   stream->percent);
2071             }
2072             GST_RTP_SESSION_UNLOCK (session);
2073           }
2074           GST_DEBUG_OBJECT (bin,
2075               "min out time %" GST_TIME_FORMAT, GST_TIME_ARGS (min_out_time));
2076
2077           /* the buffer_start is the min out time of all paused jitterbuffers */
2078           if (!active)
2079             rtpbin->buffer_start = min_out_time;
2080
2081           GST_RTP_BIN_UNLOCK (rtpbin);
2082         }
2083       }
2084       GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2085       break;
2086     }
2087     default:
2088     {
2089       GST_BIN_CLASS (parent_class)->handle_message (bin, message);
2090       break;
2091     }
2092   }
2093 }
2094
2095 static GstStateChangeReturn
2096 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
2097 {
2098   GstStateChangeReturn res;
2099   GstRtpBin *rtpbin;
2100   GstRtpBinPrivate *priv;
2101
2102   rtpbin = GST_RTP_BIN (element);
2103   priv = rtpbin->priv;
2104
2105   switch (transition) {
2106     case GST_STATE_CHANGE_NULL_TO_READY:
2107       break;
2108     case GST_STATE_CHANGE_READY_TO_PAUSED:
2109       GST_LOG_OBJECT (rtpbin, "clearing shutdown flag");
2110       g_atomic_int_set (&priv->shutdown, 0);
2111       break;
2112     case GST_STATE_CHANGE_PAUSED_TO_READY:
2113       GST_LOG_OBJECT (rtpbin, "setting shutdown flag");
2114       g_atomic_int_set (&priv->shutdown, 1);
2115       /* wait for all callbacks to end by taking the lock. No new callbacks will
2116        * be able to happen as we set the shutdown flag. */
2117       GST_RTP_BIN_DYN_LOCK (rtpbin);
2118       GST_LOG_OBJECT (rtpbin, "dynamic lock taken, we can continue shutdown");
2119       GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2120       break;
2121     default:
2122       break;
2123   }
2124
2125   res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2126
2127   switch (transition) {
2128     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2129       break;
2130     case GST_STATE_CHANGE_PAUSED_TO_READY:
2131       break;
2132     case GST_STATE_CHANGE_READY_TO_NULL:
2133       break;
2134     default:
2135       break;
2136   }
2137   return res;
2138 }
2139
2140 /* a new pad (SSRC) was created in @session. This signal is emited from the
2141  * payload demuxer. */
2142 static void
2143 new_payload_found (GstElement * element, guint pt, GstPad * pad,
2144     GstRtpBinStream * stream)
2145 {
2146   GstRtpBin *rtpbin;
2147   GstElementClass *klass;
2148   GstPadTemplate *templ;
2149   gchar *padname;
2150   GstPad *gpad;
2151
2152   rtpbin = stream->bin;
2153
2154   GST_DEBUG ("new payload pad %d", pt);
2155
2156   GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2157
2158   /* ghost the pad to the parent */
2159   klass = GST_ELEMENT_GET_CLASS (rtpbin);
2160   templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
2161   padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
2162       stream->session->id, stream->ssrc, pt);
2163   gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2164   g_free (padname);
2165   g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", gpad);
2166
2167   gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
2168   gst_pad_set_active (gpad, TRUE);
2169   GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2170
2171   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2172
2173   return;
2174
2175 shutdown:
2176   {
2177     GST_DEBUG ("ignoring, we are shutting down");
2178     return;
2179   }
2180 }
2181
2182 static void
2183 payload_pad_removed (GstElement * element, GstPad * pad,
2184     GstRtpBinStream * stream)
2185 {
2186   GstRtpBin *rtpbin;
2187   GstPad *gpad;
2188
2189   rtpbin = stream->bin;
2190
2191   GST_DEBUG ("payload pad removed");
2192
2193   GST_RTP_BIN_DYN_LOCK (rtpbin);
2194   if ((gpad = g_object_get_data (G_OBJECT (pad), "GstRTPBin.ghostpad"))) {
2195     g_object_set_data (G_OBJECT (pad), "GstRTPBin.ghostpad", NULL);
2196
2197     gst_pad_set_active (gpad, FALSE);
2198     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2199   }
2200   GST_RTP_BIN_DYN_UNLOCK (rtpbin);
2201 }
2202
2203 static GstCaps *
2204 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
2205 {
2206   GstRtpBin *rtpbin;
2207   GstCaps *caps;
2208
2209   rtpbin = session->bin;
2210
2211   GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
2212       session->id);
2213
2214   caps = get_pt_map (session, pt);
2215   if (!caps)
2216     goto no_caps;
2217
2218   return caps;
2219
2220   /* ERRORS */
2221 no_caps:
2222   {
2223     GST_DEBUG_OBJECT (rtpbin, "could not get caps");
2224     return NULL;
2225   }
2226 }
2227
2228 static void
2229 payload_type_change (GstElement * element, guint pt, GstRtpBinSession * session)
2230 {
2231   GST_DEBUG_OBJECT (session->bin,
2232       "emiting signal for pt type changed to %d in session %d", pt,
2233       session->id);
2234
2235   g_signal_emit (session->bin, gst_rtp_bin_signals[SIGNAL_PAYLOAD_TYPE_CHANGE],
2236       0, session->id, pt);
2237 }
2238
2239 /* emited when caps changed for the session */
2240 static void
2241 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
2242 {
2243   GstRtpBin *bin;
2244   GstCaps *caps;
2245   gint payload;
2246   const GstStructure *s;
2247
2248   bin = session->bin;
2249
2250   g_object_get (pad, "caps", &caps, NULL);
2251
2252   if (caps == NULL)
2253     return;
2254
2255   GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
2256
2257   s = gst_caps_get_structure (caps, 0);
2258
2259   /* get payload, finish when it's not there */
2260   if (!gst_structure_get_int (s, "payload", &payload))
2261     return;
2262
2263   GST_RTP_SESSION_LOCK (session);
2264   GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
2265   g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
2266   GST_RTP_SESSION_UNLOCK (session);
2267 }
2268
2269 /* a new pad (SSRC) was created in @session */
2270 static void
2271 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
2272     GstRtpBinSession * session)
2273 {
2274   GstRtpBin *rtpbin;
2275   GstRtpBinStream *stream;
2276   GstPad *sinkpad, *srcpad;
2277   gchar *padname;
2278
2279   rtpbin = session->bin;
2280
2281   GST_DEBUG_OBJECT (rtpbin, "new SSRC pad %08x, %s:%s", ssrc,
2282       GST_DEBUG_PAD_NAME (pad));
2283
2284   GST_RTP_BIN_SHUTDOWN_LOCK (rtpbin, shutdown);
2285
2286   GST_RTP_SESSION_LOCK (session);
2287
2288   /* create new stream */
2289   stream = create_stream (session, ssrc);
2290   if (!stream)
2291     goto no_stream;
2292
2293   /* get pad and link */
2294   GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTP");
2295   padname = g_strdup_printf ("src_%d", ssrc);
2296   srcpad = gst_element_get_static_pad (element, padname);
2297   g_free (padname);
2298   sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
2299   gst_pad_link (srcpad, sinkpad);
2300   gst_object_unref (sinkpad);
2301   gst_object_unref (srcpad);
2302
2303   GST_DEBUG_OBJECT (rtpbin, "linking jitterbuffer RTCP");
2304   padname = g_strdup_printf ("rtcp_src_%d", ssrc);
2305   srcpad = gst_element_get_static_pad (element, padname);
2306   g_free (padname);
2307   sinkpad = gst_element_get_request_pad (stream->buffer, "sink_rtcp");
2308   gst_pad_link (srcpad, sinkpad);
2309   gst_object_unref (sinkpad);
2310   gst_object_unref (srcpad);
2311
2312   /* connect to the RTCP sync signal from the jitterbuffer */
2313   GST_DEBUG_OBJECT (rtpbin, "connecting sync signal");
2314   stream->buffer_handlesync_sig = g_signal_connect (stream->buffer,
2315       "handle-sync", (GCallback) gst_rtp_bin_handle_sync, stream);
2316
2317   if (stream->demux) {
2318     /* connect to the new-pad signal of the payload demuxer, this will expose the
2319      * new pad by ghosting it. */
2320     stream->demux_newpad_sig = g_signal_connect (stream->demux,
2321         "new-payload-type", (GCallback) new_payload_found, stream);
2322     stream->demux_padremoved_sig = g_signal_connect (stream->demux,
2323         "pad-removed", (GCallback) payload_pad_removed, stream);
2324
2325     /* connect to the request-pt-map signal. This signal will be emited by the
2326      * demuxer so that it can apply a proper caps on the buffers for the
2327      * depayloaders. */
2328     stream->demux_ptreq_sig = g_signal_connect (stream->demux,
2329         "request-pt-map", (GCallback) pt_map_requested, session);
2330     /* connect to the  signal so it can be forwarded. */
2331     stream->demux_ptchange_sig = g_signal_connect (stream->demux,
2332         "payload-type-change", (GCallback) payload_type_change, session);
2333   } else {
2334     /* add gstrtpjitterbuffer src pad to pads */
2335     GstElementClass *klass;
2336     GstPadTemplate *templ;
2337     gchar *padname;
2338     GstPad *gpad, *pad;
2339
2340     pad = gst_element_get_static_pad (stream->buffer, "src");
2341
2342     /* ghost the pad to the parent */
2343     klass = GST_ELEMENT_GET_CLASS (rtpbin);
2344     templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
2345     padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
2346         stream->session->id, stream->ssrc, 255);
2347     gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
2348     g_free (padname);
2349
2350     gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
2351     gst_pad_set_active (gpad, TRUE);
2352     gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
2353
2354     gst_object_unref (pad);
2355   }
2356
2357   GST_RTP_SESSION_UNLOCK (session);
2358   GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2359
2360   return;
2361
2362   /* ERRORS */
2363 shutdown:
2364   {
2365     GST_DEBUG_OBJECT (rtpbin, "we are shutting down");
2366     return;
2367   }
2368 no_stream:
2369   {
2370     GST_RTP_SESSION_UNLOCK (session);
2371     GST_RTP_BIN_SHUTDOWN_UNLOCK (rtpbin);
2372     GST_DEBUG_OBJECT (rtpbin, "could not create stream");
2373     return;
2374   }
2375 }
2376
2377 /* Create a pad for receiving RTP for the session in @name. Must be called with
2378  * RTP_BIN_LOCK.
2379  */
2380 static GstPad *
2381 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2382 {
2383   GstPad *sinkdpad;
2384   guint sessid;
2385   GstRtpBinSession *session;
2386   GstPadLinkReturn lres;
2387
2388   /* first get the session number */
2389   if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
2390     goto no_name;
2391
2392   GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2393
2394   /* get or create session */
2395   session = find_session_by_id (rtpbin, sessid);
2396   if (!session) {
2397     GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2398     /* create session now */
2399     session = create_session (rtpbin, sessid);
2400     if (session == NULL)
2401       goto create_error;
2402   }
2403
2404   /* check if pad was requested */
2405   if (session->recv_rtp_sink_ghost != NULL)
2406     return session->recv_rtp_sink_ghost;
2407
2408   GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
2409   /* get recv_rtp pad and store */
2410   session->recv_rtp_sink =
2411       gst_element_get_request_pad (session->session, "recv_rtp_sink");
2412   if (session->recv_rtp_sink == NULL)
2413     goto pad_failed;
2414
2415   g_signal_connect (session->recv_rtp_sink, "notify::caps",
2416       (GCallback) caps_changed, session);
2417
2418   GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
2419   /* get srcpad, link to SSRCDemux */
2420   session->recv_rtp_src =
2421       gst_element_get_static_pad (session->session, "recv_rtp_src");
2422   if (session->recv_rtp_src == NULL)
2423     goto pad_failed;
2424
2425   GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
2426   sinkdpad = gst_element_get_static_pad (session->demux, "sink");
2427   GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
2428   lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
2429   gst_object_unref (sinkdpad);
2430   if (lres != GST_PAD_LINK_OK)
2431     goto link_failed;
2432
2433   /* connect to the new-ssrc-pad signal of the SSRC demuxer */
2434   session->demux_newpad_sig = g_signal_connect (session->demux,
2435       "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
2436   session->demux_padremoved_sig = g_signal_connect (session->demux,
2437       "removed-ssrc-pad", (GCallback) ssrc_demux_pad_removed, session);
2438
2439   GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
2440   session->recv_rtp_sink_ghost =
2441       gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
2442   gst_pad_set_active (session->recv_rtp_sink_ghost, TRUE);
2443   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->recv_rtp_sink_ghost);
2444
2445   return session->recv_rtp_sink_ghost;
2446
2447   /* ERRORS */
2448 no_name:
2449   {
2450     g_warning ("gstrtpbin: invalid name given");
2451     return NULL;
2452   }
2453 create_error:
2454   {
2455     /* create_session already warned */
2456     return NULL;
2457   }
2458 pad_failed:
2459   {
2460     g_warning ("gstrtpbin: failed to get session pad");
2461     return NULL;
2462   }
2463 link_failed:
2464   {
2465     g_warning ("gstrtpbin: failed to link pads");
2466     return NULL;
2467   }
2468 }
2469
2470 static void
2471 remove_recv_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2472 {
2473   if (session->demux_newpad_sig) {
2474     g_signal_handler_disconnect (session->demux, session->demux_newpad_sig);
2475     session->demux_newpad_sig = 0;
2476   }
2477   if (session->demux_padremoved_sig) {
2478     g_signal_handler_disconnect (session->demux, session->demux_padremoved_sig);
2479     session->demux_padremoved_sig = 0;
2480   }
2481   if (session->recv_rtp_src) {
2482     gst_object_unref (session->recv_rtp_src);
2483     session->recv_rtp_src = NULL;
2484   }
2485   if (session->recv_rtp_sink) {
2486     gst_element_release_request_pad (session->session, session->recv_rtp_sink);
2487     gst_object_unref (session->recv_rtp_sink);
2488     session->recv_rtp_sink = NULL;
2489   }
2490   if (session->recv_rtp_sink_ghost) {
2491     gst_pad_set_active (session->recv_rtp_sink_ghost, FALSE);
2492     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2493         session->recv_rtp_sink_ghost);
2494     session->recv_rtp_sink_ghost = NULL;
2495   }
2496 }
2497
2498 /* Create a pad for receiving RTCP for the session in @name. Must be called with
2499  * RTP_BIN_LOCK.
2500  */
2501 static GstPad *
2502 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
2503     const gchar * name)
2504 {
2505   guint sessid;
2506   GstRtpBinSession *session;
2507   GstPad *sinkdpad;
2508   GstPadLinkReturn lres;
2509
2510   /* first get the session number */
2511   if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
2512     goto no_name;
2513
2514   GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
2515
2516   /* get or create the session */
2517   session = find_session_by_id (rtpbin, sessid);
2518   if (!session) {
2519     GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
2520     /* create session now */
2521     session = create_session (rtpbin, sessid);
2522     if (session == NULL)
2523       goto create_error;
2524   }
2525
2526   /* check if pad was requested */
2527   if (session->recv_rtcp_sink_ghost != NULL)
2528     return session->recv_rtcp_sink_ghost;
2529
2530   /* get recv_rtp pad and store */
2531   GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
2532   session->recv_rtcp_sink =
2533       gst_element_get_request_pad (session->session, "recv_rtcp_sink");
2534   if (session->recv_rtcp_sink == NULL)
2535     goto pad_failed;
2536
2537   /* get srcpad, link to SSRCDemux */
2538   GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
2539   session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
2540   if (session->sync_src == NULL)
2541     goto pad_failed;
2542
2543   GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
2544   sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
2545   lres = gst_pad_link (session->sync_src, sinkdpad);
2546   gst_object_unref (sinkdpad);
2547   if (lres != GST_PAD_LINK_OK)
2548     goto link_failed;
2549
2550   session->recv_rtcp_sink_ghost =
2551       gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
2552   gst_pad_set_active (session->recv_rtcp_sink_ghost, TRUE);
2553   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin),
2554       session->recv_rtcp_sink_ghost);
2555
2556   return session->recv_rtcp_sink_ghost;
2557
2558   /* ERRORS */
2559 no_name:
2560   {
2561     g_warning ("gstrtpbin: invalid name given");
2562     return NULL;
2563   }
2564 create_error:
2565   {
2566     /* create_session already warned */
2567     return NULL;
2568   }
2569 pad_failed:
2570   {
2571     g_warning ("gstrtpbin: failed to get session pad");
2572     return NULL;
2573   }
2574 link_failed:
2575   {
2576     g_warning ("gstrtpbin: failed to link pads");
2577     return NULL;
2578   }
2579 }
2580
2581 static void
2582 remove_recv_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2583 {
2584   if (session->recv_rtcp_sink_ghost) {
2585     gst_pad_set_active (session->recv_rtcp_sink_ghost, FALSE);
2586     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2587         session->recv_rtcp_sink_ghost);
2588     session->recv_rtcp_sink_ghost = NULL;
2589   }
2590   if (session->sync_src) {
2591     /* releasing the request pad should also unref the sync pad */
2592     gst_object_unref (session->sync_src);
2593     session->sync_src = NULL;
2594   }
2595   if (session->recv_rtcp_sink) {
2596     gst_element_release_request_pad (session->session, session->recv_rtcp_sink);
2597     gst_object_unref (session->recv_rtcp_sink);
2598     session->recv_rtcp_sink = NULL;
2599   }
2600 }
2601
2602 /* Create a pad for sending RTP for the session in @name. Must be called with
2603  * RTP_BIN_LOCK.
2604  */
2605 static GstPad *
2606 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2607 {
2608   gchar *gname;
2609   guint sessid;
2610   GstRtpBinSession *session;
2611   GstElementClass *klass;
2612
2613   /* first get the session number */
2614   if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
2615     goto no_name;
2616
2617   /* get or create session */
2618   session = find_session_by_id (rtpbin, sessid);
2619   if (!session) {
2620     /* create session now */
2621     session = create_session (rtpbin, sessid);
2622     if (session == NULL)
2623       goto create_error;
2624   }
2625
2626   /* check if pad was requested */
2627   if (session->send_rtp_sink_ghost != NULL)
2628     return session->send_rtp_sink_ghost;
2629
2630   /* get send_rtp pad and store */
2631   session->send_rtp_sink =
2632       gst_element_get_request_pad (session->session, "send_rtp_sink");
2633   if (session->send_rtp_sink == NULL)
2634     goto pad_failed;
2635
2636   session->send_rtp_sink_ghost =
2637       gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
2638   gst_pad_set_active (session->send_rtp_sink_ghost, TRUE);
2639   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_sink_ghost);
2640
2641   /* get srcpad */
2642   session->send_rtp_src =
2643       gst_element_get_static_pad (session->session, "send_rtp_src");
2644   if (session->send_rtp_src == NULL)
2645     goto no_srcpad;
2646
2647   /* ghost the new source pad */
2648   klass = GST_ELEMENT_GET_CLASS (rtpbin);
2649   gname = g_strdup_printf ("send_rtp_src_%d", sessid);
2650   templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
2651   session->send_rtp_src_ghost =
2652       gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
2653   gst_pad_set_active (session->send_rtp_src_ghost, TRUE);
2654   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtp_src_ghost);
2655   g_free (gname);
2656
2657   return session->send_rtp_sink_ghost;
2658
2659   /* ERRORS */
2660 no_name:
2661   {
2662     g_warning ("gstrtpbin: invalid name given");
2663     return NULL;
2664   }
2665 create_error:
2666   {
2667     /* create_session already warned */
2668     return NULL;
2669   }
2670 pad_failed:
2671   {
2672     g_warning ("gstrtpbin: failed to get session pad for session %d", sessid);
2673     return NULL;
2674   }
2675 no_srcpad:
2676   {
2677     g_warning ("gstrtpbin: failed to get rtp source pad for session %d",
2678         sessid);
2679     return NULL;
2680   }
2681 }
2682
2683 static void
2684 remove_send_rtp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2685 {
2686   if (session->send_rtp_src_ghost) {
2687     gst_pad_set_active (session->send_rtp_src_ghost, FALSE);
2688     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2689         session->send_rtp_src_ghost);
2690     session->send_rtp_src_ghost = NULL;
2691   }
2692   if (session->send_rtp_src) {
2693     gst_object_unref (session->send_rtp_src);
2694     session->send_rtp_src = NULL;
2695   }
2696   if (session->send_rtp_sink) {
2697     gst_element_release_request_pad (GST_ELEMENT_CAST (session->session),
2698         session->send_rtp_sink);
2699     gst_object_unref (session->send_rtp_sink);
2700     session->send_rtp_sink = NULL;
2701   }
2702   if (session->send_rtp_sink_ghost) {
2703     gst_pad_set_active (session->send_rtp_sink_ghost, FALSE);
2704     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2705         session->send_rtp_sink_ghost);
2706     session->send_rtp_sink_ghost = NULL;
2707   }
2708 }
2709
2710 /* Create a pad for sending RTCP for the session in @name. Must be called with
2711  * RTP_BIN_LOCK.
2712  */
2713 static GstPad *
2714 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
2715 {
2716   guint sessid;
2717   GstRtpBinSession *session;
2718
2719   /* first get the session number */
2720   if (name == NULL || sscanf (name, "send_rtcp_src_%d", &sessid) != 1)
2721     goto no_name;
2722
2723   /* get or create session */
2724   session = find_session_by_id (rtpbin, sessid);
2725   if (!session)
2726     goto no_session;
2727
2728   /* check if pad was requested */
2729   if (session->send_rtcp_src_ghost != NULL)
2730     return session->send_rtcp_src_ghost;
2731
2732   /* get rtcp_src pad and store */
2733   session->send_rtcp_src =
2734       gst_element_get_request_pad (session->session, "send_rtcp_src");
2735   if (session->send_rtcp_src == NULL)
2736     goto pad_failed;
2737
2738   session->send_rtcp_src_ghost =
2739       gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
2740   gst_pad_set_active (session->send_rtcp_src_ghost, TRUE);
2741   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), session->send_rtcp_src_ghost);
2742
2743   return session->send_rtcp_src_ghost;
2744
2745   /* ERRORS */
2746 no_name:
2747   {
2748     g_warning ("gstrtpbin: invalid name given");
2749     return NULL;
2750   }
2751 no_session:
2752   {
2753     g_warning ("gstrtpbin: session with id %d does not exist", sessid);
2754     return NULL;
2755   }
2756 pad_failed:
2757   {
2758     g_warning ("gstrtpbin: failed to get rtcp pad for session %d", sessid);
2759     return NULL;
2760   }
2761 }
2762
2763 static void
2764 remove_rtcp (GstRtpBin * rtpbin, GstRtpBinSession * session)
2765 {
2766   if (session->send_rtcp_src_ghost) {
2767     gst_pad_set_active (session->send_rtcp_src_ghost, FALSE);
2768     gst_element_remove_pad (GST_ELEMENT_CAST (rtpbin),
2769         session->send_rtcp_src_ghost);
2770     session->send_rtcp_src_ghost = NULL;
2771   }
2772   if (session->send_rtcp_src) {
2773     gst_element_release_request_pad (session->session, session->send_rtcp_src);
2774     gst_object_unref (session->send_rtcp_src);
2775     session->send_rtcp_src = NULL;
2776   }
2777 }
2778
2779 /* If the requested name is NULL we should create a name with
2780  * the session number assuming we want the lowest posible session
2781  * with a free pad like the template */
2782 static gchar *
2783 gst_rtp_bin_get_free_pad_name (GstElement * element, GstPadTemplate * templ)
2784 {
2785   gboolean name_found = FALSE;
2786   gint session = 0;
2787   GstIterator *pad_it = NULL;
2788   gchar *pad_name = NULL;
2789
2790   GST_DEBUG_OBJECT (element, "find a free pad name for template");
2791   while (!name_found) {
2792     gboolean done = FALSE;
2793     g_free (pad_name);
2794     pad_name = g_strdup_printf (templ->name_template, session++);
2795     pad_it = gst_element_iterate_pads (GST_ELEMENT (element));
2796     name_found = TRUE;
2797     while (!done) {
2798       gpointer data;
2799
2800       switch (gst_iterator_next (pad_it, &data)) {
2801         case GST_ITERATOR_OK:
2802         {
2803           GstPad *pad;
2804           gchar *name;
2805
2806           pad = GST_PAD_CAST (data);
2807           name = gst_pad_get_name (pad);
2808
2809           if (strcmp (name, pad_name) == 0) {
2810             done = TRUE;
2811             name_found = FALSE;
2812           }
2813           g_free (name);
2814           gst_object_unref (pad);
2815           break;
2816         }
2817         case GST_ITERATOR_ERROR:
2818         case GST_ITERATOR_RESYNC:
2819           /* restart iteration */
2820           done = TRUE;
2821           name_found = FALSE;
2822           session = 0;
2823           break;
2824         case GST_ITERATOR_DONE:
2825           done = TRUE;
2826           break;
2827       }
2828     }
2829     gst_iterator_free (pad_it);
2830   }
2831
2832   GST_DEBUG_OBJECT (element, "free pad name found: '%s'", pad_name);
2833   return pad_name;
2834 }
2835
2836 /*
2837  */
2838 static GstPad *
2839 gst_rtp_bin_request_new_pad (GstElement * element,
2840     GstPadTemplate * templ, const gchar * name)
2841 {
2842   GstRtpBin *rtpbin;
2843   GstElementClass *klass;
2844   GstPad *result;
2845
2846   gchar *pad_name = NULL;
2847
2848   g_return_val_if_fail (templ != NULL, NULL);
2849   g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
2850
2851   rtpbin = GST_RTP_BIN (element);
2852   klass = GST_ELEMENT_GET_CLASS (element);
2853
2854   GST_RTP_BIN_LOCK (rtpbin);
2855
2856   if (name == NULL) {
2857     /* use a free pad name */
2858     pad_name = gst_rtp_bin_get_free_pad_name (element, templ);
2859   } else {
2860     /* use the provided name */
2861     pad_name = g_strdup (name);
2862   }
2863
2864   GST_DEBUG_OBJECT (rtpbin, "Trying to request a pad with name %s", pad_name);
2865
2866   /* figure out the template */
2867   if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
2868     result = create_recv_rtp (rtpbin, templ, pad_name);
2869   } else if (templ == gst_element_class_get_pad_template (klass,
2870           "recv_rtcp_sink_%d")) {
2871     result = create_recv_rtcp (rtpbin, templ, pad_name);
2872   } else if (templ == gst_element_class_get_pad_template (klass,
2873           "send_rtp_sink_%d")) {
2874     result = create_send_rtp (rtpbin, templ, pad_name);
2875   } else if (templ == gst_element_class_get_pad_template (klass,
2876           "send_rtcp_src_%d")) {
2877     result = create_rtcp (rtpbin, templ, pad_name);
2878   } else
2879     goto wrong_template;
2880
2881   g_free (pad_name);
2882   GST_RTP_BIN_UNLOCK (rtpbin);
2883
2884   return result;
2885
2886   /* ERRORS */
2887 wrong_template:
2888   {
2889     g_free (pad_name);
2890     GST_RTP_BIN_UNLOCK (rtpbin);
2891     g_warning ("gstrtpbin: this is not our template");
2892     return NULL;
2893   }
2894 }
2895
2896 static void
2897 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
2898 {
2899   GstRtpBinSession *session;
2900   GstRtpBin *rtpbin;
2901
2902   g_return_if_fail (GST_IS_GHOST_PAD (pad));
2903   g_return_if_fail (GST_IS_RTP_BIN (element));
2904
2905   rtpbin = GST_RTP_BIN (element);
2906
2907   GST_RTP_BIN_LOCK (rtpbin);
2908   GST_DEBUG_OBJECT (rtpbin, "Trying to release pad %s:%s",
2909       GST_DEBUG_PAD_NAME (pad));
2910
2911   if (!(session = find_session_by_pad (rtpbin, pad)))
2912     goto unknown_pad;
2913
2914   if (session->recv_rtp_sink_ghost == pad) {
2915     remove_recv_rtp (rtpbin, session);
2916   } else if (session->recv_rtcp_sink_ghost == pad) {
2917     remove_recv_rtcp (rtpbin, session);
2918   } else if (session->send_rtp_sink_ghost == pad) {
2919     remove_send_rtp (rtpbin, session);
2920   } else if (session->send_rtcp_src_ghost == pad) {
2921     remove_rtcp (rtpbin, session);
2922   }
2923
2924   /* no more request pads, free the complete session */
2925   if (session->recv_rtp_sink_ghost == NULL
2926       && session->recv_rtcp_sink_ghost == NULL
2927       && session->send_rtp_sink_ghost == NULL
2928       && session->send_rtcp_src_ghost == NULL) {
2929     GST_DEBUG_OBJECT (rtpbin, "no more pads for session %p", session);
2930     rtpbin->sessions = g_slist_remove (rtpbin->sessions, session);
2931     free_session (session, rtpbin);
2932   }
2933   GST_RTP_BIN_UNLOCK (rtpbin);
2934
2935   return;
2936
2937   /* ERROR */
2938 unknown_pad:
2939   {
2940     GST_RTP_BIN_UNLOCK (rtpbin);
2941     g_warning ("gstrtpbin: %s:%s is not one of our request pads",
2942         GST_DEBUG_PAD_NAME (pad));
2943     return;
2944   }
2945 }