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