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