jitterbuffer: do skew estimation only for new timestamps
[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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, 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   GstClockTime send_latency;
246
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_BANDWIDTH,
522       g_param_spec_double ("bandwidth", "Bandwidth",
523           "The bandwidth of the session in bytes per second (0 for auto-discover)",
524           0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
525           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
526
527   g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
528       g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
529           "The RTCP bandwidth of the session in bytes per second "
530           "(or as a real fraction of the RTP bandwidth if < 1.0)",
531           0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
532           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
533
534   g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
535       g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
536           "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
537           -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
538           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
539
540   g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
541       g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
542           "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
543           -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
544           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
545
546   g_object_class_install_property (gobject_class, PROP_SDES,
547       g_param_spec_boxed ("sdes", "SDES",
548           "The SDES items of this session",
549           GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
550
551   g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
552       g_param_spec_uint ("num-sources", "Num Sources",
553           "The number of sources in the session", 0, G_MAXUINT,
554           DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
555
556   g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
557       g_param_spec_uint ("num-active-sources", "Num Active Sources",
558           "The number of active sources in the session", 0, G_MAXUINT,
559           DEFAULT_NUM_ACTIVE_SOURCES,
560           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
561
562   g_object_class_install_property (gobject_class, PROP_INTERNAL_SESSION,
563       g_param_spec_object ("internal-session", "Internal Session",
564           "The internal RTPSession object", RTP_TYPE_SESSION,
565           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
566
567   g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
568       g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
569           "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
570           DEFAULT_USE_PIPELINE_CLOCK,
571           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
572
573   g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
574       g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
575           "Minimum interval between Regular RTCP packet (in ns)",
576           0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
577           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
578
579   g_object_class_install_property (gobject_class, PROP_PROBATION,
580       g_param_spec_uint ("probation", "Number of probations",
581           "Consecutive packet sequence numbers to accept the source",
582           0, G_MAXUINT, DEFAULT_PROBATION,
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_BANDWIDTH:
686       g_object_set_property (G_OBJECT (priv->session), "bandwidth", value);
687       break;
688     case PROP_RTCP_FRACTION:
689       g_object_set_property (G_OBJECT (priv->session), "rtcp-fraction", value);
690       break;
691     case PROP_RTCP_RR_BANDWIDTH:
692       g_object_set_property (G_OBJECT (priv->session), "rtcp-rr-bandwidth",
693           value);
694       break;
695     case PROP_RTCP_RS_BANDWIDTH:
696       g_object_set_property (G_OBJECT (priv->session), "rtcp-rs-bandwidth",
697           value);
698       break;
699     case PROP_SDES:
700       rtp_session_set_sdes_struct (priv->session, g_value_get_boxed (value));
701       break;
702     case PROP_USE_PIPELINE_CLOCK:
703       priv->use_pipeline_clock = g_value_get_boolean (value);
704       break;
705     case PROP_RTCP_MIN_INTERVAL:
706       g_object_set_property (G_OBJECT (priv->session), "rtcp-min-interval",
707           value);
708       break;
709     case PROP_PROBATION:
710       g_object_set_property (G_OBJECT (priv->session), "probation", value);
711       break;
712     default:
713       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
714       break;
715   }
716 }
717
718 static void
719 gst_rtp_session_get_property (GObject * object, guint prop_id,
720     GValue * value, GParamSpec * pspec)
721 {
722   GstRtpSession *rtpsession;
723   GstRtpSessionPrivate *priv;
724
725   rtpsession = GST_RTP_SESSION (object);
726   priv = rtpsession->priv;
727
728   switch (prop_id) {
729     case PROP_BANDWIDTH:
730       g_object_get_property (G_OBJECT (priv->session), "bandwidth", value);
731       break;
732     case PROP_RTCP_FRACTION:
733       g_object_get_property (G_OBJECT (priv->session), "rtcp-fraction", value);
734       break;
735     case PROP_RTCP_RR_BANDWIDTH:
736       g_object_get_property (G_OBJECT (priv->session), "rtcp-rr-bandwidth",
737           value);
738       break;
739     case PROP_RTCP_RS_BANDWIDTH:
740       g_object_get_property (G_OBJECT (priv->session), "rtcp-rs-bandwidth",
741           value);
742       break;
743     case PROP_SDES:
744       g_value_take_boxed (value, rtp_session_get_sdes_struct (priv->session));
745       break;
746     case PROP_NUM_SOURCES:
747       g_value_set_uint (value, rtp_session_get_num_sources (priv->session));
748       break;
749     case PROP_NUM_ACTIVE_SOURCES:
750       g_value_set_uint (value,
751           rtp_session_get_num_active_sources (priv->session));
752       break;
753     case PROP_INTERNAL_SESSION:
754       g_value_set_object (value, priv->session);
755       break;
756     case PROP_USE_PIPELINE_CLOCK:
757       g_value_set_boolean (value, priv->use_pipeline_clock);
758       break;
759     case PROP_RTCP_MIN_INTERVAL:
760       g_object_get_property (G_OBJECT (priv->session), "rtcp-min-interval",
761           value);
762       break;
763     case PROP_PROBATION:
764       g_object_get_property (G_OBJECT (priv->session), "probation", value);
765       break;
766     default:
767       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
768       break;
769   }
770 }
771
772 static void
773 get_current_times (GstRtpSession * rtpsession, GstClockTime * running_time,
774     guint64 * ntpnstime)
775 {
776   guint64 ntpns;
777   GstClock *clock;
778   GstClockTime base_time, rt, clock_time;
779
780   GST_OBJECT_LOCK (rtpsession);
781   if ((clock = GST_ELEMENT_CLOCK (rtpsession))) {
782     base_time = GST_ELEMENT_CAST (rtpsession)->base_time;
783     gst_object_ref (clock);
784     GST_OBJECT_UNLOCK (rtpsession);
785
786     /* get current clock time and convert to running time */
787     clock_time = gst_clock_get_time (clock);
788     rt = clock_time - base_time;
789
790     if (rtpsession->priv->use_pipeline_clock) {
791       ntpns = rt;
792     } else {
793       GTimeVal current;
794
795       /* get current NTP time */
796       g_get_current_time (&current);
797       ntpns = GST_TIMEVAL_TO_TIME (current);
798     }
799
800     /* add constant to convert from 1970 based time to 1900 based time */
801     ntpns += (2208988800LL * GST_SECOND);
802
803     gst_object_unref (clock);
804   } else {
805     GST_OBJECT_UNLOCK (rtpsession);
806     rt = -1;
807     ntpns = -1;
808   }
809   if (running_time)
810     *running_time = rt;
811   if (ntpnstime)
812     *ntpnstime = ntpns;
813 }
814
815 static void
816 rtcp_thread (GstRtpSession * rtpsession)
817 {
818   GstClockID id;
819   GstClockTime current_time;
820   GstClockTime next_timeout;
821   guint64 ntpnstime;
822   GstClockTime running_time;
823   RTPSession *session;
824   GstClock *sysclock;
825
826   GST_DEBUG_OBJECT (rtpsession, "entering RTCP thread");
827
828   GST_RTP_SESSION_LOCK (rtpsession);
829
830   sysclock = rtpsession->priv->sysclock;
831   current_time = gst_clock_get_time (sysclock);
832
833   session = rtpsession->priv->session;
834
835   GST_DEBUG_OBJECT (rtpsession, "starting at %" GST_TIME_FORMAT,
836       GST_TIME_ARGS (current_time));
837   session->start_time = current_time;
838
839   while (!rtpsession->priv->stop_thread) {
840     GstClockReturn res;
841
842     /* get initial estimate */
843     next_timeout = rtp_session_next_timeout (session, current_time);
844
845     GST_DEBUG_OBJECT (rtpsession, "next check time %" GST_TIME_FORMAT,
846         GST_TIME_ARGS (next_timeout));
847
848     /* leave if no more timeouts, the session ended */
849     if (next_timeout == GST_CLOCK_TIME_NONE)
850       break;
851
852     id = rtpsession->priv->id =
853         gst_clock_new_single_shot_id (sysclock, next_timeout);
854     GST_RTP_SESSION_UNLOCK (rtpsession);
855
856     res = gst_clock_id_wait (id, NULL);
857
858     GST_RTP_SESSION_LOCK (rtpsession);
859     gst_clock_id_unref (id);
860     rtpsession->priv->id = NULL;
861
862     if (rtpsession->priv->stop_thread)
863       break;
864
865     /* update current time */
866     current_time = gst_clock_get_time (sysclock);
867
868     /* get current NTP time */
869     get_current_times (rtpsession, &running_time, &ntpnstime);
870
871     /* we get unlocked because we need to perform reconsideration, don't perform
872      * the timeout but get a new reporting estimate. */
873     GST_DEBUG_OBJECT (rtpsession, "unlocked %d, current %" GST_TIME_FORMAT,
874         res, GST_TIME_ARGS (current_time));
875
876     /* perform actions, we ignore result. Release lock because it might push. */
877     GST_RTP_SESSION_UNLOCK (rtpsession);
878     rtp_session_on_timeout (session, current_time, ntpnstime, running_time);
879     GST_RTP_SESSION_LOCK (rtpsession);
880   }
881   /* mark the thread as stopped now */
882   rtpsession->priv->thread_stopped = TRUE;
883   GST_RTP_SESSION_UNLOCK (rtpsession);
884
885   GST_DEBUG_OBJECT (rtpsession, "leaving RTCP thread");
886 }
887
888 static gboolean
889 start_rtcp_thread (GstRtpSession * rtpsession)
890 {
891   GError *error = NULL;
892   gboolean res;
893
894   GST_DEBUG_OBJECT (rtpsession, "starting RTCP thread");
895
896   GST_RTP_SESSION_LOCK (rtpsession);
897   rtpsession->priv->stop_thread = FALSE;
898   if (rtpsession->priv->thread_stopped) {
899     /* if the thread stopped, and we still have a handle to the thread, join it
900      * now. We can safely join with the lock held, the thread will not take it
901      * anymore. */
902     if (rtpsession->priv->thread)
903       g_thread_join (rtpsession->priv->thread);
904     /* only create a new thread if the old one was stopped. Otherwise we can
905      * just reuse the currently running one. */
906     rtpsession->priv->thread = g_thread_try_new ("rtpsession-rtcp-thread",
907         (GThreadFunc) rtcp_thread, rtpsession, &error);
908     rtpsession->priv->thread_stopped = FALSE;
909   }
910   GST_RTP_SESSION_UNLOCK (rtpsession);
911
912   if (error != NULL) {
913     res = FALSE;
914     GST_DEBUG_OBJECT (rtpsession, "failed to start thread, %s", error->message);
915     g_error_free (error);
916   } else {
917     res = TRUE;
918   }
919   return res;
920 }
921
922 static void
923 stop_rtcp_thread (GstRtpSession * rtpsession)
924 {
925   GST_DEBUG_OBJECT (rtpsession, "stopping RTCP thread");
926
927   GST_RTP_SESSION_LOCK (rtpsession);
928   rtpsession->priv->stop_thread = TRUE;
929   if (rtpsession->priv->id)
930     gst_clock_id_unschedule (rtpsession->priv->id);
931   GST_RTP_SESSION_UNLOCK (rtpsession);
932 }
933
934 static void
935 join_rtcp_thread (GstRtpSession * rtpsession)
936 {
937   GST_RTP_SESSION_LOCK (rtpsession);
938   /* don't try to join when we have no thread */
939   if (rtpsession->priv->thread != NULL) {
940     GST_DEBUG_OBJECT (rtpsession, "joining RTCP thread");
941     GST_RTP_SESSION_UNLOCK (rtpsession);
942
943     g_thread_join (rtpsession->priv->thread);
944
945     GST_RTP_SESSION_LOCK (rtpsession);
946     /* after the join, take the lock and clear the thread structure. The caller
947      * is supposed to not concurrently call start and join. */
948     rtpsession->priv->thread = NULL;
949   }
950   GST_RTP_SESSION_UNLOCK (rtpsession);
951 }
952
953 static GstStateChangeReturn
954 gst_rtp_session_change_state (GstElement * element, GstStateChange transition)
955 {
956   GstStateChangeReturn res;
957   GstRtpSession *rtpsession;
958
959   rtpsession = GST_RTP_SESSION (element);
960
961   switch (transition) {
962     case GST_STATE_CHANGE_NULL_TO_READY:
963       break;
964     case GST_STATE_CHANGE_READY_TO_PAUSED:
965       break;
966     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
967       break;
968     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
969     case GST_STATE_CHANGE_PAUSED_TO_READY:
970       /* no need to join yet, we might want to continue later. Also, the
971        * dataflow could block downstream so that a join could just block
972        * forever. */
973       stop_rtcp_thread (rtpsession);
974       break;
975     default:
976       break;
977   }
978
979   res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
980
981   switch (transition) {
982     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
983       if (!start_rtcp_thread (rtpsession))
984         goto failed_thread;
985       break;
986     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
987       break;
988     case GST_STATE_CHANGE_PAUSED_TO_READY:
989       /* downstream is now releasing the dataflow and we can join. */
990       join_rtcp_thread (rtpsession);
991       break;
992     case GST_STATE_CHANGE_READY_TO_NULL:
993       break;
994     default:
995       break;
996   }
997   return res;
998
999   /* ERRORS */
1000 failed_thread:
1001   {
1002     return GST_STATE_CHANGE_FAILURE;
1003   }
1004 }
1005
1006 static gboolean
1007 return_true (gpointer key, gpointer value, gpointer user_data)
1008 {
1009   return TRUE;
1010 }
1011
1012 static void
1013 gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession)
1014 {
1015   g_hash_table_foreach_remove (rtpsession->priv->ptmap, return_true, NULL);
1016 }
1017
1018 /* called when the session manager has an RTP packet or a list of packets
1019  * ready for further processing */
1020 static GstFlowReturn
1021 gst_rtp_session_process_rtp (RTPSession * sess, RTPSource * src,
1022     GstBuffer * buffer, gpointer user_data)
1023 {
1024   GstFlowReturn result;
1025   GstRtpSession *rtpsession;
1026   GstPad *rtp_src;
1027
1028   rtpsession = GST_RTP_SESSION (user_data);
1029
1030   GST_RTP_SESSION_LOCK (rtpsession);
1031   if ((rtp_src = rtpsession->recv_rtp_src))
1032     gst_object_ref (rtp_src);
1033   GST_RTP_SESSION_UNLOCK (rtpsession);
1034
1035   if (rtp_src) {
1036     GST_LOG_OBJECT (rtpsession, "pushing received RTP packet");
1037     result = gst_pad_push (rtp_src, buffer);
1038     gst_object_unref (rtp_src);
1039   } else {
1040     GST_DEBUG_OBJECT (rtpsession, "dropping received RTP packet");
1041     gst_buffer_unref (buffer);
1042     result = GST_FLOW_OK;
1043   }
1044   return result;
1045 }
1046
1047 /* called when the session manager has an RTP packet ready for further
1048  * sending */
1049 static GstFlowReturn
1050 gst_rtp_session_send_rtp (RTPSession * sess, RTPSource * src,
1051     gpointer data, gpointer user_data)
1052 {
1053   GstFlowReturn result;
1054   GstRtpSession *rtpsession;
1055   GstPad *rtp_src;
1056
1057   rtpsession = GST_RTP_SESSION (user_data);
1058
1059   GST_RTP_SESSION_LOCK (rtpsession);
1060   if ((rtp_src = rtpsession->send_rtp_src))
1061     gst_object_ref (rtp_src);
1062   GST_RTP_SESSION_UNLOCK (rtpsession);
1063
1064   if (rtp_src) {
1065     if (GST_IS_BUFFER (data)) {
1066       GST_LOG_OBJECT (rtpsession, "sending RTP packet");
1067       result = gst_pad_push (rtp_src, GST_BUFFER_CAST (data));
1068     } else {
1069       GST_LOG_OBJECT (rtpsession, "sending RTP list");
1070       result = gst_pad_push_list (rtp_src, GST_BUFFER_LIST_CAST (data));
1071     }
1072     gst_object_unref (rtp_src);
1073   } else {
1074     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1075     result = GST_FLOW_OK;
1076   }
1077   return result;
1078 }
1079
1080 /* called when the session manager has an RTCP packet ready for further
1081  * sending. The eos flag is set when an EOS event should be sent downstream as
1082  * well. */
1083 static GstFlowReturn
1084 gst_rtp_session_send_rtcp (RTPSession * sess, RTPSource * src,
1085     GstBuffer * buffer, gboolean eos, gpointer user_data)
1086 {
1087   GstFlowReturn result;
1088   GstRtpSession *rtpsession;
1089   GstPad *rtcp_src;
1090
1091   rtpsession = GST_RTP_SESSION (user_data);
1092
1093   GST_RTP_SESSION_LOCK (rtpsession);
1094   if (rtpsession->priv->stop_thread)
1095     goto stopping;
1096
1097   if ((rtcp_src = rtpsession->send_rtcp_src)) {
1098     GstCaps *caps;
1099
1100     gst_object_ref (rtcp_src);
1101     GST_RTP_SESSION_UNLOCK (rtpsession);
1102
1103     /* set rtcp caps on output pad */
1104     if (!(caps = gst_pad_get_current_caps (rtcp_src))) {
1105       caps = gst_caps_new_empty_simple ("application/x-rtcp");
1106       gst_pad_set_caps (rtcp_src, caps);
1107     }
1108     gst_caps_unref (caps);
1109
1110     GST_LOG_OBJECT (rtpsession, "sending RTCP");
1111     result = gst_pad_push (rtcp_src, buffer);
1112
1113     /* we have to send EOS after this packet */
1114     if (eos) {
1115       GST_LOG_OBJECT (rtpsession, "sending EOS");
1116       gst_pad_push_event (rtcp_src, gst_event_new_eos ());
1117     }
1118     gst_object_unref (rtcp_src);
1119   } else {
1120     GST_RTP_SESSION_UNLOCK (rtpsession);
1121
1122     GST_DEBUG_OBJECT (rtpsession, "not sending RTCP, no output pad");
1123     gst_buffer_unref (buffer);
1124     result = GST_FLOW_OK;
1125   }
1126   return result;
1127
1128   /* ERRORS */
1129 stopping:
1130   {
1131     GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1132     gst_buffer_unref (buffer);
1133     GST_RTP_SESSION_UNLOCK (rtpsession);
1134     return GST_FLOW_OK;
1135   }
1136 }
1137
1138 /* called when the session manager has an SR RTCP packet ready for handling
1139  * inter stream synchronisation */
1140 static GstFlowReturn
1141 gst_rtp_session_sync_rtcp (RTPSession * sess, RTPSource * src,
1142     GstBuffer * buffer, gpointer user_data)
1143 {
1144   GstFlowReturn result;
1145   GstRtpSession *rtpsession;
1146   GstPad *sync_src;
1147
1148   rtpsession = GST_RTP_SESSION (user_data);
1149
1150   GST_RTP_SESSION_LOCK (rtpsession);
1151   if (rtpsession->priv->stop_thread)
1152     goto stopping;
1153
1154   if ((sync_src = rtpsession->sync_src)) {
1155     GstCaps *caps;
1156
1157     gst_object_ref (sync_src);
1158     GST_RTP_SESSION_UNLOCK (rtpsession);
1159
1160     /* set rtcp caps on output pad */
1161     if (!(caps = gst_pad_get_current_caps (sync_src))) {
1162       caps = gst_caps_new_empty_simple ("application/x-rtcp");
1163       gst_pad_set_caps (sync_src, caps);
1164     }
1165     gst_caps_unref (caps);
1166
1167     GST_LOG_OBJECT (rtpsession, "sending Sync RTCP");
1168     result = gst_pad_push (sync_src, buffer);
1169     gst_object_unref (sync_src);
1170   } else {
1171     GST_RTP_SESSION_UNLOCK (rtpsession);
1172
1173     GST_DEBUG_OBJECT (rtpsession, "not sending Sync RTCP, no output pad");
1174     gst_buffer_unref (buffer);
1175     result = GST_FLOW_OK;
1176   }
1177   return result;
1178
1179   /* ERRORS */
1180 stopping:
1181   {
1182     GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1183     gst_buffer_unref (buffer);
1184     GST_RTP_SESSION_UNLOCK (rtpsession);
1185     return GST_FLOW_OK;
1186   }
1187 }
1188
1189 static void
1190 gst_rtp_session_cache_caps (GstRtpSession * rtpsession, GstCaps * caps)
1191 {
1192   GstRtpSessionPrivate *priv;
1193   const GstStructure *s;
1194   gint payload;
1195
1196   priv = rtpsession->priv;
1197
1198   GST_DEBUG_OBJECT (rtpsession, "parsing caps");
1199
1200   s = gst_caps_get_structure (caps, 0);
1201   if (!gst_structure_get_int (s, "payload", &payload))
1202     return;
1203
1204   if (g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (payload)))
1205     return;
1206
1207   g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (payload),
1208       gst_caps_ref (caps));
1209 }
1210
1211 static GstCaps *
1212 gst_rtp_session_get_caps_for_pt (GstRtpSession * rtpsession, guint payload)
1213 {
1214   GstCaps *caps = NULL;
1215   GValue args[2] = { {0}, {0} };
1216   GValue ret = { 0 };
1217
1218   GST_RTP_SESSION_LOCK (rtpsession);
1219   caps = g_hash_table_lookup (rtpsession->priv->ptmap,
1220       GINT_TO_POINTER (payload));
1221   if (caps) {
1222     gst_caps_ref (caps);
1223     goto done;
1224   }
1225
1226   /* not found in the cache, try to get it with a signal */
1227   g_value_init (&args[0], GST_TYPE_ELEMENT);
1228   g_value_set_object (&args[0], rtpsession);
1229   g_value_init (&args[1], G_TYPE_UINT);
1230   g_value_set_uint (&args[1], payload);
1231
1232   g_value_init (&ret, GST_TYPE_CAPS);
1233   g_value_set_boxed (&ret, NULL);
1234
1235   GST_RTP_SESSION_UNLOCK (rtpsession);
1236
1237   g_signal_emitv (args, gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP], 0,
1238       &ret);
1239
1240   GST_RTP_SESSION_LOCK (rtpsession);
1241
1242   g_value_unset (&args[0]);
1243   g_value_unset (&args[1]);
1244   caps = (GstCaps *) g_value_dup_boxed (&ret);
1245   g_value_unset (&ret);
1246   if (!caps)
1247     goto no_caps;
1248
1249   gst_rtp_session_cache_caps (rtpsession, caps);
1250
1251 done:
1252   GST_RTP_SESSION_UNLOCK (rtpsession);
1253
1254   return caps;
1255
1256 no_caps:
1257   {
1258     GST_DEBUG_OBJECT (rtpsession, "could not get caps");
1259     goto done;
1260   }
1261 }
1262
1263 /* called when the session manager needs the clock rate */
1264 static gint
1265 gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
1266     gpointer user_data)
1267 {
1268   gint result = -1;
1269   GstRtpSession *rtpsession;
1270   GstCaps *caps;
1271   const GstStructure *s;
1272
1273   rtpsession = GST_RTP_SESSION_CAST (user_data);
1274
1275   caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1276
1277   if (!caps)
1278     goto done;
1279
1280   s = gst_caps_get_structure (caps, 0);
1281   if (!gst_structure_get_int (s, "clock-rate", &result))
1282     goto no_clock_rate;
1283
1284   gst_caps_unref (caps);
1285
1286   GST_DEBUG_OBJECT (rtpsession, "parsed clock-rate %d", result);
1287
1288 done:
1289
1290   return result;
1291
1292   /* ERRORS */
1293 no_clock_rate:
1294   {
1295     gst_caps_unref (caps);
1296     GST_DEBUG_OBJECT (rtpsession, "No clock-rate in caps!");
1297     goto done;
1298   }
1299 }
1300
1301 /* called when the session manager asks us to reconsider the timeout */
1302 static void
1303 gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data)
1304 {
1305   GstRtpSession *rtpsession;
1306
1307   rtpsession = GST_RTP_SESSION_CAST (user_data);
1308
1309   GST_RTP_SESSION_LOCK (rtpsession);
1310   GST_DEBUG_OBJECT (rtpsession, "unlock timer for reconsideration");
1311   if (rtpsession->priv->id)
1312     gst_clock_id_unschedule (rtpsession->priv->id);
1313   GST_RTP_SESSION_UNLOCK (rtpsession);
1314 }
1315
1316 static gboolean
1317 gst_rtp_session_event_recv_rtp_sink (GstPad * pad, GstObject * parent,
1318     GstEvent * event)
1319 {
1320   GstRtpSession *rtpsession;
1321   gboolean ret = FALSE;
1322
1323   rtpsession = GST_RTP_SESSION (parent);
1324
1325   GST_DEBUG_OBJECT (rtpsession, "received event %s",
1326       GST_EVENT_TYPE_NAME (event));
1327
1328   switch (GST_EVENT_TYPE (event)) {
1329     case GST_EVENT_CAPS:
1330     {
1331       GstCaps *caps;
1332
1333       /* process */
1334       gst_event_parse_caps (event, &caps);
1335       gst_rtp_session_sink_setcaps (pad, rtpsession, caps);
1336       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1337       break;
1338     }
1339     case GST_EVENT_FLUSH_STOP:
1340       gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
1341       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1342       break;
1343     case GST_EVENT_SEGMENT:
1344     {
1345       GstSegment *segment, in_segment;
1346
1347       segment = &rtpsession->recv_rtp_seg;
1348
1349       /* the newsegment event is needed to convert the RTP timestamp to
1350        * running_time, which is needed to generate a mapping from RTP to NTP
1351        * timestamps in SR reports */
1352       gst_event_copy_segment (event, &in_segment);
1353       GST_DEBUG_OBJECT (rtpsession, "received segment %" GST_SEGMENT_FORMAT,
1354           &in_segment);
1355
1356       /* accept upstream */
1357       gst_segment_copy_into (&in_segment, segment);
1358
1359       /* push event forward */
1360       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1361       break;
1362     }
1363     default:
1364       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1365       break;
1366   }
1367
1368   return ret;
1369
1370 }
1371
1372 static gboolean
1373 gst_rtp_session_request_remote_key_unit (GstRtpSession * rtpsession,
1374     guint32 ssrc, guint payload, gboolean all_headers, gint count)
1375 {
1376   GstCaps *caps;
1377
1378   caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1379
1380   if (caps) {
1381     const GstStructure *s = gst_caps_get_structure (caps, 0);
1382     gboolean pli;
1383     gboolean fir;
1384
1385     pli = gst_structure_has_field (s, "rtcp-fb-nack-pli");
1386     fir = gst_structure_has_field (s, "rtcp-fb-ccm-fir") && all_headers;
1387
1388     /* Google Talk uses FIR for repair, so send it even if we just want a
1389      * regular PLI */
1390     if (!pli &&
1391         gst_structure_has_field (s, "rtcp-fb-x-gstreamer-fir-as-repair"))
1392       fir = TRUE;
1393
1394     gst_caps_unref (caps);
1395
1396     if (pli || fir)
1397       return rtp_session_request_key_unit (rtpsession->priv->session, ssrc,
1398           gst_clock_get_time (rtpsession->priv->sysclock), fir, count);
1399   }
1400
1401   return FALSE;
1402 }
1403
1404 static gboolean
1405 gst_rtp_session_event_recv_rtp_src (GstPad * pad, GstObject * parent,
1406     GstEvent * event)
1407 {
1408   GstRtpSession *rtpsession;
1409   gboolean forward = TRUE;
1410   gboolean ret = TRUE;
1411   const GstStructure *s;
1412   guint32 ssrc;
1413   guint pt;
1414
1415   rtpsession = GST_RTP_SESSION (parent);
1416
1417   switch (GST_EVENT_TYPE (event)) {
1418     case GST_EVENT_CUSTOM_UPSTREAM:
1419       s = gst_event_get_structure (event);
1420       if (gst_structure_has_name (s, "GstForceKeyUnit") &&
1421           gst_structure_get_uint (s, "ssrc", &ssrc) &&
1422           gst_structure_get_uint (s, "payload", &pt)) {
1423         gboolean all_headers = FALSE;
1424         gint count = -1;
1425
1426         gst_structure_get_boolean (s, "all-headers", &all_headers);
1427         if (gst_structure_get_int (s, "count", &count) && count < 0)
1428           count += G_MAXINT;    /* Make sure count is positive if present */
1429         if (gst_rtp_session_request_remote_key_unit (rtpsession, ssrc, pt,
1430                 all_headers, count))
1431           forward = FALSE;
1432       }
1433       break;
1434     default:
1435       break;
1436   }
1437
1438   if (forward) {
1439     GstPad *recv_rtp_sink;
1440
1441     GST_RTP_SESSION_LOCK (rtpsession);
1442     if ((recv_rtp_sink = rtpsession->recv_rtp_sink))
1443       gst_object_ref (recv_rtp_sink);
1444     GST_RTP_SESSION_UNLOCK (rtpsession);
1445
1446     if (recv_rtp_sink) {
1447       ret = gst_pad_push_event (recv_rtp_sink, event);
1448       gst_object_unref (recv_rtp_sink);
1449     } else
1450       gst_event_unref (event);
1451   } else {
1452     gst_event_unref (event);
1453   }
1454
1455   return ret;
1456 }
1457
1458
1459 static GstIterator *
1460 gst_rtp_session_iterate_internal_links (GstPad * pad, GstObject * parent)
1461 {
1462   GstRtpSession *rtpsession;
1463   GstPad *otherpad = NULL;
1464   GstIterator *it = NULL;
1465
1466   rtpsession = GST_RTP_SESSION (parent);
1467
1468   GST_RTP_SESSION_LOCK (rtpsession);
1469   if (pad == rtpsession->recv_rtp_src) {
1470     otherpad = gst_object_ref (rtpsession->recv_rtp_sink);
1471   } else if (pad == rtpsession->recv_rtp_sink) {
1472     otherpad = gst_object_ref (rtpsession->recv_rtp_src);
1473   } else if (pad == rtpsession->send_rtp_src) {
1474     otherpad = gst_object_ref (rtpsession->send_rtp_sink);
1475   } else if (pad == rtpsession->send_rtp_sink) {
1476     otherpad = gst_object_ref (rtpsession->send_rtp_src);
1477   }
1478   GST_RTP_SESSION_UNLOCK (rtpsession);
1479
1480   if (otherpad) {
1481     GValue val = { 0, };
1482
1483     g_value_init (&val, GST_TYPE_PAD);
1484     g_value_set_object (&val, otherpad);
1485     it = gst_iterator_new_single (GST_TYPE_PAD, &val);
1486     g_value_unset (&val);
1487     gst_object_unref (otherpad);
1488   }
1489
1490   return it;
1491 }
1492
1493 static gboolean
1494 gst_rtp_session_sink_setcaps (GstPad * pad, GstRtpSession * rtpsession,
1495     GstCaps * caps)
1496 {
1497   GST_RTP_SESSION_LOCK (rtpsession);
1498   gst_rtp_session_cache_caps (rtpsession, caps);
1499   GST_RTP_SESSION_UNLOCK (rtpsession);
1500
1501   return TRUE;
1502 }
1503
1504 /* receive a packet from a sender, send it to the RTP session manager and
1505  * forward the packet on the rtp_src pad
1506  */
1507 static GstFlowReturn
1508 gst_rtp_session_chain_recv_rtp (GstPad * pad, GstObject * parent,
1509     GstBuffer * buffer)
1510 {
1511   GstRtpSession *rtpsession;
1512   GstRtpSessionPrivate *priv;
1513   GstFlowReturn ret;
1514   GstClockTime current_time, running_time;
1515   GstClockTime timestamp;
1516
1517   rtpsession = GST_RTP_SESSION (parent);
1518   priv = rtpsession->priv;
1519
1520   GST_LOG_OBJECT (rtpsession, "received RTP packet");
1521
1522   /* get NTP time when this packet was captured, this depends on the timestamp. */
1523   timestamp = GST_BUFFER_TIMESTAMP (buffer);
1524   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1525     /* convert to running time using the segment values */
1526     running_time =
1527         gst_segment_to_running_time (&rtpsession->recv_rtp_seg, GST_FORMAT_TIME,
1528         timestamp);
1529   } else {
1530     get_current_times (rtpsession, &running_time, NULL);
1531   }
1532   current_time = gst_clock_get_time (priv->sysclock);
1533
1534   ret = rtp_session_process_rtp (priv->session, buffer, current_time,
1535       running_time);
1536   if (ret != GST_FLOW_OK)
1537     goto push_error;
1538
1539 done:
1540
1541   return ret;
1542
1543   /* ERRORS */
1544 push_error:
1545   {
1546     GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1547         gst_flow_get_name (ret));
1548     goto done;
1549   }
1550 }
1551
1552 static gboolean
1553 gst_rtp_session_event_recv_rtcp_sink (GstPad * pad, GstObject * parent,
1554     GstEvent * event)
1555 {
1556   GstRtpSession *rtpsession;
1557   gboolean ret = FALSE;
1558
1559   rtpsession = GST_RTP_SESSION (parent);
1560
1561   GST_DEBUG_OBJECT (rtpsession, "received event %s",
1562       GST_EVENT_TYPE_NAME (event));
1563
1564   switch (GST_EVENT_TYPE (event)) {
1565     default:
1566       ret = gst_pad_push_event (rtpsession->sync_src, event);
1567       break;
1568   }
1569
1570   return ret;
1571 }
1572
1573 /* Receive an RTCP packet from a sender, send it to the RTP session manager and
1574  * forward the SR packets to the sync_src pad.
1575  */
1576 static GstFlowReturn
1577 gst_rtp_session_chain_recv_rtcp (GstPad * pad, GstObject * parent,
1578     GstBuffer * buffer)
1579 {
1580   GstRtpSession *rtpsession;
1581   GstRtpSessionPrivate *priv;
1582   GstClockTime current_time;
1583   guint64 ntpnstime;
1584
1585   rtpsession = GST_RTP_SESSION (parent);
1586   priv = rtpsession->priv;
1587
1588   GST_LOG_OBJECT (rtpsession, "received RTCP packet");
1589
1590   current_time = gst_clock_get_time (priv->sysclock);
1591   get_current_times (rtpsession, NULL, &ntpnstime);
1592
1593   rtp_session_process_rtcp (priv->session, buffer, current_time, ntpnstime);
1594
1595   return GST_FLOW_OK;           /* always return OK */
1596 }
1597
1598 static gboolean
1599 gst_rtp_session_query_send_rtcp_src (GstPad * pad, GstObject * parent,
1600     GstQuery * query)
1601 {
1602   GstRtpSession *rtpsession;
1603   gboolean ret = FALSE;
1604
1605   rtpsession = GST_RTP_SESSION (parent);
1606
1607   GST_DEBUG_OBJECT (rtpsession, "received QUERY %s",
1608       GST_QUERY_TYPE_NAME (query));
1609
1610   switch (GST_QUERY_TYPE (query)) {
1611     case GST_QUERY_LATENCY:
1612       ret = TRUE;
1613       /* use the defaults for the latency query. */
1614       gst_query_set_latency (query, FALSE, 0, -1);
1615       break;
1616     default:
1617       /* other queries simply fail for now */
1618       break;
1619   }
1620
1621   return ret;
1622 }
1623
1624 static gboolean
1625 gst_rtp_session_event_send_rtcp_src (GstPad * pad, GstObject * parent,
1626     GstEvent * event)
1627 {
1628   GstRtpSession *rtpsession;
1629   gboolean ret = TRUE;
1630
1631   rtpsession = GST_RTP_SESSION (parent);
1632   GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
1633       GST_EVENT_TYPE_NAME (event));
1634
1635   switch (GST_EVENT_TYPE (event)) {
1636     case GST_EVENT_SEEK:
1637     case GST_EVENT_LATENCY:
1638       gst_event_unref (event);
1639       ret = TRUE;
1640       break;
1641     default:
1642       /* other events simply fail for now */
1643       gst_event_unref (event);
1644       ret = FALSE;
1645       break;
1646   }
1647
1648   return ret;
1649 }
1650
1651
1652 static gboolean
1653 gst_rtp_session_event_send_rtp_sink (GstPad * pad, GstObject * parent,
1654     GstEvent * event)
1655 {
1656   GstRtpSession *rtpsession;
1657   gboolean ret = FALSE;
1658
1659   rtpsession = GST_RTP_SESSION (parent);
1660
1661   GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
1662       GST_EVENT_TYPE_NAME (event));
1663
1664   switch (GST_EVENT_TYPE (event)) {
1665     case GST_EVENT_CAPS:
1666     {
1667       GstCaps *caps;
1668
1669       /* process */
1670       gst_event_parse_caps (event, &caps);
1671       gst_rtp_session_setcaps_send_rtp (pad, rtpsession, caps);
1672       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1673       break;
1674     }
1675     case GST_EVENT_FLUSH_STOP:
1676       gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
1677       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1678       break;
1679     case GST_EVENT_SEGMENT:{
1680       GstSegment *segment, in_segment;
1681
1682       segment = &rtpsession->send_rtp_seg;
1683
1684       /* the newsegment event is needed to convert the RTP timestamp to
1685        * running_time, which is needed to generate a mapping from RTP to NTP
1686        * timestamps in SR reports */
1687       gst_event_copy_segment (event, &in_segment);
1688       GST_DEBUG_OBJECT (rtpsession, "received segment %" GST_SEGMENT_FORMAT,
1689           &in_segment);
1690
1691       /* accept upstream */
1692       gst_segment_copy_into (&in_segment, segment);
1693
1694       /* push event forward */
1695       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1696       break;
1697     }
1698     case GST_EVENT_EOS:{
1699       GstClockTime current_time;
1700
1701       /* push downstream FIXME, we are not supposed to leave the session just
1702        * because we stop sending. */
1703       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1704       current_time = gst_clock_get_time (rtpsession->priv->sysclock);
1705       GST_DEBUG_OBJECT (rtpsession, "scheduling BYE message");
1706       rtp_session_schedule_bye (rtpsession->priv->session, "End of stream",
1707           current_time);
1708       break;
1709     }
1710     default:{
1711       GstPad *send_rtp_src;
1712
1713       GST_RTP_SESSION_LOCK (rtpsession);
1714       if ((send_rtp_src = rtpsession->send_rtp_src))
1715         gst_object_ref (send_rtp_src);
1716       GST_RTP_SESSION_UNLOCK (rtpsession);
1717
1718       if (send_rtp_src) {
1719         ret = gst_pad_push_event (send_rtp_src, event);
1720         gst_object_unref (send_rtp_src);
1721       } else
1722         gst_event_unref (event);
1723
1724       break;
1725     }
1726   }
1727
1728   return ret;
1729 }
1730
1731 static gboolean
1732 gst_rtp_session_event_send_rtp_src (GstPad * pad, GstObject * parent,
1733     GstEvent * event)
1734 {
1735   GstRtpSession *rtpsession;
1736   gboolean ret = FALSE;
1737
1738   rtpsession = GST_RTP_SESSION (parent);
1739
1740   GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
1741       GST_EVENT_TYPE_NAME (event));
1742
1743   switch (GST_EVENT_TYPE (event)) {
1744     case GST_EVENT_LATENCY:
1745       /* save the latency, we need this to know when an RTP packet will be
1746        * rendered by the sink */
1747       gst_event_parse_latency (event, &rtpsession->priv->send_latency);
1748
1749       ret = gst_pad_event_default (pad, parent, event);
1750       break;
1751     default:
1752       ret = gst_pad_event_default (pad, parent, event);
1753       break;
1754   }
1755   return ret;
1756 }
1757
1758 static GstCaps *
1759 gst_rtp_session_getcaps_send_rtp (GstPad * pad, GstRtpSession * rtpsession,
1760     GstCaps * filter)
1761 {
1762   GstRtpSessionPrivate *priv;
1763   GstCaps *result;
1764   GstStructure *s1, *s2;
1765   guint ssrc;
1766
1767   priv = rtpsession->priv;
1768
1769   ssrc = rtp_session_get_internal_ssrc (priv->session);
1770
1771   /* we can basically accept anything but we prefer to receive packets with our
1772    * internal SSRC so that we don't have to patch it. Create a structure with
1773    * the SSRC and another one without. */
1774   s1 = gst_structure_new ("application/x-rtp", "ssrc", G_TYPE_UINT, ssrc, NULL);
1775   s2 = gst_structure_new_empty ("application/x-rtp");
1776
1777   result = gst_caps_new_full (s1, s2, NULL);
1778
1779   if (filter) {
1780     GstCaps *caps = result;
1781
1782     result = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
1783     gst_caps_unref (caps);
1784   }
1785
1786   GST_DEBUG_OBJECT (rtpsession, "getting caps %" GST_PTR_FORMAT, result);
1787
1788   return result;
1789 }
1790
1791 static gboolean
1792 gst_rtp_session_query_send_rtp (GstPad * pad, GstObject * parent,
1793     GstQuery * query)
1794 {
1795   gboolean res = FALSE;
1796   GstRtpSession *rtpsession;
1797
1798   rtpsession = GST_RTP_SESSION (parent);
1799
1800   switch (GST_QUERY_TYPE (query)) {
1801     case GST_QUERY_CAPS:
1802     {
1803       GstCaps *filter, *caps;
1804
1805       gst_query_parse_caps (query, &filter);
1806       caps = gst_rtp_session_getcaps_send_rtp (pad, rtpsession, filter);
1807       gst_query_set_caps_result (query, caps);
1808       gst_caps_unref (caps);
1809       res = TRUE;
1810       break;
1811     }
1812     default:
1813       res = gst_pad_query_default (pad, parent, query);
1814       break;
1815   }
1816
1817   return res;
1818 }
1819
1820 static gboolean
1821 gst_rtp_session_setcaps_send_rtp (GstPad * pad, GstRtpSession * rtpsession,
1822     GstCaps * caps)
1823 {
1824   GstRtpSessionPrivate *priv;
1825   GstStructure *s = gst_caps_get_structure (caps, 0);
1826   guint ssrc;
1827
1828   priv = rtpsession->priv;
1829
1830   if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
1831     GST_DEBUG_OBJECT (rtpsession, "setting internal SSRC to %08x", ssrc);
1832     rtp_session_set_internal_ssrc (priv->session, ssrc);
1833   }
1834   rtp_session_update_send_caps (priv->session, caps);
1835
1836   return TRUE;
1837 }
1838
1839 /* Recieve an RTP packet or a list of packets to be send to the receivers,
1840  * send to RTP session manager and forward to send_rtp_src.
1841  */
1842 static GstFlowReturn
1843 gst_rtp_session_chain_send_rtp_common (GstRtpSession * rtpsession,
1844     gpointer data, gboolean is_list)
1845 {
1846   GstRtpSessionPrivate *priv;
1847   GstFlowReturn ret;
1848   GstClockTime timestamp, running_time;
1849   GstClockTime current_time;
1850
1851   priv = rtpsession->priv;
1852
1853   GST_LOG_OBJECT (rtpsession, "received RTP %s", is_list ? "list" : "packet");
1854
1855   /* get NTP time when this packet was captured, this depends on the timestamp. */
1856   if (is_list) {
1857     GstBuffer *buffer = NULL;
1858
1859     /* All groups in an list have the same timestamp.
1860      * So, just take it from the first group. */
1861     buffer = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0);
1862     if (buffer)
1863       timestamp = GST_BUFFER_TIMESTAMP (buffer);
1864     else
1865       timestamp = -1;
1866   } else {
1867     timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (data));
1868   }
1869
1870   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1871     /* convert to running time using the segment start value. */
1872     running_time =
1873         gst_segment_to_running_time (&rtpsession->send_rtp_seg, GST_FORMAT_TIME,
1874         timestamp);
1875     running_time += priv->send_latency;
1876   } else {
1877     /* no timestamp. */
1878     running_time = -1;
1879   }
1880
1881   current_time = gst_clock_get_time (priv->sysclock);
1882   ret = rtp_session_send_rtp (priv->session, data, is_list, current_time,
1883       running_time);
1884   if (ret != GST_FLOW_OK)
1885     goto push_error;
1886
1887 done:
1888
1889   return ret;
1890
1891   /* ERRORS */
1892 push_error:
1893   {
1894     GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1895         gst_flow_get_name (ret));
1896     goto done;
1897   }
1898 }
1899
1900 static GstFlowReturn
1901 gst_rtp_session_chain_send_rtp (GstPad * pad, GstObject * parent,
1902     GstBuffer * buffer)
1903 {
1904   GstRtpSession *rtpsession = GST_RTP_SESSION (parent);
1905
1906   return gst_rtp_session_chain_send_rtp_common (rtpsession, buffer, FALSE);
1907 }
1908
1909 static GstFlowReturn
1910 gst_rtp_session_chain_send_rtp_list (GstPad * pad, GstObject * parent,
1911     GstBufferList * list)
1912 {
1913   GstRtpSession *rtpsession = GST_RTP_SESSION (parent);
1914
1915   return gst_rtp_session_chain_send_rtp_common (rtpsession, list, TRUE);
1916 }
1917
1918 /* Create sinkpad to receive RTP packets from senders. This will also create a
1919  * srcpad for the RTP packets.
1920  */
1921 static GstPad *
1922 create_recv_rtp_sink (GstRtpSession * rtpsession)
1923 {
1924   GST_DEBUG_OBJECT (rtpsession, "creating RTP sink pad");
1925
1926   rtpsession->recv_rtp_sink =
1927       gst_pad_new_from_static_template (&rtpsession_recv_rtp_sink_template,
1928       "recv_rtp_sink");
1929   gst_pad_set_chain_function (rtpsession->recv_rtp_sink,
1930       gst_rtp_session_chain_recv_rtp);
1931   gst_pad_set_event_function (rtpsession->recv_rtp_sink,
1932       gst_rtp_session_event_recv_rtp_sink);
1933   gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_sink,
1934       gst_rtp_session_iterate_internal_links);
1935   gst_pad_set_active (rtpsession->recv_rtp_sink, TRUE);
1936   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1937       rtpsession->recv_rtp_sink);
1938
1939   GST_DEBUG_OBJECT (rtpsession, "creating RTP src pad");
1940   rtpsession->recv_rtp_src =
1941       gst_pad_new_from_static_template (&rtpsession_recv_rtp_src_template,
1942       "recv_rtp_src");
1943   gst_pad_set_event_function (rtpsession->recv_rtp_src,
1944       gst_rtp_session_event_recv_rtp_src);
1945   gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_src,
1946       gst_rtp_session_iterate_internal_links);
1947   gst_pad_use_fixed_caps (rtpsession->recv_rtp_src);
1948   gst_pad_set_active (rtpsession->recv_rtp_src, TRUE);
1949   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->recv_rtp_src);
1950
1951   return rtpsession->recv_rtp_sink;
1952 }
1953
1954 /* Remove sinkpad to receive RTP packets from senders. This will also remove
1955  * the srcpad for the RTP packets.
1956  */
1957 static void
1958 remove_recv_rtp_sink (GstRtpSession * rtpsession)
1959 {
1960   GST_DEBUG_OBJECT (rtpsession, "removing RTP sink pad");
1961
1962   /* deactivate from source to sink */
1963   gst_pad_set_active (rtpsession->recv_rtp_src, FALSE);
1964   gst_pad_set_active (rtpsession->recv_rtp_sink, FALSE);
1965
1966   /* remove pads */
1967   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1968       rtpsession->recv_rtp_sink);
1969   rtpsession->recv_rtp_sink = NULL;
1970
1971   GST_DEBUG_OBJECT (rtpsession, "removing RTP src pad");
1972   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1973       rtpsession->recv_rtp_src);
1974   rtpsession->recv_rtp_src = NULL;
1975 }
1976
1977 /* Create a sinkpad to receive RTCP messages from senders, this will also create a
1978  * sync_src pad for the SR packets.
1979  */
1980 static GstPad *
1981 create_recv_rtcp_sink (GstRtpSession * rtpsession)
1982 {
1983   GST_DEBUG_OBJECT (rtpsession, "creating RTCP sink pad");
1984
1985   rtpsession->recv_rtcp_sink =
1986       gst_pad_new_from_static_template (&rtpsession_recv_rtcp_sink_template,
1987       "recv_rtcp_sink");
1988   gst_pad_set_chain_function (rtpsession->recv_rtcp_sink,
1989       gst_rtp_session_chain_recv_rtcp);
1990   gst_pad_set_event_function (rtpsession->recv_rtcp_sink,
1991       gst_rtp_session_event_recv_rtcp_sink);
1992   gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtcp_sink,
1993       gst_rtp_session_iterate_internal_links);
1994   gst_pad_set_active (rtpsession->recv_rtcp_sink, TRUE);
1995   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1996       rtpsession->recv_rtcp_sink);
1997
1998   GST_DEBUG_OBJECT (rtpsession, "creating sync src pad");
1999   rtpsession->sync_src =
2000       gst_pad_new_from_static_template (&rtpsession_sync_src_template,
2001       "sync_src");
2002   gst_pad_set_iterate_internal_links_function (rtpsession->sync_src,
2003       gst_rtp_session_iterate_internal_links);
2004   gst_pad_use_fixed_caps (rtpsession->sync_src);
2005   gst_pad_set_active (rtpsession->sync_src, TRUE);
2006   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
2007
2008   return rtpsession->recv_rtcp_sink;
2009 }
2010
2011 static void
2012 remove_recv_rtcp_sink (GstRtpSession * rtpsession)
2013 {
2014   GST_DEBUG_OBJECT (rtpsession, "removing RTCP sink pad");
2015
2016   gst_pad_set_active (rtpsession->sync_src, FALSE);
2017   gst_pad_set_active (rtpsession->recv_rtcp_sink, FALSE);
2018
2019   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2020       rtpsession->recv_rtcp_sink);
2021   rtpsession->recv_rtcp_sink = NULL;
2022
2023   GST_DEBUG_OBJECT (rtpsession, "removing sync src pad");
2024   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
2025   rtpsession->sync_src = NULL;
2026 }
2027
2028 /* Create a sinkpad to receive RTP packets for receivers. This will also create a
2029  * send_rtp_src pad.
2030  */
2031 static GstPad *
2032 create_send_rtp_sink (GstRtpSession * rtpsession)
2033 {
2034   GST_DEBUG_OBJECT (rtpsession, "creating pad");
2035
2036   rtpsession->send_rtp_sink =
2037       gst_pad_new_from_static_template (&rtpsession_send_rtp_sink_template,
2038       "send_rtp_sink");
2039   gst_pad_set_chain_function (rtpsession->send_rtp_sink,
2040       gst_rtp_session_chain_send_rtp);
2041   gst_pad_set_chain_list_function (rtpsession->send_rtp_sink,
2042       gst_rtp_session_chain_send_rtp_list);
2043   gst_pad_set_query_function (rtpsession->send_rtp_sink,
2044       gst_rtp_session_query_send_rtp);
2045   gst_pad_set_event_function (rtpsession->send_rtp_sink,
2046       gst_rtp_session_event_send_rtp_sink);
2047   gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_sink,
2048       gst_rtp_session_iterate_internal_links);
2049   gst_pad_set_active (rtpsession->send_rtp_sink, TRUE);
2050   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2051       rtpsession->send_rtp_sink);
2052
2053   rtpsession->send_rtp_src =
2054       gst_pad_new_from_static_template (&rtpsession_send_rtp_src_template,
2055       "send_rtp_src");
2056   gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_src,
2057       gst_rtp_session_iterate_internal_links);
2058   gst_pad_set_event_function (rtpsession->send_rtp_src,
2059       gst_rtp_session_event_send_rtp_src);
2060   gst_pad_set_active (rtpsession->send_rtp_src, TRUE);
2061   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->send_rtp_src);
2062
2063   return rtpsession->send_rtp_sink;
2064 }
2065
2066 static void
2067 remove_send_rtp_sink (GstRtpSession * rtpsession)
2068 {
2069   GST_DEBUG_OBJECT (rtpsession, "removing pad");
2070
2071   gst_pad_set_active (rtpsession->send_rtp_src, FALSE);
2072   gst_pad_set_active (rtpsession->send_rtp_sink, FALSE);
2073
2074   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2075       rtpsession->send_rtp_sink);
2076   rtpsession->send_rtp_sink = NULL;
2077
2078   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2079       rtpsession->send_rtp_src);
2080   rtpsession->send_rtp_src = NULL;
2081 }
2082
2083 /* Create a srcpad with the RTCP packets to send out.
2084  * This pad will be driven by the RTP session manager when it wants to send out
2085  * RTCP packets.
2086  */
2087 static GstPad *
2088 create_send_rtcp_src (GstRtpSession * rtpsession)
2089 {
2090   GST_DEBUG_OBJECT (rtpsession, "creating pad");
2091
2092   rtpsession->send_rtcp_src =
2093       gst_pad_new_from_static_template (&rtpsession_send_rtcp_src_template,
2094       "send_rtcp_src");
2095   gst_pad_use_fixed_caps (rtpsession->send_rtcp_src);
2096   gst_pad_set_active (rtpsession->send_rtcp_src, TRUE);
2097   gst_pad_set_iterate_internal_links_function (rtpsession->send_rtcp_src,
2098       gst_rtp_session_iterate_internal_links);
2099   gst_pad_set_query_function (rtpsession->send_rtcp_src,
2100       gst_rtp_session_query_send_rtcp_src);
2101   gst_pad_set_event_function (rtpsession->send_rtcp_src,
2102       gst_rtp_session_event_send_rtcp_src);
2103   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2104       rtpsession->send_rtcp_src);
2105
2106   return rtpsession->send_rtcp_src;
2107 }
2108
2109 static void
2110 remove_send_rtcp_src (GstRtpSession * rtpsession)
2111 {
2112   GST_DEBUG_OBJECT (rtpsession, "removing pad");
2113
2114   gst_pad_set_active (rtpsession->send_rtcp_src, FALSE);
2115
2116   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2117       rtpsession->send_rtcp_src);
2118   rtpsession->send_rtcp_src = NULL;
2119 }
2120
2121 static GstPad *
2122 gst_rtp_session_request_new_pad (GstElement * element,
2123     GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
2124 {
2125   GstRtpSession *rtpsession;
2126   GstElementClass *klass;
2127   GstPad *result;
2128
2129   g_return_val_if_fail (templ != NULL, NULL);
2130   g_return_val_if_fail (GST_IS_RTP_SESSION (element), NULL);
2131
2132   rtpsession = GST_RTP_SESSION (element);
2133   klass = GST_ELEMENT_GET_CLASS (element);
2134
2135   GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
2136
2137   GST_RTP_SESSION_LOCK (rtpsession);
2138
2139   /* figure out the template */
2140   if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink")) {
2141     if (rtpsession->recv_rtp_sink != NULL)
2142       goto exists;
2143
2144     result = create_recv_rtp_sink (rtpsession);
2145   } else if (templ == gst_element_class_get_pad_template (klass,
2146           "recv_rtcp_sink")) {
2147     if (rtpsession->recv_rtcp_sink != NULL)
2148       goto exists;
2149
2150     result = create_recv_rtcp_sink (rtpsession);
2151   } else if (templ == gst_element_class_get_pad_template (klass,
2152           "send_rtp_sink")) {
2153     if (rtpsession->send_rtp_sink != NULL)
2154       goto exists;
2155
2156     result = create_send_rtp_sink (rtpsession);
2157   } else if (templ == gst_element_class_get_pad_template (klass,
2158           "send_rtcp_src")) {
2159     if (rtpsession->send_rtcp_src != NULL)
2160       goto exists;
2161
2162     result = create_send_rtcp_src (rtpsession);
2163   } else
2164     goto wrong_template;
2165
2166   GST_RTP_SESSION_UNLOCK (rtpsession);
2167
2168   return result;
2169
2170   /* ERRORS */
2171 wrong_template:
2172   {
2173     GST_RTP_SESSION_UNLOCK (rtpsession);
2174     g_warning ("gstrtpsession: this is not our template");
2175     return NULL;
2176   }
2177 exists:
2178   {
2179     GST_RTP_SESSION_UNLOCK (rtpsession);
2180     g_warning ("gstrtpsession: pad already requested");
2181     return NULL;
2182   }
2183 }
2184
2185 static void
2186 gst_rtp_session_release_pad (GstElement * element, GstPad * pad)
2187 {
2188   GstRtpSession *rtpsession;
2189
2190   g_return_if_fail (GST_IS_RTP_SESSION (element));
2191   g_return_if_fail (GST_IS_PAD (pad));
2192
2193   rtpsession = GST_RTP_SESSION (element);
2194
2195   GST_DEBUG_OBJECT (element, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2196
2197   GST_RTP_SESSION_LOCK (rtpsession);
2198
2199   if (rtpsession->recv_rtp_sink == pad) {
2200     remove_recv_rtp_sink (rtpsession);
2201   } else if (rtpsession->recv_rtcp_sink == pad) {
2202     remove_recv_rtcp_sink (rtpsession);
2203   } else if (rtpsession->send_rtp_sink == pad) {
2204     remove_send_rtp_sink (rtpsession);
2205   } else if (rtpsession->send_rtcp_src == pad) {
2206     remove_send_rtcp_src (rtpsession);
2207   } else
2208     goto wrong_pad;
2209
2210   GST_RTP_SESSION_UNLOCK (rtpsession);
2211
2212   return;
2213
2214   /* ERRORS */
2215 wrong_pad:
2216   {
2217     GST_RTP_SESSION_UNLOCK (rtpsession);
2218     g_warning ("gstrtpsession: asked to release an unknown pad");
2219     return;
2220   }
2221 }
2222
2223 static void
2224 gst_rtp_session_request_key_unit (RTPSession * sess,
2225     gboolean all_headers, gpointer user_data)
2226 {
2227   GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2228   GstEvent *event;
2229   GstPad *send_rtp_sink;
2230
2231   GST_RTP_SESSION_LOCK (rtpsession);
2232   if ((send_rtp_sink = rtpsession->send_rtp_sink))
2233     gst_object_ref (send_rtp_sink);
2234   GST_RTP_SESSION_UNLOCK (rtpsession);
2235
2236   if (send_rtp_sink) {
2237     event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
2238         gst_structure_new ("GstForceKeyUnit",
2239             "all-headers", G_TYPE_BOOLEAN, all_headers, NULL));
2240     gst_pad_push_event (send_rtp_sink, event);
2241     gst_object_unref (send_rtp_sink);
2242   }
2243 }
2244
2245 static GstClockTime
2246 gst_rtp_session_request_time (RTPSession * session, gpointer user_data)
2247 {
2248   GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2249
2250   return gst_clock_get_time (rtpsession->priv->sysclock);
2251 }