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