Use new gst_element_class_set_static_metadata()
[platform/upstream/gstreamer.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  * @see_also: gstrtpjitterbuffer, gstrtpbin, gstrtpptdemux, gstrtpssrcdemux
23  *
24  * The RTP session manager models one participant with a unique SSRC in an RTP
25  * session. This session can be used to send and receive RTP and RTCP packets.
26  * Based on what REQUEST pads are requested from the session manager, specific
27  * functionality can be activated.
28  *
29  * The session manager currently implements RFC 3550 including:
30  * <itemizedlist>
31  *   <listitem>
32  *     <para>RTP packet validation based on consecutive sequence numbers.</para>
33  *   </listitem>
34  *   <listitem>
35  *     <para>Maintainance of the SSRC participant database.</para>
36  *   </listitem>
37  *   <listitem>
38  *     <para>Keeping per participant statistics based on received RTCP packets.</para>
39  *   </listitem>
40  *   <listitem>
41  *     <para>Scheduling of RR/SR RTCP packets.</para>
42  *   </listitem>
43  * </itemizedlist>
44  *
45  * The gstrtpsession will not demux packets based on SSRC or payload type, nor will
46  * it correct for packet reordering and jitter. Use #GstRtpsSrcDemux,
47  * #GstRtpPtDemux and GstRtpJitterBuffer in addition to #GstRtpSession to
48  * perform these tasks. It is usually a good idea to use #GstRtpBin, which
49  * combines all these features in one element.
50  *
51  * To use #GstRtpSession as an RTP receiver, request a recv_rtp_sink pad, which will
52  * automatically create recv_rtp_src pad. Data received on the recv_rtp_sink pad
53  * will be processed in the session and after being validated forwarded on the
54  * recv_rtp_src pad.
55  *
56  * To also use #GstRtpSession as an RTCP receiver, request a recv_rtcp_sink pad,
57  * which will automatically create a sync_src pad. Packets received on the RTCP
58  * pad will be used by the session manager to update the stats and database of
59  * the other participants. SR packets will be forwarded on the sync_src pad
60  * so that they can be used to perform inter-stream synchronisation when needed.
61  *
62  * If you want the session manager to generate and send RTCP packets, request
63  * the send_rtcp_src pad. Packet pushed on this pad contain SR/RR RTCP reports
64  * that should be sent to all participants in the session.
65  *
66  * To use #GstRtpSession as a sender, request a send_rtp_sink pad, which will
67  * automatically create a send_rtp_src pad. The session manager will modify the
68  * SSRC in the RTP packets to its own SSRC and wil forward the packets on the
69  * send_rtp_src pad after updating its internal state.
70  *
71  * The session manager needs the clock-rate of the payload types it is handling
72  * and will signal the #GstRtpSession::request-pt-map signal when it needs such a
73  * mapping. One can clear the cached values with the #GstRtpSession::clear-pt-map
74  * signal.
75  *
76  * <refsect2>
77  * <title>Example pipelines</title>
78  * |[
79  * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink gstrtpsession .recv_rtp_src ! rtptheoradepay ! theoradec ! xvimagesink
80  * ]| Receive theora RTP packets from port 5000 and send them to the depayloader,
81  * decoder and display. Note that the application/x-rtp caps on udpsrc should be
82  * configured based on some negotiation process such as RTSP for this pipeline
83  * to work correctly.
84  * |[
85  * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink gstrtpsession name=session \
86  *        .recv_rtp_src ! rtptheoradepay ! theoradec ! xvimagesink \
87  *     udpsrc port=5001 caps="application/x-rtcp" ! session.recv_rtcp_sink
88  * ]| Receive theora RTP packets from port 5000 and send them to the depayloader,
89  * decoder and display. Receive RTCP packets from port 5001 and process them in
90  * the session manager.
91  * Note that the application/x-rtp caps on udpsrc should be
92  * configured based on some negotiation process such as RTSP for this pipeline
93  * to work correctly.
94  * |[
95  * gst-launch videotestsrc ! theoraenc ! rtptheorapay ! .send_rtp_sink gstrtpsession .send_rtp_src ! udpsink port=5000
96  * ]| Send theora RTP packets through the session manager and out on UDP port
97  * 5000.
98  * |[
99  * gst-launch videotestsrc ! theoraenc ! rtptheorapay ! .send_rtp_sink gstrtpsession name=session .send_rtp_src \
100  *     ! udpsink port=5000  session.send_rtcp_src ! udpsink port=5001
101  * ]| Send theora RTP packets through the session manager and out on UDP port
102  * 5000. Send RTCP packets on port 5001. Note that this pipeline will not preroll
103  * correctly because the second udpsink will not preroll correctly (no RTCP
104  * packets are sent in the PAUSED state). Applications should manually set and
105  * keep (see gst_element_set_locked_state()) the RTCP udpsink to the PLAYING state.
106  * </refsect2>
107  *
108  * Last reviewed on 2007-05-28 (0.10.5)
109  */
110
111 #ifdef HAVE_CONFIG_H
112 #include "config.h"
113 #endif
114
115 #include <gst/rtp/gstrtpbuffer.h>
116
117 #include <gst/glib-compat-private.h>
118
119 #include "gstrtpbin-marshal.h"
120 #include "gstrtpsession.h"
121 #include "rtpsession.h"
122
123 GST_DEBUG_CATEGORY_STATIC (gst_rtp_session_debug);
124 #define GST_CAT_DEFAULT gst_rtp_session_debug
125
126 /* sink pads */
127 static GstStaticPadTemplate rtpsession_recv_rtp_sink_template =
128 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink",
129     GST_PAD_SINK,
130     GST_PAD_REQUEST,
131     GST_STATIC_CAPS ("application/x-rtp")
132     );
133
134 static GstStaticPadTemplate rtpsession_recv_rtcp_sink_template =
135 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink",
136     GST_PAD_SINK,
137     GST_PAD_REQUEST,
138     GST_STATIC_CAPS ("application/x-rtcp")
139     );
140
141 static GstStaticPadTemplate rtpsession_send_rtp_sink_template =
142 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink",
143     GST_PAD_SINK,
144     GST_PAD_REQUEST,
145     GST_STATIC_CAPS ("application/x-rtp")
146     );
147
148 /* src pads */
149 static GstStaticPadTemplate rtpsession_recv_rtp_src_template =
150 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src",
151     GST_PAD_SRC,
152     GST_PAD_SOMETIMES,
153     GST_STATIC_CAPS ("application/x-rtp")
154     );
155
156 static GstStaticPadTemplate rtpsession_sync_src_template =
157 GST_STATIC_PAD_TEMPLATE ("sync_src",
158     GST_PAD_SRC,
159     GST_PAD_SOMETIMES,
160     GST_STATIC_CAPS ("application/x-rtcp")
161     );
162
163 static GstStaticPadTemplate rtpsession_send_rtp_src_template =
164 GST_STATIC_PAD_TEMPLATE ("send_rtp_src",
165     GST_PAD_SRC,
166     GST_PAD_SOMETIMES,
167     GST_STATIC_CAPS ("application/x-rtp")
168     );
169
170 static GstStaticPadTemplate rtpsession_send_rtcp_src_template =
171 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src",
172     GST_PAD_SRC,
173     GST_PAD_REQUEST,
174     GST_STATIC_CAPS ("application/x-rtcp")
175     );
176
177 /* signals and args */
178 enum
179 {
180   SIGNAL_REQUEST_PT_MAP,
181   SIGNAL_CLEAR_PT_MAP,
182
183   SIGNAL_ON_NEW_SSRC,
184   SIGNAL_ON_SSRC_COLLISION,
185   SIGNAL_ON_SSRC_VALIDATED,
186   SIGNAL_ON_SSRC_ACTIVE,
187   SIGNAL_ON_SSRC_SDES,
188   SIGNAL_ON_BYE_SSRC,
189   SIGNAL_ON_BYE_TIMEOUT,
190   SIGNAL_ON_TIMEOUT,
191   SIGNAL_ON_SENDER_TIMEOUT,
192   LAST_SIGNAL
193 };
194
195 #define DEFAULT_NTP_NS_BASE          0
196 #define DEFAULT_BANDWIDTH            RTP_STATS_BANDWIDTH
197 #define DEFAULT_RTCP_FRACTION        (RTP_STATS_BANDWIDTH * RTP_STATS_RTCP_FRACTION)
198 #define DEFAULT_RTCP_RR_BANDWIDTH    -1
199 #define DEFAULT_RTCP_RS_BANDWIDTH    -1
200 #define DEFAULT_SDES                 NULL
201 #define DEFAULT_NUM_SOURCES          0
202 #define DEFAULT_NUM_ACTIVE_SOURCES   0
203 #define DEFAULT_USE_PIPELINE_CLOCK   FALSE
204 #define DEFAULT_RTCP_MIN_INTERVAL    (RTP_STATS_MIN_INTERVAL * GST_SECOND)
205
206 enum
207 {
208   PROP_0,
209   PROP_NTP_NS_BASE,
210   PROP_BANDWIDTH,
211   PROP_RTCP_FRACTION,
212   PROP_RTCP_RR_BANDWIDTH,
213   PROP_RTCP_RS_BANDWIDTH,
214   PROP_SDES,
215   PROP_NUM_SOURCES,
216   PROP_NUM_ACTIVE_SOURCES,
217   PROP_INTERNAL_SESSION,
218   PROP_USE_PIPELINE_CLOCK,
219   PROP_RTCP_MIN_INTERVAL,
220   PROP_LAST
221 };
222
223 #define GST_RTP_SESSION_GET_PRIVATE(obj)  \
224            (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_SESSION, GstRtpSessionPrivate))
225
226 #define GST_RTP_SESSION_LOCK(sess)   g_mutex_lock (&(sess)->priv->lock)
227 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock (&(sess)->priv->lock)
228
229 struct _GstRtpSessionPrivate
230 {
231   GMutex lock;
232   GstClock *sysclock;
233
234   RTPSession *session;
235
236   /* thread for sending out RTCP */
237   GstClockID id;
238   gboolean stop_thread;
239   GThread *thread;
240   gboolean thread_stopped;
241
242   /* caps mapping */
243   GHashTable *ptmap;
244
245   /* NTP base time */
246   guint64 ntpnsbase;
247   gboolean use_pipeline_clock;
248 };
249
250 /* callbacks to handle actions from the session manager */
251 static GstFlowReturn gst_rtp_session_process_rtp (RTPSession * sess,
252     RTPSource * src, GstBuffer * buffer, gpointer user_data);
253 static GstFlowReturn gst_rtp_session_send_rtp (RTPSession * sess,
254     RTPSource * src, gpointer data, gpointer user_data);
255 static GstFlowReturn gst_rtp_session_send_rtcp (RTPSession * sess,
256     RTPSource * src, GstBuffer * buffer, gboolean eos, gpointer user_data);
257 static GstFlowReturn gst_rtp_session_sync_rtcp (RTPSession * sess,
258     RTPSource * src, GstBuffer * buffer, gpointer user_data);
259 static gint gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
260     gpointer user_data);
261 static void gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data);
262 static void gst_rtp_session_request_key_unit (RTPSession * sess,
263     gboolean all_headers, gpointer user_data);
264 static GstClockTime gst_rtp_session_request_time (RTPSession * session,
265     gpointer user_data);
266
267 static RTPSessionCallbacks callbacks = {
268   gst_rtp_session_process_rtp,
269   gst_rtp_session_send_rtp,
270   gst_rtp_session_sync_rtcp,
271   gst_rtp_session_send_rtcp,
272   gst_rtp_session_clock_rate,
273   gst_rtp_session_reconsider,
274   gst_rtp_session_request_key_unit,
275   gst_rtp_session_request_time
276 };
277
278 /* GObject vmethods */
279 static void gst_rtp_session_finalize (GObject * object);
280 static void gst_rtp_session_set_property (GObject * object, guint prop_id,
281     const GValue * value, GParamSpec * pspec);
282 static void gst_rtp_session_get_property (GObject * object, guint prop_id,
283     GValue * value, GParamSpec * pspec);
284
285 /* GstElement vmethods */
286 static GstStateChangeReturn gst_rtp_session_change_state (GstElement * element,
287     GstStateChange transition);
288 static GstPad *gst_rtp_session_request_new_pad (GstElement * element,
289     GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
290 static void gst_rtp_session_release_pad (GstElement * element, GstPad * pad);
291
292 static gboolean gst_rtp_session_sink_setcaps (GstPad * pad,
293     GstRtpSession * rtpsession, GstCaps * caps);
294 static gboolean gst_rtp_session_setcaps_send_rtp (GstPad * pad,
295     GstRtpSession * rtpsession, GstCaps * caps);
296
297 static void gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession);
298
299 static guint gst_rtp_session_signals[LAST_SIGNAL] = { 0 };
300
301 static void
302 on_new_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
303 {
304   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0,
305       src->ssrc);
306 }
307
308 static void
309 on_ssrc_collision (RTPSession * session, RTPSource * src, GstRtpSession * sess)
310 {
311   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
312       src->ssrc);
313 }
314
315 static void
316 on_ssrc_validated (RTPSession * session, RTPSource * src, GstRtpSession * sess)
317 {
318   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
319       src->ssrc);
320 }
321
322 static void
323 on_ssrc_active (RTPSession * session, RTPSource * src, GstRtpSession * sess)
324 {
325   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
326       src->ssrc);
327 }
328
329 static void
330 on_ssrc_sdes (RTPSession * session, RTPSource * src, GstRtpSession * sess)
331 {
332   GstStructure *s;
333   GstMessage *m;
334
335   /* convert the new SDES info into a message */
336   RTP_SESSION_LOCK (session);
337   g_object_get (src, "sdes", &s, NULL);
338   RTP_SESSION_UNLOCK (session);
339
340   m = gst_message_new_custom (GST_MESSAGE_ELEMENT, GST_OBJECT (sess), s);
341   gst_element_post_message (GST_ELEMENT_CAST (sess), m);
342
343   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0,
344       src->ssrc);
345 }
346
347 static void
348 on_bye_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
349 {
350   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0,
351       src->ssrc);
352 }
353
354 static void
355 on_bye_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
356 {
357   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
358       src->ssrc);
359 }
360
361 static void
362 on_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
363 {
364   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_TIMEOUT], 0,
365       src->ssrc);
366 }
367
368 static void
369 on_sender_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
370 {
371   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
372       src->ssrc);
373 }
374
375 #define gst_rtp_session_parent_class parent_class
376 G_DEFINE_TYPE (GstRtpSession, gst_rtp_session, GST_TYPE_ELEMENT);
377
378 static void
379 gst_rtp_session_class_init (GstRtpSessionClass * klass)
380 {
381   GObjectClass *gobject_class;
382   GstElementClass *gstelement_class;
383
384   gobject_class = (GObjectClass *) klass;
385   gstelement_class = (GstElementClass *) klass;
386
387   g_type_class_add_private (klass, sizeof (GstRtpSessionPrivate));
388
389   gobject_class->finalize = gst_rtp_session_finalize;
390   gobject_class->set_property = gst_rtp_session_set_property;
391   gobject_class->get_property = gst_rtp_session_get_property;
392
393   /**
394    * GstRtpSession::request-pt-map:
395    * @sess: the object which received the signal
396    * @pt: the pt
397    *
398    * Request the payload type as #GstCaps for @pt.
399    */
400   gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP] =
401       g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
402       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, request_pt_map),
403       NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT, GST_TYPE_CAPS, 1,
404       G_TYPE_UINT);
405   /**
406    * GstRtpSession::clear-pt-map:
407    * @sess: the object which received the signal
408    *
409    * Clear the cached pt-maps requested with #GstRtpSession::request-pt-map.
410    */
411   gst_rtp_session_signals[SIGNAL_CLEAR_PT_MAP] =
412       g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
413       G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpSessionClass, clear_pt_map),
414       NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
415
416   /**
417    * GstRtpSession::on-new-ssrc:
418    * @sess: the object which received the signal
419    * @ssrc: the SSRC
420    *
421    * Notify of a new SSRC that entered @session.
422    */
423   gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
424       g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
425       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_new_ssrc),
426       NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
427   /**
428    * GstRtpSession::on-ssrc_collision:
429    * @sess: the object which received the signal
430    * @ssrc: the SSRC
431    *
432    * Notify when we have an SSRC collision
433    */
434   gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
435       g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
436       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
437           on_ssrc_collision), NULL, NULL, g_cclosure_marshal_VOID__UINT,
438       G_TYPE_NONE, 1, G_TYPE_UINT);
439   /**
440    * GstRtpSession::on-ssrc_validated:
441    * @sess: the object which received the signal
442    * @ssrc: the SSRC
443    *
444    * Notify of a new SSRC that became validated.
445    */
446   gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
447       g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
448       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
449           on_ssrc_validated), NULL, NULL, g_cclosure_marshal_VOID__UINT,
450       G_TYPE_NONE, 1, G_TYPE_UINT);
451   /**
452    * GstRtpSession::on-ssrc_active:
453    * @sess: the object which received the signal
454    * @ssrc: the SSRC
455    *
456    * Notify of a SSRC that is active, i.e., sending RTCP.
457    */
458   gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
459       g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
460       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
461           on_ssrc_active), NULL, NULL, g_cclosure_marshal_VOID__UINT,
462       G_TYPE_NONE, 1, G_TYPE_UINT);
463   /**
464    * GstRtpSession::on-ssrc-sdes:
465    * @session: the object which received the signal
466    * @src: the SSRC
467    *
468    * Notify that a new SDES was received for SSRC.
469    */
470   gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
471       g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
472       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_ssrc_sdes),
473       NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
474
475   /**
476    * GstRtpSession::on-bye-ssrc:
477    * @sess: the object which received the signal
478    * @ssrc: the SSRC
479    *
480    * Notify of an SSRC that became inactive because of a BYE packet.
481    */
482   gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
483       g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
484       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_ssrc),
485       NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
486   /**
487    * GstRtpSession::on-bye-timeout:
488    * @sess: the object which received the signal
489    * @ssrc: the SSRC
490    *
491    * Notify of an SSRC that has timed out because of BYE
492    */
493   gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
494       g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
495       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_timeout),
496       NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
497   /**
498    * GstRtpSession::on-timeout:
499    * @sess: the object which received the signal
500    * @ssrc: the SSRC
501    *
502    * Notify of an SSRC that has timed out
503    */
504   gst_rtp_session_signals[SIGNAL_ON_TIMEOUT] =
505       g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
506       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_timeout),
507       NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
508   /**
509    * GstRtpSession::on-sender-timeout:
510    * @sess: the object which received the signal
511    * @ssrc: the SSRC
512    *
513    * Notify of a sender SSRC that has timed out and became a receiver
514    */
515   gst_rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
516       g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
517       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
518           on_sender_timeout), NULL, NULL, g_cclosure_marshal_VOID__UINT,
519       G_TYPE_NONE, 1, G_TYPE_UINT);
520
521   g_object_class_install_property (gobject_class, PROP_NTP_NS_BASE,
522       g_param_spec_uint64 ("ntp-ns-base", "NTP base time",
523           "The NTP base time corresponding to running_time 0 (deprecated)", 0,
524           G_MAXUINT64, DEFAULT_NTP_NS_BASE,
525           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
526
527   g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
528       g_param_spec_double ("bandwidth", "Bandwidth",
529           "The bandwidth of the session in bytes per second (0 for auto-discover)",
530           0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
531           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
532
533   g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
534       g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
535           "The RTCP bandwidth of the session in bytes per second "
536           "(or as a real fraction of the RTP bandwidth if < 1.0)",
537           0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
538           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
539
540   g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
541       g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
542           "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
543           -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
544           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
545
546   g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
547       g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
548           "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
549           -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
550           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
551
552   g_object_class_install_property (gobject_class, PROP_SDES,
553       g_param_spec_boxed ("sdes", "SDES",
554           "The SDES items of this session",
555           GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
556
557   g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
558       g_param_spec_uint ("num-sources", "Num Sources",
559           "The number of sources in the session", 0, G_MAXUINT,
560           DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
561
562   g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
563       g_param_spec_uint ("num-active-sources", "Num Active Sources",
564           "The number of active sources in the session", 0, G_MAXUINT,
565           DEFAULT_NUM_ACTIVE_SOURCES,
566           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
567
568   g_object_class_install_property (gobject_class, PROP_INTERNAL_SESSION,
569       g_param_spec_object ("internal-session", "Internal Session",
570           "The internal RTPSession object", RTP_TYPE_SESSION,
571           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
572
573   g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
574       g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
575           "Use the pipeline clock to set the NTP time in the RTCP SR messages",
576           DEFAULT_USE_PIPELINE_CLOCK,
577           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
578
579   g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
580       g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
581           "Minimum interval between Regular RTCP packet (in ns)",
582           0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
583           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
584
585   gstelement_class->change_state =
586       GST_DEBUG_FUNCPTR (gst_rtp_session_change_state);
587   gstelement_class->request_new_pad =
588       GST_DEBUG_FUNCPTR (gst_rtp_session_request_new_pad);
589   gstelement_class->release_pad =
590       GST_DEBUG_FUNCPTR (gst_rtp_session_release_pad);
591
592   klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_session_clear_pt_map);
593
594   /* sink pads */
595   gst_element_class_add_pad_template (gstelement_class,
596       gst_static_pad_template_get (&rtpsession_recv_rtp_sink_template));
597   gst_element_class_add_pad_template (gstelement_class,
598       gst_static_pad_template_get (&rtpsession_recv_rtcp_sink_template));
599   gst_element_class_add_pad_template (gstelement_class,
600       gst_static_pad_template_get (&rtpsession_send_rtp_sink_template));
601
602   /* src pads */
603   gst_element_class_add_pad_template (gstelement_class,
604       gst_static_pad_template_get (&rtpsession_recv_rtp_src_template));
605   gst_element_class_add_pad_template (gstelement_class,
606       gst_static_pad_template_get (&rtpsession_sync_src_template));
607   gst_element_class_add_pad_template (gstelement_class,
608       gst_static_pad_template_get (&rtpsession_send_rtp_src_template));
609   gst_element_class_add_pad_template (gstelement_class,
610       gst_static_pad_template_get (&rtpsession_send_rtcp_src_template));
611
612   gst_element_class_set_static_metadata (gstelement_class, "RTP Session",
613       "Filter/Network/RTP",
614       "Implement an RTP session", "Wim Taymans <wim.taymans@gmail.com>");
615
616   GST_DEBUG_CATEGORY_INIT (gst_rtp_session_debug,
617       "rtpsession", 0, "RTP Session");
618 }
619
620 static void
621 gst_rtp_session_init (GstRtpSession * rtpsession)
622 {
623   rtpsession->priv = GST_RTP_SESSION_GET_PRIVATE (rtpsession);
624   g_mutex_init (&rtpsession->priv->lock);
625   rtpsession->priv->sysclock = gst_system_clock_obtain ();
626   rtpsession->priv->session = rtp_session_new ();
627   rtpsession->priv->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
628
629   /* configure callbacks */
630   rtp_session_set_callbacks (rtpsession->priv->session, &callbacks, rtpsession);
631   /* configure signals */
632   g_signal_connect (rtpsession->priv->session, "on-new-ssrc",
633       (GCallback) on_new_ssrc, rtpsession);
634   g_signal_connect (rtpsession->priv->session, "on-ssrc-collision",
635       (GCallback) on_ssrc_collision, rtpsession);
636   g_signal_connect (rtpsession->priv->session, "on-ssrc-validated",
637       (GCallback) on_ssrc_validated, rtpsession);
638   g_signal_connect (rtpsession->priv->session, "on-ssrc-active",
639       (GCallback) on_ssrc_active, rtpsession);
640   g_signal_connect (rtpsession->priv->session, "on-ssrc-sdes",
641       (GCallback) on_ssrc_sdes, rtpsession);
642   g_signal_connect (rtpsession->priv->session, "on-bye-ssrc",
643       (GCallback) on_bye_ssrc, rtpsession);
644   g_signal_connect (rtpsession->priv->session, "on-bye-timeout",
645       (GCallback) on_bye_timeout, rtpsession);
646   g_signal_connect (rtpsession->priv->session, "on-timeout",
647       (GCallback) on_timeout, rtpsession);
648   g_signal_connect (rtpsession->priv->session, "on-sender-timeout",
649       (GCallback) on_sender_timeout, rtpsession);
650   rtpsession->priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
651       (GDestroyNotify) gst_caps_unref);
652
653   gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
654   gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
655
656   rtpsession->priv->thread_stopped = TRUE;
657 }
658
659 static void
660 gst_rtp_session_finalize (GObject * object)
661 {
662   GstRtpSession *rtpsession;
663
664   rtpsession = GST_RTP_SESSION (object);
665
666   g_hash_table_destroy (rtpsession->priv->ptmap);
667   g_mutex_clear (&rtpsession->priv->lock);
668   g_object_unref (rtpsession->priv->sysclock);
669   g_object_unref (rtpsession->priv->session);
670
671   G_OBJECT_CLASS (parent_class)->finalize (object);
672 }
673
674 static void
675 gst_rtp_session_set_property (GObject * object, guint prop_id,
676     const GValue * value, GParamSpec * pspec)
677 {
678   GstRtpSession *rtpsession;
679   GstRtpSessionPrivate *priv;
680
681   rtpsession = GST_RTP_SESSION (object);
682   priv = rtpsession->priv;
683
684   switch (prop_id) {
685     case PROP_NTP_NS_BASE:
686       GST_OBJECT_LOCK (rtpsession);
687       priv->ntpnsbase = g_value_get_uint64 (value);
688       GST_DEBUG_OBJECT (rtpsession, "setting NTP base to %" GST_TIME_FORMAT,
689           GST_TIME_ARGS (priv->ntpnsbase));
690       GST_OBJECT_UNLOCK (rtpsession);
691       break;
692     case PROP_BANDWIDTH:
693       g_object_set_property (G_OBJECT (priv->session), "bandwidth", value);
694       break;
695     case PROP_RTCP_FRACTION:
696       g_object_set_property (G_OBJECT (priv->session), "rtcp-fraction", value);
697       break;
698     case PROP_RTCP_RR_BANDWIDTH:
699       g_object_set_property (G_OBJECT (priv->session), "rtcp-rr-bandwidth",
700           value);
701       break;
702     case PROP_RTCP_RS_BANDWIDTH:
703       g_object_set_property (G_OBJECT (priv->session), "rtcp-rs-bandwidth",
704           value);
705       break;
706     case PROP_SDES:
707       rtp_session_set_sdes_struct (priv->session, g_value_get_boxed (value));
708       break;
709     case PROP_USE_PIPELINE_CLOCK:
710       priv->use_pipeline_clock = g_value_get_boolean (value);
711       break;
712     case PROP_RTCP_MIN_INTERVAL:
713       g_object_set_property (G_OBJECT (priv->session), "rtcp-min-interval",
714           value);
715       break;
716     default:
717       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
718       break;
719   }
720 }
721
722 static void
723 gst_rtp_session_get_property (GObject * object, guint prop_id,
724     GValue * value, GParamSpec * pspec)
725 {
726   GstRtpSession *rtpsession;
727   GstRtpSessionPrivate *priv;
728
729   rtpsession = GST_RTP_SESSION (object);
730   priv = rtpsession->priv;
731
732   switch (prop_id) {
733     case PROP_NTP_NS_BASE:
734       GST_OBJECT_LOCK (rtpsession);
735       g_value_set_uint64 (value, priv->ntpnsbase);
736       GST_OBJECT_UNLOCK (rtpsession);
737       break;
738     case PROP_BANDWIDTH:
739       g_object_get_property (G_OBJECT (priv->session), "bandwidth", value);
740       break;
741     case PROP_RTCP_FRACTION:
742       g_object_get_property (G_OBJECT (priv->session), "rtcp-fraction", value);
743       break;
744     case PROP_RTCP_RR_BANDWIDTH:
745       g_object_get_property (G_OBJECT (priv->session), "rtcp-rr-bandwidth",
746           value);
747       break;
748     case PROP_RTCP_RS_BANDWIDTH:
749       g_object_get_property (G_OBJECT (priv->session), "rtcp-rs-bandwidth",
750           value);
751       break;
752     case PROP_SDES:
753       g_value_take_boxed (value, rtp_session_get_sdes_struct (priv->session));
754       break;
755     case PROP_NUM_SOURCES:
756       g_value_set_uint (value, rtp_session_get_num_sources (priv->session));
757       break;
758     case PROP_NUM_ACTIVE_SOURCES:
759       g_value_set_uint (value,
760           rtp_session_get_num_active_sources (priv->session));
761       break;
762     case PROP_INTERNAL_SESSION:
763       g_value_set_object (value, priv->session);
764       break;
765     case PROP_USE_PIPELINE_CLOCK:
766       g_value_set_boolean (value, priv->use_pipeline_clock);
767       break;
768     case PROP_RTCP_MIN_INTERVAL:
769       g_object_get_property (G_OBJECT (priv->session), "rtcp-min-interval",
770           value);
771       break;
772     default:
773       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
774       break;
775   }
776 }
777
778 static void
779 get_current_times (GstRtpSession * rtpsession, GstClockTime * running_time,
780     guint64 * ntpnstime)
781 {
782   guint64 ntpns;
783   GstClock *clock;
784   GstClockTime base_time, rt, clock_time;
785
786   GST_OBJECT_LOCK (rtpsession);
787   if ((clock = GST_ELEMENT_CLOCK (rtpsession))) {
788     base_time = GST_ELEMENT_CAST (rtpsession)->base_time;
789     gst_object_ref (clock);
790     GST_OBJECT_UNLOCK (rtpsession);
791
792     clock_time = gst_clock_get_time (clock);
793
794     if (rtpsession->priv->use_pipeline_clock) {
795       ntpns = clock_time;
796     } else {
797       GTimeVal current;
798
799       /* get current NTP time */
800       g_get_current_time (&current);
801       ntpns = GST_TIMEVAL_TO_TIME (current);
802     }
803
804     /* add constant to convert from 1970 based time to 1900 based time */
805     ntpns += (2208988800LL * GST_SECOND);
806
807     /* get current clock time and convert to running time */
808     rt = clock_time - base_time;
809
810     gst_object_unref (clock);
811   } else {
812     GST_OBJECT_UNLOCK (rtpsession);
813     rt = -1;
814     ntpns = -1;
815   }
816   if (running_time)
817     *running_time = rt;
818   if (ntpnstime)
819     *ntpnstime = ntpns;
820 }
821
822 static void
823 rtcp_thread (GstRtpSession * rtpsession)
824 {
825   GstClockID id;
826   GstClockTime current_time;
827   GstClockTime next_timeout;
828   guint64 ntpnstime;
829   GstClockTime running_time;
830   RTPSession *session;
831   GstClock *sysclock;
832
833   GST_DEBUG_OBJECT (rtpsession, "entering RTCP thread");
834
835   GST_RTP_SESSION_LOCK (rtpsession);
836
837   sysclock = rtpsession->priv->sysclock;
838   current_time = gst_clock_get_time (sysclock);
839
840   session = rtpsession->priv->session;
841
842   GST_DEBUG_OBJECT (rtpsession, "starting at %" GST_TIME_FORMAT,
843       GST_TIME_ARGS (current_time));
844   session->start_time = current_time;
845
846   while (!rtpsession->priv->stop_thread) {
847     GstClockReturn res;
848
849     /* get initial estimate */
850     next_timeout = rtp_session_next_timeout (session, current_time);
851
852     GST_DEBUG_OBJECT (rtpsession, "next check time %" GST_TIME_FORMAT,
853         GST_TIME_ARGS (next_timeout));
854
855     /* leave if no more timeouts, the session ended */
856     if (next_timeout == GST_CLOCK_TIME_NONE)
857       break;
858
859     id = rtpsession->priv->id =
860         gst_clock_new_single_shot_id (sysclock, next_timeout);
861     GST_RTP_SESSION_UNLOCK (rtpsession);
862
863     res = gst_clock_id_wait (id, NULL);
864
865     GST_RTP_SESSION_LOCK (rtpsession);
866     gst_clock_id_unref (id);
867     rtpsession->priv->id = NULL;
868
869     if (rtpsession->priv->stop_thread)
870       break;
871
872     /* update current time */
873     current_time = gst_clock_get_time (sysclock);
874
875     /* get current NTP time */
876     get_current_times (rtpsession, &running_time, &ntpnstime);
877
878     /* we get unlocked because we need to perform reconsideration, don't perform
879      * the timeout but get a new reporting estimate. */
880     GST_DEBUG_OBJECT (rtpsession, "unlocked %d, current %" GST_TIME_FORMAT,
881         res, GST_TIME_ARGS (current_time));
882
883     /* perform actions, we ignore result. Release lock because it might push. */
884     GST_RTP_SESSION_UNLOCK (rtpsession);
885     rtp_session_on_timeout (session, current_time, ntpnstime, running_time);
886     GST_RTP_SESSION_LOCK (rtpsession);
887   }
888   /* mark the thread as stopped now */
889   rtpsession->priv->thread_stopped = TRUE;
890   GST_RTP_SESSION_UNLOCK (rtpsession);
891
892   GST_DEBUG_OBJECT (rtpsession, "leaving RTCP thread");
893 }
894
895 static gboolean
896 start_rtcp_thread (GstRtpSession * rtpsession)
897 {
898   GError *error = NULL;
899   gboolean res;
900
901   GST_DEBUG_OBJECT (rtpsession, "starting RTCP thread");
902
903   GST_RTP_SESSION_LOCK (rtpsession);
904   rtpsession->priv->stop_thread = FALSE;
905   if (rtpsession->priv->thread_stopped) {
906     /* if the thread stopped, and we still have a handle to the thread, join it
907      * now. We can safely join with the lock held, the thread will not take it
908      * anymore. */
909     if (rtpsession->priv->thread)
910       g_thread_join (rtpsession->priv->thread);
911     /* only create a new thread if the old one was stopped. Otherwise we can
912      * just reuse the currently running one. */
913     rtpsession->priv->thread = g_thread_try_new ("rtpsession-rtcp-thread",
914         (GThreadFunc) rtcp_thread, rtpsession, &error);
915     rtpsession->priv->thread_stopped = FALSE;
916   }
917   GST_RTP_SESSION_UNLOCK (rtpsession);
918
919   if (error != NULL) {
920     res = FALSE;
921     GST_DEBUG_OBJECT (rtpsession, "failed to start thread, %s", error->message);
922     g_error_free (error);
923   } else {
924     res = TRUE;
925   }
926   return res;
927 }
928
929 static void
930 stop_rtcp_thread (GstRtpSession * rtpsession)
931 {
932   GST_DEBUG_OBJECT (rtpsession, "stopping RTCP thread");
933
934   GST_RTP_SESSION_LOCK (rtpsession);
935   rtpsession->priv->stop_thread = TRUE;
936   if (rtpsession->priv->id)
937     gst_clock_id_unschedule (rtpsession->priv->id);
938   GST_RTP_SESSION_UNLOCK (rtpsession);
939 }
940
941 static void
942 join_rtcp_thread (GstRtpSession * rtpsession)
943 {
944   GST_RTP_SESSION_LOCK (rtpsession);
945   /* don't try to join when we have no thread */
946   if (rtpsession->priv->thread != NULL) {
947     GST_DEBUG_OBJECT (rtpsession, "joining RTCP thread");
948     GST_RTP_SESSION_UNLOCK (rtpsession);
949
950     g_thread_join (rtpsession->priv->thread);
951
952     GST_RTP_SESSION_LOCK (rtpsession);
953     /* after the join, take the lock and clear the thread structure. The caller
954      * is supposed to not concurrently call start and join. */
955     rtpsession->priv->thread = NULL;
956   }
957   GST_RTP_SESSION_UNLOCK (rtpsession);
958 }
959
960 static GstStateChangeReturn
961 gst_rtp_session_change_state (GstElement * element, GstStateChange transition)
962 {
963   GstStateChangeReturn res;
964   GstRtpSession *rtpsession;
965
966   rtpsession = GST_RTP_SESSION (element);
967
968   switch (transition) {
969     case GST_STATE_CHANGE_NULL_TO_READY:
970       break;
971     case GST_STATE_CHANGE_READY_TO_PAUSED:
972       break;
973     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
974       break;
975     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
976     case GST_STATE_CHANGE_PAUSED_TO_READY:
977       /* no need to join yet, we might want to continue later. Also, the
978        * dataflow could block downstream so that a join could just block
979        * forever. */
980       stop_rtcp_thread (rtpsession);
981       break;
982     default:
983       break;
984   }
985
986   res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
987
988   switch (transition) {
989     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
990       if (!start_rtcp_thread (rtpsession))
991         goto failed_thread;
992       break;
993     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
994       break;
995     case GST_STATE_CHANGE_PAUSED_TO_READY:
996       /* downstream is now releasing the dataflow and we can join. */
997       join_rtcp_thread (rtpsession);
998       break;
999     case GST_STATE_CHANGE_READY_TO_NULL:
1000       break;
1001     default:
1002       break;
1003   }
1004   return res;
1005
1006   /* ERRORS */
1007 failed_thread:
1008   {
1009     return GST_STATE_CHANGE_FAILURE;
1010   }
1011 }
1012
1013 static gboolean
1014 return_true (gpointer key, gpointer value, gpointer user_data)
1015 {
1016   return TRUE;
1017 }
1018
1019 static void
1020 gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession)
1021 {
1022   g_hash_table_foreach_remove (rtpsession->priv->ptmap, return_true, NULL);
1023 }
1024
1025 /* called when the session manager has an RTP packet or a list of packets
1026  * ready for further processing */
1027 static GstFlowReturn
1028 gst_rtp_session_process_rtp (RTPSession * sess, RTPSource * src,
1029     GstBuffer * buffer, gpointer user_data)
1030 {
1031   GstFlowReturn result;
1032   GstRtpSession *rtpsession;
1033   GstPad *rtp_src;
1034
1035   rtpsession = GST_RTP_SESSION (user_data);
1036
1037   GST_RTP_SESSION_LOCK (rtpsession);
1038   if ((rtp_src = rtpsession->recv_rtp_src))
1039     gst_object_ref (rtp_src);
1040   GST_RTP_SESSION_UNLOCK (rtpsession);
1041
1042   if (rtp_src) {
1043     GST_LOG_OBJECT (rtpsession, "pushing received RTP packet");
1044     result = gst_pad_push (rtp_src, buffer);
1045     gst_object_unref (rtp_src);
1046   } else {
1047     GST_DEBUG_OBJECT (rtpsession, "dropping received RTP packet");
1048     gst_buffer_unref (buffer);
1049     result = GST_FLOW_OK;
1050   }
1051   return result;
1052 }
1053
1054 /* called when the session manager has an RTP packet ready for further
1055  * sending */
1056 static GstFlowReturn
1057 gst_rtp_session_send_rtp (RTPSession * sess, RTPSource * src,
1058     gpointer data, gpointer user_data)
1059 {
1060   GstFlowReturn result;
1061   GstRtpSession *rtpsession;
1062   GstPad *rtp_src;
1063
1064   rtpsession = GST_RTP_SESSION (user_data);
1065
1066   GST_RTP_SESSION_LOCK (rtpsession);
1067   if ((rtp_src = rtpsession->send_rtp_src))
1068     gst_object_ref (rtp_src);
1069   GST_RTP_SESSION_UNLOCK (rtpsession);
1070
1071   if (rtp_src) {
1072     if (GST_IS_BUFFER (data)) {
1073       GST_LOG_OBJECT (rtpsession, "sending RTP packet");
1074       result = gst_pad_push (rtp_src, GST_BUFFER_CAST (data));
1075     } else {
1076       GST_LOG_OBJECT (rtpsession, "sending RTP list");
1077       result = gst_pad_push_list (rtp_src, GST_BUFFER_LIST_CAST (data));
1078     }
1079     gst_object_unref (rtp_src);
1080   } else {
1081     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1082     result = GST_FLOW_OK;
1083   }
1084   return result;
1085 }
1086
1087 /* called when the session manager has an RTCP packet ready for further
1088  * sending. The eos flag is set when an EOS event should be sent downstream as
1089  * well. */
1090 static GstFlowReturn
1091 gst_rtp_session_send_rtcp (RTPSession * sess, RTPSource * src,
1092     GstBuffer * buffer, gboolean eos, gpointer user_data)
1093 {
1094   GstFlowReturn result;
1095   GstRtpSession *rtpsession;
1096   GstPad *rtcp_src;
1097
1098   rtpsession = GST_RTP_SESSION (user_data);
1099
1100   GST_RTP_SESSION_LOCK (rtpsession);
1101   if (rtpsession->priv->stop_thread)
1102     goto stopping;
1103
1104   if ((rtcp_src = rtpsession->send_rtcp_src)) {
1105     GstCaps *caps;
1106
1107     gst_object_ref (rtcp_src);
1108     GST_RTP_SESSION_UNLOCK (rtpsession);
1109
1110     /* set rtcp caps on output pad */
1111     if (!(caps = gst_pad_get_current_caps (rtcp_src))) {
1112       caps = gst_caps_new_empty_simple ("application/x-rtcp");
1113       gst_pad_set_caps (rtcp_src, caps);
1114     }
1115     gst_caps_unref (caps);
1116
1117     GST_LOG_OBJECT (rtpsession, "sending RTCP");
1118     result = gst_pad_push (rtcp_src, buffer);
1119
1120     /* we have to send EOS after this packet */
1121     if (eos) {
1122       GST_LOG_OBJECT (rtpsession, "sending EOS");
1123       gst_pad_push_event (rtcp_src, gst_event_new_eos ());
1124     }
1125     gst_object_unref (rtcp_src);
1126   } else {
1127     GST_RTP_SESSION_UNLOCK (rtpsession);
1128
1129     GST_DEBUG_OBJECT (rtpsession, "not sending RTCP, no output pad");
1130     gst_buffer_unref (buffer);
1131     result = GST_FLOW_OK;
1132   }
1133   return result;
1134
1135   /* ERRORS */
1136 stopping:
1137   {
1138     GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1139     gst_buffer_unref (buffer);
1140     GST_RTP_SESSION_UNLOCK (rtpsession);
1141     return GST_FLOW_OK;
1142   }
1143 }
1144
1145 /* called when the session manager has an SR RTCP packet ready for handling
1146  * inter stream synchronisation */
1147 static GstFlowReturn
1148 gst_rtp_session_sync_rtcp (RTPSession * sess, RTPSource * src,
1149     GstBuffer * buffer, gpointer user_data)
1150 {
1151   GstFlowReturn result;
1152   GstRtpSession *rtpsession;
1153   GstPad *sync_src;
1154
1155   rtpsession = GST_RTP_SESSION (user_data);
1156
1157   GST_RTP_SESSION_LOCK (rtpsession);
1158   if (rtpsession->priv->stop_thread)
1159     goto stopping;
1160
1161   if ((sync_src = rtpsession->sync_src)) {
1162     GstCaps *caps;
1163
1164     gst_object_ref (sync_src);
1165     GST_RTP_SESSION_UNLOCK (rtpsession);
1166
1167     /* set rtcp caps on output pad */
1168     if (!(caps = gst_pad_get_current_caps (sync_src))) {
1169       caps = gst_caps_new_empty_simple ("application/x-rtcp");
1170       gst_pad_set_caps (sync_src, caps);
1171     }
1172     gst_caps_unref (caps);
1173
1174     GST_LOG_OBJECT (rtpsession, "sending Sync RTCP");
1175     result = gst_pad_push (sync_src, buffer);
1176     gst_object_unref (sync_src);
1177   } else {
1178     GST_RTP_SESSION_UNLOCK (rtpsession);
1179
1180     GST_DEBUG_OBJECT (rtpsession, "not sending Sync RTCP, no output pad");
1181     gst_buffer_unref (buffer);
1182     result = GST_FLOW_OK;
1183   }
1184   return result;
1185
1186   /* ERRORS */
1187 stopping:
1188   {
1189     GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1190     gst_buffer_unref (buffer);
1191     GST_RTP_SESSION_UNLOCK (rtpsession);
1192     return GST_FLOW_OK;
1193   }
1194 }
1195
1196 static void
1197 gst_rtp_session_cache_caps (GstRtpSession * rtpsession, GstCaps * caps)
1198 {
1199   GstRtpSessionPrivate *priv;
1200   const GstStructure *s;
1201   gint payload;
1202
1203   priv = rtpsession->priv;
1204
1205   GST_DEBUG_OBJECT (rtpsession, "parsing caps");
1206
1207   s = gst_caps_get_structure (caps, 0);
1208   if (!gst_structure_get_int (s, "payload", &payload))
1209     return;
1210
1211   if (g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (payload)))
1212     return;
1213
1214   g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (payload),
1215       gst_caps_ref (caps));
1216 }
1217
1218 static GstCaps *
1219 gst_rtp_session_get_caps_for_pt (GstRtpSession * rtpsession, guint payload)
1220 {
1221   GstCaps *caps = NULL;
1222   GValue args[2] = { {0}, {0} };
1223   GValue ret = { 0 };
1224
1225   GST_RTP_SESSION_LOCK (rtpsession);
1226   caps = g_hash_table_lookup (rtpsession->priv->ptmap,
1227       GINT_TO_POINTER (payload));
1228   if (caps) {
1229     gst_caps_ref (caps);
1230     goto done;
1231   }
1232
1233   /* not found in the cache, try to get it with a signal */
1234   g_value_init (&args[0], GST_TYPE_ELEMENT);
1235   g_value_set_object (&args[0], rtpsession);
1236   g_value_init (&args[1], G_TYPE_UINT);
1237   g_value_set_uint (&args[1], payload);
1238
1239   g_value_init (&ret, GST_TYPE_CAPS);
1240   g_value_set_boxed (&ret, NULL);
1241
1242   GST_RTP_SESSION_UNLOCK (rtpsession);
1243
1244   g_signal_emitv (args, gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP], 0,
1245       &ret);
1246
1247   GST_RTP_SESSION_LOCK (rtpsession);
1248
1249   g_value_unset (&args[0]);
1250   g_value_unset (&args[1]);
1251   caps = (GstCaps *) g_value_dup_boxed (&ret);
1252   g_value_unset (&ret);
1253   if (!caps)
1254     goto no_caps;
1255
1256   gst_rtp_session_cache_caps (rtpsession, caps);
1257
1258 done:
1259   GST_RTP_SESSION_UNLOCK (rtpsession);
1260
1261   return caps;
1262
1263 no_caps:
1264   {
1265     GST_DEBUG_OBJECT (rtpsession, "could not get caps");
1266     goto done;
1267   }
1268 }
1269
1270 /* called when the session manager needs the clock rate */
1271 static gint
1272 gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
1273     gpointer user_data)
1274 {
1275   gint result = -1;
1276   GstRtpSession *rtpsession;
1277   GstCaps *caps;
1278   const GstStructure *s;
1279
1280   rtpsession = GST_RTP_SESSION_CAST (user_data);
1281
1282   caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1283
1284   if (!caps)
1285     goto done;
1286
1287   s = gst_caps_get_structure (caps, 0);
1288   if (!gst_structure_get_int (s, "clock-rate", &result))
1289     goto no_clock_rate;
1290
1291   gst_caps_unref (caps);
1292
1293   GST_DEBUG_OBJECT (rtpsession, "parsed clock-rate %d", result);
1294
1295 done:
1296
1297   return result;
1298
1299   /* ERRORS */
1300 no_clock_rate:
1301   {
1302     gst_caps_unref (caps);
1303     GST_DEBUG_OBJECT (rtpsession, "No clock-rate in caps!");
1304     goto done;
1305   }
1306 }
1307
1308 /* called when the session manager asks us to reconsider the timeout */
1309 static void
1310 gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data)
1311 {
1312   GstRtpSession *rtpsession;
1313
1314   rtpsession = GST_RTP_SESSION_CAST (user_data);
1315
1316   GST_RTP_SESSION_LOCK (rtpsession);
1317   GST_DEBUG_OBJECT (rtpsession, "unlock timer for reconsideration");
1318   if (rtpsession->priv->id)
1319     gst_clock_id_unschedule (rtpsession->priv->id);
1320   GST_RTP_SESSION_UNLOCK (rtpsession);
1321 }
1322
1323 static gboolean
1324 gst_rtp_session_event_recv_rtp_sink (GstPad * pad, GstObject * parent,
1325     GstEvent * event)
1326 {
1327   GstRtpSession *rtpsession;
1328   gboolean ret = FALSE;
1329
1330   rtpsession = GST_RTP_SESSION (parent);
1331
1332   GST_DEBUG_OBJECT (rtpsession, "received event %s",
1333       GST_EVENT_TYPE_NAME (event));
1334
1335   switch (GST_EVENT_TYPE (event)) {
1336     case GST_EVENT_CAPS:
1337     {
1338       GstCaps *caps;
1339
1340       /* process */
1341       gst_event_parse_caps (event, &caps);
1342       gst_rtp_session_sink_setcaps (pad, rtpsession, caps);
1343       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1344       break;
1345     }
1346     case GST_EVENT_FLUSH_STOP:
1347       gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
1348       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1349       break;
1350     case GST_EVENT_SEGMENT:
1351     {
1352       GstSegment *segment, in_segment;
1353
1354       segment = &rtpsession->recv_rtp_seg;
1355
1356       /* the newsegment event is needed to convert the RTP timestamp to
1357        * running_time, which is needed to generate a mapping from RTP to NTP
1358        * timestamps in SR reports */
1359       gst_event_copy_segment (event, &in_segment);
1360       GST_DEBUG_OBJECT (rtpsession, "received segment %" GST_SEGMENT_FORMAT,
1361           &in_segment);
1362
1363       /* accept upstream */
1364       gst_segment_copy_into (&in_segment, segment);
1365
1366       /* push event forward */
1367       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1368       break;
1369     }
1370     default:
1371       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1372       break;
1373   }
1374
1375   return ret;
1376
1377 }
1378
1379 static gboolean
1380 gst_rtp_session_request_remote_key_unit (GstRtpSession * rtpsession,
1381     guint32 ssrc, guint payload, gboolean all_headers, gint count)
1382 {
1383   GstCaps *caps;
1384
1385   caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1386
1387   if (caps) {
1388     const GstStructure *s = gst_caps_get_structure (caps, 0);
1389     gboolean pli;
1390     gboolean fir;
1391
1392     pli = gst_structure_has_field (s, "rtcp-fb-nack-pli");
1393     fir = gst_structure_has_field (s, "rtcp-fb-ccm-fir") && all_headers;
1394
1395     /* Google Talk uses FIR for repair, so send it even if we just want a
1396      * regular PLI */
1397     if (!pli &&
1398         gst_structure_has_field (s, "rtcp-fb-x-gstreamer-fir-as-repair"))
1399       fir = TRUE;
1400
1401     gst_caps_unref (caps);
1402
1403     if (pli || fir)
1404       return rtp_session_request_key_unit (rtpsession->priv->session, ssrc,
1405           gst_clock_get_time (rtpsession->priv->sysclock), fir, count);
1406   }
1407
1408   return FALSE;
1409 }
1410
1411 static gboolean
1412 gst_rtp_session_event_recv_rtp_src (GstPad * pad, GstObject * parent,
1413     GstEvent * event)
1414 {
1415   GstRtpSession *rtpsession;
1416   gboolean forward = TRUE;
1417   gboolean ret = TRUE;
1418   const GstStructure *s;
1419   guint32 ssrc;
1420   guint pt;
1421
1422   rtpsession = GST_RTP_SESSION (parent);
1423
1424   switch (GST_EVENT_TYPE (event)) {
1425     case GST_EVENT_CUSTOM_UPSTREAM:
1426       s = gst_event_get_structure (event);
1427       if (gst_structure_has_name (s, "GstForceKeyUnit") &&
1428           gst_structure_get_uint (s, "ssrc", &ssrc) &&
1429           gst_structure_get_uint (s, "payload", &pt)) {
1430         gboolean all_headers = FALSE;
1431         gint count = -1;
1432
1433         gst_structure_get_boolean (s, "all-headers", &all_headers);
1434         if (gst_structure_get_int (s, "count", &count) && count < 0)
1435           count += G_MAXINT;    /* Make sure count is positive if present */
1436         if (gst_rtp_session_request_remote_key_unit (rtpsession, ssrc, pt,
1437                 all_headers, count))
1438           forward = FALSE;
1439       }
1440       break;
1441     default:
1442       break;
1443   }
1444
1445   if (forward)
1446     ret = gst_pad_push_event (rtpsession->recv_rtp_sink, event);
1447
1448   return ret;
1449 }
1450
1451
1452 static GstIterator *
1453 gst_rtp_session_iterate_internal_links (GstPad * pad, GstObject * parent)
1454 {
1455   GstRtpSession *rtpsession;
1456   GstPad *otherpad = NULL;
1457   GstIterator *it = NULL;
1458
1459   rtpsession = GST_RTP_SESSION (parent);
1460
1461   GST_RTP_SESSION_LOCK (rtpsession);
1462   if (pad == rtpsession->recv_rtp_src) {
1463     otherpad = gst_object_ref (rtpsession->recv_rtp_sink);
1464   } else if (pad == rtpsession->recv_rtp_sink) {
1465     otherpad = gst_object_ref (rtpsession->recv_rtp_src);
1466   } else if (pad == rtpsession->send_rtp_src) {
1467     otherpad = gst_object_ref (rtpsession->send_rtp_sink);
1468   } else if (pad == rtpsession->send_rtp_sink) {
1469     otherpad = gst_object_ref (rtpsession->send_rtp_src);
1470   }
1471   GST_RTP_SESSION_UNLOCK (rtpsession);
1472
1473   if (otherpad) {
1474     GValue val = { 0, };
1475
1476     g_value_init (&val, GST_TYPE_PAD);
1477     g_value_set_object (&val, otherpad);
1478     it = gst_iterator_new_single (GST_TYPE_PAD, &val);
1479     g_value_unset (&val);
1480     gst_object_unref (otherpad);
1481   }
1482
1483   return it;
1484 }
1485
1486 static gboolean
1487 gst_rtp_session_sink_setcaps (GstPad * pad, GstRtpSession * rtpsession,
1488     GstCaps * caps)
1489 {
1490   GST_RTP_SESSION_LOCK (rtpsession);
1491   gst_rtp_session_cache_caps (rtpsession, caps);
1492   GST_RTP_SESSION_UNLOCK (rtpsession);
1493
1494   return TRUE;
1495 }
1496
1497 /* receive a packet from a sender, send it to the RTP session manager and
1498  * forward the packet on the rtp_src pad
1499  */
1500 static GstFlowReturn
1501 gst_rtp_session_chain_recv_rtp (GstPad * pad, GstObject * parent,
1502     GstBuffer * buffer)
1503 {
1504   GstRtpSession *rtpsession;
1505   GstRtpSessionPrivate *priv;
1506   GstFlowReturn ret;
1507   GstClockTime current_time, running_time;
1508   GstClockTime timestamp;
1509
1510   rtpsession = GST_RTP_SESSION (parent);
1511   priv = rtpsession->priv;
1512
1513   GST_LOG_OBJECT (rtpsession, "received RTP packet");
1514
1515   /* get NTP time when this packet was captured, this depends on the timestamp. */
1516   timestamp = GST_BUFFER_TIMESTAMP (buffer);
1517   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1518     /* convert to running time using the segment values */
1519     running_time =
1520         gst_segment_to_running_time (&rtpsession->recv_rtp_seg, GST_FORMAT_TIME,
1521         timestamp);
1522   } else {
1523     get_current_times (rtpsession, &running_time, NULL);
1524   }
1525   current_time = gst_clock_get_time (priv->sysclock);
1526
1527   ret = rtp_session_process_rtp (priv->session, buffer, current_time,
1528       running_time);
1529   if (ret != GST_FLOW_OK)
1530     goto push_error;
1531
1532 done:
1533
1534   return ret;
1535
1536   /* ERRORS */
1537 push_error:
1538   {
1539     GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1540         gst_flow_get_name (ret));
1541     goto done;
1542   }
1543 }
1544
1545 static gboolean
1546 gst_rtp_session_event_recv_rtcp_sink (GstPad * pad, GstObject * parent,
1547     GstEvent * event)
1548 {
1549   GstRtpSession *rtpsession;
1550   gboolean ret = FALSE;
1551
1552   rtpsession = GST_RTP_SESSION (parent);
1553
1554   GST_DEBUG_OBJECT (rtpsession, "received event %s",
1555       GST_EVENT_TYPE_NAME (event));
1556
1557   switch (GST_EVENT_TYPE (event)) {
1558     default:
1559       ret = gst_pad_push_event (rtpsession->sync_src, event);
1560       break;
1561   }
1562
1563   return ret;
1564 }
1565
1566 /* Receive an RTCP packet from a sender, send it to the RTP session manager and
1567  * forward the SR packets to the sync_src pad.
1568  */
1569 static GstFlowReturn
1570 gst_rtp_session_chain_recv_rtcp (GstPad * pad, GstObject * parent,
1571     GstBuffer * buffer)
1572 {
1573   GstRtpSession *rtpsession;
1574   GstRtpSessionPrivate *priv;
1575   GstClockTime current_time;
1576   guint64 ntpnstime;
1577
1578   rtpsession = GST_RTP_SESSION (parent);
1579   priv = rtpsession->priv;
1580
1581   GST_LOG_OBJECT (rtpsession, "received RTCP packet");
1582
1583   current_time = gst_clock_get_time (priv->sysclock);
1584   get_current_times (rtpsession, NULL, &ntpnstime);
1585
1586   rtp_session_process_rtcp (priv->session, buffer, current_time, ntpnstime);
1587
1588   return GST_FLOW_OK;           /* always return OK */
1589 }
1590
1591 static gboolean
1592 gst_rtp_session_query_send_rtcp_src (GstPad * pad, GstObject * parent,
1593     GstQuery * query)
1594 {
1595   GstRtpSession *rtpsession;
1596   gboolean ret = FALSE;
1597
1598   rtpsession = GST_RTP_SESSION (parent);
1599
1600   GST_DEBUG_OBJECT (rtpsession, "received QUERY");
1601
1602   switch (GST_QUERY_TYPE (query)) {
1603     case GST_QUERY_LATENCY:
1604       ret = TRUE;
1605       /* use the defaults for the latency query. */
1606       gst_query_set_latency (query, FALSE, 0, -1);
1607       break;
1608     default:
1609       /* other queries simply fail for now */
1610       break;
1611   }
1612
1613   return ret;
1614 }
1615
1616 static gboolean
1617 gst_rtp_session_event_send_rtcp_src (GstPad * pad, GstObject * parent,
1618     GstEvent * event)
1619 {
1620   GstRtpSession *rtpsession;
1621   gboolean ret = TRUE;
1622
1623   rtpsession = GST_RTP_SESSION (parent);
1624   GST_DEBUG_OBJECT (rtpsession, "received EVENT");
1625
1626   switch (GST_EVENT_TYPE (event)) {
1627     case GST_EVENT_SEEK:
1628     case GST_EVENT_LATENCY:
1629       gst_event_unref (event);
1630       ret = TRUE;
1631       break;
1632     default:
1633       /* other events simply fail for now */
1634       gst_event_unref (event);
1635       ret = FALSE;
1636       break;
1637   }
1638
1639   return ret;
1640 }
1641
1642
1643 static gboolean
1644 gst_rtp_session_event_send_rtp_sink (GstPad * pad, GstObject * parent,
1645     GstEvent * event)
1646 {
1647   GstRtpSession *rtpsession;
1648   gboolean ret = FALSE;
1649
1650   rtpsession = GST_RTP_SESSION (parent);
1651
1652   GST_DEBUG_OBJECT (rtpsession, "received event");
1653
1654   switch (GST_EVENT_TYPE (event)) {
1655     case GST_EVENT_CAPS:
1656     {
1657       GstCaps *caps;
1658
1659       /* process */
1660       gst_event_parse_caps (event, &caps);
1661       gst_rtp_session_setcaps_send_rtp (pad, rtpsession, caps);
1662       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1663       break;
1664     }
1665     case GST_EVENT_FLUSH_STOP:
1666       gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
1667       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1668       break;
1669     case GST_EVENT_SEGMENT:{
1670       GstSegment *segment, in_segment;
1671
1672       segment = &rtpsession->send_rtp_seg;
1673
1674       /* the newsegment event is needed to convert the RTP timestamp to
1675        * running_time, which is needed to generate a mapping from RTP to NTP
1676        * timestamps in SR reports */
1677       gst_event_copy_segment (event, &in_segment);
1678       GST_DEBUG_OBJECT (rtpsession, "received segment %" GST_SEGMENT_FORMAT,
1679           &in_segment);
1680
1681       /* accept upstream */
1682       gst_segment_copy_into (&in_segment, segment);
1683
1684       /* push event forward */
1685       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1686       break;
1687     }
1688     case GST_EVENT_EOS:{
1689       GstClockTime current_time;
1690
1691       /* push downstream FIXME, we are not supposed to leave the session just
1692        * because we stop sending. */
1693       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1694       current_time = gst_clock_get_time (rtpsession->priv->sysclock);
1695       GST_DEBUG_OBJECT (rtpsession, "scheduling BYE message");
1696       rtp_session_schedule_bye (rtpsession->priv->session, "End of stream",
1697           current_time);
1698       break;
1699     }
1700     default:{
1701       GstPad *send_rtp_src = NULL;
1702       GST_RTP_SESSION_LOCK (rtpsession);
1703       if (rtpsession->send_rtp_src)
1704         send_rtp_src = gst_object_ref (rtpsession->send_rtp_src);
1705       GST_RTP_SESSION_UNLOCK (rtpsession);
1706
1707       if (send_rtp_src) {
1708         ret = gst_pad_push_event (send_rtp_src, event);
1709         gst_object_unref (send_rtp_src);
1710       } else
1711         gst_event_unref (event);
1712
1713       break;
1714     }
1715   }
1716
1717   return ret;
1718 }
1719
1720 static GstCaps *
1721 gst_rtp_session_getcaps_send_rtp (GstPad * pad, GstRtpSession * rtpsession,
1722     GstCaps * filter)
1723 {
1724   GstRtpSessionPrivate *priv;
1725   GstCaps *result;
1726   GstStructure *s1, *s2;
1727   guint ssrc;
1728
1729   priv = rtpsession->priv;
1730
1731   ssrc = rtp_session_get_internal_ssrc (priv->session);
1732
1733   /* we can basically accept anything but we prefer to receive packets with our
1734    * internal SSRC so that we don't have to patch it. Create a structure with
1735    * the SSRC and another one without. */
1736   s1 = gst_structure_new ("application/x-rtp", "ssrc", G_TYPE_UINT, ssrc, NULL);
1737   s2 = gst_structure_new_empty ("application/x-rtp");
1738
1739   result = gst_caps_new_full (s1, s2, NULL);
1740
1741   if (filter) {
1742     GstCaps *caps = result;
1743
1744     result = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
1745     gst_caps_unref (caps);
1746   }
1747
1748   GST_DEBUG_OBJECT (rtpsession, "getting caps %" GST_PTR_FORMAT, result);
1749
1750   return result;
1751 }
1752
1753 static gboolean
1754 gst_rtp_session_query_send_rtp (GstPad * pad, GstObject * parent,
1755     GstQuery * query)
1756 {
1757   gboolean res = FALSE;
1758   GstRtpSession *rtpsession;
1759
1760   rtpsession = GST_RTP_SESSION (parent);
1761
1762   switch (GST_QUERY_TYPE (query)) {
1763     case GST_QUERY_CAPS:
1764     {
1765       GstCaps *filter, *caps;
1766
1767       gst_query_parse_caps (query, &filter);
1768       caps = gst_rtp_session_getcaps_send_rtp (pad, rtpsession, filter);
1769       gst_query_set_caps_result (query, caps);
1770       gst_caps_unref (caps);
1771       res = TRUE;
1772       break;
1773     }
1774     default:
1775       res = gst_pad_query_default (pad, parent, query);
1776       break;
1777   }
1778
1779   return res;
1780 }
1781
1782 static gboolean
1783 gst_rtp_session_setcaps_send_rtp (GstPad * pad, GstRtpSession * rtpsession,
1784     GstCaps * caps)
1785 {
1786   GstRtpSessionPrivate *priv;
1787   GstStructure *s = gst_caps_get_structure (caps, 0);
1788   guint ssrc;
1789
1790   priv = rtpsession->priv;
1791
1792   if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
1793     GST_DEBUG_OBJECT (rtpsession, "setting internal SSRC to %08x", ssrc);
1794     rtp_session_set_internal_ssrc (priv->session, ssrc);
1795   }
1796   return TRUE;
1797 }
1798
1799 /* Recieve an RTP packet or a list of packets to be send to the receivers,
1800  * send to RTP session manager and forward to send_rtp_src.
1801  */
1802 static GstFlowReturn
1803 gst_rtp_session_chain_send_rtp_common (GstRtpSession * rtpsession,
1804     gpointer data, gboolean is_list)
1805 {
1806   GstRtpSessionPrivate *priv;
1807   GstFlowReturn ret;
1808   GstClockTime timestamp, running_time;
1809   GstClockTime current_time;
1810
1811   priv = rtpsession->priv;
1812
1813   GST_LOG_OBJECT (rtpsession, "received RTP %s", is_list ? "list" : "packet");
1814
1815   /* get NTP time when this packet was captured, this depends on the timestamp. */
1816   if (is_list) {
1817     GstBuffer *buffer = NULL;
1818
1819     /* All groups in an list have the same timestamp.
1820      * So, just take it from the first group. */
1821     buffer = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0);
1822     if (buffer)
1823       timestamp = GST_BUFFER_TIMESTAMP (buffer);
1824     else
1825       timestamp = -1;
1826   } else {
1827     timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (data));
1828   }
1829
1830   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1831     /* convert to running time using the segment start value. */
1832     running_time =
1833         gst_segment_to_running_time (&rtpsession->send_rtp_seg, GST_FORMAT_TIME,
1834         timestamp);
1835   } else {
1836     /* no timestamp. */
1837     running_time = -1;
1838   }
1839
1840   current_time = gst_clock_get_time (priv->sysclock);
1841   ret = rtp_session_send_rtp (priv->session, data, is_list, current_time,
1842       running_time);
1843   if (ret != GST_FLOW_OK)
1844     goto push_error;
1845
1846 done:
1847
1848   return ret;
1849
1850   /* ERRORS */
1851 push_error:
1852   {
1853     GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1854         gst_flow_get_name (ret));
1855     goto done;
1856   }
1857 }
1858
1859 static GstFlowReturn
1860 gst_rtp_session_chain_send_rtp (GstPad * pad, GstObject * parent,
1861     GstBuffer * buffer)
1862 {
1863   GstRtpSession *rtpsession = GST_RTP_SESSION (parent);
1864
1865   return gst_rtp_session_chain_send_rtp_common (rtpsession, buffer, FALSE);
1866 }
1867
1868 static GstFlowReturn
1869 gst_rtp_session_chain_send_rtp_list (GstPad * pad, GstObject * parent,
1870     GstBufferList * list)
1871 {
1872   GstRtpSession *rtpsession = GST_RTP_SESSION (parent);
1873
1874   return gst_rtp_session_chain_send_rtp_common (rtpsession, list, TRUE);
1875 }
1876
1877 /* Create sinkpad to receive RTP packets from senders. This will also create a
1878  * srcpad for the RTP packets.
1879  */
1880 static GstPad *
1881 create_recv_rtp_sink (GstRtpSession * rtpsession)
1882 {
1883   GST_DEBUG_OBJECT (rtpsession, "creating RTP sink pad");
1884
1885   rtpsession->recv_rtp_sink =
1886       gst_pad_new_from_static_template (&rtpsession_recv_rtp_sink_template,
1887       "recv_rtp_sink");
1888   gst_pad_set_chain_function (rtpsession->recv_rtp_sink,
1889       gst_rtp_session_chain_recv_rtp);
1890   gst_pad_set_event_function (rtpsession->recv_rtp_sink,
1891       (GstPadEventFunction) gst_rtp_session_event_recv_rtp_sink);
1892   gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_sink,
1893       gst_rtp_session_iterate_internal_links);
1894   gst_pad_set_active (rtpsession->recv_rtp_sink, TRUE);
1895   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1896       rtpsession->recv_rtp_sink);
1897
1898   GST_DEBUG_OBJECT (rtpsession, "creating RTP src pad");
1899   rtpsession->recv_rtp_src =
1900       gst_pad_new_from_static_template (&rtpsession_recv_rtp_src_template,
1901       "recv_rtp_src");
1902   gst_pad_set_event_function (rtpsession->recv_rtp_src,
1903       (GstPadEventFunction) gst_rtp_session_event_recv_rtp_src);
1904   gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_src,
1905       gst_rtp_session_iterate_internal_links);
1906   gst_pad_use_fixed_caps (rtpsession->recv_rtp_src);
1907   gst_pad_set_active (rtpsession->recv_rtp_src, TRUE);
1908   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->recv_rtp_src);
1909
1910   return rtpsession->recv_rtp_sink;
1911 }
1912
1913 /* Remove sinkpad to receive RTP packets from senders. This will also remove
1914  * the srcpad for the RTP packets.
1915  */
1916 static void
1917 remove_recv_rtp_sink (GstRtpSession * rtpsession)
1918 {
1919   GST_DEBUG_OBJECT (rtpsession, "removing RTP sink pad");
1920
1921   /* deactivate from source to sink */
1922   gst_pad_set_active (rtpsession->recv_rtp_src, FALSE);
1923   gst_pad_set_active (rtpsession->recv_rtp_sink, FALSE);
1924
1925   /* remove pads */
1926   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1927       rtpsession->recv_rtp_sink);
1928   rtpsession->recv_rtp_sink = NULL;
1929
1930   GST_DEBUG_OBJECT (rtpsession, "removing RTP src pad");
1931   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1932       rtpsession->recv_rtp_src);
1933   rtpsession->recv_rtp_src = NULL;
1934 }
1935
1936 /* Create a sinkpad to receive RTCP messages from senders, this will also create a
1937  * sync_src pad for the SR packets.
1938  */
1939 static GstPad *
1940 create_recv_rtcp_sink (GstRtpSession * rtpsession)
1941 {
1942   GST_DEBUG_OBJECT (rtpsession, "creating RTCP sink pad");
1943
1944   rtpsession->recv_rtcp_sink =
1945       gst_pad_new_from_static_template (&rtpsession_recv_rtcp_sink_template,
1946       "recv_rtcp_sink");
1947   gst_pad_set_chain_function (rtpsession->recv_rtcp_sink,
1948       gst_rtp_session_chain_recv_rtcp);
1949   gst_pad_set_event_function (rtpsession->recv_rtcp_sink,
1950       (GstPadEventFunction) gst_rtp_session_event_recv_rtcp_sink);
1951   gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtcp_sink,
1952       gst_rtp_session_iterate_internal_links);
1953   gst_pad_set_active (rtpsession->recv_rtcp_sink, TRUE);
1954   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1955       rtpsession->recv_rtcp_sink);
1956
1957   GST_DEBUG_OBJECT (rtpsession, "creating sync src pad");
1958   rtpsession->sync_src =
1959       gst_pad_new_from_static_template (&rtpsession_sync_src_template,
1960       "sync_src");
1961   gst_pad_set_iterate_internal_links_function (rtpsession->sync_src,
1962       gst_rtp_session_iterate_internal_links);
1963   gst_pad_use_fixed_caps (rtpsession->sync_src);
1964   gst_pad_set_active (rtpsession->sync_src, TRUE);
1965   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
1966
1967   return rtpsession->recv_rtcp_sink;
1968 }
1969
1970 static void
1971 remove_recv_rtcp_sink (GstRtpSession * rtpsession)
1972 {
1973   GST_DEBUG_OBJECT (rtpsession, "removing RTCP sink pad");
1974
1975   gst_pad_set_active (rtpsession->sync_src, FALSE);
1976   gst_pad_set_active (rtpsession->recv_rtcp_sink, FALSE);
1977
1978   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1979       rtpsession->recv_rtcp_sink);
1980   rtpsession->recv_rtcp_sink = NULL;
1981
1982   GST_DEBUG_OBJECT (rtpsession, "removing sync src pad");
1983   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
1984   rtpsession->sync_src = NULL;
1985 }
1986
1987 /* Create a sinkpad to receive RTP packets for receivers. This will also create a
1988  * send_rtp_src pad.
1989  */
1990 static GstPad *
1991 create_send_rtp_sink (GstRtpSession * rtpsession)
1992 {
1993   GST_DEBUG_OBJECT (rtpsession, "creating pad");
1994
1995   rtpsession->send_rtp_sink =
1996       gst_pad_new_from_static_template (&rtpsession_send_rtp_sink_template,
1997       "send_rtp_sink");
1998   gst_pad_set_chain_function (rtpsession->send_rtp_sink,
1999       gst_rtp_session_chain_send_rtp);
2000   gst_pad_set_chain_list_function (rtpsession->send_rtp_sink,
2001       gst_rtp_session_chain_send_rtp_list);
2002   gst_pad_set_query_function (rtpsession->send_rtp_sink,
2003       gst_rtp_session_query_send_rtp);
2004   gst_pad_set_event_function (rtpsession->send_rtp_sink,
2005       (GstPadEventFunction) gst_rtp_session_event_send_rtp_sink);
2006   gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_sink,
2007       gst_rtp_session_iterate_internal_links);
2008   gst_pad_set_active (rtpsession->send_rtp_sink, TRUE);
2009   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2010       rtpsession->send_rtp_sink);
2011
2012   rtpsession->send_rtp_src =
2013       gst_pad_new_from_static_template (&rtpsession_send_rtp_src_template,
2014       "send_rtp_src");
2015   gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_src,
2016       gst_rtp_session_iterate_internal_links);
2017   gst_pad_set_active (rtpsession->send_rtp_src, TRUE);
2018   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->send_rtp_src);
2019
2020   return rtpsession->send_rtp_sink;
2021 }
2022
2023 static void
2024 remove_send_rtp_sink (GstRtpSession * rtpsession)
2025 {
2026   GST_DEBUG_OBJECT (rtpsession, "removing pad");
2027
2028   gst_pad_set_active (rtpsession->send_rtp_src, FALSE);
2029   gst_pad_set_active (rtpsession->send_rtp_sink, FALSE);
2030
2031   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2032       rtpsession->send_rtp_sink);
2033   rtpsession->send_rtp_sink = NULL;
2034
2035   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2036       rtpsession->send_rtp_src);
2037   rtpsession->send_rtp_src = NULL;
2038 }
2039
2040 /* Create a srcpad with the RTCP packets to send out.
2041  * This pad will be driven by the RTP session manager when it wants to send out
2042  * RTCP packets.
2043  */
2044 static GstPad *
2045 create_send_rtcp_src (GstRtpSession * rtpsession)
2046 {
2047   GST_DEBUG_OBJECT (rtpsession, "creating pad");
2048
2049   rtpsession->send_rtcp_src =
2050       gst_pad_new_from_static_template (&rtpsession_send_rtcp_src_template,
2051       "send_rtcp_src");
2052   gst_pad_use_fixed_caps (rtpsession->send_rtcp_src);
2053   gst_pad_set_active (rtpsession->send_rtcp_src, TRUE);
2054   gst_pad_set_iterate_internal_links_function (rtpsession->send_rtcp_src,
2055       gst_rtp_session_iterate_internal_links);
2056   gst_pad_set_query_function (rtpsession->send_rtcp_src,
2057       gst_rtp_session_query_send_rtcp_src);
2058   gst_pad_set_event_function (rtpsession->send_rtcp_src,
2059       gst_rtp_session_event_send_rtcp_src);
2060   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2061       rtpsession->send_rtcp_src);
2062
2063   return rtpsession->send_rtcp_src;
2064 }
2065
2066 static void
2067 remove_send_rtcp_src (GstRtpSession * rtpsession)
2068 {
2069   GST_DEBUG_OBJECT (rtpsession, "removing pad");
2070
2071   gst_pad_set_active (rtpsession->send_rtcp_src, FALSE);
2072
2073   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2074       rtpsession->send_rtcp_src);
2075   rtpsession->send_rtcp_src = NULL;
2076 }
2077
2078 static GstPad *
2079 gst_rtp_session_request_new_pad (GstElement * element,
2080     GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
2081 {
2082   GstRtpSession *rtpsession;
2083   GstElementClass *klass;
2084   GstPad *result;
2085
2086   g_return_val_if_fail (templ != NULL, NULL);
2087   g_return_val_if_fail (GST_IS_RTP_SESSION (element), NULL);
2088
2089   rtpsession = GST_RTP_SESSION (element);
2090   klass = GST_ELEMENT_GET_CLASS (element);
2091
2092   GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
2093
2094   GST_RTP_SESSION_LOCK (rtpsession);
2095
2096   /* figure out the template */
2097   if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink")) {
2098     if (rtpsession->recv_rtp_sink != NULL)
2099       goto exists;
2100
2101     result = create_recv_rtp_sink (rtpsession);
2102   } else if (templ == gst_element_class_get_pad_template (klass,
2103           "recv_rtcp_sink")) {
2104     if (rtpsession->recv_rtcp_sink != NULL)
2105       goto exists;
2106
2107     result = create_recv_rtcp_sink (rtpsession);
2108   } else if (templ == gst_element_class_get_pad_template (klass,
2109           "send_rtp_sink")) {
2110     if (rtpsession->send_rtp_sink != NULL)
2111       goto exists;
2112
2113     result = create_send_rtp_sink (rtpsession);
2114   } else if (templ == gst_element_class_get_pad_template (klass,
2115           "send_rtcp_src")) {
2116     if (rtpsession->send_rtcp_src != NULL)
2117       goto exists;
2118
2119     result = create_send_rtcp_src (rtpsession);
2120   } else
2121     goto wrong_template;
2122
2123   GST_RTP_SESSION_UNLOCK (rtpsession);
2124
2125   return result;
2126
2127   /* ERRORS */
2128 wrong_template:
2129   {
2130     GST_RTP_SESSION_UNLOCK (rtpsession);
2131     g_warning ("gstrtpsession: this is not our template");
2132     return NULL;
2133   }
2134 exists:
2135   {
2136     GST_RTP_SESSION_UNLOCK (rtpsession);
2137     g_warning ("gstrtpsession: pad already requested");
2138     return NULL;
2139   }
2140 }
2141
2142 static void
2143 gst_rtp_session_release_pad (GstElement * element, GstPad * pad)
2144 {
2145   GstRtpSession *rtpsession;
2146
2147   g_return_if_fail (GST_IS_RTP_SESSION (element));
2148   g_return_if_fail (GST_IS_PAD (pad));
2149
2150   rtpsession = GST_RTP_SESSION (element);
2151
2152   GST_DEBUG_OBJECT (element, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2153
2154   GST_RTP_SESSION_LOCK (rtpsession);
2155
2156   if (rtpsession->recv_rtp_sink == pad) {
2157     remove_recv_rtp_sink (rtpsession);
2158   } else if (rtpsession->recv_rtcp_sink == pad) {
2159     remove_recv_rtcp_sink (rtpsession);
2160   } else if (rtpsession->send_rtp_sink == pad) {
2161     remove_send_rtp_sink (rtpsession);
2162   } else if (rtpsession->send_rtcp_src == pad) {
2163     remove_send_rtcp_src (rtpsession);
2164   } else
2165     goto wrong_pad;
2166
2167   GST_RTP_SESSION_UNLOCK (rtpsession);
2168
2169   return;
2170
2171   /* ERRORS */
2172 wrong_pad:
2173   {
2174     GST_RTP_SESSION_UNLOCK (rtpsession);
2175     g_warning ("gstrtpsession: asked to release an unknown pad");
2176     return;
2177   }
2178 }
2179
2180 static void
2181 gst_rtp_session_request_key_unit (RTPSession * sess,
2182     gboolean all_headers, gpointer user_data)
2183 {
2184   GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2185   GstEvent *event;
2186
2187   event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
2188       gst_structure_new ("GstForceKeyUnit",
2189           "all-headers", G_TYPE_BOOLEAN, all_headers, NULL));
2190   gst_pad_push_event (rtpsession->send_rtp_sink, event);
2191 }
2192
2193 static GstClockTime
2194 gst_rtp_session_request_time (RTPSession * session, gpointer user_data)
2195 {
2196   GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2197
2198   return gst_clock_get_time (rtpsession->priv->sysclock);
2199 }