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