upload tizen1.0 source
[framework/multimedia/gst-plugins-good0.10.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_pad_template (element_class,
377       gst_static_pad_template_get (&rtpsession_recv_rtp_sink_template));
378   gst_element_class_add_pad_template (element_class,
379       gst_static_pad_template_get (&rtpsession_recv_rtcp_sink_template));
380   gst_element_class_add_pad_template (element_class,
381       gst_static_pad_template_get (&rtpsession_send_rtp_sink_template));
382
383   /* src pads */
384   gst_element_class_add_pad_template (element_class,
385       gst_static_pad_template_get (&rtpsession_recv_rtp_src_template));
386   gst_element_class_add_pad_template (element_class,
387       gst_static_pad_template_get (&rtpsession_sync_src_template));
388   gst_element_class_add_pad_template (element_class,
389       gst_static_pad_template_get (&rtpsession_send_rtp_src_template));
390   gst_element_class_add_pad_template (element_class,
391       gst_static_pad_template_get (&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   while (!rtpsession->priv->stop_thread) {
841     GstClockReturn res;
842
843     /* get initial estimate */
844     next_timeout = rtp_session_next_timeout (session, current_time);
845
846     GST_DEBUG_OBJECT (rtpsession, "next check time %" GST_TIME_FORMAT,
847         GST_TIME_ARGS (next_timeout));
848
849     /* leave if no more timeouts, the session ended */
850     if (next_timeout == GST_CLOCK_TIME_NONE)
851       break;
852
853     id = rtpsession->priv->id =
854         gst_clock_new_single_shot_id (sysclock, next_timeout);
855     GST_RTP_SESSION_UNLOCK (rtpsession);
856
857     res = gst_clock_id_wait (id, NULL);
858
859     GST_RTP_SESSION_LOCK (rtpsession);
860     gst_clock_id_unref (id);
861     rtpsession->priv->id = NULL;
862
863     if (rtpsession->priv->stop_thread)
864       break;
865
866     /* update current time */
867     current_time = gst_clock_get_time (sysclock);
868
869     /* get current NTP time */
870     get_current_times (rtpsession, &running_time, &ntpnstime);
871
872     /* we get unlocked because we need to perform reconsideration, don't perform
873      * the timeout but get a new reporting estimate. */
874     GST_DEBUG_OBJECT (rtpsession, "unlocked %d, current %" GST_TIME_FORMAT,
875         res, GST_TIME_ARGS (current_time));
876
877     /* perform actions, we ignore result. Release lock because it might push. */
878     GST_RTP_SESSION_UNLOCK (rtpsession);
879     rtp_session_on_timeout (session, current_time, ntpnstime, running_time);
880     GST_RTP_SESSION_LOCK (rtpsession);
881   }
882   /* mark the thread as stopped now */
883   rtpsession->priv->thread_stopped = TRUE;
884   GST_RTP_SESSION_UNLOCK (rtpsession);
885
886   GST_DEBUG_OBJECT (rtpsession, "leaving RTCP thread");
887 }
888
889 static gboolean
890 start_rtcp_thread (GstRtpSession * rtpsession)
891 {
892   GError *error = NULL;
893   gboolean res;
894
895   GST_DEBUG_OBJECT (rtpsession, "starting RTCP thread");
896
897   GST_RTP_SESSION_LOCK (rtpsession);
898   rtpsession->priv->stop_thread = FALSE;
899   if (rtpsession->priv->thread_stopped) {
900     /* if the thread stopped, and we still have a handle to the thread, join it
901      * now. We can safely join with the lock held, the thread will not take it
902      * anymore. */
903     if (rtpsession->priv->thread)
904       g_thread_join (rtpsession->priv->thread);
905     /* only create a new thread if the old one was stopped. Otherwise we can
906      * just reuse the currently running one. */
907     rtpsession->priv->thread =
908         g_thread_create ((GThreadFunc) rtcp_thread, rtpsession, TRUE, &error);
909     rtpsession->priv->thread_stopped = FALSE;
910   }
911   GST_RTP_SESSION_UNLOCK (rtpsession);
912
913   if (error != NULL) {
914     res = FALSE;
915     GST_DEBUG_OBJECT (rtpsession, "failed to start thread, %s", error->message);
916     g_error_free (error);
917   } else {
918     res = TRUE;
919   }
920   return res;
921 }
922
923 static void
924 stop_rtcp_thread (GstRtpSession * rtpsession)
925 {
926   GST_DEBUG_OBJECT (rtpsession, "stopping RTCP thread");
927
928   GST_RTP_SESSION_LOCK (rtpsession);
929   rtpsession->priv->stop_thread = TRUE;
930   if (rtpsession->priv->id)
931     gst_clock_id_unschedule (rtpsession->priv->id);
932   GST_RTP_SESSION_UNLOCK (rtpsession);
933 }
934
935 static void
936 join_rtcp_thread (GstRtpSession * rtpsession)
937 {
938   GST_RTP_SESSION_LOCK (rtpsession);
939   /* don't try to join when we have no thread */
940   if (rtpsession->priv->thread != NULL) {
941     GST_DEBUG_OBJECT (rtpsession, "joining RTCP thread");
942     GST_RTP_SESSION_UNLOCK (rtpsession);
943
944     g_thread_join (rtpsession->priv->thread);
945
946     GST_RTP_SESSION_LOCK (rtpsession);
947     /* after the join, take the lock and clear the thread structure. The caller
948      * is supposed to not concurrently call start and join. */
949     rtpsession->priv->thread = NULL;
950   }
951   GST_RTP_SESSION_UNLOCK (rtpsession);
952 }
953
954 static GstStateChangeReturn
955 gst_rtp_session_change_state (GstElement * element, GstStateChange transition)
956 {
957   GstStateChangeReturn res;
958   GstRtpSession *rtpsession;
959
960   rtpsession = GST_RTP_SESSION (element);
961
962   switch (transition) {
963     case GST_STATE_CHANGE_NULL_TO_READY:
964       break;
965     case GST_STATE_CHANGE_READY_TO_PAUSED:
966       break;
967     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
968       break;
969     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
970     case GST_STATE_CHANGE_PAUSED_TO_READY:
971       /* no need to join yet, we might want to continue later. Also, the
972        * dataflow could block downstream so that a join could just block
973        * forever. */
974       stop_rtcp_thread (rtpsession);
975       break;
976     default:
977       break;
978   }
979
980   res = parent_class->change_state (element, transition);
981
982   switch (transition) {
983     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
984       if (!start_rtcp_thread (rtpsession))
985         goto failed_thread;
986       break;
987     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
988       break;
989     case GST_STATE_CHANGE_PAUSED_TO_READY:
990       /* downstream is now releasing the dataflow and we can join. */
991       join_rtcp_thread (rtpsession);
992       break;
993     case GST_STATE_CHANGE_READY_TO_NULL:
994       break;
995     default:
996       break;
997   }
998   return res;
999
1000   /* ERRORS */
1001 failed_thread:
1002   {
1003     return GST_STATE_CHANGE_FAILURE;
1004   }
1005 }
1006
1007 static gboolean
1008 return_true (gpointer key, gpointer value, gpointer user_data)
1009 {
1010   return TRUE;
1011 }
1012
1013 static void
1014 gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession)
1015 {
1016   g_hash_table_foreach_remove (rtpsession->priv->ptmap, return_true, NULL);
1017 }
1018
1019 /* called when the session manager has an RTP packet or a list of packets
1020  * ready for further processing */
1021 static GstFlowReturn
1022 gst_rtp_session_process_rtp (RTPSession * sess, RTPSource * src,
1023     GstBuffer * buffer, gpointer user_data)
1024 {
1025   GstFlowReturn result;
1026   GstRtpSession *rtpsession;
1027   GstPad *rtp_src;
1028
1029   rtpsession = GST_RTP_SESSION (user_data);
1030
1031   GST_RTP_SESSION_LOCK (rtpsession);
1032   if ((rtp_src = rtpsession->recv_rtp_src))
1033     gst_object_ref (rtp_src);
1034   GST_RTP_SESSION_UNLOCK (rtpsession);
1035
1036   if (rtp_src) {
1037     GST_LOG_OBJECT (rtpsession, "pushing received RTP packet");
1038     result = gst_pad_push (rtp_src, buffer);
1039     gst_object_unref (rtp_src);
1040   } else {
1041     GST_DEBUG_OBJECT (rtpsession, "dropping received RTP packet");
1042     gst_buffer_unref (buffer);
1043     result = GST_FLOW_OK;
1044   }
1045   return result;
1046 }
1047
1048 /* called when the session manager has an RTP packet ready for further
1049  * sending */
1050 static GstFlowReturn
1051 gst_rtp_session_send_rtp (RTPSession * sess, RTPSource * src,
1052     gpointer data, gpointer user_data)
1053 {
1054   GstFlowReturn result;
1055   GstRtpSession *rtpsession;
1056   GstPad *rtp_src;
1057
1058   rtpsession = GST_RTP_SESSION (user_data);
1059
1060   GST_RTP_SESSION_LOCK (rtpsession);
1061   if ((rtp_src = rtpsession->send_rtp_src))
1062     gst_object_ref (rtp_src);
1063   GST_RTP_SESSION_UNLOCK (rtpsession);
1064
1065   if (rtp_src) {
1066     if (GST_IS_BUFFER (data)) {
1067       GST_LOG_OBJECT (rtpsession, "sending RTP packet");
1068       result = gst_pad_push (rtp_src, GST_BUFFER_CAST (data));
1069     } else {
1070       GST_LOG_OBJECT (rtpsession, "sending RTP list");
1071       result = gst_pad_push_list (rtp_src, GST_BUFFER_LIST_CAST (data));
1072     }
1073     gst_object_unref (rtp_src);
1074   } else {
1075     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1076     result = GST_FLOW_OK;
1077   }
1078   return result;
1079 }
1080
1081 /* called when the session manager has an RTCP packet ready for further
1082  * sending. The eos flag is set when an EOS event should be sent downstream as
1083  * well. */
1084 static GstFlowReturn
1085 gst_rtp_session_send_rtcp (RTPSession * sess, RTPSource * src,
1086     GstBuffer * buffer, gboolean eos, gpointer user_data)
1087 {
1088   GstFlowReturn result;
1089   GstRtpSession *rtpsession;
1090   GstPad *rtcp_src;
1091
1092   rtpsession = GST_RTP_SESSION (user_data);
1093
1094   GST_RTP_SESSION_LOCK (rtpsession);
1095   if (rtpsession->priv->stop_thread)
1096     goto stopping;
1097
1098   if ((rtcp_src = rtpsession->send_rtcp_src)) {
1099     GstCaps *caps;
1100
1101     /* set rtcp caps on output pad */
1102     if (!(caps = GST_PAD_CAPS (rtcp_src))) {
1103       caps = gst_caps_new_simple ("application/x-rtcp", NULL);
1104       gst_pad_set_caps (rtcp_src, caps);
1105     } else
1106       gst_caps_ref (caps);
1107     gst_buffer_set_caps (buffer, caps);
1108     gst_caps_unref (caps);
1109
1110     gst_object_ref (rtcp_src);
1111     GST_RTP_SESSION_UNLOCK (rtpsession);
1112
1113     GST_LOG_OBJECT (rtpsession, "sending RTCP");
1114     result = gst_pad_push (rtcp_src, buffer);
1115
1116     /* we have to send EOS after this packet */
1117     if (eos) {
1118       GST_LOG_OBJECT (rtpsession, "sending EOS");
1119       gst_pad_push_event (rtcp_src, gst_event_new_eos ());
1120     }
1121     gst_object_unref (rtcp_src);
1122   } else {
1123     GST_RTP_SESSION_UNLOCK (rtpsession);
1124
1125     GST_DEBUG_OBJECT (rtpsession, "not sending RTCP, no output pad");
1126     gst_buffer_unref (buffer);
1127     result = GST_FLOW_OK;
1128   }
1129   return result;
1130
1131   /* ERRORS */
1132 stopping:
1133   {
1134     GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1135     gst_buffer_unref (buffer);
1136     GST_RTP_SESSION_UNLOCK (rtpsession);
1137     return GST_FLOW_OK;
1138   }
1139 }
1140
1141 /* called when the session manager has an SR RTCP packet ready for handling
1142  * inter stream synchronisation */
1143 static GstFlowReturn
1144 gst_rtp_session_sync_rtcp (RTPSession * sess, RTPSource * src,
1145     GstBuffer * buffer, gpointer user_data)
1146 {
1147   GstFlowReturn result;
1148   GstRtpSession *rtpsession;
1149   GstPad *sync_src;
1150
1151   rtpsession = GST_RTP_SESSION (user_data);
1152
1153   GST_RTP_SESSION_LOCK (rtpsession);
1154   if (rtpsession->priv->stop_thread)
1155     goto stopping;
1156
1157   if ((sync_src = rtpsession->sync_src)) {
1158     GstCaps *caps;
1159
1160     /* set rtcp caps on output pad */
1161     if (!(caps = GST_PAD_CAPS (sync_src))) {
1162       caps = gst_caps_new_simple ("application/x-rtcp", NULL);
1163       gst_pad_set_caps (sync_src, caps);
1164     } else
1165       gst_caps_ref (caps);
1166     gst_buffer_set_caps (buffer, caps);
1167     gst_caps_unref (caps);
1168
1169     gst_object_ref (sync_src);
1170     GST_RTP_SESSION_UNLOCK (rtpsession);
1171
1172     GST_LOG_OBJECT (rtpsession, "sending Sync RTCP");
1173     result = gst_pad_push (sync_src, buffer);
1174     gst_object_unref (sync_src);
1175   } else {
1176     GST_RTP_SESSION_UNLOCK (rtpsession);
1177
1178     GST_DEBUG_OBJECT (rtpsession, "not sending Sync RTCP, no output pad");
1179     gst_buffer_unref (buffer);
1180     result = GST_FLOW_OK;
1181   }
1182   return result;
1183
1184   /* ERRORS */
1185 stopping:
1186   {
1187     GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1188     gst_buffer_unref (buffer);
1189     GST_RTP_SESSION_UNLOCK (rtpsession);
1190     return GST_FLOW_OK;
1191   }
1192 }
1193
1194 static void
1195 gst_rtp_session_cache_caps (GstRtpSession * rtpsession, GstCaps * caps)
1196 {
1197   GstRtpSessionPrivate *priv;
1198   const GstStructure *s;
1199   gint payload;
1200
1201   priv = rtpsession->priv;
1202
1203   GST_DEBUG_OBJECT (rtpsession, "parsing caps");
1204
1205   s = gst_caps_get_structure (caps, 0);
1206   if (!gst_structure_get_int (s, "payload", &payload))
1207     return;
1208
1209   if (g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (payload)))
1210     return;
1211
1212   g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (payload),
1213       gst_caps_ref (caps));
1214 }
1215
1216 static GstCaps *
1217 gst_rtp_session_get_caps_for_pt (GstRtpSession * rtpsession, guint payload)
1218 {
1219   GstCaps *caps = NULL;
1220   GValue args[2] = { {0}, {0} };
1221   GValue ret = { 0 };
1222
1223   GST_RTP_SESSION_LOCK (rtpsession);
1224   caps = g_hash_table_lookup (rtpsession->priv->ptmap,
1225       GINT_TO_POINTER (payload));
1226   if (caps) {
1227     gst_caps_ref (caps);
1228     goto done;
1229   }
1230
1231   /* not found in the cache, try to get it with a signal */
1232   g_value_init (&args[0], GST_TYPE_ELEMENT);
1233   g_value_set_object (&args[0], rtpsession);
1234   g_value_init (&args[1], G_TYPE_UINT);
1235   g_value_set_uint (&args[1], payload);
1236
1237   g_value_init (&ret, GST_TYPE_CAPS);
1238   g_value_set_boxed (&ret, NULL);
1239
1240   GST_RTP_SESSION_UNLOCK (rtpsession);
1241
1242   g_signal_emitv (args, gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP], 0,
1243       &ret);
1244
1245   GST_RTP_SESSION_LOCK (rtpsession);
1246
1247   g_value_unset (&args[0]);
1248   g_value_unset (&args[1]);
1249   caps = (GstCaps *) g_value_dup_boxed (&ret);
1250   g_value_unset (&ret);
1251   if (!caps)
1252     goto no_caps;
1253
1254   gst_rtp_session_cache_caps (rtpsession, caps);
1255
1256 done:
1257   GST_RTP_SESSION_UNLOCK (rtpsession);
1258
1259   return caps;
1260
1261 no_caps:
1262   {
1263     GST_DEBUG_OBJECT (rtpsession, "could not get caps");
1264     goto done;
1265   }
1266 }
1267
1268 /* called when the session manager needs the clock rate */
1269 static gint
1270 gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
1271     gpointer user_data)
1272 {
1273   gint result = -1;
1274   GstRtpSession *rtpsession;
1275   GstCaps *caps;
1276   const GstStructure *s;
1277
1278   rtpsession = GST_RTP_SESSION_CAST (user_data);
1279
1280   caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1281
1282   if (!caps)
1283     goto done;
1284
1285   s = gst_caps_get_structure (caps, 0);
1286   if (!gst_structure_get_int (s, "clock-rate", &result))
1287     goto no_clock_rate;
1288
1289   gst_caps_unref (caps);
1290
1291   GST_DEBUG_OBJECT (rtpsession, "parsed clock-rate %d", result);
1292
1293 done:
1294
1295   return result;
1296
1297   /* ERRORS */
1298 no_clock_rate:
1299   {
1300     gst_caps_unref (caps);
1301     GST_DEBUG_OBJECT (rtpsession, "No clock-rate in caps!");
1302     goto done;
1303   }
1304 }
1305
1306 /* called when the session manager asks us to reconsider the timeout */
1307 static void
1308 gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data)
1309 {
1310   GstRtpSession *rtpsession;
1311
1312   rtpsession = GST_RTP_SESSION_CAST (user_data);
1313
1314   GST_RTP_SESSION_LOCK (rtpsession);
1315   GST_DEBUG_OBJECT (rtpsession, "unlock timer for reconsideration");
1316   if (rtpsession->priv->id)
1317     gst_clock_id_unschedule (rtpsession->priv->id);
1318   GST_RTP_SESSION_UNLOCK (rtpsession);
1319 }
1320
1321 static gboolean
1322 gst_rtp_session_event_recv_rtp_sink (GstPad * pad, GstEvent * event)
1323 {
1324   GstRtpSession *rtpsession;
1325   gboolean ret = FALSE;
1326
1327   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1328   if (G_UNLIKELY (rtpsession == NULL)) {
1329     gst_event_unref (event);
1330     return FALSE;
1331   }
1332
1333   GST_DEBUG_OBJECT (rtpsession, "received event %s",
1334       GST_EVENT_TYPE_NAME (event));
1335
1336   switch (GST_EVENT_TYPE (event)) {
1337     case GST_EVENT_FLUSH_STOP:
1338       gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
1339       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1340       break;
1341     case GST_EVENT_NEWSEGMENT:
1342     {
1343       gboolean update;
1344       gdouble rate, arate;
1345       GstFormat format;
1346       gint64 start, stop, time;
1347       GstSegment *segment;
1348
1349       segment = &rtpsession->recv_rtp_seg;
1350
1351       /* the newsegment event is needed to convert the RTP timestamp to
1352        * running_time, which is needed to generate a mapping from RTP to NTP
1353        * timestamps in SR reports */
1354       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1355           &start, &stop, &time);
1356
1357       GST_DEBUG_OBJECT (rtpsession,
1358           "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1359           "format GST_FORMAT_TIME, "
1360           "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1361           ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1362           update, rate, arate, GST_TIME_ARGS (segment->start),
1363           GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1364           GST_TIME_ARGS (segment->accum));
1365
1366       gst_segment_set_newsegment_full (segment, update, rate,
1367           arate, format, start, stop, time);
1368
1369       /* push event forward */
1370       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1371       break;
1372     }
1373     default:
1374       ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1375       break;
1376   }
1377   gst_object_unref (rtpsession);
1378
1379   return ret;
1380
1381 }
1382
1383 static gboolean
1384 gst_rtp_session_request_remote_key_unit (GstRtpSession * rtpsession,
1385     guint32 ssrc, guint payload, gboolean all_headers)
1386 {
1387   GstCaps *caps;
1388   gboolean requested = FALSE;
1389
1390   caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1391
1392   if (caps) {
1393     const GstStructure *s = gst_caps_get_structure (caps, 0);
1394     gboolean pli;
1395
1396     pli = gst_structure_has_field (s, "rtcp-fb-nack-pli");
1397
1398     gst_caps_unref (caps);
1399
1400     if (pli) {
1401       rtp_session_request_key_unit (rtpsession->priv->session, ssrc);
1402       rtp_session_request_early_rtcp (rtpsession->priv->session,
1403           gst_clock_get_time (rtpsession->priv->sysclock), 200 * GST_MSECOND);
1404       requested = TRUE;
1405     }
1406   }
1407
1408   return requested;
1409 }
1410
1411 static gboolean
1412 gst_rtp_session_event_recv_rtp_src (GstPad * pad, GstEvent * event)
1413 {
1414   GstRtpSession *rtpsession;
1415   gboolean forward = TRUE;
1416   gboolean ret = TRUE;
1417   const GstStructure *s;
1418   guint32 ssrc;
1419   guint pt;
1420
1421   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1422   if (G_UNLIKELY (rtpsession == NULL)) {
1423     gst_event_unref (event);
1424     return FALSE;
1425   }
1426
1427   switch (GST_EVENT_TYPE (event)) {
1428     case GST_EVENT_CUSTOM_UPSTREAM:
1429       s = gst_event_get_structure (event);
1430       if (gst_structure_has_name (s, "GstForceKeyUnit") &&
1431           gst_structure_get_uint (s, "ssrc", &ssrc) &&
1432           gst_structure_get_uint (s, "payload", &pt)) {
1433         gboolean all_headers = FALSE;
1434
1435         gst_structure_get_boolean (s, "all-headers", &all_headers);
1436         if (gst_rtp_session_request_remote_key_unit (rtpsession, ssrc, pt,
1437                 all_headers))
1438           forward = FALSE;
1439       }
1440       break;
1441     default:
1442       break;
1443   }
1444
1445   if (forward)
1446     ret = gst_pad_push_event (rtpsession->recv_rtp_sink, event);
1447
1448   gst_object_unref (rtpsession);
1449
1450   return ret;
1451 }
1452
1453
1454 static GstIterator *
1455 gst_rtp_session_iterate_internal_links (GstPad * pad)
1456 {
1457   GstRtpSession *rtpsession;
1458   GstPad *otherpad = NULL;
1459   GstIterator *it = NULL;
1460
1461   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1462   if (G_UNLIKELY (rtpsession == NULL))
1463     return NULL;
1464
1465   GST_RTP_SESSION_LOCK (rtpsession);
1466   if (pad == rtpsession->recv_rtp_src) {
1467     otherpad = gst_object_ref (rtpsession->recv_rtp_sink);
1468   } else if (pad == rtpsession->recv_rtp_sink) {
1469     otherpad = gst_object_ref (rtpsession->recv_rtp_src);
1470   } else if (pad == rtpsession->send_rtp_src) {
1471     otherpad = gst_object_ref (rtpsession->send_rtp_sink);
1472   } else if (pad == rtpsession->send_rtp_sink) {
1473     otherpad = gst_object_ref (rtpsession->send_rtp_src);
1474   }
1475   GST_RTP_SESSION_UNLOCK (rtpsession);
1476
1477   if (otherpad) {
1478     it = gst_iterator_new_single (GST_TYPE_PAD, otherpad,
1479         (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
1480     gst_object_unref (otherpad);
1481   }
1482
1483   gst_object_unref (rtpsession);
1484
1485   return it;
1486 }
1487
1488 static gboolean
1489 gst_rtp_session_sink_setcaps (GstPad * pad, GstCaps * caps)
1490 {
1491   GstRtpSession *rtpsession;
1492
1493   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1494
1495   GST_RTP_SESSION_LOCK (rtpsession);
1496   gst_rtp_session_cache_caps (rtpsession, caps);
1497   GST_RTP_SESSION_UNLOCK (rtpsession);
1498
1499   gst_object_unref (rtpsession);
1500
1501   return TRUE;
1502 }
1503
1504 /* receive a packet from a sender, send it to the RTP session manager and
1505  * forward the packet on the rtp_src pad
1506  */
1507 static GstFlowReturn
1508 gst_rtp_session_chain_recv_rtp (GstPad * pad, GstBuffer * buffer)
1509 {
1510   GstRtpSession *rtpsession;
1511   GstRtpSessionPrivate *priv;
1512   GstFlowReturn ret;
1513   GstClockTime current_time, running_time;
1514   GstClockTime timestamp;
1515
1516   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1517   priv = rtpsession->priv;
1518
1519   GST_LOG_OBJECT (rtpsession, "received RTP packet");
1520
1521   /* get NTP time when this packet was captured, this depends on the timestamp. */
1522   timestamp = GST_BUFFER_TIMESTAMP (buffer);
1523   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1524     /* convert to running time using the segment values */
1525     running_time =
1526         gst_segment_to_running_time (&rtpsession->recv_rtp_seg, GST_FORMAT_TIME,
1527         timestamp);
1528   } else {
1529     get_current_times (rtpsession, &running_time, NULL);
1530   }
1531   current_time = gst_clock_get_time (priv->sysclock);
1532
1533   ret = rtp_session_process_rtp (priv->session, buffer, current_time,
1534       running_time);
1535   if (ret != GST_FLOW_OK)
1536     goto push_error;
1537
1538 done:
1539   gst_object_unref (rtpsession);
1540
1541   return ret;
1542
1543   /* ERRORS */
1544 push_error:
1545   {
1546     GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1547         gst_flow_get_name (ret));
1548     goto done;
1549   }
1550 }
1551
1552 static gboolean
1553 gst_rtp_session_event_recv_rtcp_sink (GstPad * pad, GstEvent * event)
1554 {
1555   GstRtpSession *rtpsession;
1556   gboolean ret = FALSE;
1557
1558   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1559
1560   GST_DEBUG_OBJECT (rtpsession, "received event %s",
1561       GST_EVENT_TYPE_NAME (event));
1562
1563   switch (GST_EVENT_TYPE (event)) {
1564     default:
1565       ret = gst_pad_push_event (rtpsession->sync_src, event);
1566       break;
1567   }
1568   gst_object_unref (rtpsession);
1569
1570   return ret;
1571 }
1572
1573 /* Receive an RTCP packet from a sender, send it to the RTP session manager and
1574  * forward the SR packets to the sync_src pad.
1575  */
1576 static GstFlowReturn
1577 gst_rtp_session_chain_recv_rtcp (GstPad * pad, GstBuffer * buffer)
1578 {
1579   GstRtpSession *rtpsession;
1580   GstRtpSessionPrivate *priv;
1581   GstClockTime current_time;
1582   guint64 ntpnstime;
1583
1584   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1585   priv = rtpsession->priv;
1586
1587   GST_LOG_OBJECT (rtpsession, "received RTCP packet");
1588
1589   current_time = gst_clock_get_time (priv->sysclock);
1590   get_current_times (rtpsession, NULL, &ntpnstime);
1591
1592   rtp_session_process_rtcp (priv->session, buffer, current_time, ntpnstime);
1593
1594   gst_object_unref (rtpsession);
1595
1596   return GST_FLOW_OK;           /* always return OK */
1597 }
1598
1599 static gboolean
1600 gst_rtp_session_query_send_rtcp_src (GstPad * pad, GstQuery * query)
1601 {
1602   GstRtpSession *rtpsession;
1603   gboolean ret = FALSE;
1604
1605   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1606
1607   GST_DEBUG_OBJECT (rtpsession, "received QUERY");
1608
1609   switch (GST_QUERY_TYPE (query)) {
1610     case GST_QUERY_LATENCY:
1611       ret = TRUE;
1612       /* use the defaults for the latency query. */
1613       gst_query_set_latency (query, FALSE, 0, -1);
1614       break;
1615     default:
1616       /* other queries simply fail for now */
1617       break;
1618   }
1619
1620   gst_object_unref (rtpsession);
1621
1622   return ret;
1623 }
1624
1625 static gboolean
1626 gst_rtp_session_event_send_rtcp_src (GstPad * pad, GstEvent * event)
1627 {
1628   GstRtpSession *rtpsession;
1629   gboolean ret = TRUE;
1630
1631   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1632   if (G_UNLIKELY (rtpsession == NULL)) {
1633     gst_event_unref (event);
1634     return FALSE;
1635   }
1636   GST_DEBUG_OBJECT (rtpsession, "received EVENT");
1637
1638   switch (GST_EVENT_TYPE (event)) {
1639     case GST_EVENT_SEEK:
1640     case GST_EVENT_LATENCY:
1641       gst_event_unref (event);
1642       ret = TRUE;
1643       break;
1644     default:
1645       /* other events simply fail for now */
1646       gst_event_unref (event);
1647       ret = FALSE;
1648       break;
1649   }
1650
1651   gst_object_unref (rtpsession);
1652   return ret;
1653 }
1654
1655
1656 static gboolean
1657 gst_rtp_session_event_send_rtp_sink (GstPad * pad, GstEvent * event)
1658 {
1659   GstRtpSession *rtpsession;
1660   gboolean ret = FALSE;
1661
1662   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1663
1664   GST_DEBUG_OBJECT (rtpsession, "received event");
1665
1666   switch (GST_EVENT_TYPE (event)) {
1667     case GST_EVENT_FLUSH_STOP:
1668       gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
1669       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1670       break;
1671     case GST_EVENT_NEWSEGMENT:{
1672       gboolean update;
1673       gdouble rate, arate;
1674       GstFormat format;
1675       gint64 start, stop, time;
1676       GstSegment *segment;
1677
1678       segment = &rtpsession->send_rtp_seg;
1679
1680       /* the newsegment event is needed to convert the RTP timestamp to
1681        * running_time, which is needed to generate a mapping from RTP to NTP
1682        * timestamps in SR reports */
1683       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1684           &start, &stop, &time);
1685
1686       GST_DEBUG_OBJECT (rtpsession,
1687           "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1688           "format GST_FORMAT_TIME, "
1689           "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1690           ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1691           update, rate, arate, GST_TIME_ARGS (segment->start),
1692           GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1693           GST_TIME_ARGS (segment->accum));
1694
1695       gst_segment_set_newsegment_full (segment, update, rate,
1696           arate, format, start, stop, time);
1697
1698       /* push event forward */
1699       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1700       break;
1701     }
1702     case GST_EVENT_EOS:{
1703       GstClockTime current_time;
1704
1705       /* push downstream FIXME, we are not supposed to leave the session just
1706        * because we stop sending. */
1707       ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1708       current_time = gst_clock_get_time (rtpsession->priv->sysclock);
1709       GST_DEBUG_OBJECT (rtpsession, "scheduling BYE message");
1710       rtp_session_schedule_bye (rtpsession->priv->session, "End of stream",
1711           current_time);
1712       break;
1713     }
1714     default:{
1715       GstPad *send_rtp_src = NULL;
1716       GST_RTP_SESSION_LOCK (rtpsession);
1717       if (rtpsession->send_rtp_src)
1718         send_rtp_src = gst_object_ref (rtpsession->send_rtp_src);
1719       GST_RTP_SESSION_UNLOCK (rtpsession);
1720
1721       if (send_rtp_src) {
1722         ret = gst_pad_push_event (send_rtp_src, event);
1723         gst_object_unref (send_rtp_src);
1724       } else
1725         gst_event_unref (event);
1726
1727       break;
1728     }
1729   }
1730   gst_object_unref (rtpsession);
1731
1732   return ret;
1733 }
1734
1735 static GstCaps *
1736 gst_rtp_session_getcaps_send_rtp (GstPad * pad)
1737 {
1738   GstRtpSession *rtpsession;
1739   GstRtpSessionPrivate *priv;
1740   GstCaps *result;
1741   GstStructure *s1, *s2;
1742   guint ssrc;
1743
1744   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1745   priv = rtpsession->priv;
1746
1747   ssrc = rtp_session_get_internal_ssrc (priv->session);
1748
1749   /* we can basically accept anything but we prefer to receive packets with our
1750    * internal SSRC so that we don't have to patch it. Create a structure with
1751    * the SSRC and another one without. */
1752   s1 = gst_structure_new ("application/x-rtp", "ssrc", G_TYPE_UINT, ssrc, NULL);
1753   s2 = gst_structure_new ("application/x-rtp", NULL);
1754
1755   result = gst_caps_new_full (s1, s2, NULL);
1756
1757   GST_DEBUG_OBJECT (rtpsession, "getting caps %" GST_PTR_FORMAT, result);
1758
1759   gst_object_unref (rtpsession);
1760
1761   return result;
1762 }
1763
1764 static gboolean
1765 gst_rtp_session_setcaps_send_rtp (GstPad * pad, GstCaps * caps)
1766 {
1767   GstRtpSession *rtpsession;
1768   GstRtpSessionPrivate *priv;
1769   GstStructure *s = gst_caps_get_structure (caps, 0);
1770   guint ssrc;
1771
1772   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1773   priv = rtpsession->priv;
1774
1775   if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
1776     GST_DEBUG_OBJECT (rtpsession, "setting internal SSRC to %08x", ssrc);
1777     rtp_session_set_internal_ssrc (priv->session, ssrc);
1778   }
1779
1780   gst_object_unref (rtpsession);
1781
1782   return TRUE;
1783 }
1784
1785 /* Recieve an RTP packet or a list of packets to be send to the receivers,
1786  * send to RTP session manager and forward to send_rtp_src.
1787  */
1788 static GstFlowReturn
1789 gst_rtp_session_chain_send_rtp_common (GstPad * pad, gpointer data,
1790     gboolean is_list)
1791 {
1792   GstRtpSession *rtpsession;
1793   GstRtpSessionPrivate *priv;
1794   GstFlowReturn ret;
1795   GstClockTime timestamp, running_time;
1796   GstClockTime current_time;
1797
1798   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1799   priv = rtpsession->priv;
1800
1801   GST_LOG_OBJECT (rtpsession, "received RTP %s", is_list ? "list" : "packet");
1802
1803   /* get NTP time when this packet was captured, this depends on the timestamp. */
1804   if (is_list) {
1805     GstBuffer *buffer = NULL;
1806
1807     /* All groups in an list have the same timestamp.
1808      * So, just take it from the first group. */
1809     buffer = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0, 0);
1810     if (buffer)
1811       timestamp = GST_BUFFER_TIMESTAMP (buffer);
1812     else
1813       timestamp = -1;
1814   } else {
1815     timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (data));
1816   }
1817
1818   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1819     /* convert to running time using the segment start value. */
1820     running_time =
1821         gst_segment_to_running_time (&rtpsession->send_rtp_seg, GST_FORMAT_TIME,
1822         timestamp);
1823   } else {
1824     /* no timestamp. */
1825     running_time = -1;
1826   }
1827
1828   current_time = gst_clock_get_time (priv->sysclock);
1829   ret = rtp_session_send_rtp (priv->session, data, is_list, current_time,
1830       running_time);
1831   if (ret != GST_FLOW_OK)
1832     goto push_error;
1833
1834 done:
1835   gst_object_unref (rtpsession);
1836
1837   return ret;
1838
1839   /* ERRORS */
1840 push_error:
1841   {
1842     GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1843         gst_flow_get_name (ret));
1844     goto done;
1845   }
1846 }
1847
1848 static GstFlowReturn
1849 gst_rtp_session_chain_send_rtp (GstPad * pad, GstBuffer * buffer)
1850 {
1851   return gst_rtp_session_chain_send_rtp_common (pad, buffer, FALSE);
1852 }
1853
1854 static GstFlowReturn
1855 gst_rtp_session_chain_send_rtp_list (GstPad * pad, GstBufferList * list)
1856 {
1857   return gst_rtp_session_chain_send_rtp_common (pad, list, TRUE);
1858 }
1859
1860 /* Create sinkpad to receive RTP packets from senders. This will also create a
1861  * srcpad for the RTP packets.
1862  */
1863 static GstPad *
1864 create_recv_rtp_sink (GstRtpSession * rtpsession)
1865 {
1866   GST_DEBUG_OBJECT (rtpsession, "creating RTP sink pad");
1867
1868   rtpsession->recv_rtp_sink =
1869       gst_pad_new_from_static_template (&rtpsession_recv_rtp_sink_template,
1870       "recv_rtp_sink");
1871   gst_pad_set_chain_function (rtpsession->recv_rtp_sink,
1872       gst_rtp_session_chain_recv_rtp);
1873   gst_pad_set_event_function (rtpsession->recv_rtp_sink,
1874       (GstPadEventFunction) gst_rtp_session_event_recv_rtp_sink);
1875   gst_pad_set_setcaps_function (rtpsession->recv_rtp_sink,
1876       gst_rtp_session_sink_setcaps);
1877   gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_sink,
1878       gst_rtp_session_iterate_internal_links);
1879   gst_pad_set_active (rtpsession->recv_rtp_sink, TRUE);
1880   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1881       rtpsession->recv_rtp_sink);
1882
1883   GST_DEBUG_OBJECT (rtpsession, "creating RTP src pad");
1884   rtpsession->recv_rtp_src =
1885       gst_pad_new_from_static_template (&rtpsession_recv_rtp_src_template,
1886       "recv_rtp_src");
1887   gst_pad_set_event_function (rtpsession->recv_rtp_src,
1888       (GstPadEventFunction) gst_rtp_session_event_recv_rtp_src);
1889   gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_src,
1890       gst_rtp_session_iterate_internal_links);
1891   gst_pad_use_fixed_caps (rtpsession->recv_rtp_src);
1892   gst_pad_set_active (rtpsession->recv_rtp_src, TRUE);
1893   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->recv_rtp_src);
1894
1895   return rtpsession->recv_rtp_sink;
1896 }
1897
1898 /* Remove sinkpad to receive RTP packets from senders. This will also remove
1899  * the srcpad for the RTP packets.
1900  */
1901 static void
1902 remove_recv_rtp_sink (GstRtpSession * rtpsession)
1903 {
1904   GST_DEBUG_OBJECT (rtpsession, "removing RTP sink pad");
1905
1906   /* deactivate from source to sink */
1907   gst_pad_set_active (rtpsession->recv_rtp_src, FALSE);
1908   gst_pad_set_active (rtpsession->recv_rtp_sink, FALSE);
1909
1910   /* remove pads */
1911   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1912       rtpsession->recv_rtp_sink);
1913   rtpsession->recv_rtp_sink = NULL;
1914
1915   GST_DEBUG_OBJECT (rtpsession, "removing RTP src pad");
1916   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1917       rtpsession->recv_rtp_src);
1918   rtpsession->recv_rtp_src = NULL;
1919 }
1920
1921 /* Create a sinkpad to receive RTCP messages from senders, this will also create a
1922  * sync_src pad for the SR packets.
1923  */
1924 static GstPad *
1925 create_recv_rtcp_sink (GstRtpSession * rtpsession)
1926 {
1927   GST_DEBUG_OBJECT (rtpsession, "creating RTCP sink pad");
1928
1929   rtpsession->recv_rtcp_sink =
1930       gst_pad_new_from_static_template (&rtpsession_recv_rtcp_sink_template,
1931       "recv_rtcp_sink");
1932   gst_pad_set_chain_function (rtpsession->recv_rtcp_sink,
1933       gst_rtp_session_chain_recv_rtcp);
1934   gst_pad_set_event_function (rtpsession->recv_rtcp_sink,
1935       (GstPadEventFunction) gst_rtp_session_event_recv_rtcp_sink);
1936   gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtcp_sink,
1937       gst_rtp_session_iterate_internal_links);
1938   gst_pad_set_active (rtpsession->recv_rtcp_sink, TRUE);
1939   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1940       rtpsession->recv_rtcp_sink);
1941
1942   GST_DEBUG_OBJECT (rtpsession, "creating sync src pad");
1943   rtpsession->sync_src =
1944       gst_pad_new_from_static_template (&rtpsession_sync_src_template,
1945       "sync_src");
1946   gst_pad_set_iterate_internal_links_function (rtpsession->sync_src,
1947       gst_rtp_session_iterate_internal_links);
1948   gst_pad_use_fixed_caps (rtpsession->sync_src);
1949   gst_pad_set_active (rtpsession->sync_src, TRUE);
1950   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
1951
1952   return rtpsession->recv_rtcp_sink;
1953 }
1954
1955 static void
1956 remove_recv_rtcp_sink (GstRtpSession * rtpsession)
1957 {
1958   GST_DEBUG_OBJECT (rtpsession, "removing RTCP sink pad");
1959
1960   gst_pad_set_active (rtpsession->sync_src, FALSE);
1961   gst_pad_set_active (rtpsession->recv_rtcp_sink, FALSE);
1962
1963   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1964       rtpsession->recv_rtcp_sink);
1965   rtpsession->recv_rtcp_sink = NULL;
1966
1967   GST_DEBUG_OBJECT (rtpsession, "removing sync src pad");
1968   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
1969   rtpsession->sync_src = NULL;
1970 }
1971
1972 /* Create a sinkpad to receive RTP packets for receivers. This will also create a
1973  * send_rtp_src pad.
1974  */
1975 static GstPad *
1976 create_send_rtp_sink (GstRtpSession * rtpsession)
1977 {
1978   GST_DEBUG_OBJECT (rtpsession, "creating pad");
1979
1980   rtpsession->send_rtp_sink =
1981       gst_pad_new_from_static_template (&rtpsession_send_rtp_sink_template,
1982       "send_rtp_sink");
1983   gst_pad_set_chain_function (rtpsession->send_rtp_sink,
1984       gst_rtp_session_chain_send_rtp);
1985   gst_pad_set_chain_list_function (rtpsession->send_rtp_sink,
1986       gst_rtp_session_chain_send_rtp_list);
1987   gst_pad_set_getcaps_function (rtpsession->send_rtp_sink,
1988       gst_rtp_session_getcaps_send_rtp);
1989   gst_pad_set_setcaps_function (rtpsession->send_rtp_sink,
1990       gst_rtp_session_setcaps_send_rtp);
1991   gst_pad_set_event_function (rtpsession->send_rtp_sink,
1992       (GstPadEventFunction) gst_rtp_session_event_send_rtp_sink);
1993   gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_sink,
1994       gst_rtp_session_iterate_internal_links);
1995   gst_pad_set_active (rtpsession->send_rtp_sink, TRUE);
1996   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1997       rtpsession->send_rtp_sink);
1998
1999   rtpsession->send_rtp_src =
2000       gst_pad_new_from_static_template (&rtpsession_send_rtp_src_template,
2001       "send_rtp_src");
2002   gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_src,
2003       gst_rtp_session_iterate_internal_links);
2004   gst_pad_set_active (rtpsession->send_rtp_src, TRUE);
2005   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->send_rtp_src);
2006
2007   return rtpsession->send_rtp_sink;
2008 }
2009
2010 static void
2011 remove_send_rtp_sink (GstRtpSession * rtpsession)
2012 {
2013   GST_DEBUG_OBJECT (rtpsession, "removing pad");
2014
2015   gst_pad_set_active (rtpsession->send_rtp_src, FALSE);
2016   gst_pad_set_active (rtpsession->send_rtp_sink, FALSE);
2017
2018   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2019       rtpsession->send_rtp_sink);
2020   rtpsession->send_rtp_sink = NULL;
2021
2022   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2023       rtpsession->send_rtp_src);
2024   rtpsession->send_rtp_src = NULL;
2025 }
2026
2027 /* Create a srcpad with the RTCP packets to send out.
2028  * This pad will be driven by the RTP session manager when it wants to send out
2029  * RTCP packets.
2030  */
2031 static GstPad *
2032 create_send_rtcp_src (GstRtpSession * rtpsession)
2033 {
2034   GST_DEBUG_OBJECT (rtpsession, "creating pad");
2035
2036   rtpsession->send_rtcp_src =
2037       gst_pad_new_from_static_template (&rtpsession_send_rtcp_src_template,
2038       "send_rtcp_src");
2039   gst_pad_use_fixed_caps (rtpsession->send_rtcp_src);
2040   gst_pad_set_active (rtpsession->send_rtcp_src, TRUE);
2041   gst_pad_set_iterate_internal_links_function (rtpsession->send_rtcp_src,
2042       gst_rtp_session_iterate_internal_links);
2043   gst_pad_set_query_function (rtpsession->send_rtcp_src,
2044       gst_rtp_session_query_send_rtcp_src);
2045   gst_pad_set_event_function (rtpsession->send_rtcp_src,
2046       gst_rtp_session_event_send_rtcp_src);
2047   gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2048       rtpsession->send_rtcp_src);
2049
2050   return rtpsession->send_rtcp_src;
2051 }
2052
2053 static void
2054 remove_send_rtcp_src (GstRtpSession * rtpsession)
2055 {
2056   GST_DEBUG_OBJECT (rtpsession, "removing pad");
2057
2058   gst_pad_set_active (rtpsession->send_rtcp_src, FALSE);
2059
2060   gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2061       rtpsession->send_rtcp_src);
2062   rtpsession->send_rtcp_src = NULL;
2063 }
2064
2065 static GstPad *
2066 gst_rtp_session_request_new_pad (GstElement * element,
2067     GstPadTemplate * templ, const gchar * name)
2068 {
2069   GstRtpSession *rtpsession;
2070   GstElementClass *klass;
2071   GstPad *result;
2072
2073   g_return_val_if_fail (templ != NULL, NULL);
2074   g_return_val_if_fail (GST_IS_RTP_SESSION (element), NULL);
2075
2076   rtpsession = GST_RTP_SESSION (element);
2077   klass = GST_ELEMENT_GET_CLASS (element);
2078
2079   GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
2080
2081   GST_RTP_SESSION_LOCK (rtpsession);
2082
2083   /* figure out the template */
2084   if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink")) {
2085     if (rtpsession->recv_rtp_sink != NULL)
2086       goto exists;
2087
2088     result = create_recv_rtp_sink (rtpsession);
2089   } else if (templ == gst_element_class_get_pad_template (klass,
2090           "recv_rtcp_sink")) {
2091     if (rtpsession->recv_rtcp_sink != NULL)
2092       goto exists;
2093
2094     result = create_recv_rtcp_sink (rtpsession);
2095   } else if (templ == gst_element_class_get_pad_template (klass,
2096           "send_rtp_sink")) {
2097     if (rtpsession->send_rtp_sink != NULL)
2098       goto exists;
2099
2100     result = create_send_rtp_sink (rtpsession);
2101   } else if (templ == gst_element_class_get_pad_template (klass,
2102           "send_rtcp_src")) {
2103     if (rtpsession->send_rtcp_src != NULL)
2104       goto exists;
2105
2106     result = create_send_rtcp_src (rtpsession);
2107   } else
2108     goto wrong_template;
2109
2110   GST_RTP_SESSION_UNLOCK (rtpsession);
2111
2112   return result;
2113
2114   /* ERRORS */
2115 wrong_template:
2116   {
2117     GST_RTP_SESSION_UNLOCK (rtpsession);
2118     g_warning ("gstrtpsession: this is not our template");
2119     return NULL;
2120   }
2121 exists:
2122   {
2123     GST_RTP_SESSION_UNLOCK (rtpsession);
2124     g_warning ("gstrtpsession: pad already requested");
2125     return NULL;
2126   }
2127 }
2128
2129 static void
2130 gst_rtp_session_release_pad (GstElement * element, GstPad * pad)
2131 {
2132   GstRtpSession *rtpsession;
2133
2134   g_return_if_fail (GST_IS_RTP_SESSION (element));
2135   g_return_if_fail (GST_IS_PAD (pad));
2136
2137   rtpsession = GST_RTP_SESSION (element);
2138
2139   GST_DEBUG_OBJECT (element, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2140
2141   GST_RTP_SESSION_LOCK (rtpsession);
2142
2143   if (rtpsession->recv_rtp_sink == pad) {
2144     remove_recv_rtp_sink (rtpsession);
2145   } else if (rtpsession->recv_rtcp_sink == pad) {
2146     remove_recv_rtcp_sink (rtpsession);
2147   } else if (rtpsession->send_rtp_sink == pad) {
2148     remove_send_rtp_sink (rtpsession);
2149   } else if (rtpsession->send_rtcp_src == pad) {
2150     remove_send_rtcp_src (rtpsession);
2151   } else
2152     goto wrong_pad;
2153
2154   GST_RTP_SESSION_UNLOCK (rtpsession);
2155
2156   return;
2157
2158   /* ERRORS */
2159 wrong_pad:
2160   {
2161     GST_RTP_SESSION_UNLOCK (rtpsession);
2162     g_warning ("gstrtpsession: asked to release an unknown pad");
2163     return;
2164   }
2165 }
2166
2167 static void
2168 gst_rtp_session_request_key_unit (RTPSession * sess,
2169     gboolean all_headers, gpointer user_data)
2170 {
2171   GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2172   GstEvent *event;
2173
2174   event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
2175       gst_structure_new ("GstForceKeyUnit",
2176           "all-headers", G_TYPE_BOOLEAN, all_headers, NULL));
2177   gst_pad_push_event (rtpsession->send_rtp_sink, event);
2178 }
2179
2180 static GstClockTime
2181 gst_rtp_session_request_time (RTPSession * session, gpointer user_data)
2182 {
2183   GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2184
2185   return gst_clock_get_time (rtpsession->priv->sysclock);
2186 }