gst/rtpmanager/gstrtpbin.c: Fix caps refcounting for payload maps.
[platform/upstream/gstreamer.git] / gst / rtpmanager / gstrtpbin.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim@fluendo.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  * @short_description: handle media from one RTP bin
23  * @see_also: gstrtpjitterbuffer, gstrtpsession, gstrtpptdemux, gstrtpssrcdemux
24  *
25  * <refsect2>
26  * <para>
27  * RTP bin combines the functions of gstrtpsession, gstrtpssrcdemux, gstrtpjitterbuffer
28  * and gstrtpptdemux in one element. It allows for multiple RTP sessions that will
29  * be synchronized together using RTCP SR packets.
30  * </para>
31  * <para>
32  * gstrtpbin is configured with a number of request pads that define the
33  * functionality that is activated, similar to the gstrtpsession element.
34  * </para>
35  * <para>
36  * To use gstrtpbin as an RTP receiver, request a recv_rtp_sink_%%d pad. The session
37  * number must be specified in the pad name. 
38  * Data received on the recv_rtp_sink_%%d pad will be processed in the gstrtpsession
39  * manager and after being validated forwarded on gstrtpssrcdemuxer element. Each
40  * RTP stream is demuxed based on the SSRC and send to a gstrtpjitterbuffer. After
41  * the packets are released from the jitterbuffer, they will be forwarded to a
42  * gstrtpptdemuxer element. The gstrtpptdemuxer element will demux the packets based
43  * on the payload type and will create a unique pad recv_rtp_src_%%d_%%d_%%d on
44  * gstrtpbin with the session number, SSRC and payload type respectively as the pad
45  * name.
46  * </para>
47  * <para>
48  * To also use gstrtpbin as an RTCP receiver, request a recv_rtcp_sink_%%d pad. The
49  * session number must be specified in the pad name.
50  * </para>
51  * <para>
52  * If you want the session manager to generate and send RTCP packets, request
53  * the send_rtcp_src_%%d pad with the session number in the pad name. Packet pushed
54  * on this pad contain SR/RR RTCP reports that should be sent to all participants
55  * in the session.
56  * </para>
57  * <para>
58  * To use gstrtpbin as a sender, request a send_rtp_sink_%%d pad, which will
59  * automatically create a send_rtp_src_%%d pad. The session number must be specified when
60  * requesting the sink pad. The session manager will modify the
61  * SSRC in the RTP packets to its own SSRC and wil forward the packets on the
62  * send_rtp_src_%%d pad after updating its internal state.
63  * </para>
64  * <para>
65  * The session manager needs the clock-rate of the payload types it is handling
66  * and will signal the GstRtpSession::request-pt-map signal when it needs such a
67  * mapping. One can clear the cached values with the GstRtpSession::clear-pt-map
68  * signal.
69  * </para>
70  * <title>Example pipelines</title>
71  * <para>
72  * <programlisting>
73  * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink_0 \
74  *     gstrtpbin ! rtptheoradepay ! theoradec ! xvimagesink
75  * </programlisting>
76  * Receive RTP data from port 5000 and send to the session 0 in gstrtpbin.
77  * </para>
78  * <para>
79  * <programlisting>
80  * gst-launch gstrtpbin name=rtpbin \
81  *         v4l2src ! ffmpegcolorspace ! ffenc_h263 ! rtph263ppay ! rtpbin.send_rtp_sink_0 \
82  *                   rtpbin.send_rtp_src_0 ! udpsink port=5000                            \
83  *                   rtpbin.send_rtcp_src_0 ! udpsink port=5001 sync=false async=false    \
84  *                   udpsrc port=5005 ! rtpbin.recv_rtcp_sink_0                           \
85  *         audiotestsrc ! amrnbenc ! rtpamrpay ! rtpbin.send_rtp_sink_1                   \
86  *                   rtpbin.send_rtp_src_1 ! udpsink port=5002                            \
87  *                   rtpbin.send_rtcp_src_1 ! udpsink port=5003 sync=false async=false    \
88  *                   udpsrc port=5007 ! rtpbin.recv_rtcp_sink_1
89  * </programlisting>
90  * Encode and payload H263 video captured from a v4l2src. Encode and payload AMR
91  * audio generated from audiotestsrc. The video is sent to session 0 in rtpbin
92  * and the audio is sent to session 1. Video packets are sent on UDP port 5000
93  * and audio packets on port 5002. The video RTCP packets for session 0 are sent
94  * on port 5001 and the audio RTCP packets for session 0 are sent on port 5003.
95  * RTCP packets for session 0 are received on port 5005 and RTCP for session 1
96  * is received on port 5007. Since RTCP packets from the sender should be sent
97  * as soon as possible and do not participate in preroll, sync=false and 
98  * async=false is configured on udpsink
99  * </para>
100  * <para>
101  * <programlisting>
102  *  gst-launch -v gstrtpbin name=rtpbin                                          \
103  *     udpsrc caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H263-1998" \
104  *             port=5000 ! rtpbin.recv_rtp_sink_0                                \
105  *         rtpbin. ! rtph263pdepay ! ffdec_h263 ! xvimagesink                    \
106  *      udpsrc port=5001 ! rtpbin.recv_rtcp_sink_0                               \
107  *      rtpbin.send_rtcp_src_0 ! udpsink port=5005 sync=false async=false        \
108  *     udpsrc caps="application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)AMR,encoding-params=(string)1,octet-align=(string)1" \
109  *             port=5002 ! rtpbin.recv_rtp_sink_1                                \
110  *         rtpbin. ! rtpamrdepay ! amrnbdec ! alsasink                           \
111  *      udpsrc port=5003 ! rtpbin.recv_rtcp_sink_1                               \
112  *      rtpbin.send_rtcp_src_1 ! udpsink port=5007 sync=false async=false
113  * </programlisting>
114  * Receive H263 on port 5000, send it through rtpbin in session 0, depayload,
115  * decode and display the video.
116  * Receive AMR on port 5002, send it through rtpbin in session 1, depayload,
117  * decode and play the audio.
118  * Receive server RTCP packets for session 0 on port 5001 and RTCP packets for
119  * session 1 on port 5003. These packets will be used for session management and
120  * synchronisation.
121  * Send RTCP reports for session 0 on port 5005 and RTCP reports for session 1
122  * on port 5007.
123  * </para>
124  * </refsect2>
125  *
126  * Last reviewed on 2007-08-30 (0.10.6)
127  */
128
129 #ifdef HAVE_CONFIG_H
130 #include "config.h"
131 #endif
132 #include <string.h>
133
134 #include <gst/rtp/gstrtpbuffer.h>
135 #include <gst/rtp/gstrtcpbuffer.h>
136
137 #include "gstrtpbin-marshal.h"
138 #include "gstrtpbin.h"
139
140 GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
141 #define GST_CAT_DEFAULT gst_rtp_bin_debug
142
143 /* elementfactory information */
144 static const GstElementDetails rtpbin_details = GST_ELEMENT_DETAILS ("RTP Bin",
145     "Filter/Network/RTP",
146     "Implement an RTP bin",
147     "Wim Taymans <wim@fluendo.com>");
148
149 /* sink pads */
150 static GstStaticPadTemplate rtpbin_recv_rtp_sink_template =
151 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink_%d",
152     GST_PAD_SINK,
153     GST_PAD_REQUEST,
154     GST_STATIC_CAPS ("application/x-rtp")
155     );
156
157 static GstStaticPadTemplate rtpbin_recv_rtcp_sink_template =
158 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink_%d",
159     GST_PAD_SINK,
160     GST_PAD_REQUEST,
161     GST_STATIC_CAPS ("application/x-rtcp")
162     );
163
164 static GstStaticPadTemplate rtpbin_send_rtp_sink_template =
165 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink_%d",
166     GST_PAD_SINK,
167     GST_PAD_REQUEST,
168     GST_STATIC_CAPS ("application/x-rtp")
169     );
170
171 /* src pads */
172 static GstStaticPadTemplate rtpbin_recv_rtp_src_template =
173 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src_%d_%d_%d",
174     GST_PAD_SRC,
175     GST_PAD_SOMETIMES,
176     GST_STATIC_CAPS ("application/x-rtp")
177     );
178
179 static GstStaticPadTemplate rtpbin_send_rtcp_src_template =
180 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src_%d",
181     GST_PAD_SRC,
182     GST_PAD_REQUEST,
183     GST_STATIC_CAPS ("application/x-rtcp")
184     );
185
186 static GstStaticPadTemplate rtpbin_send_rtp_src_template =
187 GST_STATIC_PAD_TEMPLATE ("send_rtp_src_%d",
188     GST_PAD_SRC,
189     GST_PAD_SOMETIMES,
190     GST_STATIC_CAPS ("application/x-rtp")
191     );
192
193 /* padtemplate for the internal pad */
194 static GstStaticPadTemplate rtpbin_sync_sink_template =
195 GST_STATIC_PAD_TEMPLATE ("sink_%d",
196     GST_PAD_SINK,
197     GST_PAD_SOMETIMES,
198     GST_STATIC_CAPS ("application/x-rtcp")
199     );
200
201 #define GST_RTP_BIN_GET_PRIVATE(obj)  \
202    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_BIN, GstRtpBinPrivate))
203
204 #define GST_RTP_BIN_LOCK(bin)   g_mutex_lock ((bin)->priv->bin_lock)
205 #define GST_RTP_BIN_UNLOCK(bin) g_mutex_unlock ((bin)->priv->bin_lock)
206
207 struct _GstRtpBinPrivate
208 {
209   GMutex *bin_lock;
210
211   GstClockTime ntp_ns_base;
212 };
213
214 /* signals and args */
215 enum
216 {
217   SIGNAL_REQUEST_PT_MAP,
218   SIGNAL_CLEAR_PT_MAP,
219
220   SIGNAL_ON_NEW_SSRC,
221   SIGNAL_ON_SSRC_COLLISION,
222   SIGNAL_ON_SSRC_VALIDATED,
223   SIGNAL_ON_SSRC_ACTIVE,
224   SIGNAL_ON_BYE_SSRC,
225   SIGNAL_ON_BYE_TIMEOUT,
226   SIGNAL_ON_TIMEOUT,
227   LAST_SIGNAL
228 };
229
230 #define DEFAULT_LATENCY_MS      200
231
232 enum
233 {
234   PROP_0,
235   PROP_LATENCY
236 };
237
238 /* helper objects */
239 typedef struct _GstRtpBinSession GstRtpBinSession;
240 typedef struct _GstRtpBinStream GstRtpBinStream;
241 typedef struct _GstRtpBinClient GstRtpBinClient;
242
243 static guint gst_rtp_bin_signals[LAST_SIGNAL] = { 0 };
244
245 static GstCaps *pt_map_requested (GstElement * element, guint pt,
246     GstRtpBinSession * session);
247
248 static void free_stream (GstRtpBinStream * stream);
249
250 /* Manages the RTP stream for one SSRC.
251  *
252  * We pipe the stream (comming from the SSRC demuxer) into a jitterbuffer.
253  * If we see an SDES RTCP packet that links multiple SSRCs together based on a
254  * common CNAME, we create a GstRtpBinClient structure to group the SSRCs
255  * together (see below).
256  */
257 struct _GstRtpBinStream
258 {
259   /* the SSRC of this stream */
260   guint32 ssrc;
261
262   /* parent bin */
263   GstRtpBin *bin;
264
265   /* the session this SSRC belongs to */
266   GstRtpBinSession *session;
267
268   /* the jitterbuffer of the SSRC */
269   GstElement *buffer;
270
271   /* the PT demuxer of the SSRC */
272   GstElement *demux;
273   gulong demux_newpad_sig;
274   gulong demux_ptreq_sig;
275
276   /* the internal pad we use to get RTCP sync messages */
277   GstPad *sync_pad;
278   gboolean have_sync;
279   guint64 last_unix;
280   guint64 last_extrtptime;
281
282   /* mapping to local RTP and NTP time */
283   guint64 local_rtp;
284   guint64 local_unix;
285   gint64 unix_delta;
286
287   /* for lip-sync */
288   guint64 clock_base;
289   gint clock_rate;
290   gint64 ts_offset;
291   gint64 prev_ts_offset;
292 };
293
294 #define GST_RTP_SESSION_LOCK(sess)   g_mutex_lock ((sess)->lock)
295 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock ((sess)->lock)
296
297 /* Manages the receiving end of the packets.
298  *
299  * There is one such structure for each RTP session (audio/video/...).
300  * We get the RTP/RTCP packets and stuff them into the session manager. From
301  * there they are pushed into an SSRC demuxer that splits the stream based on
302  * SSRC. Each of the SSRC streams go into their own jitterbuffer (managed with
303  * the GstRtpBinStream above).
304  */
305 struct _GstRtpBinSession
306 {
307   /* session id */
308   gint id;
309   /* the parent bin */
310   GstRtpBin *bin;
311   /* the session element */
312   GstElement *session;
313   /* the SSRC demuxer */
314   GstElement *demux;
315   gulong demux_newpad_sig;
316
317   GMutex *lock;
318
319   /* list of GstRtpBinStream */
320   GSList *streams;
321
322   /* mapping of payload type to caps */
323   GHashTable *ptmap;
324
325   /* the pads of the session */
326   GstPad *recv_rtp_sink;
327   GstPad *recv_rtp_src;
328   GstPad *recv_rtcp_sink;
329   GstPad *sync_src;
330   GstPad *send_rtp_sink;
331   GstPad *send_rtp_src;
332   GstPad *send_rtcp_src;
333 };
334
335 /* Manages the RTP streams that come from one client and should therefore be
336  * synchronized.
337  */
338 struct _GstRtpBinClient
339 {
340   /* the common CNAME for the streams */
341   gchar *cname;
342   guint cname_len;
343
344   /* the streams */
345   guint nstreams;
346   GSList *streams;
347
348   gint64 min_delta;
349 };
350
351 /* find a session with the given id. Must be called with RTP_BIN_LOCK */
352 static GstRtpBinSession *
353 find_session_by_id (GstRtpBin * rtpbin, gint id)
354 {
355   GSList *walk;
356
357   for (walk = rtpbin->sessions; walk; walk = g_slist_next (walk)) {
358     GstRtpBinSession *sess = (GstRtpBinSession *) walk->data;
359
360     if (sess->id == id)
361       return sess;
362   }
363   return NULL;
364 }
365
366 static void
367 on_new_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
368 {
369   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC], 0,
370       sess->id, ssrc);
371 }
372
373 static void
374 on_ssrc_collision (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
375 {
376   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION], 0,
377       sess->id, ssrc);
378 }
379
380 static void
381 on_ssrc_validated (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
382 {
383   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
384       sess->id, ssrc);
385 }
386
387 static void
388 on_ssrc_active (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
389 {
390   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
391       sess->id, ssrc);
392 }
393
394 static void
395 on_bye_ssrc (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
396 {
397   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC], 0,
398       sess->id, ssrc);
399 }
400
401 static void
402 on_bye_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
403 {
404   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
405       sess->id, ssrc);
406 }
407
408 static void
409 on_timeout (GstElement * session, guint32 ssrc, GstRtpBinSession * sess)
410 {
411   g_signal_emit (sess->bin, gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT], 0,
412       sess->id, ssrc);
413 }
414
415 /* create a session with the given id.  Must be called with RTP_BIN_LOCK */
416 static GstRtpBinSession *
417 create_session (GstRtpBin * rtpbin, gint id)
418 {
419   GstRtpBinSession *sess;
420   GstElement *session, *demux;
421
422   if (!(session = gst_element_factory_make ("gstrtpsession", NULL)))
423     goto no_session;
424
425   if (!(demux = gst_element_factory_make ("gstrtpssrcdemux", NULL)))
426     goto no_demux;
427
428   sess = g_new0 (GstRtpBinSession, 1);
429   sess->lock = g_mutex_new ();
430   sess->id = id;
431   sess->bin = rtpbin;
432   sess->session = session;
433   sess->demux = demux;
434   sess->ptmap = g_hash_table_new (NULL, NULL);
435   rtpbin->sessions = g_slist_prepend (rtpbin->sessions, sess);
436
437   /* set NTP base or new session */
438   g_object_set (session, "ntp-ns-base", rtpbin->priv->ntp_ns_base, NULL);
439
440   /* provide clock_rate to the session manager when needed */
441   g_signal_connect (session, "request-pt-map",
442       (GCallback) pt_map_requested, sess);
443
444   g_signal_connect (sess->session, "on-new-ssrc",
445       (GCallback) on_new_ssrc, sess);
446   g_signal_connect (sess->session, "on-ssrc-collision",
447       (GCallback) on_ssrc_collision, sess);
448   g_signal_connect (sess->session, "on-ssrc-validated",
449       (GCallback) on_ssrc_validated, sess);
450   g_signal_connect (sess->session, "on-ssrc-active",
451       (GCallback) on_ssrc_active, sess);
452   g_signal_connect (sess->session, "on-bye-ssrc",
453       (GCallback) on_bye_ssrc, sess);
454   g_signal_connect (sess->session, "on-bye-timeout",
455       (GCallback) on_bye_timeout, sess);
456   g_signal_connect (sess->session, "on-timeout", (GCallback) on_timeout, sess);
457
458   gst_bin_add (GST_BIN_CAST (rtpbin), session);
459   gst_element_set_state (session, GST_STATE_PLAYING);
460   gst_bin_add (GST_BIN_CAST (rtpbin), demux);
461   gst_element_set_state (demux, GST_STATE_PLAYING);
462
463   return sess;
464
465   /* ERRORS */
466 no_session:
467   {
468     g_warning ("gstrtpbin: could not create gstrtpsession element");
469     return NULL;
470   }
471 no_demux:
472   {
473     gst_object_unref (session);
474     g_warning ("gstrtpbin: could not create gstrtpssrcdemux element");
475     return NULL;
476   }
477 }
478
479 static void
480 free_session (GstRtpBinSession * sess)
481 {
482   GstRtpBin *bin;
483
484   bin = sess->bin;
485
486   gst_element_set_state (sess->session, GST_STATE_NULL);
487   gst_element_set_state (sess->demux, GST_STATE_NULL);
488
489   gst_bin_remove (GST_BIN_CAST (bin), sess->session);
490   gst_bin_remove (GST_BIN_CAST (bin), sess->demux);
491
492   g_slist_foreach (sess->streams, (GFunc) free_stream, NULL);
493   g_slist_free (sess->streams);
494
495   g_mutex_free (sess->lock);
496   g_hash_table_destroy (sess->ptmap);
497
498   bin->sessions = g_slist_remove (bin->sessions, sess);
499
500   g_free (sess);
501 }
502
503 #if 0
504 static GstRtpBinStream *
505 find_stream_by_ssrc (GstRtpBinSession * session, guint32 ssrc)
506 {
507   GSList *walk;
508
509   for (walk = session->streams; walk; walk = g_slist_next (walk)) {
510     GstRtpBinStream *stream = (GstRtpBinStream *) walk->data;
511
512     if (stream->ssrc == ssrc)
513       return stream;
514   }
515   return NULL;
516 }
517 #endif
518
519 /* get the payload type caps for the specific payload @pt in @session */
520 static GstCaps *
521 get_pt_map (GstRtpBinSession * session, guint pt)
522 {
523   GstCaps *caps = NULL;
524   GstRtpBin *bin;
525   GValue ret = { 0 };
526   GValue args[3] = { {0}, {0}, {0} };
527
528   GST_DEBUG ("searching pt %d in cache", pt);
529
530   GST_RTP_SESSION_LOCK (session);
531
532   /* first look in the cache */
533   caps = g_hash_table_lookup (session->ptmap, GINT_TO_POINTER (pt));
534   if (caps)
535     goto done;
536
537   bin = session->bin;
538
539   GST_DEBUG ("emiting signal for pt %d in session %d", pt, session->id);
540
541   /* not in cache, send signal to request caps */
542   g_value_init (&args[0], GST_TYPE_ELEMENT);
543   g_value_set_object (&args[0], bin);
544   g_value_init (&args[1], G_TYPE_UINT);
545   g_value_set_uint (&args[1], session->id);
546   g_value_init (&args[2], G_TYPE_UINT);
547   g_value_set_uint (&args[2], pt);
548
549   g_value_init (&ret, GST_TYPE_CAPS);
550   g_value_set_boxed (&ret, NULL);
551
552   g_signal_emitv (args, gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP], 0, &ret);
553
554   caps = (GstCaps *) g_value_get_boxed (&ret);
555   if (!caps)
556     goto no_caps;
557
558   GST_DEBUG ("caching pt %d as %" GST_PTR_FORMAT, pt, caps);
559
560   /* store in cache */
561   g_hash_table_insert (session->ptmap, GINT_TO_POINTER (pt), caps);
562
563 done:
564   gst_caps_ref (caps);
565   GST_RTP_SESSION_UNLOCK (session);
566
567   return caps;
568
569   /* ERRORS */
570 no_caps:
571   {
572     GST_RTP_SESSION_UNLOCK (session);
573     GST_DEBUG ("no pt map could be obtained");
574     return NULL;
575   }
576 }
577
578 static gboolean
579 return_true (gpointer key, gpointer value, gpointer user_data)
580 {
581   return TRUE;
582 }
583
584 static void
585 gst_rtp_bin_clear_pt_map (GstRtpBin * bin)
586 {
587   GSList *sessions, *streams;
588
589   GST_RTP_BIN_LOCK (bin);
590   GST_DEBUG_OBJECT (bin, "clearing pt map");
591   for (sessions = bin->sessions; sessions; sessions = g_slist_next (sessions)) {
592     GstRtpBinSession *session = (GstRtpBinSession *) sessions->data;
593
594     GST_DEBUG_OBJECT (bin, "clearing session %p", session);
595     g_signal_emit_by_name (session->session, "clear-pt-map", NULL);
596
597     GST_RTP_SESSION_LOCK (session);
598     g_hash_table_foreach_remove (session->ptmap, return_true, NULL);
599
600     for (streams = session->streams; streams; streams = g_slist_next (streams)) {
601       GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
602
603       GST_DEBUG_OBJECT (bin, "clearing stream %p", stream);
604       g_signal_emit_by_name (stream->buffer, "clear-pt-map", NULL);
605       g_signal_emit_by_name (stream->demux, "clear-pt-map", NULL);
606     }
607     GST_RTP_SESSION_UNLOCK (session);
608   }
609   GST_RTP_BIN_UNLOCK (bin);
610 }
611
612 static GstRtpBinClient *
613 get_client (GstRtpBin * bin, guint8 len, guint8 * data, gboolean * created)
614 {
615   GstRtpBinClient *result = NULL;
616   GSList *walk;
617
618   for (walk = bin->clients; walk; walk = g_slist_next (walk)) {
619     GstRtpBinClient *client = (GstRtpBinClient *) walk->data;
620
621     if (len != client->cname_len)
622       continue;
623
624     if (!strncmp ((gchar *) data, client->cname, client->cname_len)) {
625       GST_DEBUG_OBJECT (bin, "found existing client %p with CNAME %s", client,
626           client->cname);
627       result = client;
628       break;
629     }
630   }
631
632   /* nothing found, create one */
633   if (result == NULL) {
634     result = g_new0 (GstRtpBinClient, 1);
635     result->cname = g_strndup ((gchar *) data, len);
636     result->cname_len = len;
637     result->min_delta = G_MAXINT64;
638     bin->clients = g_slist_prepend (bin->clients, result);
639     GST_DEBUG_OBJECT (bin, "created new client %p with CNAME %s", result,
640         result->cname);
641   }
642   return result;
643 }
644
645 static void
646 free_client (GstRtpBinClient * client)
647 {
648   g_free (client->cname);
649   g_free (client);
650 }
651
652 /* associate a stream to the given CNAME. This will make sure all streams for
653  * that CNAME are synchronized together. */
654 static void
655 gst_rtp_bin_associate (GstRtpBin * bin, GstRtpBinStream * stream, guint8 len,
656     guint8 * data)
657 {
658   GstRtpBinClient *client;
659   gboolean created;
660   GSList *walk;
661
662   /* first find or create the CNAME */
663   client = get_client (bin, len, data, &created);
664
665   /* find stream in the client */
666   for (walk = client->streams; walk; walk = g_slist_next (walk)) {
667     GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
668
669     if (ostream == stream)
670       break;
671   }
672   /* not found, add it to the list */
673   if (walk == NULL) {
674     GST_DEBUG_OBJECT (bin,
675         "new association of SSRC %08x with client %p with CNAME %s",
676         stream->ssrc, client, client->cname);
677     client->streams = g_slist_prepend (client->streams, stream);
678     client->nstreams++;
679   } else {
680     GST_DEBUG_OBJECT (bin,
681         "found association of SSRC %08x with client %p with CNAME %s",
682         stream->ssrc, client, client->cname);
683   }
684
685   /* we can only continue if we know the local clock-base and clock-rate */
686   if (stream->clock_base == -1)
687     goto no_clock_base;
688   if (stream->clock_rate <= 0)
689     goto no_clock_rate;
690
691   /* map last RTP time to local timeline using our clock-base */
692   stream->local_rtp = stream->last_extrtptime - stream->clock_base;
693
694   GST_DEBUG_OBJECT (bin,
695       "base %" G_GUINT64_FORMAT ", extrtptime %" G_GUINT64_FORMAT
696       ", local RTP %" G_GUINT64_FORMAT ", clock-rate %d", stream->clock_base,
697       stream->last_extrtptime, stream->local_rtp, stream->clock_rate);
698
699   /* calculate local NTP time in gstreamer timestamp */
700   stream->local_unix =
701       gst_util_uint64_scale_int (stream->local_rtp, GST_SECOND,
702       stream->clock_rate);
703   /* calculate delta between server and receiver */
704   stream->unix_delta = stream->last_unix - stream->local_unix;
705
706   GST_DEBUG_OBJECT (bin,
707       "local UNIX %" G_GUINT64_FORMAT ", remote UNIX %" G_GUINT64_FORMAT
708       ", delta %" G_GINT64_FORMAT, stream->local_unix, stream->last_unix,
709       stream->unix_delta);
710
711   /* recalc inter stream playout offset, but only if there are more than one
712    * stream. */
713   if (client->nstreams > 1) {
714     gint64 min;
715
716     /* calculate the min of all deltas */
717     min = G_MAXINT64;
718     for (walk = client->streams; walk; walk = g_slist_next (walk)) {
719       GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
720
721       if (ostream->unix_delta < min)
722         min = ostream->unix_delta;
723     }
724
725     GST_DEBUG_OBJECT (bin, "client %p min delta %" G_GINT64_FORMAT, client,
726         min);
727
728     /* calculate offsets for each stream */
729     for (walk = client->streams; walk; walk = g_slist_next (walk)) {
730       GstRtpBinStream *ostream = (GstRtpBinStream *) walk->data;
731
732       ostream->ts_offset = ostream->unix_delta - min;
733
734       /* delta changed, see how much */
735       if (ostream->prev_ts_offset != ostream->ts_offset) {
736         gint64 diff;
737
738         if (ostream->prev_ts_offset > ostream->ts_offset)
739           diff = ostream->prev_ts_offset - ostream->ts_offset;
740         else
741           diff = ostream->ts_offset - ostream->prev_ts_offset;
742
743         /* only change diff when it changed more than 1 millisecond. This
744          * compensates for rounding errors in NTP to RTP timestamp
745          * conversions */
746         if (diff > GST_MSECOND)
747           g_object_set (ostream->buffer, "ts-offset", ostream->ts_offset, NULL);
748
749         ostream->prev_ts_offset = ostream->ts_offset;
750       }
751       GST_DEBUG_OBJECT (bin, "stream SSRC %08x, delta %" G_GINT64_FORMAT,
752           ostream->ssrc, ostream->ts_offset);
753     }
754   }
755   return;
756
757 no_clock_base:
758   {
759     GST_WARNING_OBJECT (bin, "we have no clock-base");
760     return;
761   }
762 no_clock_rate:
763   {
764     GST_WARNING_OBJECT (bin, "we have no clock-rate");
765     return;
766   }
767 }
768
769 #define GST_RTCP_BUFFER_FOR_PACKETS(b,buffer,packet) \
770   for ((b) = gst_rtcp_buffer_get_first_packet ((buffer), (packet)); (b); \
771           (b) = gst_rtcp_packet_move_to_next ((packet)))
772
773 #define GST_RTCP_SDES_FOR_ITEMS(b,packet) \
774   for ((b) = gst_rtcp_packet_sdes_first_item ((packet)); (b); \
775           (b) = gst_rtcp_packet_sdes_next_item ((packet)))
776
777 #define GST_RTCP_SDES_FOR_ENTRIES(b,packet) \
778   for ((b) = gst_rtcp_packet_sdes_first_entry ((packet)); (b); \
779           (b) = gst_rtcp_packet_sdes_next_entry ((packet)))
780
781 static GstFlowReturn
782 gst_rtp_bin_sync_chain (GstPad * pad, GstBuffer * buffer)
783 {
784   GstFlowReturn ret = GST_FLOW_OK;
785   GstRtpBinStream *stream;
786   GstRtpBin *bin;
787   GstRTCPPacket packet;
788   guint32 ssrc;
789   guint64 ntptime;
790   guint32 rtptime;
791   gboolean have_sr, have_sdes;
792   gboolean more;
793
794   stream = gst_pad_get_element_private (pad);
795   bin = stream->bin;
796
797   GST_DEBUG_OBJECT (bin, "received sync packet");
798
799   if (!gst_rtcp_buffer_validate (buffer))
800     goto invalid_rtcp;
801
802   have_sr = FALSE;
803   have_sdes = FALSE;
804   GST_RTCP_BUFFER_FOR_PACKETS (more, buffer, &packet) {
805     /* first packet must be SR or RR or else the validate would have failed */
806     switch (gst_rtcp_packet_get_type (&packet)) {
807       case GST_RTCP_TYPE_SR:
808         /* only parse first. There is only supposed to be one SR in the packet
809          * but we will deal with malformed packets gracefully */
810         if (have_sr)
811           break;
812         /* get NTP and RTP times */
813         gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, &ntptime, &rtptime,
814             NULL, NULL);
815
816         GST_DEBUG_OBJECT (bin, "received sync packet from SSRC %08x", ssrc);
817         /* ignore SR that is not ours */
818         if (ssrc != stream->ssrc)
819           continue;
820
821         have_sr = TRUE;
822
823         /* store values in the stream */
824         stream->have_sync = TRUE;
825         stream->last_unix = gst_rtcp_ntp_to_unix (ntptime);
826         /* use extended timestamp */
827         gst_rtp_buffer_ext_timestamp (&stream->last_extrtptime, rtptime);
828         break;
829       case GST_RTCP_TYPE_SDES:
830       {
831         gboolean more_items, more_entries;
832
833         /* only deal with first SDES, there is only supposed to be one SDES in
834          * the RTCP packet but we deal with bad packets gracefully. Also bail
835          * out if we have not seen an SR item yet. */
836         if (have_sdes || !have_sr)
837           break;
838
839         GST_RTCP_SDES_FOR_ITEMS (more_items, &packet) {
840           /* skip items that are not about the SSRC of the sender */
841           if (gst_rtcp_packet_sdes_get_ssrc (&packet) != ssrc)
842             continue;
843
844           /* find the CNAME entry */
845           GST_RTCP_SDES_FOR_ENTRIES (more_entries, &packet) {
846             GstRTCPSDESType type;
847             guint8 len;
848             guint8 *data;
849
850             gst_rtcp_packet_sdes_get_entry (&packet, &type, &len, &data);
851
852             if (type == GST_RTCP_SDES_CNAME) {
853               stream->clock_base = GST_BUFFER_OFFSET (buffer);
854               /* associate the stream to CNAME */
855               gst_rtp_bin_associate (bin, stream, len, data);
856             }
857           }
858         }
859         have_sdes = TRUE;
860         break;
861       }
862       default:
863         /* we can ignore these packets */
864         break;
865     }
866   }
867
868   gst_buffer_unref (buffer);
869
870   return ret;
871
872   /* ERRORS */
873 invalid_rtcp:
874   {
875     /* this is fatal and should be filtered earlier */
876     GST_ELEMENT_ERROR (bin, STREAM, DECODE, (NULL),
877         ("invalid RTCP packet received"));
878     gst_buffer_unref (buffer);
879     return GST_FLOW_ERROR;
880   }
881 }
882
883 /* create a new stream with @ssrc in @session. Must be called with
884  * RTP_SESSION_LOCK. */
885 static GstRtpBinStream *
886 create_stream (GstRtpBinSession * session, guint32 ssrc)
887 {
888   GstElement *buffer, *demux;
889   GstRtpBinStream *stream;
890   GstPadTemplate *templ;
891   gchar *padname;
892
893   if (!(buffer = gst_element_factory_make ("gstrtpjitterbuffer", NULL)))
894     goto no_jitterbuffer;
895
896   if (!(demux = gst_element_factory_make ("gstrtpptdemux", NULL)))
897     goto no_demux;
898
899   stream = g_new0 (GstRtpBinStream, 1);
900   stream->ssrc = ssrc;
901   stream->bin = session->bin;
902   stream->session = session;
903   stream->buffer = buffer;
904   stream->demux = demux;
905   stream->last_extrtptime = -1;
906   stream->have_sync = FALSE;
907   session->streams = g_slist_prepend (session->streams, stream);
908
909   /* make an internal sinkpad for RTCP sync packets. Take ownership of the
910    * pad. We will link this pad later. */
911   padname = g_strdup_printf ("sync_%d", ssrc);
912   templ = gst_static_pad_template_get (&rtpbin_sync_sink_template);
913   stream->sync_pad = gst_pad_new_from_template (templ, padname);
914   gst_object_unref (templ);
915   gst_object_ref (stream->sync_pad);
916   gst_object_sink (stream->sync_pad);
917   gst_pad_set_element_private (stream->sync_pad, stream);
918   gst_pad_set_chain_function (stream->sync_pad, gst_rtp_bin_sync_chain);
919   gst_pad_set_active (stream->sync_pad, TRUE);
920
921   /* provide clock_rate to the jitterbuffer when needed */
922   g_signal_connect (buffer, "request-pt-map",
923       (GCallback) pt_map_requested, session);
924
925   /* configure latency */
926   g_object_set (buffer, "latency", session->bin->latency, NULL);
927
928   gst_bin_add (GST_BIN_CAST (session->bin), buffer);
929   gst_element_set_state (buffer, GST_STATE_PLAYING);
930   gst_bin_add (GST_BIN_CAST (session->bin), demux);
931   gst_element_set_state (demux, GST_STATE_PLAYING);
932
933   /* link stuff */
934   gst_element_link (buffer, demux);
935
936   return stream;
937
938   /* ERRORS */
939 no_jitterbuffer:
940   {
941     g_warning ("gstrtpbin: could not create gstrtpjitterbuffer element");
942     return NULL;
943   }
944 no_demux:
945   {
946     gst_object_unref (buffer);
947     g_warning ("gstrtpbin: could not create gstrtpptdemux element");
948     return NULL;
949   }
950 }
951
952 static void
953 free_stream (GstRtpBinStream * stream)
954 {
955   GstRtpBinSession *session;
956
957   session = stream->session;
958
959   gst_element_set_state (stream->buffer, GST_STATE_NULL);
960   gst_element_set_state (stream->demux, GST_STATE_NULL);
961
962   gst_bin_remove (GST_BIN_CAST (session->bin), stream->buffer);
963   gst_bin_remove (GST_BIN_CAST (session->bin), stream->demux);
964
965   gst_object_unref (stream->sync_pad);
966
967   session->streams = g_slist_remove (session->streams, stream);
968
969   g_free (stream);
970 }
971
972 /* GObject vmethods */
973 static void gst_rtp_bin_dispose (GObject * object);
974 static void gst_rtp_bin_finalize (GObject * object);
975 static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
976     const GValue * value, GParamSpec * pspec);
977 static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
978     GValue * value, GParamSpec * pspec);
979
980 /* GstElement vmethods */
981 static GstClock *gst_rtp_bin_provide_clock (GstElement * element);
982 static GstStateChangeReturn gst_rtp_bin_change_state (GstElement * element,
983     GstStateChange transition);
984 static GstPad *gst_rtp_bin_request_new_pad (GstElement * element,
985     GstPadTemplate * templ, const gchar * name);
986 static void gst_rtp_bin_release_pad (GstElement * element, GstPad * pad);
987 static void gst_rtp_bin_clear_pt_map (GstRtpBin * bin);
988
989 GST_BOILERPLATE (GstRtpBin, gst_rtp_bin, GstBin, GST_TYPE_BIN);
990
991 static void
992 gst_rtp_bin_base_init (gpointer klass)
993 {
994   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
995
996   /* sink pads */
997   gst_element_class_add_pad_template (element_class,
998       gst_static_pad_template_get (&rtpbin_recv_rtp_sink_template));
999   gst_element_class_add_pad_template (element_class,
1000       gst_static_pad_template_get (&rtpbin_recv_rtcp_sink_template));
1001   gst_element_class_add_pad_template (element_class,
1002       gst_static_pad_template_get (&rtpbin_send_rtp_sink_template));
1003
1004   /* src pads */
1005   gst_element_class_add_pad_template (element_class,
1006       gst_static_pad_template_get (&rtpbin_recv_rtp_src_template));
1007   gst_element_class_add_pad_template (element_class,
1008       gst_static_pad_template_get (&rtpbin_send_rtcp_src_template));
1009   gst_element_class_add_pad_template (element_class,
1010       gst_static_pad_template_get (&rtpbin_send_rtp_src_template));
1011
1012   gst_element_class_set_details (element_class, &rtpbin_details);
1013 }
1014
1015 static void
1016 gst_rtp_bin_class_init (GstRtpBinClass * klass)
1017 {
1018   GObjectClass *gobject_class;
1019   GstElementClass *gstelement_class;
1020
1021   gobject_class = (GObjectClass *) klass;
1022   gstelement_class = (GstElementClass *) klass;
1023
1024   g_type_class_add_private (klass, sizeof (GstRtpBinPrivate));
1025
1026   gobject_class->dispose = gst_rtp_bin_dispose;
1027   gobject_class->finalize = gst_rtp_bin_finalize;
1028   gobject_class->set_property = gst_rtp_bin_set_property;
1029   gobject_class->get_property = gst_rtp_bin_get_property;
1030
1031   g_object_class_install_property (gobject_class, PROP_LATENCY,
1032       g_param_spec_uint ("latency", "Buffer latency in ms",
1033           "Default amount of ms to buffer in the jitterbuffers", 0,
1034           G_MAXUINT, DEFAULT_LATENCY_MS, G_PARAM_READWRITE));
1035
1036   /**
1037    * GstRtpBin::request-pt-map:
1038    * @rtpbin: the object which received the signal
1039    * @session: the session
1040    * @pt: the pt
1041    *
1042    * Request the payload type as #GstCaps for @pt in @session.
1043    */
1044   gst_rtp_bin_signals[SIGNAL_REQUEST_PT_MAP] =
1045       g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
1046       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, request_pt_map),
1047       NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT_UINT, GST_TYPE_CAPS, 2,
1048       G_TYPE_UINT, G_TYPE_UINT);
1049   /**
1050    * GstRtpBin::clear-pt-map:
1051    * @rtpbin: the object which received the signal
1052    *
1053    * Clear all previously cached pt-mapping obtained with
1054    * GstRtpBin::request-pt-map.
1055    */
1056   gst_rtp_bin_signals[SIGNAL_CLEAR_PT_MAP] =
1057       g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
1058       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpBinClass,
1059           clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
1060       0, G_TYPE_NONE);
1061
1062   /**
1063    * GstRtpBin::on-new-ssrc:
1064    * @rtpbin: the object which received the signal
1065    * @session: the session
1066    * @ssrc: the SSRC 
1067    *
1068    * Notify of a new SSRC that entered @session.
1069    */
1070   gst_rtp_bin_signals[SIGNAL_ON_NEW_SSRC] =
1071       g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
1072       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_new_ssrc),
1073       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1074       G_TYPE_UINT, G_TYPE_UINT);
1075   /**
1076    * GstRtpBin::on-ssrc_collision:
1077    * @rtpbin: the object which received the signal
1078    * @session: the session
1079    * @ssrc: the SSRC 
1080    *
1081    * Notify when we have an SSRC collision
1082    */
1083   gst_rtp_bin_signals[SIGNAL_ON_SSRC_COLLISION] =
1084       g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
1085       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_collision),
1086       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1087       G_TYPE_UINT, G_TYPE_UINT);
1088   /**
1089    * GstRtpBin::on-ssrc_validated:
1090    * @rtpbin: the object which received the signal
1091    * @session: the session
1092    * @ssrc: the SSRC 
1093    *
1094    * Notify of a new SSRC that became validated.
1095    */
1096   gst_rtp_bin_signals[SIGNAL_ON_SSRC_VALIDATED] =
1097       g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
1098       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_validated),
1099       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1100       G_TYPE_UINT, G_TYPE_UINT);
1101   /**
1102    * GstRtpBin::on-ssrc_active:
1103    * @rtpbin: the object which received the signal
1104    * @session: the session
1105    * @ssrc: the SSRC
1106    *
1107    * Notify of a SSRC that is active, i.e., sending RTCP.
1108    */
1109   gst_rtp_bin_signals[SIGNAL_ON_SSRC_ACTIVE] =
1110       g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
1111       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_ssrc_active),
1112       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1113       G_TYPE_UINT, G_TYPE_UINT);
1114
1115   /**
1116    * GstRtpBin::on-bye-ssrc:
1117    * @rtpbin: the object which received the signal
1118    * @session: the session
1119    * @ssrc: the SSRC 
1120    *
1121    * Notify of an SSRC that became inactive because of a BYE packet.
1122    */
1123   gst_rtp_bin_signals[SIGNAL_ON_BYE_SSRC] =
1124       g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
1125       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_ssrc),
1126       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1127       G_TYPE_UINT, G_TYPE_UINT);
1128   /**
1129    * GstRtpBin::on-bye-timeout:
1130    * @rtpbin: the object which received the signal
1131    * @session: the session
1132    * @ssrc: the SSRC 
1133    *
1134    * Notify of an SSRC that has timed out because of BYE
1135    */
1136   gst_rtp_bin_signals[SIGNAL_ON_BYE_TIMEOUT] =
1137       g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
1138       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_bye_timeout),
1139       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1140       G_TYPE_UINT, G_TYPE_UINT);
1141   /**
1142    * GstRtpBin::on-timeout:
1143    * @rtpbin: the object which received the signal
1144    * @session: the session
1145    * @ssrc: the SSRC 
1146    *
1147    * Notify of an SSRC that has timed out
1148    */
1149   gst_rtp_bin_signals[SIGNAL_ON_TIMEOUT] =
1150       g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
1151       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpBinClass, on_timeout),
1152       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT, G_TYPE_NONE, 2,
1153       G_TYPE_UINT, G_TYPE_UINT);
1154
1155   gstelement_class->provide_clock =
1156       GST_DEBUG_FUNCPTR (gst_rtp_bin_provide_clock);
1157   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
1158   gstelement_class->request_new_pad =
1159       GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
1160   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_bin_release_pad);
1161
1162   klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_bin_clear_pt_map);
1163
1164   GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP bin");
1165 }
1166
1167 static void
1168 gst_rtp_bin_init (GstRtpBin * rtpbin, GstRtpBinClass * klass)
1169 {
1170   rtpbin->priv = GST_RTP_BIN_GET_PRIVATE (rtpbin);
1171   rtpbin->priv->bin_lock = g_mutex_new ();
1172   rtpbin->provided_clock = gst_system_clock_obtain ();
1173   rtpbin->latency = DEFAULT_LATENCY_MS;
1174 }
1175
1176 static void
1177 gst_rtp_bin_dispose (GObject * object)
1178 {
1179   GstRtpBin *rtpbin;
1180
1181   rtpbin = GST_RTP_BIN (object);
1182
1183   g_slist_foreach (rtpbin->sessions, (GFunc) free_session, NULL);
1184   g_slist_free (rtpbin->sessions);
1185   rtpbin->sessions = NULL;
1186   g_slist_foreach (rtpbin->clients, (GFunc) free_client, NULL);
1187   g_slist_free (rtpbin->clients);
1188   rtpbin->clients = NULL;
1189
1190   G_OBJECT_CLASS (parent_class)->dispose (object);
1191 }
1192
1193 static void
1194 gst_rtp_bin_finalize (GObject * object)
1195 {
1196   GstRtpBin *rtpbin;
1197
1198   rtpbin = GST_RTP_BIN (object);
1199
1200   g_mutex_free (rtpbin->priv->bin_lock);
1201   gst_object_unref (rtpbin->provided_clock);
1202
1203   G_OBJECT_CLASS (parent_class)->finalize (object);
1204 }
1205
1206 static void
1207 gst_rtp_bin_set_property (GObject * object, guint prop_id,
1208     const GValue * value, GParamSpec * pspec)
1209 {
1210   GstRtpBin *rtpbin;
1211
1212   rtpbin = GST_RTP_BIN (object);
1213
1214   switch (prop_id) {
1215     case PROP_LATENCY:
1216       GST_RTP_BIN_LOCK (rtpbin);
1217       rtpbin->latency = g_value_get_uint (value);
1218       GST_RTP_BIN_UNLOCK (rtpbin);
1219       break;
1220     default:
1221       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1222       break;
1223   }
1224 }
1225
1226 static void
1227 gst_rtp_bin_get_property (GObject * object, guint prop_id,
1228     GValue * value, GParamSpec * pspec)
1229 {
1230   GstRtpBin *rtpbin;
1231
1232   rtpbin = GST_RTP_BIN (object);
1233
1234   switch (prop_id) {
1235     case PROP_LATENCY:
1236       GST_RTP_BIN_LOCK (rtpbin);
1237       g_value_set_uint (value, rtpbin->latency);
1238       GST_RTP_BIN_UNLOCK (rtpbin);
1239       break;
1240     default:
1241       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1242       break;
1243   }
1244 }
1245
1246 static GstClock *
1247 gst_rtp_bin_provide_clock (GstElement * element)
1248 {
1249   GstRtpBin *rtpbin;
1250
1251   rtpbin = GST_RTP_BIN (element);
1252
1253   return GST_CLOCK_CAST (gst_object_ref (rtpbin->provided_clock));
1254 }
1255
1256 static void
1257 calc_ntp_ns_base (GstRtpBin * bin)
1258 {
1259   GstClockTime now;
1260   GTimeVal current;
1261   GSList *walk;
1262
1263   /* get the current time and convert it to NTP time in nanoseconds */
1264   g_get_current_time (&current);
1265   now = GST_TIMEVAL_TO_TIME (current);
1266   now += (2208988800LL * GST_SECOND);
1267
1268   GST_RTP_BIN_LOCK (bin);
1269   bin->priv->ntp_ns_base = now;
1270   for (walk = bin->sessions; walk; walk = g_slist_next (walk)) {
1271     GstRtpBinSession *session = (GstRtpBinSession *) walk->data;
1272
1273     g_object_set (session->session, "ntp-ns-base", now, NULL);
1274   }
1275   GST_RTP_BIN_UNLOCK (bin);
1276
1277   return;
1278 }
1279
1280 static GstStateChangeReturn
1281 gst_rtp_bin_change_state (GstElement * element, GstStateChange transition)
1282 {
1283   GstStateChangeReturn res;
1284   GstRtpBin *rtpbin;
1285
1286   rtpbin = GST_RTP_BIN (element);
1287
1288   switch (transition) {
1289     case GST_STATE_CHANGE_NULL_TO_READY:
1290       break;
1291     case GST_STATE_CHANGE_READY_TO_PAUSED:
1292       break;
1293     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1294       calc_ntp_ns_base (rtpbin);
1295       break;
1296     default:
1297       break;
1298   }
1299
1300   res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1301
1302   switch (transition) {
1303     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1304       break;
1305     case GST_STATE_CHANGE_PAUSED_TO_READY:
1306       break;
1307     case GST_STATE_CHANGE_READY_TO_NULL:
1308       break;
1309     default:
1310       break;
1311   }
1312   return res;
1313 }
1314
1315 /* a new pad (SSRC) was created in @session */
1316 static void
1317 new_payload_found (GstElement * element, guint pt, GstPad * pad,
1318     GstRtpBinStream * stream)
1319 {
1320   GstRtpBin *rtpbin;
1321   GstElementClass *klass;
1322   GstPadTemplate *templ;
1323   gchar *padname;
1324   GstPad *gpad;
1325
1326   rtpbin = stream->bin;
1327
1328   GST_DEBUG ("new payload pad %d", pt);
1329
1330   /* ghost the pad to the parent */
1331   klass = GST_ELEMENT_GET_CLASS (rtpbin);
1332   templ = gst_element_class_get_pad_template (klass, "recv_rtp_src_%d_%d_%d");
1333   padname = g_strdup_printf ("recv_rtp_src_%d_%u_%d",
1334       stream->session->id, stream->ssrc, pt);
1335   gpad = gst_ghost_pad_new_from_template (padname, pad, templ);
1336   g_free (padname);
1337
1338   gst_pad_set_caps (gpad, GST_PAD_CAPS (pad));
1339   gst_pad_set_active (gpad, TRUE);
1340   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), gpad);
1341 }
1342
1343 static GstCaps *
1344 pt_map_requested (GstElement * element, guint pt, GstRtpBinSession * session)
1345 {
1346   GstRtpBin *rtpbin;
1347   GstCaps *caps;
1348
1349   rtpbin = session->bin;
1350
1351   GST_DEBUG_OBJECT (rtpbin, "payload map requested for pt %d in session %d", pt,
1352       session->id);
1353
1354   caps = get_pt_map (session, pt);
1355   if (!caps)
1356     goto no_caps;
1357
1358   return caps;
1359
1360   /* ERRORS */
1361 no_caps:
1362   {
1363     GST_DEBUG_OBJECT (rtpbin, "could not get caps");
1364     return NULL;
1365   }
1366 }
1367
1368 /* emited when caps changed for the session */
1369 static void
1370 caps_changed (GstPad * pad, GParamSpec * pspec, GstRtpBinSession * session)
1371 {
1372   GstRtpBin *bin;
1373   GstCaps *caps;
1374   gint payload;
1375   const GstStructure *s;
1376
1377   bin = session->bin;
1378
1379   g_object_get (pad, "caps", &caps, NULL);
1380
1381   if (caps == NULL)
1382     return;
1383
1384   GST_DEBUG_OBJECT (bin, "got caps %" GST_PTR_FORMAT, caps);
1385
1386   s = gst_caps_get_structure (caps, 0);
1387
1388   /* get payload, finish when it's not there */
1389   if (!gst_structure_get_int (s, "payload", &payload))
1390     return;
1391
1392   GST_RTP_SESSION_LOCK (session);
1393   GST_DEBUG_OBJECT (bin, "insert caps for payload %d", payload);
1394   g_hash_table_insert (session->ptmap, GINT_TO_POINTER (payload), caps);
1395   GST_RTP_SESSION_UNLOCK (session);
1396 }
1397
1398 /* a new pad (SSRC) was created in @session */
1399 static void
1400 new_ssrc_pad_found (GstElement * element, guint ssrc, GstPad * pad,
1401     GstRtpBinSession * session)
1402 {
1403   GstRtpBinStream *stream;
1404   GstPad *sinkpad, *srcpad;
1405   gchar *padname;
1406   GstCaps *caps;
1407
1408   GST_DEBUG_OBJECT (session->bin, "new SSRC pad %08x", ssrc);
1409
1410   GST_RTP_SESSION_LOCK (session);
1411
1412   /* create new stream */
1413   stream = create_stream (session, ssrc);
1414   if (!stream)
1415     goto no_stream;
1416
1417   /* get the caps of the pad, we need the clock-rate and base_time if any. */
1418   if ((caps = gst_pad_get_caps (pad))) {
1419     const GstStructure *s;
1420     guint val;
1421
1422     GST_DEBUG_OBJECT (session->bin, "pad has caps %" GST_PTR_FORMAT, caps);
1423
1424     s = gst_caps_get_structure (caps, 0);
1425
1426     if (!gst_structure_get_int (s, "clock-rate", &stream->clock_rate))
1427       stream->clock_rate = -1;
1428
1429     if (gst_structure_get_uint (s, "clock-base", &val))
1430       stream->clock_base = val;
1431     else
1432       stream->clock_base = -1;
1433   }
1434
1435   /* get pad and link */
1436   GST_DEBUG_OBJECT (session->bin, "linking jitterbuffer");
1437   padname = g_strdup_printf ("src_%d", ssrc);
1438   srcpad = gst_element_get_pad (element, padname);
1439   g_free (padname);
1440   sinkpad = gst_element_get_static_pad (stream->buffer, "sink");
1441   gst_pad_link (srcpad, sinkpad);
1442   gst_object_unref (sinkpad);
1443
1444   /* get the RTCP sync pad */
1445   GST_DEBUG_OBJECT (session->bin, "linking sync pad");
1446   padname = g_strdup_printf ("rtcp_src_%d", ssrc);
1447   srcpad = gst_element_get_pad (element, padname);
1448   g_free (padname);
1449   gst_pad_link (srcpad, stream->sync_pad);
1450   gst_object_unref (srcpad);
1451
1452   /* connect to the new-pad signal of the payload demuxer, this will expose the
1453    * new pad by ghosting it. */
1454   stream->demux_newpad_sig = g_signal_connect (stream->demux,
1455       "new-payload-type", (GCallback) new_payload_found, stream);
1456   /* connect to the request-pt-map signal. This signal will be emited by the
1457    * demuxer so that it can apply a proper caps on the buffers for the
1458    * depayloaders. */
1459   stream->demux_ptreq_sig = g_signal_connect (stream->demux,
1460       "request-pt-map", (GCallback) pt_map_requested, session);
1461
1462   GST_RTP_SESSION_UNLOCK (session);
1463
1464   return;
1465
1466   /* ERRORS */
1467 no_stream:
1468   {
1469     GST_RTP_SESSION_UNLOCK (session);
1470     GST_DEBUG_OBJECT (session->bin, "could not create stream");
1471     return;
1472   }
1473 }
1474
1475 /* Create a pad for receiving RTP for the session in @name. Must be called with
1476  * RTP_BIN_LOCK.
1477  */
1478 static GstPad *
1479 create_recv_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1480 {
1481   GstPad *result, *sinkdpad;
1482   guint sessid;
1483   GstRtpBinSession *session;
1484   GstPadLinkReturn lres;
1485
1486   /* first get the session number */
1487   if (name == NULL || sscanf (name, "recv_rtp_sink_%d", &sessid) != 1)
1488     goto no_name;
1489
1490   GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
1491
1492   /* get or create session */
1493   session = find_session_by_id (rtpbin, sessid);
1494   if (!session) {
1495     GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
1496     /* create session now */
1497     session = create_session (rtpbin, sessid);
1498     if (session == NULL)
1499       goto create_error;
1500   }
1501
1502   /* check if pad was requested */
1503   if (session->recv_rtp_sink != NULL)
1504     goto existed;
1505
1506   GST_DEBUG_OBJECT (rtpbin, "getting RTP sink pad");
1507   /* get recv_rtp pad and store */
1508   session->recv_rtp_sink =
1509       gst_element_get_request_pad (session->session, "recv_rtp_sink");
1510   if (session->recv_rtp_sink == NULL)
1511     goto pad_failed;
1512
1513   g_signal_connect (session->recv_rtp_sink, "notify::caps",
1514       (GCallback) caps_changed, session);
1515
1516   GST_DEBUG_OBJECT (rtpbin, "getting RTP src pad");
1517   /* get srcpad, link to SSRCDemux */
1518   session->recv_rtp_src =
1519       gst_element_get_static_pad (session->session, "recv_rtp_src");
1520   if (session->recv_rtp_src == NULL)
1521     goto pad_failed;
1522
1523   GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTP sink pad");
1524   sinkdpad = gst_element_get_static_pad (session->demux, "sink");
1525   GST_DEBUG_OBJECT (rtpbin, "linking demuxer RTP sink pad");
1526   lres = gst_pad_link (session->recv_rtp_src, sinkdpad);
1527   gst_object_unref (sinkdpad);
1528   if (lres != GST_PAD_LINK_OK)
1529     goto link_failed;
1530
1531   /* connect to the new-ssrc-pad signal of the SSRC demuxer */
1532   session->demux_newpad_sig = g_signal_connect (session->demux,
1533       "new-ssrc-pad", (GCallback) new_ssrc_pad_found, session);
1534
1535   GST_DEBUG_OBJECT (rtpbin, "ghosting session sink pad");
1536   result =
1537       gst_ghost_pad_new_from_template (name, session->recv_rtp_sink, templ);
1538   gst_pad_set_active (result, TRUE);
1539   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1540
1541   return result;
1542
1543   /* ERRORS */
1544 no_name:
1545   {
1546     g_warning ("gstrtpbin: invalid name given");
1547     return NULL;
1548   }
1549 create_error:
1550   {
1551     /* create_session already warned */
1552     return NULL;
1553   }
1554 existed:
1555   {
1556     g_warning ("gstrtpbin: recv_rtp pad already requested for session %d",
1557         sessid);
1558     return NULL;
1559   }
1560 pad_failed:
1561   {
1562     g_warning ("gstrtpbin: failed to get session pad");
1563     return NULL;
1564   }
1565 link_failed:
1566   {
1567     g_warning ("gstrtpbin: failed to link pads");
1568     return NULL;
1569   }
1570 }
1571
1572 /* Create a pad for receiving RTCP for the session in @name. Must be called with
1573  * RTP_BIN_LOCK.
1574  */
1575 static GstPad *
1576 create_recv_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ,
1577     const gchar * name)
1578 {
1579   GstPad *result;
1580   guint sessid;
1581   GstRtpBinSession *session;
1582   GstPad *sinkdpad;
1583   GstPadLinkReturn lres;
1584
1585   /* first get the session number */
1586   if (name == NULL || sscanf (name, "recv_rtcp_sink_%d", &sessid) != 1)
1587     goto no_name;
1588
1589   GST_DEBUG_OBJECT (rtpbin, "finding session %d", sessid);
1590
1591   /* get or create the session */
1592   session = find_session_by_id (rtpbin, sessid);
1593   if (!session) {
1594     GST_DEBUG_OBJECT (rtpbin, "creating session %d", sessid);
1595     /* create session now */
1596     session = create_session (rtpbin, sessid);
1597     if (session == NULL)
1598       goto create_error;
1599   }
1600
1601   /* check if pad was requested */
1602   if (session->recv_rtcp_sink != NULL)
1603     goto existed;
1604
1605   /* get recv_rtp pad and store */
1606   GST_DEBUG_OBJECT (rtpbin, "getting RTCP sink pad");
1607   session->recv_rtcp_sink =
1608       gst_element_get_request_pad (session->session, "recv_rtcp_sink");
1609   if (session->recv_rtcp_sink == NULL)
1610     goto pad_failed;
1611
1612   /* get srcpad, link to SSRCDemux */
1613   GST_DEBUG_OBJECT (rtpbin, "getting sync src pad");
1614   session->sync_src = gst_element_get_static_pad (session->session, "sync_src");
1615   if (session->sync_src == NULL)
1616     goto pad_failed;
1617
1618   GST_DEBUG_OBJECT (rtpbin, "getting demuxer RTCP sink pad");
1619   sinkdpad = gst_element_get_static_pad (session->demux, "rtcp_sink");
1620   lres = gst_pad_link (session->sync_src, sinkdpad);
1621   gst_object_unref (sinkdpad);
1622   if (lres != GST_PAD_LINK_OK)
1623     goto link_failed;
1624
1625   result =
1626       gst_ghost_pad_new_from_template (name, session->recv_rtcp_sink, templ);
1627   gst_pad_set_active (result, TRUE);
1628   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1629
1630   return result;
1631
1632   /* ERRORS */
1633 no_name:
1634   {
1635     g_warning ("gstrtpbin: invalid name given");
1636     return NULL;
1637   }
1638 create_error:
1639   {
1640     /* create_session already warned */
1641     return NULL;
1642   }
1643 existed:
1644   {
1645     g_warning ("gstrtpbin: recv_rtcp pad already requested for session %d",
1646         sessid);
1647     return NULL;
1648   }
1649 pad_failed:
1650   {
1651     g_warning ("gstrtpbin: failed to get session pad");
1652     return NULL;
1653   }
1654 link_failed:
1655   {
1656     g_warning ("gstrtpbin: failed to link pads");
1657     return NULL;
1658   }
1659 }
1660
1661 /* Create a pad for sending RTP for the session in @name. Must be called with
1662  * RTP_BIN_LOCK.
1663  */
1664 static GstPad *
1665 create_send_rtp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1666 {
1667   GstPad *result, *srcghost;
1668   gchar *gname;
1669   guint sessid;
1670   GstRtpBinSession *session;
1671   GstElementClass *klass;
1672
1673   /* first get the session number */
1674   if (name == NULL || sscanf (name, "send_rtp_sink_%d", &sessid) != 1)
1675     goto no_name;
1676
1677   /* get or create session */
1678   session = find_session_by_id (rtpbin, sessid);
1679   if (!session) {
1680     /* create session now */
1681     session = create_session (rtpbin, sessid);
1682     if (session == NULL)
1683       goto create_error;
1684   }
1685
1686   /* check if pad was requested */
1687   if (session->send_rtp_sink != NULL)
1688     goto existed;
1689
1690   /* get send_rtp pad and store */
1691   session->send_rtp_sink =
1692       gst_element_get_request_pad (session->session, "send_rtp_sink");
1693   if (session->send_rtp_sink == NULL)
1694     goto pad_failed;
1695
1696   result =
1697       gst_ghost_pad_new_from_template (name, session->send_rtp_sink, templ);
1698   gst_pad_set_active (result, TRUE);
1699   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1700
1701   /* get srcpad */
1702   session->send_rtp_src =
1703       gst_element_get_static_pad (session->session, "send_rtp_src");
1704   if (session->send_rtp_src == NULL)
1705     goto no_srcpad;
1706
1707   /* ghost the new source pad */
1708   klass = GST_ELEMENT_GET_CLASS (rtpbin);
1709   gname = g_strdup_printf ("send_rtp_src_%d", sessid);
1710   templ = gst_element_class_get_pad_template (klass, "send_rtp_src_%d");
1711   srcghost =
1712       gst_ghost_pad_new_from_template (gname, session->send_rtp_src, templ);
1713   gst_pad_set_active (srcghost, TRUE);
1714   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), srcghost);
1715   g_free (gname);
1716
1717   return result;
1718
1719   /* ERRORS */
1720 no_name:
1721   {
1722     g_warning ("gstrtpbin: invalid name given");
1723     return NULL;
1724   }
1725 create_error:
1726   {
1727     /* create_session already warned */
1728     return NULL;
1729   }
1730 existed:
1731   {
1732     g_warning ("gstrtpbin: send_rtp pad already requested for session %d",
1733         sessid);
1734     return NULL;
1735   }
1736 pad_failed:
1737   {
1738     g_warning ("gstrtpbin: failed to get session pad for session %d", sessid);
1739     return NULL;
1740   }
1741 no_srcpad:
1742   {
1743     g_warning ("gstrtpbin: failed to get rtp source pad for session %d",
1744         sessid);
1745     return NULL;
1746   }
1747 }
1748
1749 /* Create a pad for sending RTCP for the session in @name. Must be called with
1750  * RTP_BIN_LOCK.
1751  */
1752 static GstPad *
1753 create_rtcp (GstRtpBin * rtpbin, GstPadTemplate * templ, const gchar * name)
1754 {
1755   GstPad *result;
1756   guint sessid;
1757   GstRtpBinSession *session;
1758
1759   /* first get the session number */
1760   if (name == NULL || sscanf (name, "send_rtcp_src_%d", &sessid) != 1)
1761     goto no_name;
1762
1763   /* get or create session */
1764   session = find_session_by_id (rtpbin, sessid);
1765   if (!session)
1766     goto no_session;
1767
1768   /* check if pad was requested */
1769   if (session->send_rtcp_src != NULL)
1770     goto existed;
1771
1772   /* get rtcp_src pad and store */
1773   session->send_rtcp_src =
1774       gst_element_get_request_pad (session->session, "send_rtcp_src");
1775   if (session->send_rtcp_src == NULL)
1776     goto pad_failed;
1777
1778   result =
1779       gst_ghost_pad_new_from_template (name, session->send_rtcp_src, templ);
1780   gst_pad_set_active (result, TRUE);
1781   gst_element_add_pad (GST_ELEMENT_CAST (rtpbin), result);
1782
1783   return result;
1784
1785   /* ERRORS */
1786 no_name:
1787   {
1788     g_warning ("gstrtpbin: invalid name given");
1789     return NULL;
1790   }
1791 no_session:
1792   {
1793     g_warning ("gstrtpbin: session with id %d does not exist", sessid);
1794     return NULL;
1795   }
1796 existed:
1797   {
1798     g_warning ("gstrtpbin: send_rtcp_src pad already requested for session %d",
1799         sessid);
1800     return NULL;
1801   }
1802 pad_failed:
1803   {
1804     g_warning ("gstrtpbin: failed to get rtcp pad for session %d", sessid);
1805     return NULL;
1806   }
1807 }
1808
1809 /* 
1810  */
1811 static GstPad *
1812 gst_rtp_bin_request_new_pad (GstElement * element,
1813     GstPadTemplate * templ, const gchar * name)
1814 {
1815   GstRtpBin *rtpbin;
1816   GstElementClass *klass;
1817   GstPad *result;
1818
1819   g_return_val_if_fail (templ != NULL, NULL);
1820   g_return_val_if_fail (GST_IS_RTP_BIN (element), NULL);
1821
1822   rtpbin = GST_RTP_BIN (element);
1823   klass = GST_ELEMENT_GET_CLASS (element);
1824
1825   GST_RTP_BIN_LOCK (rtpbin);
1826
1827   /* figure out the template */
1828   if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink_%d")) {
1829     result = create_recv_rtp (rtpbin, templ, name);
1830   } else if (templ == gst_element_class_get_pad_template (klass,
1831           "recv_rtcp_sink_%d")) {
1832     result = create_recv_rtcp (rtpbin, templ, name);
1833   } else if (templ == gst_element_class_get_pad_template (klass,
1834           "send_rtp_sink_%d")) {
1835     result = create_send_rtp (rtpbin, templ, name);
1836   } else if (templ == gst_element_class_get_pad_template (klass,
1837           "send_rtcp_src_%d")) {
1838     result = create_rtcp (rtpbin, templ, name);
1839   } else
1840     goto wrong_template;
1841
1842   GST_RTP_BIN_UNLOCK (rtpbin);
1843
1844   return result;
1845
1846   /* ERRORS */
1847 wrong_template:
1848   {
1849     GST_RTP_BIN_UNLOCK (rtpbin);
1850     g_warning ("gstrtpbin: this is not our template");
1851     return NULL;
1852   }
1853 }
1854
1855 static void
1856 gst_rtp_bin_release_pad (GstElement * element, GstPad * pad)
1857 {
1858 }