gst/rtpmanager/gstrtpbin.c: Ref caps when inserting into the cache.
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / gstrtpsession.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:element-gstrtpsession
22  * @short_description: an RTP session manager
23  * @see_also: gstrtpjitterbuffer, gstrtpbin, gstrtpptdemux, gstrtpssrcdemux
24  *
25  * <refsect2>
26  * <para>
27  * The RTP session manager models one participant with a unique SSRC in an RTP
28  * session. This session can be used to send and receive RTP and RTCP packets.
29  * Based on what REQUEST pads are requested from the session manager, specific
30  * functionality can be activated.
31  * </para>
32  * <para>
33  * The session manager currently implements RFC 3550 including:
34  * <itemizedlist>
35  *   <listitem>
36  *     <para>RTP packet validation based on consecutive sequence numbers.</para>
37  *   </listitem>
38  *   <listitem>
39  *     <para>Maintainance of the SSRC participant database.</para>
40  *   </listitem>
41  *   <listitem>
42  *     <para>Keeping per participant statistics based on received RTCP packets.</para>
43  *   </listitem>
44  *   <listitem>
45  *     <para>Scheduling of RR/SR RTCP packets.</para>
46  *   </listitem>
47  * </itemizedlist>
48  * </para>
49  * <para>
50  * The gstrtpsession will not demux packets based on SSRC or payload type, nor will
51  * it correct for packet reordering and jitter. Use gstrtpssrcdemux, gstrtpptdemux and
52  * gstrtpjitterbuffer in addition to gstrtpsession to perform these tasks. It is
53  * usually a good idea to use gstrtpbin, which combines all these features in one
54  * element.
55  * </para>
56  * <para>
57  * To use gstrtpsession as an RTP receiver, request a recv_rtp_sink pad, which will
58  * automatically create recv_rtp_src pad. Data received on the recv_rtp_sink pad
59  * will be processed in the session and after being validated forwarded on the
60  * recv_rtp_src pad.
61  * </para>
62  * <para>
63  * To also use gstrtpsession as an RTCP receiver, request a recv_rtcp_sink pad,
64  * which will automatically create a sync_src pad. Packets received on the RTCP
65  * pad will be used by the session manager to update the stats and database of
66  * the other participants. SR packets will be forwarded on the sync_src pad
67  * so that they can be used to perform inter-stream synchronisation when needed.
68  * </para>
69  * <para>
70  * If you want the session manager to generate and send RTCP packets, request
71  * the send_rtcp_src pad. Packet pushed on this pad contain SR/RR RTCP reports
72  * that should be sent to all participants in the session.
73  * </para>
74  * <para>
75  * To use gstrtpsession as a sender, request a send_rtp_sink pad, which will
76  * automatically create a send_rtp_src pad. The session manager will modify the
77  * SSRC in the RTP packets to its own SSRC and wil forward the packets on the
78  * send_rtp_src pad after updating its internal state.
79  * </para>
80  * <para>
81  * The session manager needs the clock-rate of the payload types it is handling
82  * and will signal the GstRtpSession::request-pt-map signal when it needs such a
83  * mapping. One can clear the cached values with the GstRtpSession::clear-pt-map
84  * signal.
85  * </para>
86  * <title>Example pipelines</title>
87  * <para>
88  * <programlisting>
89  * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink gstrtpsession .recv_rtp_src ! rtptheoradepay ! theoradec ! xvimagesink
90  * </programlisting>
91  * Receive theora RTP packets from port 5000 and send them to the depayloader,
92  * decoder and display. Note that the application/x-rtp caps on udpsrc should be
93  * configured based on some negotiation process such as RTSP for this pipeline
94  * to work correctly.
95  * </para>
96  * <para>
97  * <programlisting>
98  * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink gstrtpsession name=session \
99  *        .recv_rtp_src ! rtptheoradepay ! theoradec ! xvimagesink \
100  *     udpsrc port=5001 caps="application/x-rtcp" ! session.recv_rtcp_sink
101  * </programlisting>
102  * Receive theora RTP packets from port 5000 and send them to the depayloader,
103  * decoder and display. Receive RTCP packets from port 5001 and process them in
104  * the session manager.
105  * Note that the application/x-rtp caps on udpsrc should be
106  * configured based on some negotiation process such as RTSP for this pipeline
107  * to work correctly.
108  * </para>
109  * <para>
110  * <programlisting>
111  * gst-launch videotestsrc ! theoraenc ! rtptheorapay ! .send_rtp_sink gstrtpsession .send_rtp_src ! udpsink port=5000
112  * </programlisting>
113  * Send theora RTP packets through the session manager and out on UDP port 5000.
114  * </para>
115  * <para>
116  * <programlisting>
117  * gst-launch videotestsrc ! theoraenc ! rtptheorapay ! .send_rtp_sink gstrtpsession name=session .send_rtp_src \
118  *     ! udpsink port=5000  session.send_rtcp_src ! udpsink port=5001
119  * </programlisting>
120  * Send theora RTP packets through the session manager and out on UDP port 5000.
121  * Send RTCP packets on port 5001. Note that this pipeline will not preroll
122  * correctly because the second udpsink will not preroll correctly (no RTCP
123  * packets are sent in the PAUSED state). Applications should manually set and
124  * keep (see #gst_element_set_locked_state()) the RTCP udpsink to the PLAYING state.
125  * </para>
126  * </refsect2>
127  *
128  * Last reviewed on 2007-05-28 (0.10.5)
129  */
130
131 #ifdef HAVE_CONFIG_H
132 #include "config.h"
133 #endif
134
135 #include <gst/rtp/gstrtpbuffer.h>
136
137 #include "gstrtpbin-marshal.h"
138 #include "gstrtpsession.h"
139 #include "rtpsession.h"
140
141 GST_DEBUG_CATEGORY_STATIC (gst_rtp_session_debug);
142 #define GST_CAT_DEFAULT gst_rtp_session_debug
143
144 /* elementfactory information */
145 static const GstElementDetails rtpsession_details =
146 GST_ELEMENT_DETAILS ("RTP Session",
147     "Filter/Network/RTP",
148     "Implement an RTP session",
149     "Wim Taymans <wim.taymans@gmail.com>");
150
151 /* sink pads */
152 static GstStaticPadTemplate rtpsession_recv_rtp_sink_template =
153 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink",
154     GST_PAD_SINK,
155     GST_PAD_REQUEST,
156     GST_STATIC_CAPS ("application/x-rtp")
157     );
158
159 static GstStaticPadTemplate rtpsession_recv_rtcp_sink_template =
160 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink",
161     GST_PAD_SINK,
162     GST_PAD_REQUEST,
163     GST_STATIC_CAPS ("application/x-rtcp")
164     );
165
166 static GstStaticPadTemplate rtpsession_send_rtp_sink_template =
167 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink",
168     GST_PAD_SINK,
169     GST_PAD_REQUEST,
170     GST_STATIC_CAPS ("application/x-rtp")
171     );
172
173 /* src pads */
174 static GstStaticPadTemplate rtpsession_recv_rtp_src_template =
175 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src",
176     GST_PAD_SRC,
177     GST_PAD_SOMETIMES,
178     GST_STATIC_CAPS ("application/x-rtp")
179     );
180
181 static GstStaticPadTemplate rtpsession_sync_src_template =
182 GST_STATIC_PAD_TEMPLATE ("sync_src",
183     GST_PAD_SRC,
184     GST_PAD_SOMETIMES,
185     GST_STATIC_CAPS ("application/x-rtcp")
186     );
187
188 static GstStaticPadTemplate rtpsession_send_rtp_src_template =
189 GST_STATIC_PAD_TEMPLATE ("send_rtp_src",
190     GST_PAD_SRC,
191     GST_PAD_SOMETIMES,
192     GST_STATIC_CAPS ("application/x-rtp")
193     );
194
195 static GstStaticPadTemplate rtpsession_send_rtcp_src_template =
196 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src",
197     GST_PAD_SRC,
198     GST_PAD_REQUEST,
199     GST_STATIC_CAPS ("application/x-rtcp")
200     );
201
202 /* signals and args */
203 enum
204 {
205   SIGNAL_REQUEST_PT_MAP,
206   SIGNAL_CLEAR_PT_MAP,
207
208   SIGNAL_ON_NEW_SSRC,
209   SIGNAL_ON_SSRC_COLLISION,
210   SIGNAL_ON_SSRC_VALIDATED,
211   SIGNAL_ON_SSRC_ACTIVE,
212   SIGNAL_ON_SSRC_SDES,
213   SIGNAL_ON_BYE_SSRC,
214   SIGNAL_ON_BYE_TIMEOUT,
215   SIGNAL_ON_TIMEOUT,
216   LAST_SIGNAL
217 };
218
219 #define DEFAULT_NTP_NS_BASE          0
220 #define DEFAULT_BANDWIDTH            RTP_STATS_BANDWIDTH
221 #define DEFAULT_RTCP_FRACTION        RTP_STATS_RTCP_BANDWIDTH
222 #define DEFAULT_SDES_CNAME           NULL
223 #define DEFAULT_SDES_NAME            NULL
224 #define DEFAULT_SDES_EMAIL           NULL
225 #define DEFAULT_SDES_PHONE           NULL
226 #define DEFAULT_SDES_LOCATION        NULL
227 #define DEFAULT_SDES_TOOL            NULL
228 #define DEFAULT_SDES_NOTE            NULL
229 #define DEFAULT_NUM_SOURCES          0
230 #define DEFAULT_NUM_ACTIVE_SOURCES   0
231
232 enum
233 {
234   PROP_0,
235   PROP_NTP_NS_BASE,
236   PROP_BANDWIDTH,
237   PROP_RTCP_FRACTION,
238   PROP_SDES_CNAME,
239   PROP_SDES_NAME,
240   PROP_SDES_EMAIL,
241   PROP_SDES_PHONE,
242   PROP_SDES_LOCATION,
243   PROP_SDES_TOOL,
244   PROP_SDES_NOTE,
245   PROP_NUM_SOURCES,
246   PROP_NUM_ACTIVE_SOURCES,
247   PROP_LAST
248 };
249
250 #define GST_RTP_SESSION_GET_PRIVATE(obj)  \
251            (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_SESSION, GstRtpSessionPrivate))
252
253 #define GST_RTP_SESSION_LOCK(sess)   g_mutex_lock ((sess)->priv->lock)
254 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock ((sess)->priv->lock)
255
256 struct _GstRtpSessionPrivate
257 {
258   GMutex *lock;
259   RTPSession *session;
260
261   /* thread for sending out RTCP */
262   GstClockID id;
263   gboolean stop_thread;
264   GThread *thread;
265   gboolean thread_stopped;
266
267   /* caps mapping */
268   GHashTable *ptmap;
269
270   /* NTP base time */
271   guint64 ntpnsbase;
272 };
273
274 /* callbacks to handle actions from the session manager */
275 static GstFlowReturn gst_rtp_session_process_rtp (RTPSession * sess,
276     RTPSource * src, GstBuffer * buffer, gpointer user_data);
277 static GstFlowReturn gst_rtp_session_send_rtp (RTPSession * sess,
278     RTPSource * src, GstBuffer * buffer, gpointer user_data);
279 static GstFlowReturn gst_rtp_session_send_rtcp (RTPSession * sess,
280     RTPSource * src, GstBuffer * buffer, gpointer user_data);
281 static GstFlowReturn gst_rtp_session_sync_rtcp (RTPSession * sess,
282     RTPSource * src, GstBuffer * buffer, gpointer user_data);
283 static gint gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
284     gpointer user_data);
285 static void gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data);
286
287 static RTPSessionCallbacks callbacks = {
288   gst_rtp_session_process_rtp,
289   gst_rtp_session_send_rtp,
290   gst_rtp_session_sync_rtcp,
291   gst_rtp_session_send_rtcp,
292   gst_rtp_session_clock_rate,
293   gst_rtp_session_reconsider
294 };
295
296 /* GObject vmethods */
297 static void gst_rtp_session_finalize (GObject * object);
298 static void gst_rtp_session_set_property (GObject * object, guint prop_id,
299     const GValue * value, GParamSpec * pspec);
300 static void gst_rtp_session_get_property (GObject * object, guint prop_id,
301     GValue * value, GParamSpec * pspec);
302
303 /* GstElement vmethods */
304 static GstStateChangeReturn gst_rtp_session_change_state (GstElement * element,
305     GstStateChange transition);
306 static GstPad *gst_rtp_session_request_new_pad (GstElement * element,
307     GstPadTemplate * templ, const gchar * name);
308 static void gst_rtp_session_release_pad (GstElement * element, GstPad * pad);
309
310 static void gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession);
311
312 static guint gst_rtp_session_signals[LAST_SIGNAL] = { 0 };
313
314 static void
315 on_new_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
316 {
317   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0,
318       src->ssrc);
319 }
320
321 static void
322 on_ssrc_collision (RTPSession * session, RTPSource * src, GstRtpSession * sess)
323 {
324   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
325       src->ssrc);
326 }
327
328 static void
329 on_ssrc_validated (RTPSession * session, RTPSource * src, GstRtpSession * sess)
330 {
331   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
332       src->ssrc);
333 }
334
335 static void
336 on_ssrc_active (RTPSession * session, RTPSource * src, GstRtpSession * sess)
337 {
338   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
339       src->ssrc);
340 }
341
342 static GstStructure *
343 source_get_sdes_structure (RTPSource * src)
344 {
345   GstStructure *result;
346   GValue val = { 0 };
347   gchar *str;
348
349   result = gst_structure_empty_new ("GstRTPSessionSDES");
350
351   gst_structure_set (result, "ssrc", G_TYPE_UINT, src->ssrc, NULL);
352
353   g_value_init (&val, G_TYPE_STRING);
354   str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_CNAME);
355   if (str) {
356     g_value_take_string (&val, str);
357     gst_structure_set_value (result, "cname", &val);
358   }
359   str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_NAME);
360   if (str) {
361     g_value_take_string (&val, str);
362     gst_structure_set_value (result, "name", &val);
363   }
364   str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_EMAIL);
365   if (str) {
366     g_value_take_string (&val, str);
367     gst_structure_set_value (result, "email", &val);
368   }
369   str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_PHONE);
370   if (str) {
371     g_value_take_string (&val, str);
372     gst_structure_set_value (result, "phone", &val);
373   }
374   str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_LOC);
375   if (str) {
376     g_value_take_string (&val, str);
377     gst_structure_set_value (result, "location", &val);
378   }
379   str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_TOOL);
380   if (str) {
381     g_value_take_string (&val, str);
382     gst_structure_set_value (result, "tool", &val);
383   }
384   str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_NOTE);
385   if (str) {
386     g_value_take_string (&val, str);
387     gst_structure_set_value (result, "note", &val);
388   }
389   str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_PRIV);
390   if (str) {
391     g_value_take_string (&val, str);
392     gst_structure_set_value (result, "priv", &val);
393   }
394   g_value_unset (&val);
395
396   return result;
397 }
398
399 static void
400 on_ssrc_sdes (RTPSession * session, RTPSource * src, GstRtpSession * sess)
401 {
402   GstStructure *s;
403   GstMessage *m;
404
405   /* convert the new SDES info into a message */
406   RTP_SESSION_LOCK (session);
407   s = source_get_sdes_structure (src);
408   RTP_SESSION_UNLOCK (session);
409   m = gst_message_new_custom (GST_MESSAGE_ELEMENT, GST_OBJECT (sess), s);
410   gst_element_post_message (GST_ELEMENT_CAST (sess), m);
411
412   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0,
413       src->ssrc);
414 }
415
416 static void
417 on_bye_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
418 {
419   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0,
420       src->ssrc);
421 }
422
423 static void
424 on_bye_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
425 {
426   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
427       src->ssrc);
428 }
429
430 static void
431 on_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
432 {
433   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_TIMEOUT], 0,
434       src->ssrc);
435 }
436
437 GST_BOILERPLATE (GstRtpSession, gst_rtp_session, GstElement, GST_TYPE_ELEMENT);
438
439 static void
440 gst_rtp_session_base_init (gpointer klass)
441 {
442   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
443
444   /* sink pads */
445   gst_element_class_add_pad_template (element_class,
446       gst_static_pad_template_get (&rtpsession_recv_rtp_sink_template));
447   gst_element_class_add_pad_template (element_class,
448       gst_static_pad_template_get (&rtpsession_recv_rtcp_sink_template));
449   gst_element_class_add_pad_template (element_class,
450       gst_static_pad_template_get (&rtpsession_send_rtp_sink_template));
451
452   /* src pads */
453   gst_element_class_add_pad_template (element_class,
454       gst_static_pad_template_get (&rtpsession_recv_rtp_src_template));
455   gst_element_class_add_pad_template (element_class,
456       gst_static_pad_template_get (&rtpsession_sync_src_template));
457   gst_element_class_add_pad_template (element_class,
458       gst_static_pad_template_get (&rtpsession_send_rtp_src_template));
459   gst_element_class_add_pad_template (element_class,
460       gst_static_pad_template_get (&rtpsession_send_rtcp_src_template));
461
462   gst_element_class_set_details (element_class, &rtpsession_details);
463 }
464
465 static void
466 gst_rtp_session_class_init (GstRtpSessionClass * klass)
467 {
468   GObjectClass *gobject_class;
469   GstElementClass *gstelement_class;
470
471   gobject_class = (GObjectClass *) klass;
472   gstelement_class = (GstElementClass *) klass;
473
474   g_type_class_add_private (klass, sizeof (GstRtpSessionPrivate));
475
476   gobject_class->finalize = gst_rtp_session_finalize;
477   gobject_class->set_property = gst_rtp_session_set_property;
478   gobject_class->get_property = gst_rtp_session_get_property;
479
480   /**
481    * GstRtpSession::request-pt-map:
482    * @sess: the object which received the signal
483    * @pt: the pt
484    *
485    * Request the payload type as #GstCaps for @pt.
486    */
487   gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP] =
488       g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
489       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, request_pt_map),
490       NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT, GST_TYPE_CAPS, 1,
491       G_TYPE_UINT);
492   /**
493    * GstRtpSession::clear-pt-map:
494    * @sess: the object which received the signal
495    *
496    * Clear the cached pt-maps requested with GstRtpSession::request-pt-map.
497    */
498   gst_rtp_session_signals[SIGNAL_CLEAR_PT_MAP] =
499       g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
500       G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpSessionClass, clear_pt_map),
501       NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
502
503   /**
504    * GstRtpSession::on-new-ssrc:
505    * @sess: the object which received the signal
506    * @ssrc: the SSRC 
507    *
508    * Notify of a new SSRC that entered @session.
509    */
510   gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
511       g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
512       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_new_ssrc),
513       NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
514   /**
515    * GstRtpSession::on-ssrc_collision:
516    * @sess: the object which received the signal
517    * @ssrc: the SSRC 
518    *
519    * Notify when we have an SSRC collision
520    */
521   gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
522       g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
523       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
524           on_ssrc_collision), NULL, NULL, g_cclosure_marshal_VOID__UINT,
525       G_TYPE_NONE, 1, G_TYPE_UINT);
526   /**
527    * GstRtpSession::on-ssrc_validated:
528    * @sess: the object which received the signal
529    * @ssrc: the SSRC 
530    *
531    * Notify of a new SSRC that became validated.
532    */
533   gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
534       g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
535       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
536           on_ssrc_validated), NULL, NULL, g_cclosure_marshal_VOID__UINT,
537       G_TYPE_NONE, 1, G_TYPE_UINT);
538   /**
539    * GstRtpSession::on-ssrc_active:
540    * @sess: the object which received the signal
541    * @ssrc: the SSRC
542    *
543    * Notify of a SSRC that is active, i.e., sending RTCP.
544    */
545   gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
546       g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
547       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
548           on_ssrc_active), NULL, NULL, g_cclosure_marshal_VOID__UINT,
549       G_TYPE_NONE, 1, G_TYPE_UINT);
550   /**
551    * GstRtpSession::on-ssrc-sdes:
552    * @session: the object which received the signal
553    * @src: the SSRC
554    *
555    * Notify that a new SDES was received for SSRC.
556    */
557   gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
558       g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
559       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_ssrc_sdes),
560       NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
561
562   /**
563    * GstRtpSession::on-bye-ssrc:
564    * @sess: the object which received the signal
565    * @ssrc: the SSRC 
566    *
567    * Notify of an SSRC that became inactive because of a BYE packet.
568    */
569   gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
570       g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
571       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_ssrc),
572       NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
573   /**
574    * GstRtpSession::on-bye-timeout:
575    * @sess: the object which received the signal
576    * @ssrc: the SSRC 
577    *
578    * Notify of an SSRC that has timed out because of BYE
579    */
580   gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
581       g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
582       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_timeout),
583       NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
584   /**
585    * GstRtpSession::on-timeout:
586    * @sess: the object which received the signal
587    * @ssrc: the SSRC 
588    *
589    * Notify of an SSRC that has timed out
590    */
591   gst_rtp_session_signals[SIGNAL_ON_TIMEOUT] =
592       g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
593       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_timeout),
594       NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
595
596   g_object_class_install_property (gobject_class, PROP_NTP_NS_BASE,
597       g_param_spec_uint64 ("ntp-ns-base", "NTP base time",
598           "The NTP base time corresponding to running_time 0", 0,
599           G_MAXUINT64, DEFAULT_NTP_NS_BASE, G_PARAM_READWRITE));
600
601   g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
602       g_param_spec_double ("bandwidth", "Bandwidth",
603           "The bandwidth of the session",
604           0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH, G_PARAM_READWRITE));
605
606   g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
607       g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
608           "The fraction of the bandwidth used for RTCP",
609           0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION, G_PARAM_READWRITE));
610
611   g_object_class_install_property (gobject_class, PROP_SDES_CNAME,
612       g_param_spec_string ("sdes-cname", "SDES CNAME",
613           "The CNAME to put in SDES messages of this session",
614           DEFAULT_SDES_CNAME, G_PARAM_READWRITE));
615
616   g_object_class_install_property (gobject_class, PROP_SDES_NAME,
617       g_param_spec_string ("sdes-name", "SDES NAME",
618           "The NAME to put in SDES messages of this session",
619           DEFAULT_SDES_NAME, G_PARAM_READWRITE));
620
621   g_object_class_install_property (gobject_class, PROP_SDES_EMAIL,
622       g_param_spec_string ("sdes-email", "SDES EMAIL",
623           "The EMAIL to put in SDES messages of this session",
624           DEFAULT_SDES_EMAIL, G_PARAM_READWRITE));
625
626   g_object_class_install_property (gobject_class, PROP_SDES_PHONE,
627       g_param_spec_string ("sdes-phone", "SDES PHONE",
628           "The PHONE to put in SDES messages of this session",
629           DEFAULT_SDES_PHONE, G_PARAM_READWRITE));
630
631   g_object_class_install_property (gobject_class, PROP_SDES_LOCATION,
632       g_param_spec_string ("sdes-location", "SDES LOCATION",
633           "The LOCATION to put in SDES messages of this session",
634           DEFAULT_SDES_LOCATION, G_PARAM_READWRITE));
635
636   g_object_class_install_property (gobject_class, PROP_SDES_TOOL,
637       g_param_spec_string ("sdes-tool", "SDES TOOL",
638           "The TOOL to put in SDES messages of this session",
639           DEFAULT_SDES_TOOL, G_PARAM_READWRITE));
640
641   g_object_class_install_property (gobject_class, PROP_SDES_NOTE,
642       g_param_spec_string ("sdes-note", "SDES NOTE",
643           "The NOTE to put in SDES messages of this session",
644           DEFAULT_SDES_NOTE, G_PARAM_READWRITE));
645
646   g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
647       g_param_spec_uint ("num-sources", "Num Sources",
648           "The number of sources in the session", 0, G_MAXUINT,
649           DEFAULT_NUM_SOURCES, G_PARAM_READABLE));
650
651   g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
652       g_param_spec_uint ("num-active-sources", "Num Active Sources",
653           "The number of active sources in the session", 0, G_MAXUINT,
654           DEFAULT_NUM_ACTIVE_SOURCES, G_PARAM_READABLE));
655
656   gstelement_class->change_state =
657       GST_DEBUG_FUNCPTR (gst_rtp_session_change_state);
658   gstelement_class->request_new_pad =
659       GST_DEBUG_FUNCPTR (gst_rtp_session_request_new_pad);
660   gstelement_class->release_pad =
661       GST_DEBUG_FUNCPTR (gst_rtp_session_release_pad);
662
663   klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_session_clear_pt_map);
664
665   GST_DEBUG_CATEGORY_INIT (gst_rtp_session_debug,
666       "rtpsession", 0, "RTP Session");
667 }
668
669 static void
670 gst_rtp_session_init (GstRtpSession * rtpsession, GstRtpSessionClass * klass)
671 {
672   rtpsession->priv = GST_RTP_SESSION_GET_PRIVATE (rtpsession);
673   rtpsession->priv->lock = g_mutex_new ();
674   rtpsession->priv->session = rtp_session_new ();
675   /* configure callbacks */
676   rtp_session_set_callbacks (rtpsession->priv->session, &callbacks, rtpsession);
677   /* configure signals */
678   g_signal_connect (rtpsession->priv->session, "on-new-ssrc",
679       (GCallback) on_new_ssrc, rtpsession);
680   g_signal_connect (rtpsession->priv->session, "on-ssrc-collision",
681       (GCallback) on_ssrc_collision, rtpsession);
682   g_signal_connect (rtpsession->priv->session, "on-ssrc-validated",
683       (GCallback) on_ssrc_validated, rtpsession);
684   g_signal_connect (rtpsession->priv->session, "on-ssrc-active",
685       (GCallback) on_ssrc_active, rtpsession);
686   g_signal_connect (rtpsession->priv->session, "on-ssrc-sdes",
687       (GCallback) on_ssrc_sdes, rtpsession);
688   g_signal_connect (rtpsession->priv->session, "on-bye-ssrc",
689       (GCallback) on_bye_ssrc, rtpsession);
690   g_signal_connect (rtpsession->priv->session, "on-bye-timeout",
691       (GCallback) on_bye_timeout, rtpsession);
692   g_signal_connect (rtpsession->priv->session, "on-timeout",
693       (GCallback) on_timeout, rtpsession);
694   rtpsession->priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
695       (GDestroyNotify) gst_caps_unref);
696
697   gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
698   gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
699
700   rtpsession->priv->thread_stopped = TRUE;
701 }
702
703 static void
704 gst_rtp_session_finalize (GObject * object)
705 {
706   GstRtpSession *rtpsession;
707
708   rtpsession = GST_RTP_SESSION (object);
709
710   if (rtpsession->recv_rtp_sink != NULL)
711     gst_object_unref (rtpsession->recv_rtp_sink);
712   if (rtpsession->recv_rtcp_sink != NULL)
713     gst_object_unref (rtpsession->recv_rtcp_sink);
714   if (rtpsession->send_rtp_sink != NULL)
715     gst_object_unref (rtpsession->send_rtp_sink);
716   if (rtpsession->send_rtcp_src != NULL)
717     gst_object_unref (rtpsession->send_rtcp_src);
718
719   g_hash_table_destroy (rtpsession->priv->ptmap);
720   g_mutex_free (rtpsession->priv->lock);
721   g_object_unref (rtpsession->priv->session);
722
723   G_OBJECT_CLASS (parent_class)->finalize (object);
724 }
725
726 static void
727 gst_rtp_session_set_property (GObject * object, guint prop_id,
728     const GValue * value, GParamSpec * pspec)
729 {
730   GstRtpSession *rtpsession;
731   GstRtpSessionPrivate *priv;
732
733   rtpsession = GST_RTP_SESSION (object);
734   priv = rtpsession->priv;
735
736   switch (prop_id) {
737     case PROP_NTP_NS_BASE:
738       GST_OBJECT_LOCK (rtpsession);
739       priv->ntpnsbase = g_value_get_uint64 (value);
740       GST_DEBUG_OBJECT (rtpsession, "setting NTP base to %" GST_TIME_FORMAT,
741           GST_TIME_ARGS (priv->ntpnsbase));
742       GST_OBJECT_UNLOCK (rtpsession);
743       break;
744     case PROP_BANDWIDTH:
745       rtp_session_set_bandwidth (priv->session, g_value_get_double (value));
746       break;
747     case PROP_RTCP_FRACTION:
748       rtp_session_set_rtcp_fraction (priv->session, g_value_get_double (value));
749       break;
750     case PROP_SDES_CNAME:
751       rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_CNAME,
752           g_value_get_string (value));
753       break;
754     case PROP_SDES_NAME:
755       rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_NAME,
756           g_value_get_string (value));
757       break;
758     case PROP_SDES_EMAIL:
759       rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_EMAIL,
760           g_value_get_string (value));
761       break;
762     case PROP_SDES_PHONE:
763       rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_PHONE,
764           g_value_get_string (value));
765       break;
766     case PROP_SDES_LOCATION:
767       rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_LOC,
768           g_value_get_string (value));
769       break;
770     case PROP_SDES_TOOL:
771       rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_TOOL,
772           g_value_get_string (value));
773       break;
774     case PROP_SDES_NOTE:
775       rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_NOTE,
776           g_value_get_string (value));
777       break;
778     default:
779       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
780       break;
781   }
782 }
783
784 static void
785 gst_rtp_session_get_property (GObject * object, guint prop_id,
786     GValue * value, GParamSpec * pspec)
787 {
788   GstRtpSession *rtpsession;
789   GstRtpSessionPrivate *priv;
790
791   rtpsession = GST_RTP_SESSION (object);
792   priv = rtpsession->priv;
793
794   switch (prop_id) {
795     case PROP_NTP_NS_BASE:
796       GST_OBJECT_LOCK (rtpsession);
797       g_value_set_uint64 (value, priv->ntpnsbase);
798       GST_OBJECT_UNLOCK (rtpsession);
799       break;
800     case PROP_BANDWIDTH:
801       g_value_set_double (value, rtp_session_get_bandwidth (priv->session));
802       break;
803     case PROP_RTCP_FRACTION:
804       g_value_set_double (value, rtp_session_get_rtcp_fraction (priv->session));
805       break;
806     case PROP_SDES_CNAME:
807       g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
808               GST_RTCP_SDES_CNAME));
809       break;
810     case PROP_SDES_NAME:
811       g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
812               GST_RTCP_SDES_NAME));
813       break;
814     case PROP_SDES_EMAIL:
815       g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
816               GST_RTCP_SDES_EMAIL));
817       break;
818     case PROP_SDES_PHONE:
819       g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
820               GST_RTCP_SDES_PHONE));
821       break;
822     case PROP_SDES_LOCATION:
823       g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
824               GST_RTCP_SDES_LOC));
825       break;
826     case PROP_SDES_TOOL:
827       g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
828               GST_RTCP_SDES_TOOL));
829       break;
830     case PROP_SDES_NOTE:
831       g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
832               GST_RTCP_SDES_NOTE));
833       break;
834     case PROP_NUM_SOURCES:
835       g_value_set_uint (value, rtp_session_get_num_sources (priv->session));
836       break;
837     case PROP_NUM_ACTIVE_SOURCES:
838       g_value_set_uint (value,
839           rtp_session_get_num_active_sources (priv->session));
840       break;
841     default:
842       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
843       break;
844   }
845 }
846
847 static guint64
848 get_current_ntp_ns_time (GstRtpSession * rtpsession)
849 {
850   guint64 ntpnstime;
851   GstClock *clock;
852   GstClockTime base_time, ntpnsbase;
853
854   GST_OBJECT_LOCK (rtpsession);
855   if ((clock = GST_ELEMENT_CLOCK (rtpsession))) {
856     base_time = GST_ELEMENT_CAST (rtpsession)->base_time;
857     ntpnsbase = rtpsession->priv->ntpnsbase;
858     gst_object_ref (clock);
859     GST_OBJECT_UNLOCK (rtpsession);
860
861     /* get current NTP time */
862     ntpnstime = gst_clock_get_time (clock);
863     /* convert to running time */
864     ntpnstime -= base_time;
865     /* add NTP base offset */
866     ntpnstime += ntpnsbase;
867
868     gst_object_unref (clock);
869   } else {
870     GST_OBJECT_UNLOCK (rtpsession);
871     ntpnstime = -1;
872   }
873
874   return ntpnstime;
875 }
876
877 static void
878 rtcp_thread (GstRtpSession * rtpsession)
879 {
880   GstClock *sysclock;
881   GstClockID id;
882   GstClockTime current_time;
883   GstClockTime next_timeout;
884   guint64 ntpnstime;
885
886   /* for RTCP timeouts we use the system clock */
887   sysclock = gst_system_clock_obtain ();
888   if (sysclock == NULL)
889     goto no_sysclock;
890
891   current_time = gst_clock_get_time (sysclock);
892
893   GST_DEBUG_OBJECT (rtpsession, "entering RTCP thread");
894
895   GST_RTP_SESSION_LOCK (rtpsession);
896
897   while (!rtpsession->priv->stop_thread) {
898     GstClockReturn res;
899
900     /* get initial estimate */
901     next_timeout =
902         rtp_session_next_timeout (rtpsession->priv->session, current_time);
903
904     GST_DEBUG_OBJECT (rtpsession, "next check time %" GST_TIME_FORMAT,
905         GST_TIME_ARGS (next_timeout));
906
907     /* leave if no more timeouts, the session ended */
908     if (next_timeout == GST_CLOCK_TIME_NONE)
909       break;
910
911     id = rtpsession->priv->id =
912         gst_clock_new_single_shot_id (sysclock, next_timeout);
913     GST_RTP_SESSION_UNLOCK (rtpsession);
914
915     res = gst_clock_id_wait (id, NULL);
916
917     GST_RTP_SESSION_LOCK (rtpsession);
918     gst_clock_id_unref (id);
919     rtpsession->priv->id = NULL;
920
921     if (rtpsession->priv->stop_thread)
922       break;
923
924     /* update current time */
925     current_time = gst_clock_get_time (sysclock);
926
927     /* get current NTP time */
928     ntpnstime = get_current_ntp_ns_time (rtpsession);
929
930     /* we get unlocked because we need to perform reconsideration, don't perform
931      * the timeout but get a new reporting estimate. */
932     GST_DEBUG_OBJECT (rtpsession, "unlocked %d, current %" GST_TIME_FORMAT,
933         res, GST_TIME_ARGS (current_time));
934
935     /* perform actions, we ignore result. Release lock because it might push. */
936     GST_RTP_SESSION_UNLOCK (rtpsession);
937     rtp_session_on_timeout (rtpsession->priv->session, current_time, ntpnstime);
938     GST_RTP_SESSION_LOCK (rtpsession);
939   }
940   /* mark the thread as stopped now */
941   rtpsession->priv->thread_stopped = TRUE;
942   GST_RTP_SESSION_UNLOCK (rtpsession);
943
944   gst_object_unref (sysclock);
945
946   GST_DEBUG_OBJECT (rtpsession, "leaving RTCP thread");
947   return;
948
949   /* ERRORS */
950 no_sysclock:
951   {
952     GST_ELEMENT_ERROR (rtpsession, CORE, CLOCK, (NULL),
953         ("Could not get system clock"));
954     return;
955   }
956 }
957
958 static gboolean
959 start_rtcp_thread (GstRtpSession * rtpsession)
960 {
961   GError *error = NULL;
962   gboolean res;
963
964   GST_DEBUG_OBJECT (rtpsession, "starting RTCP thread");
965
966   GST_RTP_SESSION_LOCK (rtpsession);
967   rtpsession->priv->stop_thread = FALSE;
968   if (rtpsession->priv->thread_stopped) {
969     /* only create a new thread if the old one was stopped. Otherwise we can
970      * just reuse the currently running one. */
971     rtpsession->priv->thread =
972         g_thread_create ((GThreadFunc) rtcp_thread, rtpsession, TRUE, &error);
973     rtpsession->priv->thread_stopped = FALSE;
974   }
975   GST_RTP_SESSION_UNLOCK (rtpsession);
976
977   if (error != NULL) {
978     res = FALSE;
979     GST_DEBUG_OBJECT (rtpsession, "failed to start thread, %s", error->message);
980     g_error_free (error);
981   } else {
982     res = TRUE;
983   }
984   return res;
985 }
986
987 static void
988 stop_rtcp_thread (GstRtpSession * rtpsession)
989 {
990   GST_DEBUG_OBJECT (rtpsession, "stopping RTCP thread");
991
992   GST_RTP_SESSION_LOCK (rtpsession);
993   rtpsession->priv->stop_thread = TRUE;
994   if (rtpsession->priv->id)
995     gst_clock_id_unschedule (rtpsession->priv->id);
996   GST_RTP_SESSION_UNLOCK (rtpsession);
997 }
998
999 static void
1000 join_rtcp_thread (GstRtpSession * rtpsession)
1001 {
1002   GST_RTP_SESSION_LOCK (rtpsession);
1003   /* don't try to join when we have no thread */
1004   if (rtpsession->priv->thread != NULL) {
1005     GST_DEBUG_OBJECT (rtpsession, "joining RTCP thread");
1006     GST_RTP_SESSION_UNLOCK (rtpsession);
1007
1008     g_thread_join (rtpsession->priv->thread);
1009
1010     GST_RTP_SESSION_LOCK (rtpsession);
1011     /* after the join, take the lock and clear the thread structure. The caller
1012      * is supposed to not concurrently call start and join. */
1013     rtpsession->priv->thread = NULL;
1014   }
1015   GST_RTP_SESSION_UNLOCK (rtpsession);
1016 }
1017
1018 static GstStateChangeReturn
1019 gst_rtp_session_change_state (GstElement * element, GstStateChange transition)
1020 {
1021   GstStateChangeReturn res;
1022   GstRtpSession *rtpsession;
1023   GstRtpSessionPrivate *priv;
1024
1025   rtpsession = GST_RTP_SESSION (element);
1026   priv = rtpsession->priv;
1027
1028   switch (transition) {
1029     case GST_STATE_CHANGE_NULL_TO_READY:
1030       break;
1031     case GST_STATE_CHANGE_READY_TO_PAUSED:
1032       break;
1033     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1034       break;
1035     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1036     case GST_STATE_CHANGE_PAUSED_TO_READY:
1037       /* no need to join yet, we might want to continue later. Also, the
1038        * dataflow could block downstream so that a join could just block
1039        * forever. */
1040       stop_rtcp_thread (rtpsession);
1041       break;
1042     default:
1043       break;
1044   }
1045
1046   res = parent_class->change_state (element, transition);
1047
1048   switch (transition) {
1049     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1050       if (!start_rtcp_thread (rtpsession))
1051         goto failed_thread;
1052       break;
1053     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1054       break;
1055     case GST_STATE_CHANGE_PAUSED_TO_READY:
1056       /* downstream is now releasing the dataflow and we can join. */
1057       join_rtcp_thread (rtpsession);
1058       break;
1059     case GST_STATE_CHANGE_READY_TO_NULL:
1060       break;
1061     default:
1062       break;
1063   }
1064   return res;
1065
1066   /* ERRORS */
1067 failed_thread:
1068   {
1069     return GST_STATE_CHANGE_FAILURE;
1070   }
1071 }
1072
1073 static gboolean
1074 return_true (gpointer key, gpointer value, gpointer user_data)
1075 {
1076   return TRUE;
1077 }
1078
1079 static void
1080 gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession)
1081 {
1082   g_hash_table_foreach_remove (rtpsession->priv->ptmap, return_true, NULL);
1083 }
1084
1085 /* called when the session manager has an RTP packet ready for further
1086  * processing */
1087 static GstFlowReturn
1088 gst_rtp_session_process_rtp (RTPSession * sess, RTPSource * src,
1089     GstBuffer * buffer, gpointer user_data)
1090 {
1091   GstFlowReturn result;
1092   GstRtpSession *rtpsession;
1093   GstRtpSessionPrivate *priv;
1094
1095   rtpsession = GST_RTP_SESSION (user_data);
1096   priv = rtpsession->priv;
1097
1098   if (rtpsession->recv_rtp_src) {
1099     GST_DEBUG_OBJECT (rtpsession, "pushing received RTP packet");
1100     result = gst_pad_push (rtpsession->recv_rtp_src, buffer);
1101   } else {
1102     GST_DEBUG_OBJECT (rtpsession, "dropping received RTP packet");
1103     gst_buffer_unref (buffer);
1104     result = GST_FLOW_OK;
1105   }
1106   return result;
1107 }
1108
1109 /* called when the session manager has an RTP packet ready for further
1110  * sending */
1111 static GstFlowReturn
1112 gst_rtp_session_send_rtp (RTPSession * sess, RTPSource * src,
1113     GstBuffer * buffer, gpointer user_data)
1114 {
1115   GstFlowReturn result;
1116   GstRtpSession *rtpsession;
1117   GstRtpSessionPrivate *priv;
1118
1119   rtpsession = GST_RTP_SESSION (user_data);
1120   priv = rtpsession->priv;
1121
1122   GST_DEBUG_OBJECT (rtpsession, "sending RTP packet");
1123
1124   if (rtpsession->send_rtp_src) {
1125     result = gst_pad_push (rtpsession->send_rtp_src, buffer);
1126   } else {
1127     gst_buffer_unref (buffer);
1128     result = GST_FLOW_OK;
1129   }
1130   return result;
1131 }
1132
1133 /* called when the session manager has an RTCP packet ready for further
1134  * sending */
1135 static GstFlowReturn
1136 gst_rtp_session_send_rtcp (RTPSession * sess, RTPSource * src,
1137     GstBuffer * buffer, gpointer user_data)
1138 {
1139   GstFlowReturn result;
1140   GstRtpSession *rtpsession;
1141   GstRtpSessionPrivate *priv;
1142
1143   rtpsession = GST_RTP_SESSION (user_data);
1144   priv = rtpsession->priv;
1145
1146   if (rtpsession->send_rtcp_src) {
1147     GstCaps *caps;
1148
1149     /* set rtcp caps on output pad */
1150     if (!(caps = GST_PAD_CAPS (rtpsession->send_rtcp_src))) {
1151       caps = gst_caps_new_simple ("application/x-rtcp", NULL);
1152       gst_pad_set_caps (rtpsession->send_rtcp_src, caps);
1153       gst_caps_unref (caps);
1154     }
1155     gst_buffer_set_caps (buffer, caps);
1156     GST_DEBUG_OBJECT (rtpsession, "sending RTCP");
1157     result = gst_pad_push (rtpsession->send_rtcp_src, buffer);
1158   } else {
1159     GST_DEBUG_OBJECT (rtpsession, "not sending RTCP, no output pad");
1160     gst_buffer_unref (buffer);
1161     result = GST_FLOW_OK;
1162   }
1163   return result;
1164 }
1165
1166 /* called when the session manager has an SR RTCP packet ready for handling
1167  * inter stream synchronisation */
1168 static GstFlowReturn
1169 gst_rtp_session_sync_rtcp (RTPSession * sess,
1170     RTPSource * src, GstBuffer * buffer, gpointer user_data)
1171 {
1172   GstFlowReturn result;
1173   GstRtpSession *rtpsession;
1174   GstRtpSessionPrivate *priv;
1175
1176   rtpsession = GST_RTP_SESSION (user_data);
1177   priv = rtpsession->priv;
1178
1179   if (rtpsession->sync_src) {
1180     GstCaps *caps;
1181
1182     /* set rtcp caps on output pad */
1183     if (!(caps = GST_PAD_CAPS (rtpsession->sync_src))) {
1184       caps = gst_caps_new_simple ("application/x-rtcp", NULL);
1185       gst_pad_set_caps (rtpsession->sync_src, caps);
1186       gst_caps_unref (caps);
1187     }
1188     gst_buffer_set_caps (buffer, caps);
1189     GST_DEBUG_OBJECT (rtpsession, "sending Sync RTCP");
1190     result = gst_pad_push (rtpsession->sync_src, buffer);
1191   } else {
1192     GST_DEBUG_OBJECT (rtpsession, "not sending Sync RTCP, no output pad");
1193     gst_buffer_unref (buffer);
1194     result = GST_FLOW_OK;
1195   }
1196   return result;
1197 }
1198
1199 static void
1200 gst_rtp_session_cache_caps (GstRtpSession * rtpsession, GstCaps * caps)
1201 {
1202   GstRtpSessionPrivate *priv;
1203   const GstStructure *s;
1204   gint payload;
1205
1206   priv = rtpsession->priv;
1207
1208   GST_DEBUG_OBJECT (rtpsession, "parsing caps");
1209
1210   s = gst_caps_get_structure (caps, 0);
1211   if (!gst_structure_get_int (s, "payload", &payload))
1212     return;
1213
1214   if (g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (payload)))
1215     return;
1216
1217   g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (payload),
1218       gst_caps_ref (caps));
1219 }
1220
1221 /* called when the session manager needs the clock rate */
1222 static gint
1223 gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
1224     gpointer user_data)
1225 {
1226   gint ipayload, result = -1;
1227   GstRtpSession *rtpsession;
1228   GstRtpSessionPrivate *priv;
1229   GValue ret = { 0 };
1230   GValue args[2] = { {0}, {0} };
1231   GstCaps *caps;
1232   const GstStructure *s;
1233
1234   rtpsession = GST_RTP_SESSION_CAST (user_data);
1235   priv = rtpsession->priv;
1236
1237   GST_RTP_SESSION_LOCK (rtpsession);
1238   ipayload = payload;           /* make compiler happy */
1239   caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (ipayload));
1240   if (caps) {
1241     gst_caps_ref (caps);
1242     goto found;
1243   }
1244
1245   /* not found in the cache, try to get it with a signal */
1246   g_value_init (&args[0], GST_TYPE_ELEMENT);
1247   g_value_set_object (&args[0], rtpsession);
1248   g_value_init (&args[1], G_TYPE_UINT);
1249   g_value_set_uint (&args[1], payload);
1250
1251   g_value_init (&ret, GST_TYPE_CAPS);
1252   g_value_set_boxed (&ret, NULL);
1253
1254   g_signal_emitv (args, gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP], 0,
1255       &ret);
1256
1257   g_value_unset (&args[0]);
1258   g_value_unset (&args[1]);
1259   caps = (GstCaps *) g_value_dup_boxed (&ret);
1260   g_value_unset (&ret);
1261   if (!caps)
1262     goto no_caps;
1263
1264   gst_rtp_session_cache_caps (rtpsession, caps);
1265
1266 found:
1267   s = gst_caps_get_structure (caps, 0);
1268   if (!gst_structure_get_int (s, "clock-rate", &result))
1269     goto no_clock_rate;
1270
1271   gst_caps_unref (caps);
1272
1273   GST_DEBUG_OBJECT (rtpsession, "parsed clock-rate %d", result);
1274
1275 done:
1276   GST_RTP_SESSION_UNLOCK (rtpsession);
1277
1278   return result;
1279
1280   /* ERRORS */
1281 no_caps:
1282   {
1283     GST_DEBUG_OBJECT (rtpsession, "could not get caps");
1284     goto done;
1285   }
1286 no_clock_rate:
1287   {
1288     gst_caps_unref (caps);
1289     GST_DEBUG_OBJECT (rtpsession, "No clock-rate in caps!");
1290     goto done;
1291   }
1292 }
1293
1294 /* called when the session manager asks us to reconsider the timeout */
1295 static void
1296 gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data)
1297 {
1298   GstRtpSession *rtpsession;
1299
1300   rtpsession = GST_RTP_SESSION_CAST (user_data);
1301
1302   GST_RTP_SESSION_LOCK (rtpsession);
1303   GST_DEBUG_OBJECT (rtpsession, "unlock timer for reconsideration");
1304   if (rtpsession->priv->id)
1305     gst_clock_id_unschedule (rtpsession->priv->id);
1306   GST_RTP_SESSION_UNLOCK (rtpsession);
1307 }
1308
1309 static gboolean
1310 gst_rtp_session_event_recv_rtp_sink (GstPad * pad, GstEvent * event)
1311 {
1312   GstRtpSession *rtpsession;
1313   GstRtpSessionPrivate *priv;
1314   gboolean ret = FALSE;
1315
1316   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1317   priv = rtpsession->priv;
1318
1319   GST_DEBUG_OBJECT (rtpsession, "received event %s",
1320       GST_EVENT_TYPE_NAME (event));
1321
1322   switch (GST_EVENT_TYPE (event)) {
1323     case GST_EVENT_FLUSH_STOP:
1324       gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
1325       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1326       break;
1327     case GST_EVENT_NEWSEGMENT:
1328     {
1329       gboolean update;
1330       gdouble rate, arate;
1331       GstFormat format;
1332       gint64 start, stop, time;
1333       GstSegment *segment;
1334
1335       segment = &rtpsession->recv_rtp_seg;
1336
1337       /* the newsegment event is needed to convert the RTP timestamp to
1338        * running_time, which is needed to generate a mapping from RTP to NTP
1339        * timestamps in SR reports */
1340       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1341           &start, &stop, &time);
1342
1343       GST_DEBUG_OBJECT (rtpsession,
1344           "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1345           "format GST_FORMAT_TIME, "
1346           "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1347           ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1348           update, rate, arate, GST_TIME_ARGS (segment->start),
1349           GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1350           GST_TIME_ARGS (segment->accum));
1351
1352       gst_segment_set_newsegment_full (segment, update, rate,
1353           arate, format, start, stop, time);
1354
1355       /* push event forward */
1356       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1357       break;
1358     }
1359     default:
1360       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1361       break;
1362   }
1363   gst_object_unref (rtpsession);
1364
1365   return ret;
1366
1367 }
1368 static GList *
1369 gst_rtp_session_internal_links (GstPad * pad)
1370 {
1371   GstRtpSession *rtpsession;
1372   GstRtpSessionPrivate *priv;
1373   GList *res = NULL;
1374
1375   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1376   priv = rtpsession->priv;
1377
1378   if (pad == rtpsession->recv_rtp_src) {
1379     res = g_list_prepend (res, rtpsession->recv_rtp_sink);
1380   } else if (pad == rtpsession->recv_rtp_sink) {
1381     res = g_list_prepend (res, rtpsession->recv_rtp_src);
1382   } else if (pad == rtpsession->send_rtp_src) {
1383     res = g_list_prepend (res, rtpsession->send_rtp_sink);
1384   } else if (pad == rtpsession->send_rtp_sink) {
1385     res = g_list_prepend (res, rtpsession->send_rtp_src);
1386   }
1387
1388   gst_object_unref (rtpsession);
1389
1390   return res;
1391 }
1392
1393 static gboolean
1394 gst_rtp_session_sink_setcaps (GstPad * pad, GstCaps * caps)
1395 {
1396   GstRtpSession *rtpsession;
1397   GstRtpSessionPrivate *priv;
1398
1399   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1400   priv = rtpsession->priv;
1401
1402   GST_RTP_SESSION_LOCK (rtpsession);
1403   gst_rtp_session_cache_caps (rtpsession, caps);
1404   GST_RTP_SESSION_UNLOCK (rtpsession);
1405
1406   gst_object_unref (rtpsession);
1407
1408   return TRUE;
1409 }
1410
1411 /* receive a packet from a sender, send it to the RTP session manager and
1412  * forward the packet on the rtp_src pad
1413  */
1414 static GstFlowReturn
1415 gst_rtp_session_chain_recv_rtp (GstPad * pad, GstBuffer * buffer)
1416 {
1417   GstRtpSession *rtpsession;
1418   GstRtpSessionPrivate *priv;
1419   GstFlowReturn ret;
1420   guint64 ntpnstime;
1421   GstClockTime timestamp;
1422
1423   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1424   priv = rtpsession->priv;
1425
1426   GST_DEBUG_OBJECT (rtpsession, "received RTP packet");
1427
1428   /* get NTP time when this packet was captured, this depends on the timestamp. */
1429   timestamp = GST_BUFFER_TIMESTAMP (buffer);
1430   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1431     /* convert to running time using the segment values */
1432     ntpnstime =
1433         gst_segment_to_running_time (&rtpsession->recv_rtp_seg, GST_FORMAT_TIME,
1434         timestamp);
1435     /* add constant to convert running time to NTP time */
1436     ntpnstime += priv->ntpnsbase;
1437   } else {
1438     ntpnstime = get_current_ntp_ns_time (rtpsession);
1439   }
1440
1441   ret = rtp_session_process_rtp (priv->session, buffer, ntpnstime);
1442   if (ret != GST_FLOW_OK)
1443     goto push_error;
1444
1445
1446 done:
1447   gst_object_unref (rtpsession);
1448
1449   return ret;
1450
1451   /* ERRORS */
1452 push_error:
1453   {
1454     GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1455         gst_flow_get_name (ret));
1456     goto done;
1457   }
1458 }
1459
1460 static gboolean
1461 gst_rtp_session_event_recv_rtcp_sink (GstPad * pad, GstEvent * event)
1462 {
1463   GstRtpSession *rtpsession;
1464   GstRtpSessionPrivate *priv;
1465   gboolean ret = FALSE;
1466
1467   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1468   priv = rtpsession->priv;
1469
1470   GST_DEBUG_OBJECT (rtpsession, "received event %s",
1471       GST_EVENT_TYPE_NAME (event));
1472
1473   switch (GST_EVENT_TYPE (event)) {
1474     default:
1475       if (rtpsession->send_rtcp_src) {
1476         gst_event_ref (event);
1477         ret = gst_pad_push_event (rtpsession->send_rtcp_src, event);
1478       }
1479       ret = gst_pad_push_event (rtpsession->sync_src, event);
1480       break;
1481   }
1482   gst_object_unref (rtpsession);
1483
1484   return ret;
1485 }
1486
1487 /* Receive an RTCP packet from a sender, send it to the RTP session manager and
1488  * forward the SR packets to the sync_src pad.
1489  */
1490 static GstFlowReturn
1491 gst_rtp_session_chain_recv_rtcp (GstPad * pad, GstBuffer * buffer)
1492 {
1493   GstRtpSession *rtpsession;
1494   GstRtpSessionPrivate *priv;
1495   GstFlowReturn ret;
1496
1497   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1498   priv = rtpsession->priv;
1499
1500   GST_DEBUG_OBJECT (rtpsession, "received RTCP packet");
1501
1502   ret = rtp_session_process_rtcp (priv->session, buffer);
1503
1504   gst_object_unref (rtpsession);
1505
1506   return GST_FLOW_OK;
1507 }
1508
1509 static gboolean
1510 gst_rtp_session_event_send_rtp_sink (GstPad * pad, GstEvent * event)
1511 {
1512   GstRtpSession *rtpsession;
1513   GstRtpSessionPrivate *priv;
1514   gboolean ret = FALSE;
1515
1516   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1517   priv = rtpsession->priv;
1518
1519   GST_DEBUG_OBJECT (rtpsession, "received event");
1520
1521   switch (GST_EVENT_TYPE (event)) {
1522     case GST_EVENT_FLUSH_STOP:
1523       gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
1524       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1525       break;
1526     case GST_EVENT_NEWSEGMENT:
1527     {
1528       gboolean update;
1529       gdouble rate, arate;
1530       GstFormat format;
1531       gint64 start, stop, time;
1532       GstSegment *segment;
1533
1534       segment = &rtpsession->send_rtp_seg;
1535
1536       /* the newsegment event is needed to convert the RTP timestamp to
1537        * running_time, which is needed to generate a mapping from RTP to NTP
1538        * timestamps in SR reports */
1539       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1540           &start, &stop, &time);
1541
1542       GST_DEBUG_OBJECT (rtpsession,
1543           "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1544           "format GST_FORMAT_TIME, "
1545           "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1546           ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1547           update, rate, arate, GST_TIME_ARGS (segment->start),
1548           GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1549           GST_TIME_ARGS (segment->accum));
1550
1551       gst_segment_set_newsegment_full (segment, update, rate,
1552           arate, format, start, stop, time);
1553
1554       /* push event forward */
1555       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1556       break;
1557     }
1558     case GST_EVENT_EOS:
1559       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1560       break;
1561     default:
1562       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1563       break;
1564   }
1565   gst_object_unref (rtpsession);
1566
1567   return ret;
1568 }
1569
1570 static GstCaps *
1571 gst_rtp_session_getcaps_send_rtp (GstPad * pad)
1572 {
1573   GstRtpSession *rtpsession;
1574   GstRtpSessionPrivate *priv;
1575   GstCaps *result;
1576   GstStructure *s1, *s2;
1577
1578   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1579   priv = rtpsession->priv;
1580
1581   /* we can basically accept anything but we prefer to receive packets with our
1582    * internal SSRC so that we don't have to patch it. Create a structure with
1583    * the SSRC and another one without. */
1584   s1 = gst_structure_new ("application/x-rtp",
1585       "ssrc", G_TYPE_UINT, priv->session->source->ssrc, NULL);
1586   s2 = gst_structure_new ("application/x-rtp", NULL);
1587
1588   result = gst_caps_new_full (s1, s2, NULL);
1589
1590   GST_DEBUG_OBJECT (rtpsession, "getting caps %" GST_PTR_FORMAT, result);
1591
1592   gst_object_unref (rtpsession);
1593
1594   return result;
1595 }
1596
1597 /* Recieve an RTP packet to be send to the receivers, send to RTP session
1598  * manager and forward to send_rtp_src.
1599  */
1600 static GstFlowReturn
1601 gst_rtp_session_chain_send_rtp (GstPad * pad, GstBuffer * buffer)
1602 {
1603   GstRtpSession *rtpsession;
1604   GstRtpSessionPrivate *priv;
1605   GstFlowReturn ret;
1606   GstClockTime timestamp;
1607   guint64 ntpnstime;
1608
1609   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1610   priv = rtpsession->priv;
1611
1612   GST_DEBUG_OBJECT (rtpsession, "received RTP packet");
1613
1614   /* get NTP time when this packet was captured, this depends on the timestamp. */
1615   timestamp = GST_BUFFER_TIMESTAMP (buffer);
1616   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1617     /* convert to running time using the segment start value. */
1618     ntpnstime =
1619         gst_segment_to_running_time (&rtpsession->send_rtp_seg, GST_FORMAT_TIME,
1620         timestamp);
1621     /* convert to NTP time by adding the NTP base */
1622     ntpnstime += priv->ntpnsbase;
1623   } else {
1624     /* no timestamp, we could take the current running_time and convert it to
1625      * NTP time. */
1626     ntpnstime = -1;
1627   }
1628
1629   ret = rtp_session_send_rtp (priv->session, buffer, ntpnstime);
1630   if (ret != GST_FLOW_OK)
1631     goto push_error;
1632
1633 done:
1634   gst_object_unref (rtpsession);
1635
1636   return ret;
1637
1638   /* ERRORS */
1639 push_error:
1640   {
1641     GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1642         gst_flow_get_name (ret));
1643     goto done;
1644   }
1645 }
1646
1647 /* Create sinkpad to receive RTP packets from senders. This will also create a
1648  * srcpad for the RTP packets.
1649  */
1650 static GstPad *
1651 create_recv_rtp_sink (GstRtpSession * rtpsession)
1652 {
1653   GST_DEBUG_OBJECT (rtpsession, "creating RTP sink pad");
1654
1655   rtpsession->recv_rtp_sink =
1656       gst_pad_new_from_static_template (&rtpsession_recv_rtp_sink_template,
1657       "recv_rtp_sink");
1658   gst_pad_set_chain_function (rtpsession->recv_rtp_sink,
1659       gst_rtp_session_chain_recv_rtp);
1660   gst_pad_set_event_function (rtpsession->recv_rtp_sink,
1661       (GstPadEventFunction) gst_rtp_session_event_recv_rtp_sink);
1662   gst_pad_set_setcaps_function (rtpsession->recv_rtp_sink,
1663       gst_rtp_session_sink_setcaps);
1664   gst_pad_set_internal_link_function (rtpsession->recv_rtp_sink,
1665       gst_rtp_session_internal_links);
1666   gst_pad_set_active (rtpsession->recv_rtp_sink, TRUE);
1667   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1668       rtpsession->recv_rtp_sink);
1669
1670   GST_DEBUG_OBJECT (rtpsession, "creating RTP src pad");
1671   rtpsession->recv_rtp_src =
1672       gst_pad_new_from_static_template (&rtpsession_recv_rtp_src_template,
1673       "recv_rtp_src");
1674   gst_pad_set_internal_link_function (rtpsession->recv_rtp_src,
1675       gst_rtp_session_internal_links);
1676   gst_pad_use_fixed_caps (rtpsession->recv_rtp_src);
1677   gst_pad_set_active (rtpsession->recv_rtp_src, TRUE);
1678   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->recv_rtp_src);
1679
1680   return rtpsession->recv_rtp_sink;
1681 }
1682
1683 /* Create a sinkpad to receive RTCP messages from senders, this will also create a
1684  * sync_src pad for the SR packets.
1685  */
1686 static GstPad *
1687 create_recv_rtcp_sink (GstRtpSession * rtpsession)
1688 {
1689   GST_DEBUG_OBJECT (rtpsession, "creating RTCP sink pad");
1690
1691   rtpsession->recv_rtcp_sink =
1692       gst_pad_new_from_static_template (&rtpsession_recv_rtcp_sink_template,
1693       "recv_rtcp_sink");
1694   gst_pad_set_chain_function (rtpsession->recv_rtcp_sink,
1695       gst_rtp_session_chain_recv_rtcp);
1696   gst_pad_set_event_function (rtpsession->recv_rtcp_sink,
1697       (GstPadEventFunction) gst_rtp_session_event_recv_rtcp_sink);
1698   gst_pad_set_active (rtpsession->recv_rtcp_sink, TRUE);
1699   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1700       rtpsession->recv_rtcp_sink);
1701
1702   GST_DEBUG_OBJECT (rtpsession, "creating sync src pad");
1703   rtpsession->sync_src =
1704       gst_pad_new_from_static_template (&rtpsession_sync_src_template,
1705       "sync_src");
1706   gst_pad_use_fixed_caps (rtpsession->sync_src);
1707   gst_pad_set_active (rtpsession->sync_src, TRUE);
1708   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
1709
1710   return rtpsession->recv_rtcp_sink;
1711 }
1712
1713 /* Create a sinkpad to receive RTP packets for receivers. This will also create a
1714  * send_rtp_src pad.
1715  */
1716 static GstPad *
1717 create_send_rtp_sink (GstRtpSession * rtpsession)
1718 {
1719   GST_DEBUG_OBJECT (rtpsession, "creating pad");
1720
1721   rtpsession->send_rtp_sink =
1722       gst_pad_new_from_static_template (&rtpsession_send_rtp_sink_template,
1723       "send_rtp_sink");
1724   gst_pad_set_chain_function (rtpsession->send_rtp_sink,
1725       gst_rtp_session_chain_send_rtp);
1726   gst_pad_set_getcaps_function (rtpsession->send_rtp_sink,
1727       gst_rtp_session_getcaps_send_rtp);
1728   gst_pad_set_event_function (rtpsession->send_rtp_sink,
1729       (GstPadEventFunction) gst_rtp_session_event_send_rtp_sink);
1730   gst_pad_set_internal_link_function (rtpsession->send_rtp_sink,
1731       gst_rtp_session_internal_links);
1732   gst_pad_set_active (rtpsession->send_rtp_sink, TRUE);
1733   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1734       rtpsession->send_rtp_sink);
1735
1736   rtpsession->send_rtp_src =
1737       gst_pad_new_from_static_template (&rtpsession_send_rtp_src_template,
1738       "send_rtp_src");
1739   gst_pad_set_internal_link_function (rtpsession->send_rtp_src,
1740       gst_rtp_session_internal_links);
1741   gst_pad_set_active (rtpsession->send_rtp_src, TRUE);
1742   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->send_rtp_src);
1743
1744   return rtpsession->send_rtp_sink;
1745 }
1746
1747 /* Create a srcpad with the RTCP packets to send out.
1748  * This pad will be driven by the RTP session manager when it wants to send out
1749  * RTCP packets.
1750  */
1751 static GstPad *
1752 create_send_rtcp_src (GstRtpSession * rtpsession)
1753 {
1754   GST_DEBUG_OBJECT (rtpsession, "creating pad");
1755
1756   rtpsession->send_rtcp_src =
1757       gst_pad_new_from_static_template (&rtpsession_send_rtcp_src_template,
1758       "send_rtcp_src");
1759   gst_pad_use_fixed_caps (rtpsession->send_rtcp_src);
1760   gst_pad_set_active (rtpsession->send_rtcp_src, TRUE);
1761   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1762       rtpsession->send_rtcp_src);
1763
1764   return rtpsession->send_rtcp_src;
1765 }
1766
1767 static GstPad *
1768 gst_rtp_session_request_new_pad (GstElement * element,
1769     GstPadTemplate * templ, const gchar * name)
1770 {
1771   GstRtpSession *rtpsession;
1772   GstElementClass *klass;
1773   GstPad *result;
1774
1775   g_return_val_if_fail (templ != NULL, NULL);
1776   g_return_val_if_fail (GST_IS_RTP_SESSION (element), NULL);
1777
1778   rtpsession = GST_RTP_SESSION (element);
1779   klass = GST_ELEMENT_GET_CLASS (element);
1780
1781   GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
1782
1783   GST_RTP_SESSION_LOCK (rtpsession);
1784
1785   /* figure out the template */
1786   if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink")) {
1787     if (rtpsession->recv_rtp_sink != NULL)
1788       goto exists;
1789
1790     result = create_recv_rtp_sink (rtpsession);
1791   } else if (templ == gst_element_class_get_pad_template (klass,
1792           "recv_rtcp_sink")) {
1793     if (rtpsession->recv_rtcp_sink != NULL)
1794       goto exists;
1795
1796     result = create_recv_rtcp_sink (rtpsession);
1797   } else if (templ == gst_element_class_get_pad_template (klass,
1798           "send_rtp_sink")) {
1799     if (rtpsession->send_rtp_sink != NULL)
1800       goto exists;
1801
1802     result = create_send_rtp_sink (rtpsession);
1803   } else if (templ == gst_element_class_get_pad_template (klass,
1804           "send_rtcp_src")) {
1805     if (rtpsession->send_rtcp_src != NULL)
1806       goto exists;
1807
1808     result = create_send_rtcp_src (rtpsession);
1809   } else
1810     goto wrong_template;
1811
1812   GST_RTP_SESSION_UNLOCK (rtpsession);
1813
1814   return result;
1815
1816   /* ERRORS */
1817 wrong_template:
1818   {
1819     GST_RTP_SESSION_UNLOCK (rtpsession);
1820     g_warning ("gstrtpsession: this is not our template");
1821     return NULL;
1822   }
1823 exists:
1824   {
1825     GST_RTP_SESSION_UNLOCK (rtpsession);
1826     g_warning ("gstrtpsession: pad already requested");
1827     return NULL;
1828   }
1829 }
1830
1831 static void
1832 gst_rtp_session_release_pad (GstElement * element, GstPad * pad)
1833 {
1834 }