04fca371610cfa3c3942fa716a34fae0931e6fbe
[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   GST_RTP_SESSION_LOCK (rtpsession);
1323   g_hash_table_foreach_remove (rtpsession->priv->ptmap, return_true, NULL);
1324   GST_RTP_SESSION_UNLOCK (rtpsession);
1325 }
1326
1327 /* called when the session manager has an RTP packet or a list of packets
1328  * ready for further processing */
1329 static GstFlowReturn
1330 gst_rtp_session_process_rtp (RTPSession * sess, RTPSource * src,
1331     GstBuffer * buffer, gpointer user_data)
1332 {
1333   GstFlowReturn result;
1334   GstRtpSession *rtpsession;
1335   GstPad *rtp_src;
1336
1337   rtpsession = GST_RTP_SESSION (user_data);
1338
1339   GST_RTP_SESSION_LOCK (rtpsession);
1340   if ((rtp_src = rtpsession->recv_rtp_src))
1341     gst_object_ref (rtp_src);
1342   GST_RTP_SESSION_UNLOCK (rtpsession);
1343
1344   if (rtp_src) {
1345     GST_LOG_OBJECT (rtpsession, "pushing received RTP packet");
1346     result = gst_pad_push (rtp_src, buffer);
1347     gst_object_unref (rtp_src);
1348   } else {
1349     GST_DEBUG_OBJECT (rtpsession, "dropping received RTP packet");
1350     gst_buffer_unref (buffer);
1351     result = GST_FLOW_OK;
1352   }
1353   return result;
1354 }
1355
1356 /* called when the session manager has an RTP packet ready for further
1357  * sending */
1358 static GstFlowReturn
1359 gst_rtp_session_send_rtp (RTPSession * sess, RTPSource * src,
1360     gpointer data, gpointer user_data)
1361 {
1362   GstFlowReturn result;
1363   GstRtpSession *rtpsession;
1364   GstPad *rtp_src;
1365
1366   rtpsession = GST_RTP_SESSION (user_data);
1367
1368   GST_RTP_SESSION_LOCK (rtpsession);
1369   if ((rtp_src = rtpsession->send_rtp_src))
1370     gst_object_ref (rtp_src);
1371   signal_waiting_rtcp_thread_unlocked (rtpsession);
1372   GST_RTP_SESSION_UNLOCK (rtpsession);
1373
1374   if (rtp_src) {
1375     if (GST_IS_BUFFER (data)) {
1376       GST_LOG_OBJECT (rtpsession, "sending RTP packet");
1377       result = gst_pad_push (rtp_src, GST_BUFFER_CAST (data));
1378     } else {
1379       GST_LOG_OBJECT (rtpsession, "sending RTP list");
1380       result = gst_pad_push_list (rtp_src, GST_BUFFER_LIST_CAST (data));
1381     }
1382     gst_object_unref (rtp_src);
1383   } else {
1384     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1385     result = GST_FLOW_OK;
1386   }
1387   return result;
1388 }
1389
1390 static void
1391 do_rtcp_events (GstRtpSession * rtpsession, GstPad * srcpad)
1392 {
1393   GstCaps *caps;
1394   GstSegment seg;
1395   GstEvent *event;
1396   gchar *stream_id;
1397   gboolean have_group_id;
1398   guint group_id;
1399
1400   stream_id =
1401       g_strdup_printf ("%08x%08x%08x%08x", g_random_int (), g_random_int (),
1402       g_random_int (), g_random_int ());
1403
1404   GST_RTP_SESSION_LOCK (rtpsession);
1405   if (rtpsession->recv_rtp_sink) {
1406     event =
1407         gst_pad_get_sticky_event (rtpsession->recv_rtp_sink,
1408         GST_EVENT_STREAM_START, 0);
1409     if (event) {
1410       if (gst_event_parse_group_id (event, &group_id))
1411         have_group_id = TRUE;
1412       else
1413         have_group_id = FALSE;
1414       gst_event_unref (event);
1415     } else {
1416       have_group_id = TRUE;
1417       group_id = gst_util_group_id_next ();
1418     }
1419   } else {
1420     have_group_id = TRUE;
1421     group_id = gst_util_group_id_next ();
1422   }
1423   GST_RTP_SESSION_UNLOCK (rtpsession);
1424
1425   event = gst_event_new_stream_start (stream_id);
1426   rtpsession->recv_rtcp_segment_seqnum = gst_event_get_seqnum (event);
1427   gst_event_set_seqnum (event, rtpsession->recv_rtcp_segment_seqnum);
1428   if (have_group_id)
1429     gst_event_set_group_id (event, group_id);
1430   gst_pad_push_event (srcpad, event);
1431   g_free (stream_id);
1432
1433   caps = gst_caps_new_empty_simple ("application/x-rtcp");
1434   gst_pad_set_caps (srcpad, caps);
1435   gst_caps_unref (caps);
1436
1437   gst_segment_init (&seg, GST_FORMAT_TIME);
1438   event = gst_event_new_segment (&seg);
1439   gst_event_set_seqnum (event, rtpsession->recv_rtcp_segment_seqnum);
1440   gst_pad_push_event (srcpad, event);
1441 }
1442
1443 /* called when the session manager has an RTCP packet ready for further
1444  * sending. The eos flag is set when an EOS event should be sent downstream as
1445  * well. */
1446 static GstFlowReturn
1447 gst_rtp_session_send_rtcp (RTPSession * sess, RTPSource * src,
1448     GstBuffer * buffer, gboolean all_sources_bye, gpointer user_data)
1449 {
1450   GstFlowReturn result;
1451   GstRtpSession *rtpsession;
1452   GstPad *rtcp_src;
1453
1454   rtpsession = GST_RTP_SESSION (user_data);
1455
1456   GST_RTP_SESSION_LOCK (rtpsession);
1457   if (rtpsession->priv->stop_thread)
1458     goto stopping;
1459
1460   if ((rtcp_src = rtpsession->send_rtcp_src)) {
1461     gst_object_ref (rtcp_src);
1462     GST_RTP_SESSION_UNLOCK (rtpsession);
1463
1464     /* set rtcp caps on output pad */
1465     if (!gst_pad_has_current_caps (rtcp_src))
1466       do_rtcp_events (rtpsession, rtcp_src);
1467
1468     GST_LOG_OBJECT (rtpsession, "sending RTCP");
1469     result = gst_pad_push (rtcp_src, buffer);
1470
1471     /* Forward send an EOS on the RTCP sink if we received an EOS on the
1472      * send_rtp_sink. We don't need to check the recv_rtp_sink since in this
1473      * case the EOS event would already have been sent */
1474     if (all_sources_bye && rtpsession->send_rtp_sink &&
1475         GST_PAD_IS_EOS (rtpsession->send_rtp_sink)) {
1476       GstEvent *event;
1477
1478       GST_LOG_OBJECT (rtpsession, "sending EOS");
1479
1480       event = gst_event_new_eos ();
1481       gst_event_set_seqnum (event, rtpsession->recv_rtcp_segment_seqnum);
1482       gst_pad_push_event (rtcp_src, event);
1483     }
1484     gst_object_unref (rtcp_src);
1485   } else {
1486     GST_RTP_SESSION_UNLOCK (rtpsession);
1487
1488     GST_DEBUG_OBJECT (rtpsession, "not sending RTCP, no output pad");
1489     gst_buffer_unref (buffer);
1490     result = GST_FLOW_OK;
1491   }
1492   return result;
1493
1494   /* ERRORS */
1495 stopping:
1496   {
1497     GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1498     gst_buffer_unref (buffer);
1499     GST_RTP_SESSION_UNLOCK (rtpsession);
1500     return GST_FLOW_OK;
1501   }
1502 }
1503
1504 /* called when the session manager has an SR RTCP packet ready for handling
1505  * inter stream synchronisation */
1506 static GstFlowReturn
1507 gst_rtp_session_sync_rtcp (RTPSession * sess,
1508     GstBuffer * buffer, gpointer user_data)
1509 {
1510   GstFlowReturn result;
1511   GstRtpSession *rtpsession;
1512   GstPad *sync_src;
1513
1514   rtpsession = GST_RTP_SESSION (user_data);
1515
1516   GST_RTP_SESSION_LOCK (rtpsession);
1517   if (rtpsession->priv->stop_thread)
1518     goto stopping;
1519
1520   if ((sync_src = rtpsession->sync_src)) {
1521     gst_object_ref (sync_src);
1522     GST_RTP_SESSION_UNLOCK (rtpsession);
1523
1524     /* set rtcp caps on output pad, this happens
1525      * when we receive RTCP muxed with RTP according
1526      * to RFC5761. Otherwise we would have forwarded
1527      * the events from the recv_rtcp_sink pad already
1528      */
1529     if (!gst_pad_has_current_caps (sync_src))
1530       do_rtcp_events (rtpsession, sync_src);
1531
1532     GST_LOG_OBJECT (rtpsession, "sending Sync RTCP");
1533     result = gst_pad_push (sync_src, buffer);
1534     gst_object_unref (sync_src);
1535   } else {
1536     GST_RTP_SESSION_UNLOCK (rtpsession);
1537
1538     GST_DEBUG_OBJECT (rtpsession, "not sending Sync RTCP, no output pad");
1539     gst_buffer_unref (buffer);
1540     result = GST_FLOW_OK;
1541   }
1542   return result;
1543
1544   /* ERRORS */
1545 stopping:
1546   {
1547     GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1548     gst_buffer_unref (buffer);
1549     GST_RTP_SESSION_UNLOCK (rtpsession);
1550     return GST_FLOW_OK;
1551   }
1552 }
1553
1554 static void
1555 gst_rtp_session_cache_caps (GstRtpSession * rtpsession, GstCaps * caps)
1556 {
1557   GstRtpSessionPrivate *priv;
1558   const GstStructure *s;
1559   gint payload;
1560
1561   priv = rtpsession->priv;
1562
1563   GST_DEBUG_OBJECT (rtpsession, "parsing caps");
1564
1565   s = gst_caps_get_structure (caps, 0);
1566   if (!gst_structure_get_int (s, "payload", &payload))
1567     return;
1568
1569   if (g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (payload)))
1570     return;
1571
1572   g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (payload),
1573       gst_caps_ref (caps));
1574 }
1575
1576 static GstCaps *
1577 gst_rtp_session_get_caps_for_pt (GstRtpSession * rtpsession, guint payload)
1578 {
1579   GstCaps *caps = NULL;
1580   GValue args[2] = { {0}, {0} };
1581   GValue ret = { 0 };
1582
1583   GST_RTP_SESSION_LOCK (rtpsession);
1584   caps = g_hash_table_lookup (rtpsession->priv->ptmap,
1585       GINT_TO_POINTER (payload));
1586   if (caps) {
1587     gst_caps_ref (caps);
1588     goto done;
1589   }
1590
1591   /* not found in the cache, try to get it with a signal */
1592   g_value_init (&args[0], GST_TYPE_ELEMENT);
1593   g_value_set_object (&args[0], rtpsession);
1594   g_value_init (&args[1], G_TYPE_UINT);
1595   g_value_set_uint (&args[1], payload);
1596
1597   g_value_init (&ret, GST_TYPE_CAPS);
1598   g_value_set_boxed (&ret, NULL);
1599
1600   GST_RTP_SESSION_UNLOCK (rtpsession);
1601
1602   g_signal_emitv (args, gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP], 0,
1603       &ret);
1604
1605   GST_RTP_SESSION_LOCK (rtpsession);
1606
1607   g_value_unset (&args[0]);
1608   g_value_unset (&args[1]);
1609   caps = (GstCaps *) g_value_dup_boxed (&ret);
1610   g_value_unset (&ret);
1611   if (!caps)
1612     goto no_caps;
1613
1614   gst_rtp_session_cache_caps (rtpsession, caps);
1615
1616 done:
1617   GST_RTP_SESSION_UNLOCK (rtpsession);
1618
1619   return caps;
1620
1621 no_caps:
1622   {
1623     GST_DEBUG_OBJECT (rtpsession, "could not get caps");
1624     goto done;
1625   }
1626 }
1627
1628 /* called when the session manager needs the clock rate */
1629 static gint
1630 gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
1631     gpointer user_data)
1632 {
1633   gint result = -1;
1634   GstRtpSession *rtpsession;
1635   GstCaps *caps;
1636   const GstStructure *s;
1637
1638   rtpsession = GST_RTP_SESSION_CAST (user_data);
1639
1640   caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1641
1642   if (!caps)
1643     goto done;
1644
1645   s = gst_caps_get_structure (caps, 0);
1646   if (!gst_structure_get_int (s, "clock-rate", &result))
1647     goto no_clock_rate;
1648
1649   gst_caps_unref (caps);
1650
1651   GST_DEBUG_OBJECT (rtpsession, "parsed clock-rate %d", result);
1652
1653 done:
1654
1655   return result;
1656
1657   /* ERRORS */
1658 no_clock_rate:
1659   {
1660     gst_caps_unref (caps);
1661     GST_DEBUG_OBJECT (rtpsession, "No clock-rate in caps!");
1662     goto done;
1663   }
1664 }
1665
1666 /* called when the session manager asks us to reconsider the timeout */
1667 static void
1668 gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data)
1669 {
1670   GstRtpSession *rtpsession;
1671
1672   rtpsession = GST_RTP_SESSION_CAST (user_data);
1673
1674   GST_RTP_SESSION_LOCK (rtpsession);
1675   GST_DEBUG_OBJECT (rtpsession, "unlock timer for reconsideration");
1676   if (rtpsession->priv->id)
1677     gst_clock_id_unschedule (rtpsession->priv->id);
1678   GST_RTP_SESSION_UNLOCK (rtpsession);
1679 }
1680
1681 static gboolean
1682 gst_rtp_session_event_recv_rtp_sink (GstPad * pad, GstObject * parent,
1683     GstEvent * event)
1684 {
1685   GstRtpSession *rtpsession;
1686   gboolean ret = FALSE;
1687
1688   rtpsession = GST_RTP_SESSION (parent);
1689
1690   GST_DEBUG_OBJECT (rtpsession, "received event %s",
1691       GST_EVENT_TYPE_NAME (event));
1692
1693   switch (GST_EVENT_TYPE (event)) {
1694     case GST_EVENT_CAPS:
1695     {
1696       GstCaps *caps;
1697
1698       /* process */
1699       gst_event_parse_caps (event, &caps);
1700       gst_rtp_session_sink_setcaps (pad, rtpsession, caps);
1701       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1702       break;
1703     }
1704     case GST_EVENT_FLUSH_STOP:
1705       gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
1706       rtpsession->recv_rtcp_segment_seqnum = GST_SEQNUM_INVALID;
1707       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1708       break;
1709     case GST_EVENT_SEGMENT:
1710     {
1711       GstSegment *segment, in_segment;
1712
1713       segment = &rtpsession->recv_rtp_seg;
1714
1715       /* the newsegment event is needed to convert the RTP timestamp to
1716        * running_time, which is needed to generate a mapping from RTP to NTP
1717        * timestamps in SR reports */
1718       gst_event_copy_segment (event, &in_segment);
1719       GST_DEBUG_OBJECT (rtpsession, "received segment %" GST_SEGMENT_FORMAT,
1720           &in_segment);
1721
1722       /* accept upstream */
1723       gst_segment_copy_into (&in_segment, segment);
1724
1725       /* push event forward */
1726       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1727       break;
1728     }
1729     case GST_EVENT_EOS:
1730     {
1731       GstPad *rtcp_src;
1732
1733       ret =
1734           gst_pad_push_event (rtpsession->recv_rtp_src, gst_event_ref (event));
1735
1736       GST_RTP_SESSION_LOCK (rtpsession);
1737       if ((rtcp_src = rtpsession->send_rtcp_src))
1738         gst_object_ref (rtcp_src);
1739       GST_RTP_SESSION_UNLOCK (rtpsession);
1740
1741       gst_event_unref (event);
1742
1743       if (rtcp_src) {
1744         event = gst_event_new_eos ();
1745         if (rtpsession->recv_rtcp_segment_seqnum != GST_SEQNUM_INVALID)
1746           gst_event_set_seqnum (event, rtpsession->recv_rtcp_segment_seqnum);
1747         ret = gst_pad_push_event (rtcp_src, event);
1748         gst_object_unref (rtcp_src);
1749       } else {
1750         ret = TRUE;
1751       }
1752       break;
1753     }
1754     default:
1755       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1756       break;
1757   }
1758
1759   return ret;
1760
1761 }
1762
1763 static gboolean
1764 gst_rtp_session_request_remote_key_unit (GstRtpSession * rtpsession,
1765     guint32 ssrc, guint payload, gboolean all_headers, gint count)
1766 {
1767   GstCaps *caps;
1768
1769   caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1770
1771   if (caps) {
1772     const GstStructure *s = gst_caps_get_structure (caps, 0);
1773     gboolean pli;
1774     gboolean fir;
1775
1776     pli = gst_structure_has_field (s, "rtcp-fb-nack-pli");
1777     fir = gst_structure_has_field (s, "rtcp-fb-ccm-fir") && all_headers;
1778
1779     /* Google Talk uses FIR for repair, so send it even if we just want a
1780      * regular PLI */
1781     if (!pli &&
1782         gst_structure_has_field (s, "rtcp-fb-x-gstreamer-fir-as-repair"))
1783       fir = TRUE;
1784
1785     gst_caps_unref (caps);
1786
1787     if (pli || fir)
1788       return rtp_session_request_key_unit (rtpsession->priv->session, ssrc,
1789           fir, count);
1790   }
1791
1792   return FALSE;
1793 }
1794
1795 static gboolean
1796 gst_rtp_session_event_recv_rtp_src (GstPad * pad, GstObject * parent,
1797     GstEvent * event)
1798 {
1799   GstRtpSession *rtpsession;
1800   gboolean forward = TRUE;
1801   gboolean ret = TRUE;
1802   const GstStructure *s;
1803   guint32 ssrc;
1804   guint pt;
1805
1806   rtpsession = GST_RTP_SESSION (parent);
1807
1808   switch (GST_EVENT_TYPE (event)) {
1809     case GST_EVENT_CUSTOM_UPSTREAM:
1810       s = gst_event_get_structure (event);
1811       if (gst_structure_has_name (s, "GstForceKeyUnit") &&
1812           gst_structure_get_uint (s, "ssrc", &ssrc) &&
1813           gst_structure_get_uint (s, "payload", &pt)) {
1814         gboolean all_headers = FALSE;
1815         gint count = -1;
1816
1817         gst_structure_get_boolean (s, "all-headers", &all_headers);
1818         if (gst_structure_get_int (s, "count", &count) && count < 0)
1819           count += G_MAXINT;    /* Make sure count is positive if present */
1820         if (gst_rtp_session_request_remote_key_unit (rtpsession, ssrc, pt,
1821                 all_headers, count))
1822           forward = FALSE;
1823       } else if (gst_structure_has_name (s, "GstRTPRetransmissionRequest")) {
1824         guint seqnum, delay, deadline, max_delay, avg_rtt;
1825
1826         GST_RTP_SESSION_LOCK (rtpsession);
1827         rtpsession->priv->recv_rtx_req_count++;
1828         GST_RTP_SESSION_UNLOCK (rtpsession);
1829
1830         if (!gst_structure_get_uint (s, "ssrc", &ssrc))
1831           ssrc = -1;
1832         if (!gst_structure_get_uint (s, "seqnum", &seqnum))
1833           seqnum = -1;
1834         if (!gst_structure_get_uint (s, "delay", &delay))
1835           delay = 0;
1836         if (!gst_structure_get_uint (s, "deadline", &deadline))
1837           deadline = 100;
1838         if (!gst_structure_get_uint (s, "avg-rtt", &avg_rtt))
1839           avg_rtt = 40;
1840
1841         /* remaining time to receive the packet */
1842         max_delay = deadline;
1843         if (max_delay > delay)
1844           max_delay -= delay;
1845         /* estimated RTT */
1846         if (max_delay > avg_rtt)
1847           max_delay -= avg_rtt;
1848         else
1849           max_delay = 0;
1850
1851         if (rtp_session_request_nack (rtpsession->priv->session, ssrc, seqnum,
1852                 max_delay * GST_MSECOND))
1853           forward = FALSE;
1854       }
1855       break;
1856     default:
1857       break;
1858   }
1859
1860   if (forward) {
1861     GstPad *recv_rtp_sink;
1862
1863     GST_RTP_SESSION_LOCK (rtpsession);
1864     if ((recv_rtp_sink = rtpsession->recv_rtp_sink))
1865       gst_object_ref (recv_rtp_sink);
1866     GST_RTP_SESSION_UNLOCK (rtpsession);
1867
1868     if (recv_rtp_sink) {
1869       ret = gst_pad_push_event (recv_rtp_sink, event);
1870       gst_object_unref (recv_rtp_sink);
1871     } else
1872       gst_event_unref (event);
1873   } else {
1874     gst_event_unref (event);
1875   }
1876
1877   return ret;
1878 }
1879
1880
1881 static GstIterator *
1882 gst_rtp_session_iterate_internal_links (GstPad * pad, GstObject * parent)
1883 {
1884   GstRtpSession *rtpsession;
1885   GstPad *otherpad = NULL;
1886   GstIterator *it = NULL;
1887
1888   rtpsession = GST_RTP_SESSION (parent);
1889
1890   GST_RTP_SESSION_LOCK (rtpsession);
1891   if (pad == rtpsession->recv_rtp_src) {
1892     otherpad = gst_object_ref (rtpsession->recv_rtp_sink);
1893   } else if (pad == rtpsession->recv_rtp_sink) {
1894     otherpad = gst_object_ref (rtpsession->recv_rtp_src);
1895   } else if (pad == rtpsession->send_rtp_src) {
1896     otherpad = gst_object_ref (rtpsession->send_rtp_sink);
1897   } else if (pad == rtpsession->send_rtp_sink) {
1898     otherpad = gst_object_ref (rtpsession->send_rtp_src);
1899   }
1900   GST_RTP_SESSION_UNLOCK (rtpsession);
1901
1902   if (otherpad) {
1903     GValue val = { 0, };
1904
1905     g_value_init (&val, GST_TYPE_PAD);
1906     g_value_set_object (&val, otherpad);
1907     it = gst_iterator_new_single (GST_TYPE_PAD, &val);
1908     g_value_unset (&val);
1909     gst_object_unref (otherpad);
1910   } else {
1911     it = gst_iterator_new_single (GST_TYPE_PAD, NULL);
1912   }
1913
1914   return it;
1915 }
1916
1917 static gboolean
1918 gst_rtp_session_sink_setcaps (GstPad * pad, GstRtpSession * rtpsession,
1919     GstCaps * caps)
1920 {
1921   GST_RTP_SESSION_LOCK (rtpsession);
1922   gst_rtp_session_cache_caps (rtpsession, caps);
1923   GST_RTP_SESSION_UNLOCK (rtpsession);
1924
1925   return TRUE;
1926 }
1927
1928 /* receive a packet from a sender, send it to the RTP session manager and
1929  * forward the packet on the rtp_src pad
1930  */
1931 static GstFlowReturn
1932 gst_rtp_session_chain_recv_rtp (GstPad * pad, GstObject * parent,
1933     GstBuffer * buffer)
1934 {
1935   GstRtpSession *rtpsession;
1936   GstRtpSessionPrivate *priv;
1937   GstFlowReturn ret;
1938   GstClockTime current_time, running_time;
1939   GstClockTime timestamp;
1940   guint64 ntpnstime;
1941
1942   rtpsession = GST_RTP_SESSION (parent);
1943   priv = rtpsession->priv;
1944
1945   GST_LOG_OBJECT (rtpsession, "received RTP packet");
1946
1947   GST_RTP_SESSION_LOCK (rtpsession);
1948   signal_waiting_rtcp_thread_unlocked (rtpsession);
1949   GST_RTP_SESSION_UNLOCK (rtpsession);
1950
1951   /* get NTP time when this packet was captured, this depends on the timestamp. */
1952   timestamp = GST_BUFFER_PTS (buffer);
1953   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1954     /* convert to running time using the segment values */
1955     running_time =
1956         gst_segment_to_running_time (&rtpsession->recv_rtp_seg, GST_FORMAT_TIME,
1957         timestamp);
1958     ntpnstime = GST_CLOCK_TIME_NONE;
1959   } else {
1960     get_current_times (rtpsession, &running_time, &ntpnstime);
1961   }
1962   current_time = gst_clock_get_time (priv->sysclock);
1963
1964   ret = rtp_session_process_rtp (priv->session, buffer, current_time,
1965       running_time, ntpnstime);
1966   if (ret != GST_FLOW_OK)
1967     goto push_error;
1968
1969 done:
1970
1971   return ret;
1972
1973   /* ERRORS */
1974 push_error:
1975   {
1976     GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1977         gst_flow_get_name (ret));
1978     goto done;
1979   }
1980 }
1981
1982 static gboolean
1983 gst_rtp_session_event_recv_rtcp_sink (GstPad * pad, GstObject * parent,
1984     GstEvent * event)
1985 {
1986   GstRtpSession *rtpsession;
1987   gboolean ret = FALSE;
1988
1989   rtpsession = GST_RTP_SESSION (parent);
1990
1991   GST_DEBUG_OBJECT (rtpsession, "received event %s",
1992       GST_EVENT_TYPE_NAME (event));
1993
1994   switch (GST_EVENT_TYPE (event)) {
1995     case GST_EVENT_SEGMENT:
1996       /* Make sure that the sync_src pad has caps before the segment event.
1997        * Otherwise we might get a segment event before caps from the receive
1998        * RTCP pad, and then later when receiving RTCP packets will set caps.
1999        * This will results in a sticky event misordering warning
2000        */
2001       if (!gst_pad_has_current_caps (rtpsession->sync_src)) {
2002         GstCaps *caps = gst_caps_new_empty_simple ("application/x-rtcp");
2003         gst_pad_set_caps (rtpsession->sync_src, caps);
2004         gst_caps_unref (caps);
2005       }
2006       /* fall through */
2007     default:
2008       ret = gst_pad_push_event (rtpsession->sync_src, event);
2009       break;
2010   }
2011
2012   return ret;
2013 }
2014
2015 /* Receive an RTCP packet from a sender, send it to the RTP session manager and
2016  * forward the SR packets to the sync_src pad.
2017  */
2018 static GstFlowReturn
2019 gst_rtp_session_chain_recv_rtcp (GstPad * pad, GstObject * parent,
2020     GstBuffer * buffer)
2021 {
2022   GstRtpSession *rtpsession;
2023   GstRtpSessionPrivate *priv;
2024   GstClockTime current_time;
2025   GstClockTime running_time;
2026   guint64 ntpnstime;
2027
2028   rtpsession = GST_RTP_SESSION (parent);
2029   priv = rtpsession->priv;
2030
2031   GST_LOG_OBJECT (rtpsession, "received RTCP packet");
2032
2033   GST_RTP_SESSION_LOCK (rtpsession);
2034   signal_waiting_rtcp_thread_unlocked (rtpsession);
2035   GST_RTP_SESSION_UNLOCK (rtpsession);
2036
2037   current_time = gst_clock_get_time (priv->sysclock);
2038   get_current_times (rtpsession, &running_time, &ntpnstime);
2039
2040   rtp_session_process_rtcp (priv->session, buffer, current_time, running_time,
2041       ntpnstime);
2042
2043   return GST_FLOW_OK;           /* always return OK */
2044 }
2045
2046 static gboolean
2047 gst_rtp_session_query_send_rtcp_src (GstPad * pad, GstObject * parent,
2048     GstQuery * query)
2049 {
2050   GstRtpSession *rtpsession;
2051   gboolean ret = FALSE;
2052
2053   rtpsession = GST_RTP_SESSION (parent);
2054
2055   GST_DEBUG_OBJECT (rtpsession, "received QUERY %s",
2056       GST_QUERY_TYPE_NAME (query));
2057
2058   switch (GST_QUERY_TYPE (query)) {
2059     case GST_QUERY_LATENCY:
2060       ret = TRUE;
2061       /* use the defaults for the latency query. */
2062       gst_query_set_latency (query, FALSE, 0, -1);
2063       break;
2064     default:
2065       /* other queries simply fail for now */
2066       break;
2067   }
2068
2069   return ret;
2070 }
2071
2072 static gboolean
2073 gst_rtp_session_event_send_rtcp_src (GstPad * pad, GstObject * parent,
2074     GstEvent * event)
2075 {
2076   GstRtpSession *rtpsession;
2077   gboolean ret = TRUE;
2078
2079   rtpsession = GST_RTP_SESSION (parent);
2080   GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
2081       GST_EVENT_TYPE_NAME (event));
2082
2083   switch (GST_EVENT_TYPE (event)) {
2084     case GST_EVENT_SEEK:
2085     case GST_EVENT_LATENCY:
2086       gst_event_unref (event);
2087       ret = TRUE;
2088       break;
2089     default:
2090       /* other events simply fail for now */
2091       gst_event_unref (event);
2092       ret = FALSE;
2093       break;
2094   }
2095
2096   return ret;
2097 }
2098
2099
2100 static gboolean
2101 gst_rtp_session_event_send_rtp_sink (GstPad * pad, GstObject * parent,
2102     GstEvent * event)
2103 {
2104   GstRtpSession *rtpsession;
2105   gboolean ret = FALSE;
2106
2107   rtpsession = GST_RTP_SESSION (parent);
2108
2109   GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
2110       GST_EVENT_TYPE_NAME (event));
2111
2112   switch (GST_EVENT_TYPE (event)) {
2113     case GST_EVENT_CAPS:
2114     {
2115       GstCaps *caps;
2116
2117       /* process */
2118       gst_event_parse_caps (event, &caps);
2119       gst_rtp_session_setcaps_send_rtp (pad, rtpsession, caps);
2120       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
2121       break;
2122     }
2123     case GST_EVENT_FLUSH_STOP:
2124       gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
2125       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
2126       break;
2127     case GST_EVENT_SEGMENT:{
2128       GstSegment *segment, in_segment;
2129
2130       segment = &rtpsession->send_rtp_seg;
2131
2132       /* the newsegment event is needed to convert the RTP timestamp to
2133        * running_time, which is needed to generate a mapping from RTP to NTP
2134        * timestamps in SR reports */
2135       gst_event_copy_segment (event, &in_segment);
2136       GST_DEBUG_OBJECT (rtpsession, "received segment %" GST_SEGMENT_FORMAT,
2137           &in_segment);
2138
2139       /* accept upstream */
2140       gst_segment_copy_into (&in_segment, segment);
2141
2142       /* push event forward */
2143       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
2144       break;
2145     }
2146     case GST_EVENT_EOS:{
2147       GstClockTime current_time;
2148
2149       /* push downstream FIXME, we are not supposed to leave the session just
2150        * because we stop sending. */
2151       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
2152       current_time = gst_clock_get_time (rtpsession->priv->sysclock);
2153
2154       GST_DEBUG_OBJECT (rtpsession, "scheduling BYE message");
2155       rtp_session_mark_all_bye (rtpsession->priv->session, "End Of Stream");
2156       rtp_session_schedule_bye (rtpsession->priv->session, current_time);
2157       break;
2158     }
2159     default:{
2160       GstPad *send_rtp_src;
2161
2162       GST_RTP_SESSION_LOCK (rtpsession);
2163       if ((send_rtp_src = rtpsession->send_rtp_src))
2164         gst_object_ref (send_rtp_src);
2165       GST_RTP_SESSION_UNLOCK (rtpsession);
2166
2167       if (send_rtp_src) {
2168         ret = gst_pad_push_event (send_rtp_src, event);
2169         gst_object_unref (send_rtp_src);
2170       } else
2171         gst_event_unref (event);
2172
2173       break;
2174     }
2175   }
2176
2177   return ret;
2178 }
2179
2180 static gboolean
2181 gst_rtp_session_event_send_rtp_src (GstPad * pad, GstObject * parent,
2182     GstEvent * event)
2183 {
2184   GstRtpSession *rtpsession;
2185   gboolean ret = FALSE;
2186
2187   rtpsession = GST_RTP_SESSION (parent);
2188
2189   GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
2190       GST_EVENT_TYPE_NAME (event));
2191
2192   switch (GST_EVENT_TYPE (event)) {
2193     case GST_EVENT_LATENCY:
2194       /* save the latency, we need this to know when an RTP packet will be
2195        * rendered by the sink */
2196       gst_event_parse_latency (event, &rtpsession->priv->send_latency);
2197
2198       ret = gst_pad_event_default (pad, parent, event);
2199       break;
2200     default:
2201       ret = gst_pad_event_default (pad, parent, event);
2202       break;
2203   }
2204   return ret;
2205 }
2206
2207 static GstCaps *
2208 gst_rtp_session_getcaps_send_rtp (GstPad * pad, GstRtpSession * rtpsession,
2209     GstCaps * filter)
2210 {
2211   GstRtpSessionPrivate *priv;
2212   GstCaps *result;
2213   GstStructure *s1, *s2;
2214   guint ssrc;
2215   gboolean is_random;
2216
2217   priv = rtpsession->priv;
2218
2219   ssrc = rtp_session_suggest_ssrc (priv->session, &is_random);
2220
2221   /* we can basically accept anything but we prefer to receive packets with our
2222    * internal SSRC so that we don't have to patch it. Create a structure with
2223    * the SSRC and another one without.
2224    * Only do this if the session actually decided on an ssrc already,
2225    * otherwise we give upstream the opportunity to select an ssrc itself */
2226   if (!is_random) {
2227     s1 = gst_structure_new ("application/x-rtp", "ssrc", G_TYPE_UINT, ssrc,
2228         NULL);
2229     s2 = gst_structure_new_empty ("application/x-rtp");
2230
2231     result = gst_caps_new_full (s1, s2, NULL);
2232   } else {
2233     result = gst_caps_new_empty_simple ("application/x-rtp");
2234   }
2235
2236   if (filter) {
2237     GstCaps *caps = result;
2238
2239     result = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
2240     gst_caps_unref (caps);
2241   }
2242
2243   GST_DEBUG_OBJECT (rtpsession, "getting caps %" GST_PTR_FORMAT, result);
2244
2245   return result;
2246 }
2247
2248 static gboolean
2249 gst_rtp_session_query_send_rtp (GstPad * pad, GstObject * parent,
2250     GstQuery * query)
2251 {
2252   gboolean res = FALSE;
2253   GstRtpSession *rtpsession;
2254
2255   rtpsession = GST_RTP_SESSION (parent);
2256
2257   switch (GST_QUERY_TYPE (query)) {
2258     case GST_QUERY_CAPS:
2259     {
2260       GstCaps *filter, *caps;
2261
2262       gst_query_parse_caps (query, &filter);
2263       caps = gst_rtp_session_getcaps_send_rtp (pad, rtpsession, filter);
2264       gst_query_set_caps_result (query, caps);
2265       gst_caps_unref (caps);
2266       res = TRUE;
2267       break;
2268     }
2269     default:
2270       res = gst_pad_query_default (pad, parent, query);
2271       break;
2272   }
2273
2274   return res;
2275 }
2276
2277 static gboolean
2278 gst_rtp_session_setcaps_send_rtp (GstPad * pad, GstRtpSession * rtpsession,
2279     GstCaps * caps)
2280 {
2281   GstRtpSessionPrivate *priv;
2282
2283   priv = rtpsession->priv;
2284
2285   rtp_session_update_send_caps (priv->session, caps);
2286
2287   return TRUE;
2288 }
2289
2290 /* Receive an RTP packet or a list of packets to be sent to the receivers,
2291  * send to RTP session manager and forward to send_rtp_src.
2292  */
2293 static GstFlowReturn
2294 gst_rtp_session_chain_send_rtp_common (GstRtpSession * rtpsession,
2295     gpointer data, gboolean is_list)
2296 {
2297   GstRtpSessionPrivate *priv;
2298   GstFlowReturn ret;
2299   GstClockTime timestamp, running_time;
2300   GstClockTime current_time;
2301
2302   priv = rtpsession->priv;
2303
2304   GST_LOG_OBJECT (rtpsession, "received RTP %s", is_list ? "list" : "packet");
2305
2306   /* get NTP time when this packet was captured, this depends on the timestamp. */
2307   if (is_list) {
2308     GstBuffer *buffer = NULL;
2309
2310     /* All buffers in a list have the same timestamp.
2311      * So, just take it from the first buffer. */
2312     buffer = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0);
2313     if (buffer)
2314       timestamp = GST_BUFFER_PTS (buffer);
2315     else
2316       timestamp = -1;
2317   } else {
2318     timestamp = GST_BUFFER_PTS (GST_BUFFER_CAST (data));
2319   }
2320
2321   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
2322     /* convert to running time using the segment start value. */
2323     running_time =
2324         gst_segment_to_running_time (&rtpsession->send_rtp_seg, GST_FORMAT_TIME,
2325         timestamp);
2326     if (priv->rtcp_sync_send_time)
2327       running_time += priv->send_latency;
2328   } else {
2329     /* no timestamp. */
2330     running_time = -1;
2331   }
2332
2333   current_time = gst_clock_get_time (priv->sysclock);
2334   ret = rtp_session_send_rtp (priv->session, data, is_list, current_time,
2335       running_time);
2336   if (ret != GST_FLOW_OK)
2337     goto push_error;
2338
2339 done:
2340
2341   return ret;
2342
2343   /* ERRORS */
2344 push_error:
2345   {
2346     GST_DEBUG_OBJECT (rtpsession, "process returned %s",
2347         gst_flow_get_name (ret));
2348     goto done;
2349   }
2350 }
2351
2352 static GstFlowReturn
2353 gst_rtp_session_chain_send_rtp (GstPad * pad, GstObject * parent,
2354     GstBuffer * buffer)
2355 {
2356   GstRtpSession *rtpsession = GST_RTP_SESSION (parent);
2357
2358   return gst_rtp_session_chain_send_rtp_common (rtpsession, buffer, FALSE);
2359 }
2360
2361 static GstFlowReturn
2362 gst_rtp_session_chain_send_rtp_list (GstPad * pad, GstObject * parent,
2363     GstBufferList * list)
2364 {
2365   GstRtpSession *rtpsession = GST_RTP_SESSION (parent);
2366
2367   return gst_rtp_session_chain_send_rtp_common (rtpsession, list, TRUE);
2368 }
2369
2370 /* Create sinkpad to receive RTP packets from senders. This will also create a
2371  * srcpad for the RTP packets.
2372  */
2373 static GstPad *
2374 create_recv_rtp_sink (GstRtpSession * rtpsession)
2375 {
2376   GST_DEBUG_OBJECT (rtpsession, "creating RTP sink pad");
2377
2378   rtpsession->recv_rtp_sink =
2379       gst_pad_new_from_static_template (&rtpsession_recv_rtp_sink_template,
2380       "recv_rtp_sink");
2381   gst_pad_set_chain_function (rtpsession->recv_rtp_sink,
2382       gst_rtp_session_chain_recv_rtp);
2383   gst_pad_set_event_function (rtpsession->recv_rtp_sink,
2384       gst_rtp_session_event_recv_rtp_sink);
2385   gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_sink,
2386       gst_rtp_session_iterate_internal_links);
2387   GST_PAD_SET_PROXY_ALLOCATION (rtpsession->recv_rtp_sink);
2388   gst_pad_set_active (rtpsession->recv_rtp_sink, TRUE);
2389   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2390       rtpsession->recv_rtp_sink);
2391
2392   GST_DEBUG_OBJECT (rtpsession, "creating RTP src pad");
2393   rtpsession->recv_rtp_src =
2394       gst_pad_new_from_static_template (&rtpsession_recv_rtp_src_template,
2395       "recv_rtp_src");
2396   gst_pad_set_event_function (rtpsession->recv_rtp_src,
2397       gst_rtp_session_event_recv_rtp_src);
2398   gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_src,
2399       gst_rtp_session_iterate_internal_links);
2400   gst_pad_use_fixed_caps (rtpsession->recv_rtp_src);
2401   gst_pad_set_active (rtpsession->recv_rtp_src, TRUE);
2402   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->recv_rtp_src);
2403
2404   return rtpsession->recv_rtp_sink;
2405 }
2406
2407 /* Remove sinkpad to receive RTP packets from senders. This will also remove
2408  * the srcpad for the RTP packets.
2409  */
2410 static void
2411 remove_recv_rtp_sink (GstRtpSession * rtpsession)
2412 {
2413   GST_DEBUG_OBJECT (rtpsession, "removing RTP sink pad");
2414
2415   /* deactivate from source to sink */
2416   gst_pad_set_active (rtpsession->recv_rtp_src, FALSE);
2417   gst_pad_set_active (rtpsession->recv_rtp_sink, FALSE);
2418
2419   /* remove pads */
2420   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2421       rtpsession->recv_rtp_sink);
2422   rtpsession->recv_rtp_sink = NULL;
2423
2424   GST_DEBUG_OBJECT (rtpsession, "removing RTP src pad");
2425   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2426       rtpsession->recv_rtp_src);
2427   rtpsession->recv_rtp_src = NULL;
2428 }
2429
2430 /* Create a sinkpad to receive RTCP messages from senders, this will also create a
2431  * sync_src pad for the SR packets.
2432  */
2433 static GstPad *
2434 create_recv_rtcp_sink (GstRtpSession * rtpsession)
2435 {
2436   GST_DEBUG_OBJECT (rtpsession, "creating RTCP sink pad");
2437
2438   rtpsession->recv_rtcp_sink =
2439       gst_pad_new_from_static_template (&rtpsession_recv_rtcp_sink_template,
2440       "recv_rtcp_sink");
2441   gst_pad_set_chain_function (rtpsession->recv_rtcp_sink,
2442       gst_rtp_session_chain_recv_rtcp);
2443   gst_pad_set_event_function (rtpsession->recv_rtcp_sink,
2444       gst_rtp_session_event_recv_rtcp_sink);
2445   gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtcp_sink,
2446       gst_rtp_session_iterate_internal_links);
2447   gst_pad_set_active (rtpsession->recv_rtcp_sink, TRUE);
2448   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2449       rtpsession->recv_rtcp_sink);
2450
2451   GST_DEBUG_OBJECT (rtpsession, "creating sync src pad");
2452   rtpsession->sync_src =
2453       gst_pad_new_from_static_template (&rtpsession_sync_src_template,
2454       "sync_src");
2455   gst_pad_set_iterate_internal_links_function (rtpsession->sync_src,
2456       gst_rtp_session_iterate_internal_links);
2457   gst_pad_use_fixed_caps (rtpsession->sync_src);
2458   gst_pad_set_active (rtpsession->sync_src, TRUE);
2459   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
2460
2461   return rtpsession->recv_rtcp_sink;
2462 }
2463
2464 static void
2465 remove_recv_rtcp_sink (GstRtpSession * rtpsession)
2466 {
2467   GST_DEBUG_OBJECT (rtpsession, "removing RTCP sink pad");
2468
2469   gst_pad_set_active (rtpsession->sync_src, FALSE);
2470   gst_pad_set_active (rtpsession->recv_rtcp_sink, FALSE);
2471
2472   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2473       rtpsession->recv_rtcp_sink);
2474   rtpsession->recv_rtcp_sink = NULL;
2475
2476   GST_DEBUG_OBJECT (rtpsession, "removing sync src pad");
2477   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
2478   rtpsession->sync_src = NULL;
2479 }
2480
2481 /* Create a sinkpad to receive RTP packets for receivers. This will also create a
2482  * send_rtp_src pad.
2483  */
2484 static GstPad *
2485 create_send_rtp_sink (GstRtpSession * rtpsession)
2486 {
2487   GST_DEBUG_OBJECT (rtpsession, "creating pad");
2488
2489   rtpsession->send_rtp_sink =
2490       gst_pad_new_from_static_template (&rtpsession_send_rtp_sink_template,
2491       "send_rtp_sink");
2492   gst_pad_set_chain_function (rtpsession->send_rtp_sink,
2493       gst_rtp_session_chain_send_rtp);
2494   gst_pad_set_chain_list_function (rtpsession->send_rtp_sink,
2495       gst_rtp_session_chain_send_rtp_list);
2496   gst_pad_set_query_function (rtpsession->send_rtp_sink,
2497       gst_rtp_session_query_send_rtp);
2498   gst_pad_set_event_function (rtpsession->send_rtp_sink,
2499       gst_rtp_session_event_send_rtp_sink);
2500   gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_sink,
2501       gst_rtp_session_iterate_internal_links);
2502   GST_PAD_SET_PROXY_CAPS (rtpsession->send_rtp_sink);
2503   GST_PAD_SET_PROXY_ALLOCATION (rtpsession->send_rtp_sink);
2504   gst_pad_set_active (rtpsession->send_rtp_sink, TRUE);
2505   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2506       rtpsession->send_rtp_sink);
2507
2508   rtpsession->send_rtp_src =
2509       gst_pad_new_from_static_template (&rtpsession_send_rtp_src_template,
2510       "send_rtp_src");
2511   gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_src,
2512       gst_rtp_session_iterate_internal_links);
2513   gst_pad_set_event_function (rtpsession->send_rtp_src,
2514       gst_rtp_session_event_send_rtp_src);
2515   GST_PAD_SET_PROXY_CAPS (rtpsession->send_rtp_src);
2516   gst_pad_set_active (rtpsession->send_rtp_src, TRUE);
2517   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->send_rtp_src);
2518
2519   return rtpsession->send_rtp_sink;
2520 }
2521
2522 static void
2523 remove_send_rtp_sink (GstRtpSession * rtpsession)
2524 {
2525   GST_DEBUG_OBJECT (rtpsession, "removing pad");
2526
2527   gst_pad_set_active (rtpsession->send_rtp_src, FALSE);
2528   gst_pad_set_active (rtpsession->send_rtp_sink, FALSE);
2529
2530   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2531       rtpsession->send_rtp_sink);
2532   rtpsession->send_rtp_sink = NULL;
2533
2534   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2535       rtpsession->send_rtp_src);
2536   rtpsession->send_rtp_src = NULL;
2537 }
2538
2539 /* Create a srcpad with the RTCP packets to send out.
2540  * This pad will be driven by the RTP session manager when it wants to send out
2541  * RTCP packets.
2542  */
2543 static GstPad *
2544 create_send_rtcp_src (GstRtpSession * rtpsession)
2545 {
2546   GST_DEBUG_OBJECT (rtpsession, "creating pad");
2547
2548   rtpsession->send_rtcp_src =
2549       gst_pad_new_from_static_template (&rtpsession_send_rtcp_src_template,
2550       "send_rtcp_src");
2551   gst_pad_use_fixed_caps (rtpsession->send_rtcp_src);
2552   gst_pad_set_active (rtpsession->send_rtcp_src, TRUE);
2553   gst_pad_set_iterate_internal_links_function (rtpsession->send_rtcp_src,
2554       gst_rtp_session_iterate_internal_links);
2555   gst_pad_set_query_function (rtpsession->send_rtcp_src,
2556       gst_rtp_session_query_send_rtcp_src);
2557   gst_pad_set_event_function (rtpsession->send_rtcp_src,
2558       gst_rtp_session_event_send_rtcp_src);
2559   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2560       rtpsession->send_rtcp_src);
2561
2562   return rtpsession->send_rtcp_src;
2563 }
2564
2565 static void
2566 remove_send_rtcp_src (GstRtpSession * rtpsession)
2567 {
2568   GST_DEBUG_OBJECT (rtpsession, "removing pad");
2569
2570   gst_pad_set_active (rtpsession->send_rtcp_src, FALSE);
2571
2572   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2573       rtpsession->send_rtcp_src);
2574   rtpsession->send_rtcp_src = NULL;
2575 }
2576
2577 static GstPad *
2578 gst_rtp_session_request_new_pad (GstElement * element,
2579     GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
2580 {
2581   GstRtpSession *rtpsession;
2582   GstElementClass *klass;
2583   GstPad *result;
2584
2585   g_return_val_if_fail (templ != NULL, NULL);
2586   g_return_val_if_fail (GST_IS_RTP_SESSION (element), NULL);
2587
2588   rtpsession = GST_RTP_SESSION (element);
2589   klass = GST_ELEMENT_GET_CLASS (element);
2590
2591   GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
2592
2593   GST_RTP_SESSION_LOCK (rtpsession);
2594
2595   /* figure out the template */
2596   if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink")) {
2597     if (rtpsession->recv_rtp_sink != NULL)
2598       goto exists;
2599
2600     result = create_recv_rtp_sink (rtpsession);
2601   } else if (templ == gst_element_class_get_pad_template (klass,
2602           "recv_rtcp_sink")) {
2603     if (rtpsession->recv_rtcp_sink != NULL)
2604       goto exists;
2605
2606     result = create_recv_rtcp_sink (rtpsession);
2607   } else if (templ == gst_element_class_get_pad_template (klass,
2608           "send_rtp_sink")) {
2609     if (rtpsession->send_rtp_sink != NULL)
2610       goto exists;
2611
2612     result = create_send_rtp_sink (rtpsession);
2613   } else if (templ == gst_element_class_get_pad_template (klass,
2614           "send_rtcp_src")) {
2615     if (rtpsession->send_rtcp_src != NULL)
2616       goto exists;
2617
2618     result = create_send_rtcp_src (rtpsession);
2619   } else
2620     goto wrong_template;
2621
2622   GST_RTP_SESSION_UNLOCK (rtpsession);
2623
2624   return result;
2625
2626   /* ERRORS */
2627 wrong_template:
2628   {
2629     GST_RTP_SESSION_UNLOCK (rtpsession);
2630     g_warning ("rtpsession: this is not our template");
2631     return NULL;
2632   }
2633 exists:
2634   {
2635     GST_RTP_SESSION_UNLOCK (rtpsession);
2636     g_warning ("rtpsession: pad already requested");
2637     return NULL;
2638   }
2639 }
2640
2641 static void
2642 gst_rtp_session_release_pad (GstElement * element, GstPad * pad)
2643 {
2644   GstRtpSession *rtpsession;
2645
2646   g_return_if_fail (GST_IS_RTP_SESSION (element));
2647   g_return_if_fail (GST_IS_PAD (pad));
2648
2649   rtpsession = GST_RTP_SESSION (element);
2650
2651   GST_DEBUG_OBJECT (element, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2652
2653   GST_RTP_SESSION_LOCK (rtpsession);
2654
2655   if (rtpsession->recv_rtp_sink == pad) {
2656     remove_recv_rtp_sink (rtpsession);
2657   } else if (rtpsession->recv_rtcp_sink == pad) {
2658     remove_recv_rtcp_sink (rtpsession);
2659   } else if (rtpsession->send_rtp_sink == pad) {
2660     remove_send_rtp_sink (rtpsession);
2661   } else if (rtpsession->send_rtcp_src == pad) {
2662     remove_send_rtcp_src (rtpsession);
2663   } else
2664     goto wrong_pad;
2665
2666   GST_RTP_SESSION_UNLOCK (rtpsession);
2667
2668   return;
2669
2670   /* ERRORS */
2671 wrong_pad:
2672   {
2673     GST_RTP_SESSION_UNLOCK (rtpsession);
2674     g_warning ("rtpsession: asked to release an unknown pad");
2675     return;
2676   }
2677 }
2678
2679 static void
2680 gst_rtp_session_request_key_unit (RTPSession * sess,
2681     guint32 ssrc, gboolean all_headers, gpointer user_data)
2682 {
2683   GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2684   GstEvent *event;
2685   GstPad *send_rtp_sink;
2686
2687   GST_RTP_SESSION_LOCK (rtpsession);
2688   if ((send_rtp_sink = rtpsession->send_rtp_sink))
2689     gst_object_ref (send_rtp_sink);
2690   GST_RTP_SESSION_UNLOCK (rtpsession);
2691
2692   if (send_rtp_sink) {
2693     event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
2694         gst_structure_new ("GstForceKeyUnit", "ssrc", G_TYPE_UINT, ssrc,
2695             "all-headers", G_TYPE_BOOLEAN, all_headers, NULL));
2696     gst_pad_push_event (send_rtp_sink, event);
2697     gst_object_unref (send_rtp_sink);
2698   }
2699 }
2700
2701 static GstClockTime
2702 gst_rtp_session_request_time (RTPSession * session, gpointer user_data)
2703 {
2704   GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2705
2706   return gst_clock_get_time (rtpsession->priv->sysclock);
2707 }
2708
2709 static void
2710 gst_rtp_session_notify_nack (RTPSession * sess, guint16 seqnum,
2711     guint16 blp, guint32 ssrc, gpointer user_data)
2712 {
2713   GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2714   GstEvent *event;
2715   GstPad *send_rtp_sink;
2716
2717   GST_RTP_SESSION_LOCK (rtpsession);
2718   if ((send_rtp_sink = rtpsession->send_rtp_sink))
2719     gst_object_ref (send_rtp_sink);
2720   GST_RTP_SESSION_UNLOCK (rtpsession);
2721
2722   if (send_rtp_sink) {
2723     while (TRUE) {
2724       event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
2725           gst_structure_new ("GstRTPRetransmissionRequest",
2726               "seqnum", G_TYPE_UINT, (guint) seqnum,
2727               "ssrc", G_TYPE_UINT, (guint) ssrc, NULL));
2728       gst_pad_push_event (send_rtp_sink, event);
2729
2730       GST_RTP_SESSION_LOCK (rtpsession);
2731       rtpsession->priv->sent_rtx_req_count++;
2732       GST_RTP_SESSION_UNLOCK (rtpsession);
2733
2734       if (blp == 0)
2735         break;
2736
2737       seqnum++;
2738       while ((blp & 1) == 0) {
2739         seqnum++;
2740         blp >>= 1;
2741       }
2742       blp >>= 1;
2743     }
2744     gst_object_unref (send_rtp_sink);
2745   }
2746 }
2747
2748 static void
2749 gst_rtp_session_reconfigure (RTPSession * sess, gpointer user_data)
2750 {
2751   GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2752   GstPad *send_rtp_sink;
2753
2754   GST_RTP_SESSION_LOCK (rtpsession);
2755   if ((send_rtp_sink = rtpsession->send_rtp_sink))
2756     gst_object_ref (send_rtp_sink);
2757   GST_RTP_SESSION_UNLOCK (rtpsession);
2758
2759   if (send_rtp_sink) {
2760     gst_pad_push_event (send_rtp_sink, gst_event_new_reconfigure ());
2761     gst_object_unref (send_rtp_sink);
2762   }
2763 }
2764
2765 static void
2766 gst_rtp_session_notify_early_rtcp (RTPSession * sess, gpointer user_data)
2767 {
2768   GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2769
2770   GST_DEBUG_OBJECT (rtpsession, "Notified of early RTCP");
2771   /* with an early RTCP request, we might have to start the RTCP thread */
2772   GST_RTP_SESSION_LOCK (rtpsession);
2773   signal_waiting_rtcp_thread_unlocked (rtpsession);
2774   GST_RTP_SESSION_UNLOCK (rtpsession);
2775 }