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