1d2e96ec5afa18804edc572b7e668a1e65e9e726
[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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * SECTION:element-rtpsession
22  * @see_also: rtpjitterbuffer, rtpbin, rtpptdemux, rtpssrcdemux
23  *
24  * The RTP session manager models participants with 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  *   <listitem>
44  *     <para>Support for multiple sender SSRC.</para>
45  *   </listitem>
46  * </itemizedlist>
47  *
48  * The rtpsession will not demux packets based on SSRC or payload type, nor will
49  * it correct for packet reordering and jitter. Use #GstRtpsSrcDemux,
50  * #GstRtpPtDemux and GstRtpJitterBuffer in addition to #GstRtpSession to
51  * perform these tasks. It is usually a good idea to use #GstRtpBin, which
52  * combines all these features in one element.
53  *
54  * To use #GstRtpSession as an RTP receiver, request a recv_rtp_sink pad, which will
55  * automatically create recv_rtp_src pad. Data received on the recv_rtp_sink pad
56  * will be processed in the session and after being validated forwarded on the
57  * recv_rtp_src pad.
58  *
59  * To also use #GstRtpSession as an RTCP receiver, request a recv_rtcp_sink pad,
60  * which will automatically create a sync_src pad. Packets received on the RTCP
61  * pad will be used by the session manager to update the stats and database of
62  * the other participants. SR packets will be forwarded on the sync_src pad
63  * so that they can be used to perform inter-stream synchronisation when needed.
64  *
65  * If you want the session manager to generate and send RTCP packets, request
66  * the send_rtcp_src pad. Packet pushed on this pad contain SR/RR RTCP reports
67  * that should be sent to all participants in the session.
68  *
69  * To use #GstRtpSession as a sender, request a send_rtp_sink pad, which will
70  * automatically create a send_rtp_src pad. The session manager will
71  * forward the packets on the send_rtp_src pad after updating its internal state.
72  *
73  * The session manager needs the clock-rate of the payload types it is handling
74  * and will signal the #GstRtpSession::request-pt-map signal when it needs such a
75  * mapping. One can clear the cached values with the #GstRtpSession::clear-pt-map
76  * signal.
77  *
78  * <refsect2>
79  * <title>Example pipelines</title>
80  * |[
81  * gst-launch-1.0 udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink rtpsession .recv_rtp_src ! rtptheoradepay ! theoradec ! xvimagesink
82  * ]| Receive theora RTP packets from port 5000 and send them to the depayloader,
83  * decoder and display. Note that the application/x-rtp caps on udpsrc should be
84  * configured based on some negotiation process such as RTSP for this pipeline
85  * to work correctly.
86  * |[
87  * gst-launch-1.0 udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink rtpsession name=session \
88  *        .recv_rtp_src ! rtptheoradepay ! theoradec ! xvimagesink \
89  *     udpsrc port=5001 caps="application/x-rtcp" ! session.recv_rtcp_sink
90  * ]| Receive theora RTP packets from port 5000 and send them to the depayloader,
91  * decoder and display. Receive RTCP packets from port 5001 and process them in
92  * the session manager.
93  * Note that the application/x-rtp caps on udpsrc should be
94  * configured based on some negotiation process such as RTSP for this pipeline
95  * to work correctly.
96  * |[
97  * gst-launch-1.0 videotestsrc ! theoraenc ! rtptheorapay ! .send_rtp_sink rtpsession .send_rtp_src ! udpsink port=5000
98  * ]| Send theora RTP packets through the session manager and out on UDP port
99  * 5000.
100  * |[
101  * gst-launch-1.0 videotestsrc ! theoraenc ! rtptheorapay ! .send_rtp_sink rtpsession name=session .send_rtp_src \
102  *     ! udpsink port=5000  session.send_rtcp_src ! udpsink port=5001
103  * ]| Send theora RTP packets through the session manager and out on UDP port
104  * 5000. Send RTCP packets on port 5001. Note that this pipeline will not preroll
105  * correctly because the second udpsink will not preroll correctly (no RTCP
106  * packets are sent in the PAUSED state). Applications should manually set and
107  * keep (see gst_element_set_locked_state()) the RTCP udpsink to the PLAYING state.
108  * </refsect2>
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 "gstrtpsession.h"
120 #include "rtpsession.h"
121
122 GST_DEBUG_CATEGORY_STATIC (gst_rtp_session_debug);
123 #define GST_CAT_DEFAULT gst_rtp_session_debug
124
125 GType
126 gst_rtp_ntp_time_source_get_type (void)
127 {
128   static GType type = 0;
129   static const GEnumValue values[] = {
130     {GST_RTP_NTP_TIME_SOURCE_NTP, "NTP time based on realtime clock", "ntp"},
131     {GST_RTP_NTP_TIME_SOURCE_UNIX, "UNIX time based on realtime clock", "unix"},
132     {GST_RTP_NTP_TIME_SOURCE_RUNNING_TIME,
133           "Running time based on pipeline clock",
134         "running-time"},
135     {GST_RTP_NTP_TIME_SOURCE_CLOCK_TIME, "Pipeline clock time", "clock-time"},
136     {0, NULL, NULL},
137   };
138
139   if (!type) {
140     type = g_enum_register_static ("GstRtpNtpTimeSource", values);
141   }
142   return type;
143 }
144
145 /* sink pads */
146 static GstStaticPadTemplate rtpsession_recv_rtp_sink_template =
147 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink",
148     GST_PAD_SINK,
149     GST_PAD_REQUEST,
150     GST_STATIC_CAPS ("application/x-rtp")
151     );
152
153 static GstStaticPadTemplate rtpsession_recv_rtcp_sink_template =
154 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink",
155     GST_PAD_SINK,
156     GST_PAD_REQUEST,
157     GST_STATIC_CAPS ("application/x-rtcp")
158     );
159
160 static GstStaticPadTemplate rtpsession_send_rtp_sink_template =
161 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink",
162     GST_PAD_SINK,
163     GST_PAD_REQUEST,
164     GST_STATIC_CAPS ("application/x-rtp")
165     );
166
167 /* src pads */
168 static GstStaticPadTemplate rtpsession_recv_rtp_src_template =
169 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src",
170     GST_PAD_SRC,
171     GST_PAD_SOMETIMES,
172     GST_STATIC_CAPS ("application/x-rtp")
173     );
174
175 static GstStaticPadTemplate rtpsession_sync_src_template =
176 GST_STATIC_PAD_TEMPLATE ("sync_src",
177     GST_PAD_SRC,
178     GST_PAD_SOMETIMES,
179     GST_STATIC_CAPS ("application/x-rtcp")
180     );
181
182 static GstStaticPadTemplate rtpsession_send_rtp_src_template =
183 GST_STATIC_PAD_TEMPLATE ("send_rtp_src",
184     GST_PAD_SRC,
185     GST_PAD_SOMETIMES,
186     GST_STATIC_CAPS ("application/x-rtp")
187     );
188
189 static GstStaticPadTemplate rtpsession_send_rtcp_src_template =
190 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src",
191     GST_PAD_SRC,
192     GST_PAD_REQUEST,
193     GST_STATIC_CAPS ("application/x-rtcp")
194     );
195
196 /* signals and args */
197 enum
198 {
199   SIGNAL_REQUEST_PT_MAP,
200   SIGNAL_CLEAR_PT_MAP,
201
202   SIGNAL_ON_NEW_SSRC,
203   SIGNAL_ON_SSRC_COLLISION,
204   SIGNAL_ON_SSRC_VALIDATED,
205   SIGNAL_ON_SSRC_ACTIVE,
206   SIGNAL_ON_SSRC_SDES,
207   SIGNAL_ON_BYE_SSRC,
208   SIGNAL_ON_BYE_TIMEOUT,
209   SIGNAL_ON_TIMEOUT,
210   SIGNAL_ON_SENDER_TIMEOUT,
211   SIGNAL_ON_NEW_SENDER_SSRC,
212   SIGNAL_ON_SENDER_SSRC_ACTIVE,
213   LAST_SIGNAL
214 };
215
216 #define DEFAULT_BANDWIDTH            0
217 #define DEFAULT_RTCP_FRACTION        RTP_STATS_RTCP_FRACTION
218 #define DEFAULT_RTCP_RR_BANDWIDTH    -1
219 #define DEFAULT_RTCP_RS_BANDWIDTH    -1
220 #define DEFAULT_SDES                 NULL
221 #define DEFAULT_NUM_SOURCES          0
222 #define DEFAULT_NUM_ACTIVE_SOURCES   0
223 #define DEFAULT_USE_PIPELINE_CLOCK   FALSE
224 #define DEFAULT_RTCP_MIN_INTERVAL    (RTP_STATS_MIN_INTERVAL * GST_SECOND)
225 #define DEFAULT_PROBATION            RTP_DEFAULT_PROBATION
226 #define DEFAULT_MAX_DROPOUT_TIME     60000
227 #define DEFAULT_MAX_MISORDER_TIME    2000
228 #define DEFAULT_RTP_PROFILE          GST_RTP_PROFILE_AVP
229 #define DEFAULT_NTP_TIME_SOURCE      GST_RTP_NTP_TIME_SOURCE_NTP
230 #define DEFAULT_RTCP_SYNC_SEND_TIME  TRUE
231
232 enum
233 {
234   PROP_0,
235   PROP_BANDWIDTH,
236   PROP_RTCP_FRACTION,
237   PROP_RTCP_RR_BANDWIDTH,
238   PROP_RTCP_RS_BANDWIDTH,
239   PROP_SDES,
240   PROP_NUM_SOURCES,
241   PROP_NUM_ACTIVE_SOURCES,
242   PROP_INTERNAL_SESSION,
243   PROP_USE_PIPELINE_CLOCK,
244   PROP_RTCP_MIN_INTERVAL,
245   PROP_PROBATION,
246   PROP_MAX_DROPOUT_TIME,
247   PROP_MAX_MISORDER_TIME,
248   PROP_STATS,
249   PROP_RTP_PROFILE,
250   PROP_NTP_TIME_SOURCE,
251   PROP_RTCP_SYNC_SEND_TIME
252 };
253
254 #define GST_RTP_SESSION_LOCK(sess)   g_mutex_lock (&(sess)->priv->lock)
255 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock (&(sess)->priv->lock)
256
257 #define GST_RTP_SESSION_WAIT(sess)   g_cond_wait (&(sess)->priv->cond, &(sess)->priv->lock)
258 #define GST_RTP_SESSION_SIGNAL(sess) g_cond_signal (&(sess)->priv->cond)
259
260 struct _GstRtpSessionPrivate
261 {
262   GMutex lock;
263   GCond cond;
264   GstClock *sysclock;
265
266   RTPSession *session;
267
268   /* thread for sending out RTCP */
269   GstClockID id;
270   gboolean stop_thread;
271   GThread *thread;
272   gboolean thread_stopped;
273   gboolean wait_send;
274
275   /* caps mapping */
276   GHashTable *ptmap;
277
278   GstClockTime send_latency;
279
280   gboolean use_pipeline_clock;
281   GstRtpNtpTimeSource ntp_time_source;
282   gboolean rtcp_sync_send_time;
283
284   guint recv_rtx_req_count;
285   guint sent_rtx_req_count;
286 };
287
288 /* callbacks to handle actions from the session manager */
289 static GstFlowReturn gst_rtp_session_process_rtp (RTPSession * sess,
290     RTPSource * src, GstBuffer * buffer, gpointer user_data);
291 static GstFlowReturn gst_rtp_session_send_rtp (RTPSession * sess,
292     RTPSource * src, gpointer data, gpointer user_data);
293 static GstFlowReturn gst_rtp_session_send_rtcp (RTPSession * sess,
294     RTPSource * src, GstBuffer * buffer, gboolean eos, gpointer user_data);
295 static GstFlowReturn gst_rtp_session_sync_rtcp (RTPSession * sess,
296     GstBuffer * buffer, gpointer user_data);
297 static gint gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
298     gpointer user_data);
299 static void gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data);
300 static void gst_rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc,
301     gboolean all_headers, gpointer user_data);
302 static GstClockTime gst_rtp_session_request_time (RTPSession * session,
303     gpointer user_data);
304 static void gst_rtp_session_notify_nack (RTPSession * sess,
305     guint16 seqnum, guint16 blp, guint32 ssrc, gpointer user_data);
306 static void gst_rtp_session_reconfigure (RTPSession * sess, gpointer user_data);
307 static void gst_rtp_session_notify_early_rtcp (RTPSession * sess,
308     gpointer user_data);
309 static GstFlowReturn gst_rtp_session_chain_recv_rtp (GstPad * pad,
310     GstObject * parent, GstBuffer * buffer);
311 static GstFlowReturn gst_rtp_session_chain_recv_rtcp (GstPad * pad,
312     GstObject * parent, GstBuffer * buffer);
313 static GstFlowReturn gst_rtp_session_chain_send_rtp (GstPad * pad,
314     GstObject * parent, GstBuffer * buffer);
315 static GstFlowReturn gst_rtp_session_chain_send_rtp_list (GstPad * pad,
316     GstObject * parent, GstBufferList * list);
317
318 static RTPSessionCallbacks callbacks = {
319   gst_rtp_session_process_rtp,
320   gst_rtp_session_send_rtp,
321   gst_rtp_session_sync_rtcp,
322   gst_rtp_session_send_rtcp,
323   gst_rtp_session_clock_rate,
324   gst_rtp_session_reconsider,
325   gst_rtp_session_request_key_unit,
326   gst_rtp_session_request_time,
327   gst_rtp_session_notify_nack,
328   gst_rtp_session_reconfigure,
329   gst_rtp_session_notify_early_rtcp
330 };
331
332 /* GObject vmethods */
333 static void gst_rtp_session_finalize (GObject * object);
334 static void gst_rtp_session_set_property (GObject * object, guint prop_id,
335     const GValue * value, GParamSpec * pspec);
336 static void gst_rtp_session_get_property (GObject * object, guint prop_id,
337     GValue * value, GParamSpec * pspec);
338
339 /* GstElement vmethods */
340 static GstStateChangeReturn gst_rtp_session_change_state (GstElement * element,
341     GstStateChange transition);
342 static GstPad *gst_rtp_session_request_new_pad (GstElement * element,
343     GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
344 static void gst_rtp_session_release_pad (GstElement * element, GstPad * pad);
345
346 static gboolean gst_rtp_session_sink_setcaps (GstPad * pad,
347     GstRtpSession * rtpsession, GstCaps * caps);
348 static gboolean gst_rtp_session_setcaps_send_rtp (GstPad * pad,
349     GstRtpSession * rtpsession, GstCaps * caps);
350
351 static void gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession);
352
353 static GstStructure *gst_rtp_session_create_stats (GstRtpSession * rtpsession);
354
355 static guint gst_rtp_session_signals[LAST_SIGNAL] = { 0 };
356
357 static void
358 on_new_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
359 {
360   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0,
361       src->ssrc);
362 }
363
364 static void
365 on_ssrc_collision (RTPSession * session, RTPSource * src, GstRtpSession * sess)
366 {
367   GstPad *send_rtp_sink;
368
369   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
370       src->ssrc);
371
372   GST_RTP_SESSION_LOCK (sess);
373   if ((send_rtp_sink = sess->send_rtp_sink))
374     gst_object_ref (send_rtp_sink);
375   GST_RTP_SESSION_UNLOCK (sess);
376
377   if (send_rtp_sink) {
378     GstStructure *structure;
379     GstEvent *event;
380     RTPSource *internal_src;
381     guint32 suggested_ssrc;
382
383     structure = gst_structure_new ("GstRTPCollision", "ssrc", G_TYPE_UINT,
384         (guint) src->ssrc, NULL);
385
386     /* if there is no source using the suggested ssrc, most probably because
387      * this ssrc has just collided, suggest upstream to use it */
388     suggested_ssrc = rtp_session_suggest_ssrc (session, NULL);
389     internal_src = rtp_session_get_source_by_ssrc (session, suggested_ssrc);
390     if (!internal_src)
391       gst_structure_set (structure, "suggested-ssrc", G_TYPE_UINT,
392           (guint) suggested_ssrc, NULL);
393     else
394       g_object_unref (internal_src);
395
396     event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, structure);
397     gst_pad_push_event (send_rtp_sink, event);
398     gst_object_unref (send_rtp_sink);
399   }
400 }
401
402 static void
403 on_ssrc_validated (RTPSession * session, RTPSource * src, GstRtpSession * sess)
404 {
405   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
406       src->ssrc);
407 }
408
409 static void
410 on_ssrc_active (RTPSession * session, RTPSource * src, GstRtpSession * sess)
411 {
412   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
413       src->ssrc);
414 }
415
416 static void
417 on_ssrc_sdes (RTPSession * session, RTPSource * src, GstRtpSession * sess)
418 {
419   GstStructure *s;
420   GstMessage *m;
421
422   /* convert the new SDES info into a message */
423   RTP_SESSION_LOCK (session);
424   g_object_get (src, "sdes", &s, NULL);
425   RTP_SESSION_UNLOCK (session);
426
427   m = gst_message_new_custom (GST_MESSAGE_ELEMENT, GST_OBJECT (sess), s);
428   gst_element_post_message (GST_ELEMENT_CAST (sess), m);
429
430   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0,
431       src->ssrc);
432 }
433
434 static void
435 on_bye_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
436 {
437   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0,
438       src->ssrc);
439 }
440
441 static void
442 on_bye_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
443 {
444   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
445       src->ssrc);
446 }
447
448 static void
449 on_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
450 {
451   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_TIMEOUT], 0,
452       src->ssrc);
453 }
454
455 static void
456 on_sender_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
457 {
458   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
459       src->ssrc);
460 }
461
462 static void
463 on_new_sender_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
464 {
465   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_NEW_SENDER_SSRC], 0,
466       src->ssrc);
467 }
468
469 static void
470 on_sender_ssrc_active (RTPSession * session, RTPSource * src,
471     GstRtpSession * sess)
472 {
473   g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE], 0,
474       src->ssrc);
475 }
476
477 static void
478 on_notify_stats (RTPSession * session, GParamSpec * spec,
479     GstRtpSession * rtpsession)
480 {
481   g_object_notify (G_OBJECT (rtpsession), "stats");
482 }
483
484 #define gst_rtp_session_parent_class parent_class
485 G_DEFINE_TYPE_WITH_PRIVATE (GstRtpSession, gst_rtp_session, GST_TYPE_ELEMENT);
486
487 static void
488 gst_rtp_session_class_init (GstRtpSessionClass * klass)
489 {
490   GObjectClass *gobject_class;
491   GstElementClass *gstelement_class;
492
493   gobject_class = (GObjectClass *) klass;
494   gstelement_class = (GstElementClass *) klass;
495
496   gobject_class->finalize = gst_rtp_session_finalize;
497   gobject_class->set_property = gst_rtp_session_set_property;
498   gobject_class->get_property = gst_rtp_session_get_property;
499
500   /**
501    * GstRtpSession::request-pt-map:
502    * @sess: the object which received the signal
503    * @pt: the pt
504    *
505    * Request the payload type as #GstCaps for @pt.
506    */
507   gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP] =
508       g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
509       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, request_pt_map),
510       NULL, NULL, g_cclosure_marshal_generic, GST_TYPE_CAPS, 1, G_TYPE_UINT);
511   /**
512    * GstRtpSession::clear-pt-map:
513    * @sess: the object which received the signal
514    *
515    * Clear the cached pt-maps requested with #GstRtpSession::request-pt-map.
516    */
517   gst_rtp_session_signals[SIGNAL_CLEAR_PT_MAP] =
518       g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
519       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
520       G_STRUCT_OFFSET (GstRtpSessionClass, clear_pt_map),
521       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
522
523   /**
524    * GstRtpSession::on-new-ssrc:
525    * @sess: the object which received the signal
526    * @ssrc: the SSRC
527    *
528    * Notify of a new SSRC that entered @session.
529    */
530   gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
531       g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
532       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_new_ssrc),
533       NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
534   /**
535    * GstRtpSession::on-ssrc_collision:
536    * @sess: the object which received the signal
537    * @ssrc: the SSRC
538    *
539    * Notify when we have an SSRC collision
540    */
541   gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
542       g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
543       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
544           on_ssrc_collision), NULL, NULL, g_cclosure_marshal_VOID__UINT,
545       G_TYPE_NONE, 1, G_TYPE_UINT);
546   /**
547    * GstRtpSession::on-ssrc_validated:
548    * @sess: the object which received the signal
549    * @ssrc: the SSRC
550    *
551    * Notify of a new SSRC that became validated.
552    */
553   gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
554       g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
555       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
556           on_ssrc_validated), NULL, NULL, g_cclosure_marshal_VOID__UINT,
557       G_TYPE_NONE, 1, G_TYPE_UINT);
558   /**
559    * GstRtpSession::on-ssrc-active:
560    * @sess: the object which received the signal
561    * @ssrc: the SSRC
562    *
563    * Notify of a SSRC that is active, i.e., sending RTCP.
564    */
565   gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
566       g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
567       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
568           on_ssrc_active), NULL, NULL, g_cclosure_marshal_VOID__UINT,
569       G_TYPE_NONE, 1, G_TYPE_UINT);
570   /**
571    * GstRtpSession::on-ssrc-sdes:
572    * @session: the object which received the signal
573    * @src: the SSRC
574    *
575    * Notify that a new SDES was received for SSRC.
576    */
577   gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
578       g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
579       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_ssrc_sdes),
580       NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
581
582   /**
583    * GstRtpSession::on-bye-ssrc:
584    * @sess: the object which received the signal
585    * @ssrc: the SSRC
586    *
587    * Notify of an SSRC that became inactive because of a BYE packet.
588    */
589   gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
590       g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
591       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_ssrc),
592       NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
593   /**
594    * GstRtpSession::on-bye-timeout:
595    * @sess: the object which received the signal
596    * @ssrc: the SSRC
597    *
598    * Notify of an SSRC that has timed out because of BYE
599    */
600   gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
601       g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
602       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_timeout),
603       NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
604   /**
605    * GstRtpSession::on-timeout:
606    * @sess: the object which received the signal
607    * @ssrc: the SSRC
608    *
609    * Notify of an SSRC that has timed out
610    */
611   gst_rtp_session_signals[SIGNAL_ON_TIMEOUT] =
612       g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
613       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_timeout),
614       NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
615   /**
616    * GstRtpSession::on-sender-timeout:
617    * @sess: the object which received the signal
618    * @ssrc: the SSRC
619    *
620    * Notify of a sender SSRC that has timed out and became a receiver
621    */
622   gst_rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
623       g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
624       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
625           on_sender_timeout), NULL, NULL, g_cclosure_marshal_VOID__UINT,
626       G_TYPE_NONE, 1, G_TYPE_UINT);
627
628   /**
629    * GstRtpSession::on-new-sender-ssrc:
630    * @sess: the object which received the signal
631    * @ssrc: the sender SSRC
632    *
633    * Notify of a new sender SSRC that entered @session.
634    *
635    * Since: 1.8
636    */
637   gst_rtp_session_signals[SIGNAL_ON_NEW_SENDER_SSRC] =
638       g_signal_new ("on-new-sender-ssrc", G_TYPE_FROM_CLASS (klass),
639       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_new_ssrc),
640       NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
641
642   /**
643    * GstRtpSession::on-sender-ssrc-active:
644    * @sess: the object which received the signal
645    * @ssrc: the sender SSRC
646    *
647    * Notify of a sender SSRC that is active, i.e., sending RTCP.
648    *
649    * Since: 1.8
650    */
651   gst_rtp_session_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE] =
652       g_signal_new ("on-sender-ssrc-active", G_TYPE_FROM_CLASS (klass),
653       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
654           on_ssrc_active), NULL, NULL, g_cclosure_marshal_VOID__UINT,
655       G_TYPE_NONE, 1, G_TYPE_UINT);
656
657   g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
658       g_param_spec_double ("bandwidth", "Bandwidth",
659           "The bandwidth of the session in bytes per second (0 for auto-discover)",
660           0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
661           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
662
663   g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
664       g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
665           "The RTCP bandwidth of the session in bytes per second "
666           "(or as a real fraction of the RTP bandwidth if < 1.0)",
667           0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
668           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
669
670   g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
671       g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
672           "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
673           -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
674           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
675
676   g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
677       g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
678           "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
679           -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
680           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
681
682   g_object_class_install_property (gobject_class, PROP_SDES,
683       g_param_spec_boxed ("sdes", "SDES",
684           "The SDES items of this session",
685           GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
686
687   g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
688       g_param_spec_uint ("num-sources", "Num Sources",
689           "The number of sources in the session", 0, G_MAXUINT,
690           DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
691
692   g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
693       g_param_spec_uint ("num-active-sources", "Num Active Sources",
694           "The number of active sources in the session", 0, G_MAXUINT,
695           DEFAULT_NUM_ACTIVE_SOURCES,
696           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
697
698   g_object_class_install_property (gobject_class, PROP_INTERNAL_SESSION,
699       g_param_spec_object ("internal-session", "Internal Session",
700           "The internal RTPSession object", RTP_TYPE_SESSION,
701           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
702
703   g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
704       g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
705           "Use the pipeline running-time to set the NTP time in the RTCP SR messages "
706           "(DEPRECATED: Use ntp-time-source property)",
707           DEFAULT_USE_PIPELINE_CLOCK,
708           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
709
710   g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
711       g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
712           "Minimum interval between Regular RTCP packet (in ns)",
713           0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
714           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
715
716   g_object_class_install_property (gobject_class, PROP_PROBATION,
717       g_param_spec_uint ("probation", "Number of probations",
718           "Consecutive packet sequence numbers to accept the source",
719           0, G_MAXUINT, DEFAULT_PROBATION,
720           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
721
722   g_object_class_install_property (gobject_class, PROP_MAX_DROPOUT_TIME,
723       g_param_spec_uint ("max-dropout-time", "Max dropout time",
724           "The maximum time (milliseconds) of missing packets tolerated.",
725           0, G_MAXUINT, DEFAULT_MAX_DROPOUT_TIME,
726           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
727
728   g_object_class_install_property (gobject_class, PROP_MAX_MISORDER_TIME,
729       g_param_spec_uint ("max-misorder-time", "Max misorder time",
730           "The maximum time (milliseconds) of misordered packets tolerated.",
731           0, G_MAXUINT, DEFAULT_MAX_MISORDER_TIME,
732           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
733
734   /**
735    * GstRtpSession::stats:
736    *
737    * Various session statistics. This property returns a GstStructure
738    * with name application/x-rtp-session-stats with the following fields:
739    *
740    *  "recv-rtx-req-count  G_TYPE_UINT   The number of retransmission event
741    *      received from downstream (in receiver mode) (Since 1.16)
742    *  "sent-rtx-req-count" G_TYPE_UINT   The number of retransmission event
743    *      sent downstream (in sender mode) (Since 1.16)
744    *  "rtx-count"          G_TYPE_UINT   DEPRECATED Since 1.16, same as
745    *      "recv-rtx-req-count".
746    *  "rtx-drop-count"     G_TYPE_UINT   The number of retransmission events
747    *      dropped (due to bandwidth constraints)
748    *  "sent-nack-count"    G_TYPE_UINT   Number of NACKs sent
749    *  "recv-nack-count"    G_TYPE_UINT   Number of NACKs received
750    *  "source-stats"       G_TYPE_BOXED  GValueArray of #RTPSource::stats for all
751    *      RTP sources (Since 1.8)
752    *
753    * Since: 1.4
754    */
755   g_object_class_install_property (gobject_class, PROP_STATS,
756       g_param_spec_boxed ("stats", "Statistics",
757           "Various statistics", GST_TYPE_STRUCTURE,
758           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
759
760   g_object_class_install_property (gobject_class, PROP_RTP_PROFILE,
761       g_param_spec_enum ("rtp-profile", "RTP Profile",
762           "RTP profile to use", GST_TYPE_RTP_PROFILE, DEFAULT_RTP_PROFILE,
763           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
764
765   g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
766       g_param_spec_enum ("ntp-time-source", "NTP Time Source",
767           "NTP time source for RTCP packets",
768           gst_rtp_ntp_time_source_get_type (), DEFAULT_NTP_TIME_SOURCE,
769           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
770
771   g_object_class_install_property (gobject_class, PROP_RTCP_SYNC_SEND_TIME,
772       g_param_spec_boolean ("rtcp-sync-send-time", "RTCP Sync Send Time",
773           "Use send time or capture time for RTCP sync "
774           "(TRUE = send time, FALSE = capture time)",
775           DEFAULT_RTCP_SYNC_SEND_TIME,
776           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
777
778   gstelement_class->change_state =
779       GST_DEBUG_FUNCPTR (gst_rtp_session_change_state);
780   gstelement_class->request_new_pad =
781       GST_DEBUG_FUNCPTR (gst_rtp_session_request_new_pad);
782   gstelement_class->release_pad =
783       GST_DEBUG_FUNCPTR (gst_rtp_session_release_pad);
784
785   klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_session_clear_pt_map);
786
787   /* sink pads */
788   gst_element_class_add_static_pad_template (gstelement_class,
789       &rtpsession_recv_rtp_sink_template);
790   gst_element_class_add_static_pad_template (gstelement_class,
791       &rtpsession_recv_rtcp_sink_template);
792   gst_element_class_add_static_pad_template (gstelement_class,
793       &rtpsession_send_rtp_sink_template);
794
795   /* src pads */
796   gst_element_class_add_static_pad_template (gstelement_class,
797       &rtpsession_recv_rtp_src_template);
798   gst_element_class_add_static_pad_template (gstelement_class,
799       &rtpsession_sync_src_template);
800   gst_element_class_add_static_pad_template (gstelement_class,
801       &rtpsession_send_rtp_src_template);
802   gst_element_class_add_static_pad_template (gstelement_class,
803       &rtpsession_send_rtcp_src_template);
804
805   gst_element_class_set_static_metadata (gstelement_class, "RTP Session",
806       "Filter/Network/RTP",
807       "Implement an RTP session", "Wim Taymans <wim.taymans@gmail.com>");
808
809   GST_DEBUG_CATEGORY_INIT (gst_rtp_session_debug,
810       "rtpsession", 0, "RTP Session");
811
812   GST_DEBUG_REGISTER_FUNCPTR (gst_rtp_session_chain_recv_rtp);
813   GST_DEBUG_REGISTER_FUNCPTR (gst_rtp_session_chain_recv_rtcp);
814   GST_DEBUG_REGISTER_FUNCPTR (gst_rtp_session_chain_send_rtp);
815   GST_DEBUG_REGISTER_FUNCPTR (gst_rtp_session_chain_send_rtp_list);
816
817 }
818
819 static void
820 gst_rtp_session_init (GstRtpSession * rtpsession)
821 {
822   rtpsession->priv = gst_rtp_session_get_instance_private (rtpsession);
823   g_mutex_init (&rtpsession->priv->lock);
824   g_cond_init (&rtpsession->priv->cond);
825   rtpsession->priv->sysclock = gst_system_clock_obtain ();
826   rtpsession->priv->session = rtp_session_new ();
827   rtpsession->priv->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
828   rtpsession->priv->rtcp_sync_send_time = DEFAULT_RTCP_SYNC_SEND_TIME;
829
830   /* configure callbacks */
831   rtp_session_set_callbacks (rtpsession->priv->session, &callbacks, rtpsession);
832   /* configure signals */
833   g_signal_connect (rtpsession->priv->session, "on-new-ssrc",
834       (GCallback) on_new_ssrc, rtpsession);
835   g_signal_connect (rtpsession->priv->session, "on-ssrc-collision",
836       (GCallback) on_ssrc_collision, rtpsession);
837   g_signal_connect (rtpsession->priv->session, "on-ssrc-validated",
838       (GCallback) on_ssrc_validated, rtpsession);
839   g_signal_connect (rtpsession->priv->session, "on-ssrc-active",
840       (GCallback) on_ssrc_active, rtpsession);
841   g_signal_connect (rtpsession->priv->session, "on-ssrc-sdes",
842       (GCallback) on_ssrc_sdes, rtpsession);
843   g_signal_connect (rtpsession->priv->session, "on-bye-ssrc",
844       (GCallback) on_bye_ssrc, rtpsession);
845   g_signal_connect (rtpsession->priv->session, "on-bye-timeout",
846       (GCallback) on_bye_timeout, rtpsession);
847   g_signal_connect (rtpsession->priv->session, "on-timeout",
848       (GCallback) on_timeout, rtpsession);
849   g_signal_connect (rtpsession->priv->session, "on-sender-timeout",
850       (GCallback) on_sender_timeout, rtpsession);
851   g_signal_connect (rtpsession->priv->session, "on-new-sender-ssrc",
852       (GCallback) on_new_sender_ssrc, rtpsession);
853   g_signal_connect (rtpsession->priv->session, "on-sender-ssrc-active",
854       (GCallback) on_sender_ssrc_active, rtpsession);
855   g_signal_connect (rtpsession->priv->session, "notify::stats",
856       (GCallback) on_notify_stats, rtpsession);
857   rtpsession->priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
858       (GDestroyNotify) gst_caps_unref);
859
860   rtpsession->recv_rtcp_segment_seqnum = GST_SEQNUM_INVALID;
861
862   gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
863   gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
864
865   rtpsession->priv->thread_stopped = TRUE;
866
867   rtpsession->priv->recv_rtx_req_count = 0;
868   rtpsession->priv->sent_rtx_req_count = 0;
869
870   rtpsession->priv->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
871 }
872
873 static void
874 gst_rtp_session_finalize (GObject * object)
875 {
876   GstRtpSession *rtpsession;
877
878   rtpsession = GST_RTP_SESSION (object);
879
880   g_hash_table_destroy (rtpsession->priv->ptmap);
881   g_mutex_clear (&rtpsession->priv->lock);
882   g_cond_clear (&rtpsession->priv->cond);
883   g_object_unref (rtpsession->priv->sysclock);
884   g_object_unref (rtpsession->priv->session);
885
886   G_OBJECT_CLASS (parent_class)->finalize (object);
887 }
888
889 static void
890 gst_rtp_session_set_property (GObject * object, guint prop_id,
891     const GValue * value, GParamSpec * pspec)
892 {
893   GstRtpSession *rtpsession;
894   GstRtpSessionPrivate *priv;
895
896   rtpsession = GST_RTP_SESSION (object);
897   priv = rtpsession->priv;
898
899   switch (prop_id) {
900     case PROP_BANDWIDTH:
901       g_object_set_property (G_OBJECT (priv->session), "bandwidth", value);
902       break;
903     case PROP_RTCP_FRACTION:
904       g_object_set_property (G_OBJECT (priv->session), "rtcp-fraction", value);
905       break;
906     case PROP_RTCP_RR_BANDWIDTH:
907       g_object_set_property (G_OBJECT (priv->session), "rtcp-rr-bandwidth",
908           value);
909       break;
910     case PROP_RTCP_RS_BANDWIDTH:
911       g_object_set_property (G_OBJECT (priv->session), "rtcp-rs-bandwidth",
912           value);
913       break;
914     case PROP_SDES:
915       rtp_session_set_sdes_struct (priv->session, g_value_get_boxed (value));
916       break;
917     case PROP_USE_PIPELINE_CLOCK:
918       priv->use_pipeline_clock = g_value_get_boolean (value);
919       break;
920     case PROP_RTCP_MIN_INTERVAL:
921       g_object_set_property (G_OBJECT (priv->session), "rtcp-min-interval",
922           value);
923       break;
924     case PROP_PROBATION:
925       g_object_set_property (G_OBJECT (priv->session), "probation", value);
926       break;
927     case PROP_MAX_DROPOUT_TIME:
928       g_object_set_property (G_OBJECT (priv->session), "max-dropout-time",
929           value);
930       break;
931     case PROP_MAX_MISORDER_TIME:
932       g_object_set_property (G_OBJECT (priv->session), "max-misorder-time",
933           value);
934       break;
935     case PROP_RTP_PROFILE:
936       g_object_set_property (G_OBJECT (priv->session), "rtp-profile", value);
937       break;
938     case PROP_NTP_TIME_SOURCE:
939       priv->ntp_time_source = g_value_get_enum (value);
940       break;
941     case PROP_RTCP_SYNC_SEND_TIME:
942       priv->rtcp_sync_send_time = g_value_get_boolean (value);
943       break;
944     default:
945       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
946       break;
947   }
948 }
949
950 static void
951 gst_rtp_session_get_property (GObject * object, guint prop_id,
952     GValue * value, GParamSpec * pspec)
953 {
954   GstRtpSession *rtpsession;
955   GstRtpSessionPrivate *priv;
956
957   rtpsession = GST_RTP_SESSION (object);
958   priv = rtpsession->priv;
959
960   switch (prop_id) {
961     case PROP_BANDWIDTH:
962       g_object_get_property (G_OBJECT (priv->session), "bandwidth", value);
963       break;
964     case PROP_RTCP_FRACTION:
965       g_object_get_property (G_OBJECT (priv->session), "rtcp-fraction", value);
966       break;
967     case PROP_RTCP_RR_BANDWIDTH:
968       g_object_get_property (G_OBJECT (priv->session), "rtcp-rr-bandwidth",
969           value);
970       break;
971     case PROP_RTCP_RS_BANDWIDTH:
972       g_object_get_property (G_OBJECT (priv->session), "rtcp-rs-bandwidth",
973           value);
974       break;
975     case PROP_SDES:
976       g_value_take_boxed (value, rtp_session_get_sdes_struct (priv->session));
977       break;
978     case PROP_NUM_SOURCES:
979       g_value_set_uint (value, rtp_session_get_num_sources (priv->session));
980       break;
981     case PROP_NUM_ACTIVE_SOURCES:
982       g_value_set_uint (value,
983           rtp_session_get_num_active_sources (priv->session));
984       break;
985     case PROP_INTERNAL_SESSION:
986       g_value_set_object (value, priv->session);
987       break;
988     case PROP_USE_PIPELINE_CLOCK:
989       g_value_set_boolean (value, priv->use_pipeline_clock);
990       break;
991     case PROP_RTCP_MIN_INTERVAL:
992       g_object_get_property (G_OBJECT (priv->session), "rtcp-min-interval",
993           value);
994       break;
995     case PROP_PROBATION:
996       g_object_get_property (G_OBJECT (priv->session), "probation", value);
997       break;
998     case PROP_MAX_DROPOUT_TIME:
999       g_object_get_property (G_OBJECT (priv->session), "max-dropout-time",
1000           value);
1001       break;
1002     case PROP_MAX_MISORDER_TIME:
1003       g_object_get_property (G_OBJECT (priv->session), "max-misorder-time",
1004           value);
1005       break;
1006     case PROP_STATS:
1007       g_value_take_boxed (value, gst_rtp_session_create_stats (rtpsession));
1008       break;
1009     case PROP_RTP_PROFILE:
1010       g_object_get_property (G_OBJECT (priv->session), "rtp-profile", value);
1011       break;
1012     case PROP_NTP_TIME_SOURCE:
1013       g_value_set_enum (value, priv->ntp_time_source);
1014       break;
1015     case PROP_RTCP_SYNC_SEND_TIME:
1016       g_value_set_boolean (value, priv->rtcp_sync_send_time);
1017       break;
1018     default:
1019       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1020       break;
1021   }
1022 }
1023
1024 static GstStructure *
1025 gst_rtp_session_create_stats (GstRtpSession * rtpsession)
1026 {
1027   GstStructure *s;
1028
1029   g_object_get (rtpsession->priv->session, "stats", &s, NULL);
1030   gst_structure_set (s, "rtx-count", G_TYPE_UINT,
1031       rtpsession->priv->recv_rtx_req_count, "recv-rtx-req-count", G_TYPE_UINT,
1032       rtpsession->priv->recv_rtx_req_count, "sent-rtx-req-count", G_TYPE_UINT,
1033       rtpsession->priv->sent_rtx_req_count, NULL);
1034
1035   return s;
1036 }
1037
1038 static void
1039 get_current_times (GstRtpSession * rtpsession, GstClockTime * running_time,
1040     guint64 * ntpnstime)
1041 {
1042   guint64 ntpns = -1;
1043   GstClock *clock;
1044   GstClockTime base_time, rt, clock_time;
1045
1046   GST_OBJECT_LOCK (rtpsession);
1047   if ((clock = GST_ELEMENT_CLOCK (rtpsession))) {
1048     base_time = GST_ELEMENT_CAST (rtpsession)->base_time;
1049     gst_object_ref (clock);
1050     GST_OBJECT_UNLOCK (rtpsession);
1051
1052     /* get current clock time and convert to running time */
1053     clock_time = gst_clock_get_time (clock);
1054     rt = clock_time - base_time;
1055
1056     if (rtpsession->priv->use_pipeline_clock) {
1057       ntpns = rt;
1058       /* add constant to convert from 1970 based time to 1900 based time */
1059       ntpns += (2208988800LL * GST_SECOND);
1060     } else {
1061       switch (rtpsession->priv->ntp_time_source) {
1062         case GST_RTP_NTP_TIME_SOURCE_NTP:
1063         case GST_RTP_NTP_TIME_SOURCE_UNIX:{
1064           GTimeVal current;
1065
1066           /* get current NTP time */
1067           g_get_current_time (&current);
1068           ntpns = GST_TIMEVAL_TO_TIME (current);
1069
1070           /* add constant to convert from 1970 based time to 1900 based time */
1071           if (rtpsession->priv->ntp_time_source == GST_RTP_NTP_TIME_SOURCE_NTP)
1072             ntpns += (2208988800LL * GST_SECOND);
1073           break;
1074         }
1075         case GST_RTP_NTP_TIME_SOURCE_RUNNING_TIME:
1076           ntpns = rt;
1077           break;
1078         case GST_RTP_NTP_TIME_SOURCE_CLOCK_TIME:
1079           ntpns = clock_time;
1080           break;
1081         default:
1082           ntpns = -1;
1083           g_assert_not_reached ();
1084           break;
1085       }
1086     }
1087
1088     gst_object_unref (clock);
1089   } else {
1090     GST_OBJECT_UNLOCK (rtpsession);
1091     rt = -1;
1092     ntpns = -1;
1093   }
1094   if (running_time)
1095     *running_time = rt;
1096   if (ntpnstime)
1097     *ntpnstime = ntpns;
1098 }
1099
1100 /* must be called with GST_RTP_SESSION_LOCK */
1101 static void
1102 signal_waiting_rtcp_thread_unlocked (GstRtpSession * rtpsession)
1103 {
1104   if (rtpsession->priv->wait_send) {
1105     GST_LOG_OBJECT (rtpsession, "signal RTCP thread");
1106     rtpsession->priv->wait_send = FALSE;
1107     GST_RTP_SESSION_SIGNAL (rtpsession);
1108   }
1109 }
1110
1111 static void
1112 rtcp_thread (GstRtpSession * rtpsession)
1113 {
1114   GstClockID id;
1115   GstClockTime current_time;
1116   GstClockTime next_timeout;
1117   guint64 ntpnstime;
1118   GstClockTime running_time;
1119   RTPSession *session;
1120   GstClock *sysclock;
1121
1122   GST_DEBUG_OBJECT (rtpsession, "entering RTCP thread");
1123
1124   GST_RTP_SESSION_LOCK (rtpsession);
1125
1126   while (rtpsession->priv->wait_send) {
1127     GST_LOG_OBJECT (rtpsession, "waiting for getting started");
1128     GST_RTP_SESSION_WAIT (rtpsession);
1129     GST_LOG_OBJECT (rtpsession, "signaled...");
1130   }
1131
1132   sysclock = rtpsession->priv->sysclock;
1133   current_time = gst_clock_get_time (sysclock);
1134
1135   session = rtpsession->priv->session;
1136
1137   GST_DEBUG_OBJECT (rtpsession, "starting at %" GST_TIME_FORMAT,
1138       GST_TIME_ARGS (current_time));
1139   session->start_time = current_time;
1140
1141   while (!rtpsession->priv->stop_thread) {
1142     GstClockReturn res;
1143
1144     /* get initial estimate */
1145     next_timeout = rtp_session_next_timeout (session, current_time);
1146
1147     GST_DEBUG_OBJECT (rtpsession, "next check time %" GST_TIME_FORMAT,
1148         GST_TIME_ARGS (next_timeout));
1149
1150     /* leave if no more timeouts, the session ended */
1151     if (next_timeout == GST_CLOCK_TIME_NONE)
1152       break;
1153
1154     id = rtpsession->priv->id =
1155         gst_clock_new_single_shot_id (sysclock, next_timeout);
1156     GST_RTP_SESSION_UNLOCK (rtpsession);
1157
1158     res = gst_clock_id_wait (id, NULL);
1159
1160     GST_RTP_SESSION_LOCK (rtpsession);
1161     gst_clock_id_unref (id);
1162     rtpsession->priv->id = NULL;
1163
1164     if (rtpsession->priv->stop_thread)
1165       break;
1166
1167     /* update current time */
1168     current_time = gst_clock_get_time (sysclock);
1169
1170     /* get current NTP time */
1171     get_current_times (rtpsession, &running_time, &ntpnstime);
1172
1173     /* we get unlocked because we need to perform reconsideration, don't perform
1174      * the timeout but get a new reporting estimate. */
1175     GST_DEBUG_OBJECT (rtpsession, "unlocked %d, current %" GST_TIME_FORMAT,
1176         res, GST_TIME_ARGS (current_time));
1177
1178     /* perform actions, we ignore result. Release lock because it might push. */
1179     GST_RTP_SESSION_UNLOCK (rtpsession);
1180     rtp_session_on_timeout (session, current_time, ntpnstime, running_time);
1181     GST_RTP_SESSION_LOCK (rtpsession);
1182   }
1183   /* mark the thread as stopped now */
1184   rtpsession->priv->thread_stopped = TRUE;
1185   GST_RTP_SESSION_UNLOCK (rtpsession);
1186
1187   GST_DEBUG_OBJECT (rtpsession, "leaving RTCP thread");
1188 }
1189
1190 static gboolean
1191 start_rtcp_thread (GstRtpSession * rtpsession)
1192 {
1193   GError *error = NULL;
1194   gboolean res;
1195
1196   GST_DEBUG_OBJECT (rtpsession, "starting RTCP thread");
1197
1198   GST_RTP_SESSION_LOCK (rtpsession);
1199   rtpsession->priv->stop_thread = FALSE;
1200   if (rtpsession->priv->thread_stopped) {
1201     /* if the thread stopped, and we still have a handle to the thread, join it
1202      * now. We can safely join with the lock held, the thread will not take it
1203      * anymore. */
1204     if (rtpsession->priv->thread)
1205       g_thread_join (rtpsession->priv->thread);
1206     /* only create a new thread if the old one was stopped. Otherwise we can
1207      * just reuse the currently running one. */
1208     rtpsession->priv->thread = g_thread_try_new ("rtpsession-rtcp-thread",
1209         (GThreadFunc) rtcp_thread, rtpsession, &error);
1210     rtpsession->priv->thread_stopped = FALSE;
1211   }
1212   GST_RTP_SESSION_UNLOCK (rtpsession);
1213
1214   if (error != NULL) {
1215     res = FALSE;
1216     GST_DEBUG_OBJECT (rtpsession, "failed to start thread, %s", error->message);
1217     g_error_free (error);
1218   } else {
1219     res = TRUE;
1220   }
1221   return res;
1222 }
1223
1224 static void
1225 stop_rtcp_thread (GstRtpSession * rtpsession)
1226 {
1227   GST_DEBUG_OBJECT (rtpsession, "stopping RTCP thread");
1228
1229   GST_RTP_SESSION_LOCK (rtpsession);
1230   rtpsession->priv->stop_thread = TRUE;
1231   signal_waiting_rtcp_thread_unlocked (rtpsession);
1232   if (rtpsession->priv->id)
1233     gst_clock_id_unschedule (rtpsession->priv->id);
1234   GST_RTP_SESSION_UNLOCK (rtpsession);
1235 }
1236
1237 static void
1238 join_rtcp_thread (GstRtpSession * rtpsession)
1239 {
1240   GST_RTP_SESSION_LOCK (rtpsession);
1241   /* don't try to join when we have no thread */
1242   if (rtpsession->priv->thread != NULL) {
1243     GST_DEBUG_OBJECT (rtpsession, "joining RTCP thread");
1244     GST_RTP_SESSION_UNLOCK (rtpsession);
1245
1246     g_thread_join (rtpsession->priv->thread);
1247
1248     GST_RTP_SESSION_LOCK (rtpsession);
1249     /* after the join, take the lock and clear the thread structure. The caller
1250      * is supposed to not concurrently call start and join. */
1251     rtpsession->priv->thread = NULL;
1252   }
1253   GST_RTP_SESSION_UNLOCK (rtpsession);
1254 }
1255
1256 static GstStateChangeReturn
1257 gst_rtp_session_change_state (GstElement * element, GstStateChange transition)
1258 {
1259   GstStateChangeReturn res;
1260   GstRtpSession *rtpsession;
1261
1262   rtpsession = GST_RTP_SESSION (element);
1263
1264   switch (transition) {
1265     case GST_STATE_CHANGE_NULL_TO_READY:
1266       break;
1267     case GST_STATE_CHANGE_READY_TO_PAUSED:
1268       GST_RTP_SESSION_LOCK (rtpsession);
1269       rtpsession->priv->wait_send = TRUE;
1270       GST_RTP_SESSION_UNLOCK (rtpsession);
1271       break;
1272     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1273       break;
1274     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1275     case GST_STATE_CHANGE_PAUSED_TO_READY:
1276       /* no need to join yet, we might want to continue later. Also, the
1277        * dataflow could block downstream so that a join could just block
1278        * forever. */
1279       stop_rtcp_thread (rtpsession);
1280       break;
1281     default:
1282       break;
1283   }
1284
1285   res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1286
1287   switch (transition) {
1288     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1289       if (!start_rtcp_thread (rtpsession))
1290         goto failed_thread;
1291       break;
1292     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1293       break;
1294     case GST_STATE_CHANGE_PAUSED_TO_READY:
1295       /* downstream is now releasing the dataflow and we can join. */
1296       join_rtcp_thread (rtpsession);
1297       rtp_session_reset (rtpsession->priv->session);
1298       break;
1299     case GST_STATE_CHANGE_READY_TO_NULL:
1300       break;
1301     default:
1302       break;
1303   }
1304   return res;
1305
1306   /* ERRORS */
1307 failed_thread:
1308   {
1309     return GST_STATE_CHANGE_FAILURE;
1310   }
1311 }
1312
1313 static gboolean
1314 return_true (gpointer key, gpointer value, gpointer user_data)
1315 {
1316   return TRUE;
1317 }
1318
1319 static void
1320 gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession)
1321 {
1322   g_hash_table_foreach_remove (rtpsession->priv->ptmap, return_true, NULL);
1323 }
1324
1325 /* called when the session manager has an RTP packet or a list of packets
1326  * ready for further processing */
1327 static GstFlowReturn
1328 gst_rtp_session_process_rtp (RTPSession * sess, RTPSource * src,
1329     GstBuffer * buffer, gpointer user_data)
1330 {
1331   GstFlowReturn result;
1332   GstRtpSession *rtpsession;
1333   GstPad *rtp_src;
1334
1335   rtpsession = GST_RTP_SESSION (user_data);
1336
1337   GST_RTP_SESSION_LOCK (rtpsession);
1338   if ((rtp_src = rtpsession->recv_rtp_src))
1339     gst_object_ref (rtp_src);
1340   GST_RTP_SESSION_UNLOCK (rtpsession);
1341
1342   if (rtp_src) {
1343     GST_LOG_OBJECT (rtpsession, "pushing received RTP packet");
1344     result = gst_pad_push (rtp_src, buffer);
1345     gst_object_unref (rtp_src);
1346   } else {
1347     GST_DEBUG_OBJECT (rtpsession, "dropping received RTP packet");
1348     gst_buffer_unref (buffer);
1349     result = GST_FLOW_OK;
1350   }
1351   return result;
1352 }
1353
1354 /* called when the session manager has an RTP packet ready for further
1355  * sending */
1356 static GstFlowReturn
1357 gst_rtp_session_send_rtp (RTPSession * sess, RTPSource * src,
1358     gpointer data, gpointer user_data)
1359 {
1360   GstFlowReturn result;
1361   GstRtpSession *rtpsession;
1362   GstPad *rtp_src;
1363
1364   rtpsession = GST_RTP_SESSION (user_data);
1365
1366   GST_RTP_SESSION_LOCK (rtpsession);
1367   if ((rtp_src = rtpsession->send_rtp_src))
1368     gst_object_ref (rtp_src);
1369   signal_waiting_rtcp_thread_unlocked (rtpsession);
1370   GST_RTP_SESSION_UNLOCK (rtpsession);
1371
1372   if (rtp_src) {
1373     if (GST_IS_BUFFER (data)) {
1374       GST_LOG_OBJECT (rtpsession, "sending RTP packet");
1375       result = gst_pad_push (rtp_src, GST_BUFFER_CAST (data));
1376     } else {
1377       GST_LOG_OBJECT (rtpsession, "sending RTP list");
1378       result = gst_pad_push_list (rtp_src, GST_BUFFER_LIST_CAST (data));
1379     }
1380     gst_object_unref (rtp_src);
1381   } else {
1382     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1383     result = GST_FLOW_OK;
1384   }
1385   return result;
1386 }
1387
1388 static void
1389 do_rtcp_events (GstRtpSession * rtpsession, GstPad * srcpad)
1390 {
1391   GstCaps *caps;
1392   GstSegment seg;
1393   GstEvent *event;
1394   gchar *stream_id;
1395   gboolean have_group_id;
1396   guint group_id;
1397
1398   stream_id =
1399       g_strdup_printf ("%08x%08x%08x%08x", g_random_int (), g_random_int (),
1400       g_random_int (), g_random_int ());
1401
1402   GST_RTP_SESSION_LOCK (rtpsession);
1403   if (rtpsession->recv_rtp_sink) {
1404     event =
1405         gst_pad_get_sticky_event (rtpsession->recv_rtp_sink,
1406         GST_EVENT_STREAM_START, 0);
1407     if (event) {
1408       if (gst_event_parse_group_id (event, &group_id))
1409         have_group_id = TRUE;
1410       else
1411         have_group_id = FALSE;
1412       gst_event_unref (event);
1413     } else {
1414       have_group_id = TRUE;
1415       group_id = gst_util_group_id_next ();
1416     }
1417   } else {
1418     have_group_id = TRUE;
1419     group_id = gst_util_group_id_next ();
1420   }
1421   GST_RTP_SESSION_UNLOCK (rtpsession);
1422
1423   event = gst_event_new_stream_start (stream_id);
1424   rtpsession->recv_rtcp_segment_seqnum = gst_event_get_seqnum (event);
1425   gst_event_set_seqnum (event, rtpsession->recv_rtcp_segment_seqnum);
1426   if (have_group_id)
1427     gst_event_set_group_id (event, group_id);
1428   gst_pad_push_event (srcpad, event);
1429   g_free (stream_id);
1430
1431   caps = gst_caps_new_empty_simple ("application/x-rtcp");
1432   gst_pad_set_caps (srcpad, caps);
1433   gst_caps_unref (caps);
1434
1435   gst_segment_init (&seg, GST_FORMAT_TIME);
1436   event = gst_event_new_segment (&seg);
1437   gst_event_set_seqnum (event, rtpsession->recv_rtcp_segment_seqnum);
1438   gst_pad_push_event (srcpad, event);
1439 }
1440
1441 /* called when the session manager has an RTCP packet ready for further
1442  * sending. The eos flag is set when an EOS event should be sent downstream as
1443  * well. */
1444 static GstFlowReturn
1445 gst_rtp_session_send_rtcp (RTPSession * sess, RTPSource * src,
1446     GstBuffer * buffer, gboolean all_sources_bye, gpointer user_data)
1447 {
1448   GstFlowReturn result;
1449   GstRtpSession *rtpsession;
1450   GstPad *rtcp_src;
1451
1452   rtpsession = GST_RTP_SESSION (user_data);
1453
1454   GST_RTP_SESSION_LOCK (rtpsession);
1455   if (rtpsession->priv->stop_thread)
1456     goto stopping;
1457
1458   if ((rtcp_src = rtpsession->send_rtcp_src)) {
1459     gst_object_ref (rtcp_src);
1460     GST_RTP_SESSION_UNLOCK (rtpsession);
1461
1462     /* set rtcp caps on output pad */
1463     if (!gst_pad_has_current_caps (rtcp_src))
1464       do_rtcp_events (rtpsession, rtcp_src);
1465
1466     GST_LOG_OBJECT (rtpsession, "sending RTCP");
1467     result = gst_pad_push (rtcp_src, buffer);
1468
1469     /* Forward send an EOS on the RTCP sink if we received an EOS on the
1470      * send_rtp_sink. We don't need to check the recv_rtp_sink since in this
1471      * case the EOS event would already have been sent */
1472     if (all_sources_bye && rtpsession->send_rtp_sink &&
1473         GST_PAD_IS_EOS (rtpsession->send_rtp_sink)) {
1474       GstEvent *event;
1475
1476       GST_LOG_OBJECT (rtpsession, "sending EOS");
1477
1478       event = gst_event_new_eos ();
1479       gst_event_set_seqnum (event, rtpsession->recv_rtcp_segment_seqnum);
1480       gst_pad_push_event (rtcp_src, event);
1481     }
1482     gst_object_unref (rtcp_src);
1483   } else {
1484     GST_RTP_SESSION_UNLOCK (rtpsession);
1485
1486     GST_DEBUG_OBJECT (rtpsession, "not sending RTCP, no output pad");
1487     gst_buffer_unref (buffer);
1488     result = GST_FLOW_OK;
1489   }
1490   return result;
1491
1492   /* ERRORS */
1493 stopping:
1494   {
1495     GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1496     gst_buffer_unref (buffer);
1497     GST_RTP_SESSION_UNLOCK (rtpsession);
1498     return GST_FLOW_OK;
1499   }
1500 }
1501
1502 /* called when the session manager has an SR RTCP packet ready for handling
1503  * inter stream synchronisation */
1504 static GstFlowReturn
1505 gst_rtp_session_sync_rtcp (RTPSession * sess,
1506     GstBuffer * buffer, gpointer user_data)
1507 {
1508   GstFlowReturn result;
1509   GstRtpSession *rtpsession;
1510   GstPad *sync_src;
1511
1512   rtpsession = GST_RTP_SESSION (user_data);
1513
1514   GST_RTP_SESSION_LOCK (rtpsession);
1515   if (rtpsession->priv->stop_thread)
1516     goto stopping;
1517
1518   if ((sync_src = rtpsession->sync_src)) {
1519     gst_object_ref (sync_src);
1520     GST_RTP_SESSION_UNLOCK (rtpsession);
1521
1522     /* set rtcp caps on output pad, this happens
1523      * when we receive RTCP muxed with RTP according
1524      * to RFC5761. Otherwise we would have forwarded
1525      * the events from the recv_rtcp_sink pad already
1526      */
1527     if (!gst_pad_has_current_caps (sync_src))
1528       do_rtcp_events (rtpsession, sync_src);
1529
1530     GST_LOG_OBJECT (rtpsession, "sending Sync RTCP");
1531     result = gst_pad_push (sync_src, buffer);
1532     gst_object_unref (sync_src);
1533   } else {
1534     GST_RTP_SESSION_UNLOCK (rtpsession);
1535
1536     GST_DEBUG_OBJECT (rtpsession, "not sending Sync RTCP, no output pad");
1537     gst_buffer_unref (buffer);
1538     result = GST_FLOW_OK;
1539   }
1540   return result;
1541
1542   /* ERRORS */
1543 stopping:
1544   {
1545     GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1546     gst_buffer_unref (buffer);
1547     GST_RTP_SESSION_UNLOCK (rtpsession);
1548     return GST_FLOW_OK;
1549   }
1550 }
1551
1552 static void
1553 gst_rtp_session_cache_caps (GstRtpSession * rtpsession, GstCaps * caps)
1554 {
1555   GstRtpSessionPrivate *priv;
1556   const GstStructure *s;
1557   gint payload;
1558
1559   priv = rtpsession->priv;
1560
1561   GST_DEBUG_OBJECT (rtpsession, "parsing caps");
1562
1563   s = gst_caps_get_structure (caps, 0);
1564   if (!gst_structure_get_int (s, "payload", &payload))
1565     return;
1566
1567   if (g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (payload)))
1568     return;
1569
1570   g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (payload),
1571       gst_caps_ref (caps));
1572 }
1573
1574 static GstCaps *
1575 gst_rtp_session_get_caps_for_pt (GstRtpSession * rtpsession, guint payload)
1576 {
1577   GstCaps *caps = NULL;
1578   GValue args[2] = { {0}, {0} };
1579   GValue ret = { 0 };
1580
1581   GST_RTP_SESSION_LOCK (rtpsession);
1582   caps = g_hash_table_lookup (rtpsession->priv->ptmap,
1583       GINT_TO_POINTER (payload));
1584   if (caps) {
1585     gst_caps_ref (caps);
1586     goto done;
1587   }
1588
1589   /* not found in the cache, try to get it with a signal */
1590   g_value_init (&args[0], GST_TYPE_ELEMENT);
1591   g_value_set_object (&args[0], rtpsession);
1592   g_value_init (&args[1], G_TYPE_UINT);
1593   g_value_set_uint (&args[1], payload);
1594
1595   g_value_init (&ret, GST_TYPE_CAPS);
1596   g_value_set_boxed (&ret, NULL);
1597
1598   GST_RTP_SESSION_UNLOCK (rtpsession);
1599
1600   g_signal_emitv (args, gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP], 0,
1601       &ret);
1602
1603   GST_RTP_SESSION_LOCK (rtpsession);
1604
1605   g_value_unset (&args[0]);
1606   g_value_unset (&args[1]);
1607   caps = (GstCaps *) g_value_dup_boxed (&ret);
1608   g_value_unset (&ret);
1609   if (!caps)
1610     goto no_caps;
1611
1612   gst_rtp_session_cache_caps (rtpsession, caps);
1613
1614 done:
1615   GST_RTP_SESSION_UNLOCK (rtpsession);
1616
1617   return caps;
1618
1619 no_caps:
1620   {
1621     GST_DEBUG_OBJECT (rtpsession, "could not get caps");
1622     goto done;
1623   }
1624 }
1625
1626 /* called when the session manager needs the clock rate */
1627 static gint
1628 gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
1629     gpointer user_data)
1630 {
1631   gint result = -1;
1632   GstRtpSession *rtpsession;
1633   GstCaps *caps;
1634   const GstStructure *s;
1635
1636   rtpsession = GST_RTP_SESSION_CAST (user_data);
1637
1638   caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1639
1640   if (!caps)
1641     goto done;
1642
1643   s = gst_caps_get_structure (caps, 0);
1644   if (!gst_structure_get_int (s, "clock-rate", &result))
1645     goto no_clock_rate;
1646
1647   gst_caps_unref (caps);
1648
1649   GST_DEBUG_OBJECT (rtpsession, "parsed clock-rate %d", result);
1650
1651 done:
1652
1653   return result;
1654
1655   /* ERRORS */
1656 no_clock_rate:
1657   {
1658     gst_caps_unref (caps);
1659     GST_DEBUG_OBJECT (rtpsession, "No clock-rate in caps!");
1660     goto done;
1661   }
1662 }
1663
1664 /* called when the session manager asks us to reconsider the timeout */
1665 static void
1666 gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data)
1667 {
1668   GstRtpSession *rtpsession;
1669
1670   rtpsession = GST_RTP_SESSION_CAST (user_data);
1671
1672   GST_RTP_SESSION_LOCK (rtpsession);
1673   GST_DEBUG_OBJECT (rtpsession, "unlock timer for reconsideration");
1674   if (rtpsession->priv->id)
1675     gst_clock_id_unschedule (rtpsession->priv->id);
1676   GST_RTP_SESSION_UNLOCK (rtpsession);
1677 }
1678
1679 static gboolean
1680 gst_rtp_session_event_recv_rtp_sink (GstPad * pad, GstObject * parent,
1681     GstEvent * event)
1682 {
1683   GstRtpSession *rtpsession;
1684   gboolean ret = FALSE;
1685
1686   rtpsession = GST_RTP_SESSION (parent);
1687
1688   GST_DEBUG_OBJECT (rtpsession, "received event %s",
1689       GST_EVENT_TYPE_NAME (event));
1690
1691   switch (GST_EVENT_TYPE (event)) {
1692     case GST_EVENT_CAPS:
1693     {
1694       GstCaps *caps;
1695
1696       /* process */
1697       gst_event_parse_caps (event, &caps);
1698       gst_rtp_session_sink_setcaps (pad, rtpsession, caps);
1699       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1700       break;
1701     }
1702     case GST_EVENT_FLUSH_STOP:
1703       gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
1704       rtpsession->recv_rtcp_segment_seqnum = GST_SEQNUM_INVALID;
1705       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1706       break;
1707     case GST_EVENT_SEGMENT:
1708     {
1709       GstSegment *segment, in_segment;
1710
1711       segment = &rtpsession->recv_rtp_seg;
1712
1713       /* the newsegment event is needed to convert the RTP timestamp to
1714        * running_time, which is needed to generate a mapping from RTP to NTP
1715        * timestamps in SR reports */
1716       gst_event_copy_segment (event, &in_segment);
1717       GST_DEBUG_OBJECT (rtpsession, "received segment %" GST_SEGMENT_FORMAT,
1718           &in_segment);
1719
1720       /* accept upstream */
1721       gst_segment_copy_into (&in_segment, segment);
1722
1723       /* push event forward */
1724       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1725       break;
1726     }
1727     case GST_EVENT_EOS:
1728     {
1729       GstPad *rtcp_src;
1730
1731       ret =
1732           gst_pad_push_event (rtpsession->recv_rtp_src, gst_event_ref (event));
1733
1734       GST_RTP_SESSION_LOCK (rtpsession);
1735       if ((rtcp_src = rtpsession->send_rtcp_src))
1736         gst_object_ref (rtcp_src);
1737       GST_RTP_SESSION_UNLOCK (rtpsession);
1738
1739       gst_event_unref (event);
1740
1741       if (rtcp_src) {
1742         event = gst_event_new_eos ();
1743         if (rtpsession->recv_rtcp_segment_seqnum != GST_SEQNUM_INVALID)
1744           gst_event_set_seqnum (event, rtpsession->recv_rtcp_segment_seqnum);
1745         ret = gst_pad_push_event (rtcp_src, event);
1746         gst_object_unref (rtcp_src);
1747       } else {
1748         ret = TRUE;
1749       }
1750       break;
1751     }
1752     default:
1753       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1754       break;
1755   }
1756
1757   return ret;
1758
1759 }
1760
1761 static gboolean
1762 gst_rtp_session_request_remote_key_unit (GstRtpSession * rtpsession,
1763     guint32 ssrc, guint payload, gboolean all_headers, gint count)
1764 {
1765   GstCaps *caps;
1766
1767   caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1768
1769   if (caps) {
1770     const GstStructure *s = gst_caps_get_structure (caps, 0);
1771     gboolean pli;
1772     gboolean fir;
1773
1774     pli = gst_structure_has_field (s, "rtcp-fb-nack-pli");
1775     fir = gst_structure_has_field (s, "rtcp-fb-ccm-fir") && all_headers;
1776
1777     /* Google Talk uses FIR for repair, so send it even if we just want a
1778      * regular PLI */
1779     if (!pli &&
1780         gst_structure_has_field (s, "rtcp-fb-x-gstreamer-fir-as-repair"))
1781       fir = TRUE;
1782
1783     gst_caps_unref (caps);
1784
1785     if (pli || fir)
1786       return rtp_session_request_key_unit (rtpsession->priv->session, ssrc,
1787           fir, count);
1788   }
1789
1790   return FALSE;
1791 }
1792
1793 static gboolean
1794 gst_rtp_session_event_recv_rtp_src (GstPad * pad, GstObject * parent,
1795     GstEvent * event)
1796 {
1797   GstRtpSession *rtpsession;
1798   gboolean forward = TRUE;
1799   gboolean ret = TRUE;
1800   const GstStructure *s;
1801   guint32 ssrc;
1802   guint pt;
1803
1804   rtpsession = GST_RTP_SESSION (parent);
1805
1806   switch (GST_EVENT_TYPE (event)) {
1807     case GST_EVENT_CUSTOM_UPSTREAM:
1808       s = gst_event_get_structure (event);
1809       if (gst_structure_has_name (s, "GstForceKeyUnit") &&
1810           gst_structure_get_uint (s, "ssrc", &ssrc) &&
1811           gst_structure_get_uint (s, "payload", &pt)) {
1812         gboolean all_headers = FALSE;
1813         gint count = -1;
1814
1815         gst_structure_get_boolean (s, "all-headers", &all_headers);
1816         if (gst_structure_get_int (s, "count", &count) && count < 0)
1817           count += G_MAXINT;    /* Make sure count is positive if present */
1818         if (gst_rtp_session_request_remote_key_unit (rtpsession, ssrc, pt,
1819                 all_headers, count))
1820           forward = FALSE;
1821       } else if (gst_structure_has_name (s, "GstRTPRetransmissionRequest")) {
1822         guint seqnum, delay, deadline, max_delay, avg_rtt;
1823
1824         GST_RTP_SESSION_LOCK (rtpsession);
1825         rtpsession->priv->recv_rtx_req_count++;
1826         GST_RTP_SESSION_UNLOCK (rtpsession);
1827
1828         if (!gst_structure_get_uint (s, "ssrc", &ssrc))
1829           ssrc = -1;
1830         if (!gst_structure_get_uint (s, "seqnum", &seqnum))
1831           seqnum = -1;
1832         if (!gst_structure_get_uint (s, "delay", &delay))
1833           delay = 0;
1834         if (!gst_structure_get_uint (s, "deadline", &deadline))
1835           deadline = 100;
1836         if (!gst_structure_get_uint (s, "avg-rtt", &avg_rtt))
1837           avg_rtt = 40;
1838
1839         /* remaining time to receive the packet */
1840         max_delay = deadline;
1841         if (max_delay > delay)
1842           max_delay -= delay;
1843         /* estimated RTT */
1844         if (max_delay > avg_rtt)
1845           max_delay -= avg_rtt;
1846         else
1847           max_delay = 0;
1848
1849         if (rtp_session_request_nack (rtpsession->priv->session, ssrc, seqnum,
1850                 max_delay * GST_MSECOND))
1851           forward = FALSE;
1852       }
1853       break;
1854     default:
1855       break;
1856   }
1857
1858   if (forward) {
1859     GstPad *recv_rtp_sink;
1860
1861     GST_RTP_SESSION_LOCK (rtpsession);
1862     if ((recv_rtp_sink = rtpsession->recv_rtp_sink))
1863       gst_object_ref (recv_rtp_sink);
1864     GST_RTP_SESSION_UNLOCK (rtpsession);
1865
1866     if (recv_rtp_sink) {
1867       ret = gst_pad_push_event (recv_rtp_sink, event);
1868       gst_object_unref (recv_rtp_sink);
1869     } else
1870       gst_event_unref (event);
1871   } else {
1872     gst_event_unref (event);
1873   }
1874
1875   return ret;
1876 }
1877
1878
1879 static GstIterator *
1880 gst_rtp_session_iterate_internal_links (GstPad * pad, GstObject * parent)
1881 {
1882   GstRtpSession *rtpsession;
1883   GstPad *otherpad = NULL;
1884   GstIterator *it = NULL;
1885
1886   rtpsession = GST_RTP_SESSION (parent);
1887
1888   GST_RTP_SESSION_LOCK (rtpsession);
1889   if (pad == rtpsession->recv_rtp_src) {
1890     otherpad = gst_object_ref (rtpsession->recv_rtp_sink);
1891   } else if (pad == rtpsession->recv_rtp_sink) {
1892     otherpad = gst_object_ref (rtpsession->recv_rtp_src);
1893   } else if (pad == rtpsession->send_rtp_src) {
1894     otherpad = gst_object_ref (rtpsession->send_rtp_sink);
1895   } else if (pad == rtpsession->send_rtp_sink) {
1896     otherpad = gst_object_ref (rtpsession->send_rtp_src);
1897   }
1898   GST_RTP_SESSION_UNLOCK (rtpsession);
1899
1900   if (otherpad) {
1901     GValue val = { 0, };
1902
1903     g_value_init (&val, GST_TYPE_PAD);
1904     g_value_set_object (&val, otherpad);
1905     it = gst_iterator_new_single (GST_TYPE_PAD, &val);
1906     g_value_unset (&val);
1907     gst_object_unref (otherpad);
1908   } else {
1909     it = gst_iterator_new_single (GST_TYPE_PAD, NULL);
1910   }
1911
1912   return it;
1913 }
1914
1915 static gboolean
1916 gst_rtp_session_sink_setcaps (GstPad * pad, GstRtpSession * rtpsession,
1917     GstCaps * caps)
1918 {
1919   GST_RTP_SESSION_LOCK (rtpsession);
1920   gst_rtp_session_cache_caps (rtpsession, caps);
1921   GST_RTP_SESSION_UNLOCK (rtpsession);
1922
1923   return TRUE;
1924 }
1925
1926 /* receive a packet from a sender, send it to the RTP session manager and
1927  * forward the packet on the rtp_src pad
1928  */
1929 static GstFlowReturn
1930 gst_rtp_session_chain_recv_rtp (GstPad * pad, GstObject * parent,
1931     GstBuffer * buffer)
1932 {
1933   GstRtpSession *rtpsession;
1934   GstRtpSessionPrivate *priv;
1935   GstFlowReturn ret;
1936   GstClockTime current_time, running_time;
1937   GstClockTime timestamp;
1938   guint64 ntpnstime;
1939
1940   rtpsession = GST_RTP_SESSION (parent);
1941   priv = rtpsession->priv;
1942
1943   GST_LOG_OBJECT (rtpsession, "received RTP packet");
1944
1945   GST_RTP_SESSION_LOCK (rtpsession);
1946   signal_waiting_rtcp_thread_unlocked (rtpsession);
1947   GST_RTP_SESSION_UNLOCK (rtpsession);
1948
1949   /* get NTP time when this packet was captured, this depends on the timestamp. */
1950   timestamp = GST_BUFFER_PTS (buffer);
1951   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1952     /* convert to running time using the segment values */
1953     running_time =
1954         gst_segment_to_running_time (&rtpsession->recv_rtp_seg, GST_FORMAT_TIME,
1955         timestamp);
1956     ntpnstime = GST_CLOCK_TIME_NONE;
1957   } else {
1958     get_current_times (rtpsession, &running_time, &ntpnstime);
1959   }
1960   current_time = gst_clock_get_time (priv->sysclock);
1961
1962   ret = rtp_session_process_rtp (priv->session, buffer, current_time,
1963       running_time, ntpnstime);
1964   if (ret != GST_FLOW_OK)
1965     goto push_error;
1966
1967 done:
1968
1969   return ret;
1970
1971   /* ERRORS */
1972 push_error:
1973   {
1974     GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1975         gst_flow_get_name (ret));
1976     goto done;
1977   }
1978 }
1979
1980 static gboolean
1981 gst_rtp_session_event_recv_rtcp_sink (GstPad * pad, GstObject * parent,
1982     GstEvent * event)
1983 {
1984   GstRtpSession *rtpsession;
1985   gboolean ret = FALSE;
1986
1987   rtpsession = GST_RTP_SESSION (parent);
1988
1989   GST_DEBUG_OBJECT (rtpsession, "received event %s",
1990       GST_EVENT_TYPE_NAME (event));
1991
1992   switch (GST_EVENT_TYPE (event)) {
1993     case GST_EVENT_SEGMENT:
1994       /* Make sure that the sync_src pad has caps before the segment event.
1995        * Otherwise we might get a segment event before caps from the receive
1996        * RTCP pad, and then later when receiving RTCP packets will set caps.
1997        * This will results in a sticky event misordering warning
1998        */
1999       if (!gst_pad_has_current_caps (rtpsession->sync_src)) {
2000         GstCaps *caps = gst_caps_new_empty_simple ("application/x-rtcp");
2001         gst_pad_set_caps (rtpsession->sync_src, caps);
2002         gst_caps_unref (caps);
2003       }
2004       /* fall through */
2005     default:
2006       ret = gst_pad_push_event (rtpsession->sync_src, event);
2007       break;
2008   }
2009
2010   return ret;
2011 }
2012
2013 /* Receive an RTCP packet from a sender, send it to the RTP session manager and
2014  * forward the SR packets to the sync_src pad.
2015  */
2016 static GstFlowReturn
2017 gst_rtp_session_chain_recv_rtcp (GstPad * pad, GstObject * parent,
2018     GstBuffer * buffer)
2019 {
2020   GstRtpSession *rtpsession;
2021   GstRtpSessionPrivate *priv;
2022   GstClockTime current_time;
2023   GstClockTime running_time;
2024   guint64 ntpnstime;
2025
2026   rtpsession = GST_RTP_SESSION (parent);
2027   priv = rtpsession->priv;
2028
2029   GST_LOG_OBJECT (rtpsession, "received RTCP packet");
2030
2031   GST_RTP_SESSION_LOCK (rtpsession);
2032   signal_waiting_rtcp_thread_unlocked (rtpsession);
2033   GST_RTP_SESSION_UNLOCK (rtpsession);
2034
2035   current_time = gst_clock_get_time (priv->sysclock);
2036   get_current_times (rtpsession, &running_time, &ntpnstime);
2037
2038   rtp_session_process_rtcp (priv->session, buffer, current_time, running_time,
2039       ntpnstime);
2040
2041   return GST_FLOW_OK;           /* always return OK */
2042 }
2043
2044 static gboolean
2045 gst_rtp_session_query_send_rtcp_src (GstPad * pad, GstObject * parent,
2046     GstQuery * query)
2047 {
2048   GstRtpSession *rtpsession;
2049   gboolean ret = FALSE;
2050
2051   rtpsession = GST_RTP_SESSION (parent);
2052
2053   GST_DEBUG_OBJECT (rtpsession, "received QUERY %s",
2054       GST_QUERY_TYPE_NAME (query));
2055
2056   switch (GST_QUERY_TYPE (query)) {
2057     case GST_QUERY_LATENCY:
2058       ret = TRUE;
2059       /* use the defaults for the latency query. */
2060       gst_query_set_latency (query, FALSE, 0, -1);
2061       break;
2062     default:
2063       /* other queries simply fail for now */
2064       break;
2065   }
2066
2067   return ret;
2068 }
2069
2070 static gboolean
2071 gst_rtp_session_event_send_rtcp_src (GstPad * pad, GstObject * parent,
2072     GstEvent * event)
2073 {
2074   GstRtpSession *rtpsession;
2075   gboolean ret = TRUE;
2076
2077   rtpsession = GST_RTP_SESSION (parent);
2078   GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
2079       GST_EVENT_TYPE_NAME (event));
2080
2081   switch (GST_EVENT_TYPE (event)) {
2082     case GST_EVENT_SEEK:
2083     case GST_EVENT_LATENCY:
2084       gst_event_unref (event);
2085       ret = TRUE;
2086       break;
2087     default:
2088       /* other events simply fail for now */
2089       gst_event_unref (event);
2090       ret = FALSE;
2091       break;
2092   }
2093
2094   return ret;
2095 }
2096
2097
2098 static gboolean
2099 gst_rtp_session_event_send_rtp_sink (GstPad * pad, GstObject * parent,
2100     GstEvent * event)
2101 {
2102   GstRtpSession *rtpsession;
2103   gboolean ret = FALSE;
2104
2105   rtpsession = GST_RTP_SESSION (parent);
2106
2107   GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
2108       GST_EVENT_TYPE_NAME (event));
2109
2110   switch (GST_EVENT_TYPE (event)) {
2111     case GST_EVENT_CAPS:
2112     {
2113       GstCaps *caps;
2114
2115       /* process */
2116       gst_event_parse_caps (event, &caps);
2117       gst_rtp_session_setcaps_send_rtp (pad, rtpsession, caps);
2118       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
2119       break;
2120     }
2121     case GST_EVENT_FLUSH_STOP:
2122       gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
2123       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
2124       break;
2125     case GST_EVENT_SEGMENT:{
2126       GstSegment *segment, in_segment;
2127
2128       segment = &rtpsession->send_rtp_seg;
2129
2130       /* the newsegment event is needed to convert the RTP timestamp to
2131        * running_time, which is needed to generate a mapping from RTP to NTP
2132        * timestamps in SR reports */
2133       gst_event_copy_segment (event, &in_segment);
2134       GST_DEBUG_OBJECT (rtpsession, "received segment %" GST_SEGMENT_FORMAT,
2135           &in_segment);
2136
2137       /* accept upstream */
2138       gst_segment_copy_into (&in_segment, segment);
2139
2140       /* push event forward */
2141       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
2142       break;
2143     }
2144     case GST_EVENT_EOS:{
2145       GstClockTime current_time;
2146
2147       /* push downstream FIXME, we are not supposed to leave the session just
2148        * because we stop sending. */
2149       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
2150       current_time = gst_clock_get_time (rtpsession->priv->sysclock);
2151
2152       GST_DEBUG_OBJECT (rtpsession, "scheduling BYE message");
2153       rtp_session_mark_all_bye (rtpsession->priv->session, "End Of Stream");
2154       rtp_session_schedule_bye (rtpsession->priv->session, current_time);
2155       break;
2156     }
2157     default:{
2158       GstPad *send_rtp_src;
2159
2160       GST_RTP_SESSION_LOCK (rtpsession);
2161       if ((send_rtp_src = rtpsession->send_rtp_src))
2162         gst_object_ref (send_rtp_src);
2163       GST_RTP_SESSION_UNLOCK (rtpsession);
2164
2165       if (send_rtp_src) {
2166         ret = gst_pad_push_event (send_rtp_src, event);
2167         gst_object_unref (send_rtp_src);
2168       } else
2169         gst_event_unref (event);
2170
2171       break;
2172     }
2173   }
2174
2175   return ret;
2176 }
2177
2178 static gboolean
2179 gst_rtp_session_event_send_rtp_src (GstPad * pad, GstObject * parent,
2180     GstEvent * event)
2181 {
2182   GstRtpSession *rtpsession;
2183   gboolean ret = FALSE;
2184
2185   rtpsession = GST_RTP_SESSION (parent);
2186
2187   GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
2188       GST_EVENT_TYPE_NAME (event));
2189
2190   switch (GST_EVENT_TYPE (event)) {
2191     case GST_EVENT_LATENCY:
2192       /* save the latency, we need this to know when an RTP packet will be
2193        * rendered by the sink */
2194       gst_event_parse_latency (event, &rtpsession->priv->send_latency);
2195
2196       ret = gst_pad_event_default (pad, parent, event);
2197       break;
2198     default:
2199       ret = gst_pad_event_default (pad, parent, event);
2200       break;
2201   }
2202   return ret;
2203 }
2204
2205 static GstCaps *
2206 gst_rtp_session_getcaps_send_rtp (GstPad * pad, GstRtpSession * rtpsession,
2207     GstCaps * filter)
2208 {
2209   GstRtpSessionPrivate *priv;
2210   GstCaps *result;
2211   GstStructure *s1, *s2;
2212   guint ssrc;
2213   gboolean is_random;
2214
2215   priv = rtpsession->priv;
2216
2217   ssrc = rtp_session_suggest_ssrc (priv->session, &is_random);
2218
2219   /* we can basically accept anything but we prefer to receive packets with our
2220    * internal SSRC so that we don't have to patch it. Create a structure with
2221    * the SSRC and another one without.
2222    * Only do this if the session actually decided on an ssrc already,
2223    * otherwise we give upstream the opportunity to select an ssrc itself */
2224   if (!is_random) {
2225     s1 = gst_structure_new ("application/x-rtp", "ssrc", G_TYPE_UINT, ssrc,
2226         NULL);
2227     s2 = gst_structure_new_empty ("application/x-rtp");
2228
2229     result = gst_caps_new_full (s1, s2, NULL);
2230   } else {
2231     result = gst_caps_new_empty_simple ("application/x-rtp");
2232   }
2233
2234   if (filter) {
2235     GstCaps *caps = result;
2236
2237     result = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
2238     gst_caps_unref (caps);
2239   }
2240
2241   GST_DEBUG_OBJECT (rtpsession, "getting caps %" GST_PTR_FORMAT, result);
2242
2243   return result;
2244 }
2245
2246 static gboolean
2247 gst_rtp_session_query_send_rtp (GstPad * pad, GstObject * parent,
2248     GstQuery * query)
2249 {
2250   gboolean res = FALSE;
2251   GstRtpSession *rtpsession;
2252
2253   rtpsession = GST_RTP_SESSION (parent);
2254
2255   switch (GST_QUERY_TYPE (query)) {
2256     case GST_QUERY_CAPS:
2257     {
2258       GstCaps *filter, *caps;
2259
2260       gst_query_parse_caps (query, &filter);
2261       caps = gst_rtp_session_getcaps_send_rtp (pad, rtpsession, filter);
2262       gst_query_set_caps_result (query, caps);
2263       gst_caps_unref (caps);
2264       res = TRUE;
2265       break;
2266     }
2267     default:
2268       res = gst_pad_query_default (pad, parent, query);
2269       break;
2270   }
2271
2272   return res;
2273 }
2274
2275 static gboolean
2276 gst_rtp_session_setcaps_send_rtp (GstPad * pad, GstRtpSession * rtpsession,
2277     GstCaps * caps)
2278 {
2279   GstRtpSessionPrivate *priv;
2280
2281   priv = rtpsession->priv;
2282
2283   rtp_session_update_send_caps (priv->session, caps);
2284
2285   return TRUE;
2286 }
2287
2288 /* Receive an RTP packet or a list of packets to be sent to the receivers,
2289  * send to RTP session manager and forward to send_rtp_src.
2290  */
2291 static GstFlowReturn
2292 gst_rtp_session_chain_send_rtp_common (GstRtpSession * rtpsession,
2293     gpointer data, gboolean is_list)
2294 {
2295   GstRtpSessionPrivate *priv;
2296   GstFlowReturn ret;
2297   GstClockTime timestamp, running_time;
2298   GstClockTime current_time;
2299
2300   priv = rtpsession->priv;
2301
2302   GST_LOG_OBJECT (rtpsession, "received RTP %s", is_list ? "list" : "packet");
2303
2304   /* get NTP time when this packet was captured, this depends on the timestamp. */
2305   if (is_list) {
2306     GstBuffer *buffer = NULL;
2307
2308     /* All buffers in a list have the same timestamp.
2309      * So, just take it from the first buffer. */
2310     buffer = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0);
2311     if (buffer)
2312       timestamp = GST_BUFFER_PTS (buffer);
2313     else
2314       timestamp = -1;
2315   } else {
2316     timestamp = GST_BUFFER_PTS (GST_BUFFER_CAST (data));
2317   }
2318
2319   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
2320     /* convert to running time using the segment start value. */
2321     running_time =
2322         gst_segment_to_running_time (&rtpsession->send_rtp_seg, GST_FORMAT_TIME,
2323         timestamp);
2324     if (priv->rtcp_sync_send_time)
2325       running_time += priv->send_latency;
2326   } else {
2327     /* no timestamp. */
2328     running_time = -1;
2329   }
2330
2331   current_time = gst_clock_get_time (priv->sysclock);
2332   ret = rtp_session_send_rtp (priv->session, data, is_list, current_time,
2333       running_time);
2334   if (ret != GST_FLOW_OK)
2335     goto push_error;
2336
2337 done:
2338
2339   return ret;
2340
2341   /* ERRORS */
2342 push_error:
2343   {
2344     GST_DEBUG_OBJECT (rtpsession, "process returned %s",
2345         gst_flow_get_name (ret));
2346     goto done;
2347   }
2348 }
2349
2350 static GstFlowReturn
2351 gst_rtp_session_chain_send_rtp (GstPad * pad, GstObject * parent,
2352     GstBuffer * buffer)
2353 {
2354   GstRtpSession *rtpsession = GST_RTP_SESSION (parent);
2355
2356   return gst_rtp_session_chain_send_rtp_common (rtpsession, buffer, FALSE);
2357 }
2358
2359 static GstFlowReturn
2360 gst_rtp_session_chain_send_rtp_list (GstPad * pad, GstObject * parent,
2361     GstBufferList * list)
2362 {
2363   GstRtpSession *rtpsession = GST_RTP_SESSION (parent);
2364
2365   return gst_rtp_session_chain_send_rtp_common (rtpsession, list, TRUE);
2366 }
2367
2368 /* Create sinkpad to receive RTP packets from senders. This will also create a
2369  * srcpad for the RTP packets.
2370  */
2371 static GstPad *
2372 create_recv_rtp_sink (GstRtpSession * rtpsession)
2373 {
2374   GST_DEBUG_OBJECT (rtpsession, "creating RTP sink pad");
2375
2376   rtpsession->recv_rtp_sink =
2377       gst_pad_new_from_static_template (&rtpsession_recv_rtp_sink_template,
2378       "recv_rtp_sink");
2379   gst_pad_set_chain_function (rtpsession->recv_rtp_sink,
2380       gst_rtp_session_chain_recv_rtp);
2381   gst_pad_set_event_function (rtpsession->recv_rtp_sink,
2382       gst_rtp_session_event_recv_rtp_sink);
2383   gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_sink,
2384       gst_rtp_session_iterate_internal_links);
2385   GST_PAD_SET_PROXY_ALLOCATION (rtpsession->recv_rtp_sink);
2386   gst_pad_set_active (rtpsession->recv_rtp_sink, TRUE);
2387   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2388       rtpsession->recv_rtp_sink);
2389
2390   GST_DEBUG_OBJECT (rtpsession, "creating RTP src pad");
2391   rtpsession->recv_rtp_src =
2392       gst_pad_new_from_static_template (&rtpsession_recv_rtp_src_template,
2393       "recv_rtp_src");
2394   gst_pad_set_event_function (rtpsession->recv_rtp_src,
2395       gst_rtp_session_event_recv_rtp_src);
2396   gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_src,
2397       gst_rtp_session_iterate_internal_links);
2398   gst_pad_use_fixed_caps (rtpsession->recv_rtp_src);
2399   gst_pad_set_active (rtpsession->recv_rtp_src, TRUE);
2400   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->recv_rtp_src);
2401
2402   return rtpsession->recv_rtp_sink;
2403 }
2404
2405 /* Remove sinkpad to receive RTP packets from senders. This will also remove
2406  * the srcpad for the RTP packets.
2407  */
2408 static void
2409 remove_recv_rtp_sink (GstRtpSession * rtpsession)
2410 {
2411   GST_DEBUG_OBJECT (rtpsession, "removing RTP sink pad");
2412
2413   /* deactivate from source to sink */
2414   gst_pad_set_active (rtpsession->recv_rtp_src, FALSE);
2415   gst_pad_set_active (rtpsession->recv_rtp_sink, FALSE);
2416
2417   /* remove pads */
2418   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2419       rtpsession->recv_rtp_sink);
2420   rtpsession->recv_rtp_sink = NULL;
2421
2422   GST_DEBUG_OBJECT (rtpsession, "removing RTP src pad");
2423   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2424       rtpsession->recv_rtp_src);
2425   rtpsession->recv_rtp_src = NULL;
2426 }
2427
2428 /* Create a sinkpad to receive RTCP messages from senders, this will also create a
2429  * sync_src pad for the SR packets.
2430  */
2431 static GstPad *
2432 create_recv_rtcp_sink (GstRtpSession * rtpsession)
2433 {
2434   GST_DEBUG_OBJECT (rtpsession, "creating RTCP sink pad");
2435
2436   rtpsession->recv_rtcp_sink =
2437       gst_pad_new_from_static_template (&rtpsession_recv_rtcp_sink_template,
2438       "recv_rtcp_sink");
2439   gst_pad_set_chain_function (rtpsession->recv_rtcp_sink,
2440       gst_rtp_session_chain_recv_rtcp);
2441   gst_pad_set_event_function (rtpsession->recv_rtcp_sink,
2442       gst_rtp_session_event_recv_rtcp_sink);
2443   gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtcp_sink,
2444       gst_rtp_session_iterate_internal_links);
2445   gst_pad_set_active (rtpsession->recv_rtcp_sink, TRUE);
2446   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2447       rtpsession->recv_rtcp_sink);
2448
2449   GST_DEBUG_OBJECT (rtpsession, "creating sync src pad");
2450   rtpsession->sync_src =
2451       gst_pad_new_from_static_template (&rtpsession_sync_src_template,
2452       "sync_src");
2453   gst_pad_set_iterate_internal_links_function (rtpsession->sync_src,
2454       gst_rtp_session_iterate_internal_links);
2455   gst_pad_use_fixed_caps (rtpsession->sync_src);
2456   gst_pad_set_active (rtpsession->sync_src, TRUE);
2457   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
2458
2459   return rtpsession->recv_rtcp_sink;
2460 }
2461
2462 static void
2463 remove_recv_rtcp_sink (GstRtpSession * rtpsession)
2464 {
2465   GST_DEBUG_OBJECT (rtpsession, "removing RTCP sink pad");
2466
2467   gst_pad_set_active (rtpsession->sync_src, FALSE);
2468   gst_pad_set_active (rtpsession->recv_rtcp_sink, FALSE);
2469
2470   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2471       rtpsession->recv_rtcp_sink);
2472   rtpsession->recv_rtcp_sink = NULL;
2473
2474   GST_DEBUG_OBJECT (rtpsession, "removing sync src pad");
2475   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
2476   rtpsession->sync_src = NULL;
2477 }
2478
2479 /* Create a sinkpad to receive RTP packets for receivers. This will also create a
2480  * send_rtp_src pad.
2481  */
2482 static GstPad *
2483 create_send_rtp_sink (GstRtpSession * rtpsession)
2484 {
2485   GST_DEBUG_OBJECT (rtpsession, "creating pad");
2486
2487   rtpsession->send_rtp_sink =
2488       gst_pad_new_from_static_template (&rtpsession_send_rtp_sink_template,
2489       "send_rtp_sink");
2490   gst_pad_set_chain_function (rtpsession->send_rtp_sink,
2491       gst_rtp_session_chain_send_rtp);
2492   gst_pad_set_chain_list_function (rtpsession->send_rtp_sink,
2493       gst_rtp_session_chain_send_rtp_list);
2494   gst_pad_set_query_function (rtpsession->send_rtp_sink,
2495       gst_rtp_session_query_send_rtp);
2496   gst_pad_set_event_function (rtpsession->send_rtp_sink,
2497       gst_rtp_session_event_send_rtp_sink);
2498   gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_sink,
2499       gst_rtp_session_iterate_internal_links);
2500   GST_PAD_SET_PROXY_CAPS (rtpsession->send_rtp_sink);
2501   GST_PAD_SET_PROXY_ALLOCATION (rtpsession->send_rtp_sink);
2502   gst_pad_set_active (rtpsession->send_rtp_sink, TRUE);
2503   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2504       rtpsession->send_rtp_sink);
2505
2506   rtpsession->send_rtp_src =
2507       gst_pad_new_from_static_template (&rtpsession_send_rtp_src_template,
2508       "send_rtp_src");
2509   gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_src,
2510       gst_rtp_session_iterate_internal_links);
2511   gst_pad_set_event_function (rtpsession->send_rtp_src,
2512       gst_rtp_session_event_send_rtp_src);
2513   GST_PAD_SET_PROXY_CAPS (rtpsession->send_rtp_src);
2514   gst_pad_set_active (rtpsession->send_rtp_src, TRUE);
2515   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->send_rtp_src);
2516
2517   return rtpsession->send_rtp_sink;
2518 }
2519
2520 static void
2521 remove_send_rtp_sink (GstRtpSession * rtpsession)
2522 {
2523   GST_DEBUG_OBJECT (rtpsession, "removing pad");
2524
2525   gst_pad_set_active (rtpsession->send_rtp_src, FALSE);
2526   gst_pad_set_active (rtpsession->send_rtp_sink, FALSE);
2527
2528   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2529       rtpsession->send_rtp_sink);
2530   rtpsession->send_rtp_sink = NULL;
2531
2532   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2533       rtpsession->send_rtp_src);
2534   rtpsession->send_rtp_src = NULL;
2535 }
2536
2537 /* Create a srcpad with the RTCP packets to send out.
2538  * This pad will be driven by the RTP session manager when it wants to send out
2539  * RTCP packets.
2540  */
2541 static GstPad *
2542 create_send_rtcp_src (GstRtpSession * rtpsession)
2543 {
2544   GST_DEBUG_OBJECT (rtpsession, "creating pad");
2545
2546   rtpsession->send_rtcp_src =
2547       gst_pad_new_from_static_template (&rtpsession_send_rtcp_src_template,
2548       "send_rtcp_src");
2549   gst_pad_use_fixed_caps (rtpsession->send_rtcp_src);
2550   gst_pad_set_active (rtpsession->send_rtcp_src, TRUE);
2551   gst_pad_set_iterate_internal_links_function (rtpsession->send_rtcp_src,
2552       gst_rtp_session_iterate_internal_links);
2553   gst_pad_set_query_function (rtpsession->send_rtcp_src,
2554       gst_rtp_session_query_send_rtcp_src);
2555   gst_pad_set_event_function (rtpsession->send_rtcp_src,
2556       gst_rtp_session_event_send_rtcp_src);
2557   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2558       rtpsession->send_rtcp_src);
2559
2560   return rtpsession->send_rtcp_src;
2561 }
2562
2563 static void
2564 remove_send_rtcp_src (GstRtpSession * rtpsession)
2565 {
2566   GST_DEBUG_OBJECT (rtpsession, "removing pad");
2567
2568   gst_pad_set_active (rtpsession->send_rtcp_src, FALSE);
2569
2570   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2571       rtpsession->send_rtcp_src);
2572   rtpsession->send_rtcp_src = NULL;
2573 }
2574
2575 static GstPad *
2576 gst_rtp_session_request_new_pad (GstElement * element,
2577     GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
2578 {
2579   GstRtpSession *rtpsession;
2580   GstElementClass *klass;
2581   GstPad *result;
2582
2583   g_return_val_if_fail (templ != NULL, NULL);
2584   g_return_val_if_fail (GST_IS_RTP_SESSION (element), NULL);
2585
2586   rtpsession = GST_RTP_SESSION (element);
2587   klass = GST_ELEMENT_GET_CLASS (element);
2588
2589   GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
2590
2591   GST_RTP_SESSION_LOCK (rtpsession);
2592
2593   /* figure out the template */
2594   if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink")) {
2595     if (rtpsession->recv_rtp_sink != NULL)
2596       goto exists;
2597
2598     result = create_recv_rtp_sink (rtpsession);
2599   } else if (templ == gst_element_class_get_pad_template (klass,
2600           "recv_rtcp_sink")) {
2601     if (rtpsession->recv_rtcp_sink != NULL)
2602       goto exists;
2603
2604     result = create_recv_rtcp_sink (rtpsession);
2605   } else if (templ == gst_element_class_get_pad_template (klass,
2606           "send_rtp_sink")) {
2607     if (rtpsession->send_rtp_sink != NULL)
2608       goto exists;
2609
2610     result = create_send_rtp_sink (rtpsession);
2611   } else if (templ == gst_element_class_get_pad_template (klass,
2612           "send_rtcp_src")) {
2613     if (rtpsession->send_rtcp_src != NULL)
2614       goto exists;
2615
2616     result = create_send_rtcp_src (rtpsession);
2617   } else
2618     goto wrong_template;
2619
2620   GST_RTP_SESSION_UNLOCK (rtpsession);
2621
2622   return result;
2623
2624   /* ERRORS */
2625 wrong_template:
2626   {
2627     GST_RTP_SESSION_UNLOCK (rtpsession);
2628     g_warning ("rtpsession: this is not our template");
2629     return NULL;
2630   }
2631 exists:
2632   {
2633     GST_RTP_SESSION_UNLOCK (rtpsession);
2634     g_warning ("rtpsession: pad already requested");
2635     return NULL;
2636   }
2637 }
2638
2639 static void
2640 gst_rtp_session_release_pad (GstElement * element, GstPad * pad)
2641 {
2642   GstRtpSession *rtpsession;
2643
2644   g_return_if_fail (GST_IS_RTP_SESSION (element));
2645   g_return_if_fail (GST_IS_PAD (pad));
2646
2647   rtpsession = GST_RTP_SESSION (element);
2648
2649   GST_DEBUG_OBJECT (element, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2650
2651   GST_RTP_SESSION_LOCK (rtpsession);
2652
2653   if (rtpsession->recv_rtp_sink == pad) {
2654     remove_recv_rtp_sink (rtpsession);
2655   } else if (rtpsession->recv_rtcp_sink == pad) {
2656     remove_recv_rtcp_sink (rtpsession);
2657   } else if (rtpsession->send_rtp_sink == pad) {
2658     remove_send_rtp_sink (rtpsession);
2659   } else if (rtpsession->send_rtcp_src == pad) {
2660     remove_send_rtcp_src (rtpsession);
2661   } else
2662     goto wrong_pad;
2663
2664   GST_RTP_SESSION_UNLOCK (rtpsession);
2665
2666   return;
2667
2668   /* ERRORS */
2669 wrong_pad:
2670   {
2671     GST_RTP_SESSION_UNLOCK (rtpsession);
2672     g_warning ("rtpsession: asked to release an unknown pad");
2673     return;
2674   }
2675 }
2676
2677 static void
2678 gst_rtp_session_request_key_unit (RTPSession * sess,
2679     guint32 ssrc, gboolean all_headers, gpointer user_data)
2680 {
2681   GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2682   GstEvent *event;
2683   GstPad *send_rtp_sink;
2684
2685   GST_RTP_SESSION_LOCK (rtpsession);
2686   if ((send_rtp_sink = rtpsession->send_rtp_sink))
2687     gst_object_ref (send_rtp_sink);
2688   GST_RTP_SESSION_UNLOCK (rtpsession);
2689
2690   if (send_rtp_sink) {
2691     event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
2692         gst_structure_new ("GstForceKeyUnit", "ssrc", G_TYPE_UINT, ssrc,
2693             "all-headers", G_TYPE_BOOLEAN, all_headers, NULL));
2694     gst_pad_push_event (send_rtp_sink, event);
2695     gst_object_unref (send_rtp_sink);
2696   }
2697 }
2698
2699 static GstClockTime
2700 gst_rtp_session_request_time (RTPSession * session, gpointer user_data)
2701 {
2702   GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2703
2704   return gst_clock_get_time (rtpsession->priv->sysclock);
2705 }
2706
2707 static void
2708 gst_rtp_session_notify_nack (RTPSession * sess, guint16 seqnum,
2709     guint16 blp, guint32 ssrc, gpointer user_data)
2710 {
2711   GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2712   GstEvent *event;
2713   GstPad *send_rtp_sink;
2714
2715   GST_RTP_SESSION_LOCK (rtpsession);
2716   if ((send_rtp_sink = rtpsession->send_rtp_sink))
2717     gst_object_ref (send_rtp_sink);
2718   GST_RTP_SESSION_UNLOCK (rtpsession);
2719
2720   if (send_rtp_sink) {
2721     while (TRUE) {
2722       event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
2723           gst_structure_new ("GstRTPRetransmissionRequest",
2724               "seqnum", G_TYPE_UINT, (guint) seqnum,
2725               "ssrc", G_TYPE_UINT, (guint) ssrc, NULL));
2726       gst_pad_push_event (send_rtp_sink, event);
2727
2728       GST_RTP_SESSION_LOCK (rtpsession);
2729       rtpsession->priv->sent_rtx_req_count++;
2730       GST_RTP_SESSION_UNLOCK (rtpsession);
2731
2732       if (blp == 0)
2733         break;
2734
2735       seqnum++;
2736       while ((blp & 1) == 0) {
2737         seqnum++;
2738         blp >>= 1;
2739       }
2740       blp >>= 1;
2741     }
2742     gst_object_unref (send_rtp_sink);
2743   }
2744 }
2745
2746 static void
2747 gst_rtp_session_reconfigure (RTPSession * sess, gpointer user_data)
2748 {
2749   GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2750   GstPad *send_rtp_sink;
2751
2752   GST_RTP_SESSION_LOCK (rtpsession);
2753   if ((send_rtp_sink = rtpsession->send_rtp_sink))
2754     gst_object_ref (send_rtp_sink);
2755   GST_RTP_SESSION_UNLOCK (rtpsession);
2756
2757   if (send_rtp_sink) {
2758     gst_pad_push_event (send_rtp_sink, gst_event_new_reconfigure ());
2759     gst_object_unref (send_rtp_sink);
2760   }
2761 }
2762
2763 static void
2764 gst_rtp_session_notify_early_rtcp (RTPSession * sess, gpointer user_data)
2765 {
2766   GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2767
2768   GST_DEBUG_OBJECT (rtpsession, "Notified of early RTCP");
2769   /* with an early RTCP request, we might have to start the RTCP thread */
2770   GST_RTP_SESSION_LOCK (rtpsession);
2771   signal_waiting_rtcp_thread_unlocked (rtpsession);
2772   GST_RTP_SESSION_UNLOCK (rtpsession);
2773 }