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