2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
21 * SECTION:element-gstrtpsession
22 * @short_description: an RTP session manager
23 * @see_also: gstrtpjitterbuffer, gstrtpbin, gstrtpptdemux, gstrtpssrcdemux
27 * The RTP session manager models one participant with a unique SSRC in an RTP
28 * session. This session can be used to send and receive RTP and RTCP packets.
29 * Based on what REQUEST pads are requested from the session manager, specific
30 * functionality can be activated.
33 * The session manager currently implements RFC 3550 including:
36 * <para>RTP packet validation based on consecutive sequence numbers.</para>
39 * <para>Maintainance of the SSRC participant database.</para>
42 * <para>Keeping per participant statistics based on received RTCP packets.</para>
45 * <para>Scheduling of RR/SR RTCP packets.</para>
50 * The gstrtpsession will not demux packets based on SSRC or payload type, nor will
51 * it correct for packet reordering and jitter. Use gstrtpssrcdemux, gstrtpptdemux and
52 * gstrtpjitterbuffer in addition to gstrtpsession to perform these tasks. It is
53 * usually a good idea to use gstrtpbin, which combines all these features in one
57 * To use gstrtpsession as an RTP receiver, request a recv_rtp_sink pad, which will
58 * automatically create recv_rtp_src pad. Data received on the recv_rtp_sink pad
59 * will be processed in the session and after being validated forwarded on the
63 * To also use gstrtpsession as an RTCP receiver, request a recv_rtcp_sink pad,
64 * which will automatically create a sync_src pad. Packets received on the RTCP
65 * pad will be used by the session manager to update the stats and database of
66 * the other participants. SR packets will be forwarded on the sync_src pad
67 * so that they can be used to perform inter-stream synchronisation when needed.
70 * If you want the session manager to generate and send RTCP packets, request
71 * the send_rtcp_src pad. Packet pushed on this pad contain SR/RR RTCP reports
72 * that should be sent to all participants in the session.
75 * To use gstrtpsession as a sender, request a send_rtp_sink pad, which will
76 * automatically create a send_rtp_src pad. The session manager will modify the
77 * SSRC in the RTP packets to its own SSRC and wil forward the packets on the
78 * send_rtp_src pad after updating its internal state.
81 * The session manager needs the clock-rate of the payload types it is handling
82 * and will signal the GstRtpSession::request-pt-map signal when it needs such a
83 * mapping. One can clear the cached values with the GstRtpSession::clear-pt-map
86 * <title>Example pipelines</title>
89 * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink gstrtpsession .recv_rtp_src ! rtptheoradepay ! theoradec ! xvimagesink
91 * Receive theora RTP packets from port 5000 and send them to the depayloader,
92 * decoder and display. Note that the application/x-rtp caps on udpsrc should be
93 * configured based on some negotiation process such as RTSP for this pipeline
98 * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink gstrtpsession name=session \
99 * .recv_rtp_src ! rtptheoradepay ! theoradec ! xvimagesink \
100 * udpsrc port=5001 caps="application/x-rtcp" ! session.recv_rtcp_sink
102 * Receive theora RTP packets from port 5000 and send them to the depayloader,
103 * decoder and display. Receive RTCP packets from port 5001 and process them in
104 * the session manager.
105 * Note that the application/x-rtp caps on udpsrc should be
106 * configured based on some negotiation process such as RTSP for this pipeline
111 * gst-launch videotestsrc ! theoraenc ! rtptheorapay ! .send_rtp_sink gstrtpsession .send_rtp_src ! udpsink port=5000
113 * Send theora RTP packets through the session manager and out on UDP port 5000.
117 * gst-launch videotestsrc ! theoraenc ! rtptheorapay ! .send_rtp_sink gstrtpsession name=session .send_rtp_src \
118 * ! udpsink port=5000 session.send_rtcp_src ! udpsink port=5001
120 * Send theora RTP packets through the session manager and out on UDP port 5000.
121 * Send RTCP packets on port 5001. Note that this pipeline will not preroll
122 * correctly because the second udpsink will not preroll correctly (no RTCP
123 * packets are sent in the PAUSED state). Applications should manually set and
124 * keep (see #gst_element_set_locked_state()) the RTCP udpsink to the PLAYING state.
128 * Last reviewed on 2007-05-28 (0.10.5)
135 #include <gst/rtp/gstrtpbuffer.h>
137 #include "gstrtpbin-marshal.h"
138 #include "gstrtpsession.h"
139 #include "rtpsession.h"
141 GST_DEBUG_CATEGORY_STATIC (gst_rtp_session_debug);
142 #define GST_CAT_DEFAULT gst_rtp_session_debug
144 /* elementfactory information */
145 static const GstElementDetails rtpsession_details =
146 GST_ELEMENT_DETAILS ("RTP Session",
147 "Filter/Network/RTP",
148 "Implement an RTP session",
149 "Wim Taymans <wim.taymans@gmail.com>");
152 static GstStaticPadTemplate rtpsession_recv_rtp_sink_template =
153 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink",
156 GST_STATIC_CAPS ("application/x-rtp")
159 static GstStaticPadTemplate rtpsession_recv_rtcp_sink_template =
160 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink",
163 GST_STATIC_CAPS ("application/x-rtcp")
166 static GstStaticPadTemplate rtpsession_send_rtp_sink_template =
167 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink",
170 GST_STATIC_CAPS ("application/x-rtp")
174 static GstStaticPadTemplate rtpsession_recv_rtp_src_template =
175 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src",
178 GST_STATIC_CAPS ("application/x-rtp")
181 static GstStaticPadTemplate rtpsession_sync_src_template =
182 GST_STATIC_PAD_TEMPLATE ("sync_src",
185 GST_STATIC_CAPS ("application/x-rtcp")
188 static GstStaticPadTemplate rtpsession_send_rtp_src_template =
189 GST_STATIC_PAD_TEMPLATE ("send_rtp_src",
192 GST_STATIC_CAPS ("application/x-rtp")
195 static GstStaticPadTemplate rtpsession_send_rtcp_src_template =
196 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src",
199 GST_STATIC_CAPS ("application/x-rtcp")
202 /* signals and args */
205 SIGNAL_REQUEST_PT_MAP,
209 SIGNAL_ON_SSRC_COLLISION,
210 SIGNAL_ON_SSRC_VALIDATED,
211 SIGNAL_ON_SSRC_ACTIVE,
214 SIGNAL_ON_BYE_TIMEOUT,
219 #define DEFAULT_NTP_NS_BASE 0
220 #define DEFAULT_BANDWIDTH RTP_STATS_BANDWIDTH
221 #define DEFAULT_RTCP_FRACTION RTP_STATS_RTCP_BANDWIDTH
222 #define DEFAULT_SDES_CNAME NULL
223 #define DEFAULT_SDES_NAME NULL
224 #define DEFAULT_SDES_EMAIL NULL
225 #define DEFAULT_SDES_PHONE NULL
226 #define DEFAULT_SDES_LOCATION NULL
227 #define DEFAULT_SDES_TOOL NULL
228 #define DEFAULT_SDES_NOTE NULL
229 #define DEFAULT_NUM_SOURCES 0
230 #define DEFAULT_NUM_ACTIVE_SOURCES 0
246 PROP_NUM_ACTIVE_SOURCES,
250 #define GST_RTP_SESSION_GET_PRIVATE(obj) \
251 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_SESSION, GstRtpSessionPrivate))
253 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock ((sess)->priv->lock)
254 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock ((sess)->priv->lock)
256 struct _GstRtpSessionPrivate
261 /* thread for sending out RTCP */
263 gboolean stop_thread;
265 gboolean thread_stopped;
274 /* callbacks to handle actions from the session manager */
275 static GstFlowReturn gst_rtp_session_process_rtp (RTPSession * sess,
276 RTPSource * src, GstBuffer * buffer, gpointer user_data);
277 static GstFlowReturn gst_rtp_session_send_rtp (RTPSession * sess,
278 RTPSource * src, GstBuffer * buffer, gpointer user_data);
279 static GstFlowReturn gst_rtp_session_send_rtcp (RTPSession * sess,
280 RTPSource * src, GstBuffer * buffer, gpointer user_data);
281 static GstFlowReturn gst_rtp_session_sync_rtcp (RTPSession * sess,
282 RTPSource * src, GstBuffer * buffer, gpointer user_data);
283 static gint gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
285 static void gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data);
287 static RTPSessionCallbacks callbacks = {
288 gst_rtp_session_process_rtp,
289 gst_rtp_session_send_rtp,
290 gst_rtp_session_sync_rtcp,
291 gst_rtp_session_send_rtcp,
292 gst_rtp_session_clock_rate,
293 gst_rtp_session_reconsider
296 /* GObject vmethods */
297 static void gst_rtp_session_finalize (GObject * object);
298 static void gst_rtp_session_set_property (GObject * object, guint prop_id,
299 const GValue * value, GParamSpec * pspec);
300 static void gst_rtp_session_get_property (GObject * object, guint prop_id,
301 GValue * value, GParamSpec * pspec);
303 /* GstElement vmethods */
304 static GstStateChangeReturn gst_rtp_session_change_state (GstElement * element,
305 GstStateChange transition);
306 static GstPad *gst_rtp_session_request_new_pad (GstElement * element,
307 GstPadTemplate * templ, const gchar * name);
308 static void gst_rtp_session_release_pad (GstElement * element, GstPad * pad);
310 static void gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession);
312 static guint gst_rtp_session_signals[LAST_SIGNAL] = { 0 };
315 on_new_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
317 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0,
322 on_ssrc_collision (RTPSession * session, RTPSource * src, GstRtpSession * sess)
324 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
329 on_ssrc_validated (RTPSession * session, RTPSource * src, GstRtpSession * sess)
331 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
336 on_ssrc_active (RTPSession * session, RTPSource * src, GstRtpSession * sess)
338 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
342 static GstStructure *
343 source_get_sdes_structure (RTPSource * src)
345 GstStructure *result;
349 result = gst_structure_empty_new ("GstRTPSessionSDES");
351 gst_structure_set (result, "ssrc", G_TYPE_UINT, src->ssrc, NULL);
353 g_value_init (&val, G_TYPE_STRING);
354 str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_CNAME);
356 g_value_take_string (&val, str);
357 gst_structure_set_value (result, "cname", &val);
359 str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_NAME);
361 g_value_take_string (&val, str);
362 gst_structure_set_value (result, "name", &val);
364 str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_EMAIL);
366 g_value_take_string (&val, str);
367 gst_structure_set_value (result, "email", &val);
369 str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_PHONE);
371 g_value_take_string (&val, str);
372 gst_structure_set_value (result, "phone", &val);
374 str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_LOC);
376 g_value_take_string (&val, str);
377 gst_structure_set_value (result, "location", &val);
379 str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_TOOL);
381 g_value_take_string (&val, str);
382 gst_structure_set_value (result, "tool", &val);
384 str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_NOTE);
386 g_value_take_string (&val, str);
387 gst_structure_set_value (result, "note", &val);
389 str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_PRIV);
391 g_value_take_string (&val, str);
392 gst_structure_set_value (result, "priv", &val);
399 on_ssrc_sdes (RTPSession * session, RTPSource * src, GstRtpSession * sess)
404 /* convert the new SDES info into a message */
405 RTP_SESSION_LOCK (session);
406 s = source_get_sdes_structure (src);
407 RTP_SESSION_UNLOCK (session);
408 m = gst_message_new_custom (GST_MESSAGE_ELEMENT, GST_OBJECT (sess), s);
409 gst_element_post_message (GST_ELEMENT_CAST (sess), m);
411 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0,
416 on_bye_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
418 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0,
423 on_bye_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
425 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
430 on_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
432 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_TIMEOUT], 0,
436 GST_BOILERPLATE (GstRtpSession, gst_rtp_session, GstElement, GST_TYPE_ELEMENT);
439 gst_rtp_session_base_init (gpointer klass)
441 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
444 gst_element_class_add_pad_template (element_class,
445 gst_static_pad_template_get (&rtpsession_recv_rtp_sink_template));
446 gst_element_class_add_pad_template (element_class,
447 gst_static_pad_template_get (&rtpsession_recv_rtcp_sink_template));
448 gst_element_class_add_pad_template (element_class,
449 gst_static_pad_template_get (&rtpsession_send_rtp_sink_template));
452 gst_element_class_add_pad_template (element_class,
453 gst_static_pad_template_get (&rtpsession_recv_rtp_src_template));
454 gst_element_class_add_pad_template (element_class,
455 gst_static_pad_template_get (&rtpsession_sync_src_template));
456 gst_element_class_add_pad_template (element_class,
457 gst_static_pad_template_get (&rtpsession_send_rtp_src_template));
458 gst_element_class_add_pad_template (element_class,
459 gst_static_pad_template_get (&rtpsession_send_rtcp_src_template));
461 gst_element_class_set_details (element_class, &rtpsession_details);
465 gst_rtp_session_class_init (GstRtpSessionClass * klass)
467 GObjectClass *gobject_class;
468 GstElementClass *gstelement_class;
470 gobject_class = (GObjectClass *) klass;
471 gstelement_class = (GstElementClass *) klass;
473 g_type_class_add_private (klass, sizeof (GstRtpSessionPrivate));
475 gobject_class->finalize = gst_rtp_session_finalize;
476 gobject_class->set_property = gst_rtp_session_set_property;
477 gobject_class->get_property = gst_rtp_session_get_property;
480 * GstRtpSession::request-pt-map:
481 * @sess: the object which received the signal
484 * Request the payload type as #GstCaps for @pt.
486 gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP] =
487 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
488 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, request_pt_map),
489 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT, GST_TYPE_CAPS, 1,
492 * GstRtpSession::clear-pt-map:
493 * @sess: the object which received the signal
495 * Clear the cached pt-maps requested with GstRtpSession::request-pt-map.
497 gst_rtp_session_signals[SIGNAL_CLEAR_PT_MAP] =
498 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
499 G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpSessionClass, clear_pt_map),
500 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
503 * GstRtpSession::on-new-ssrc:
504 * @sess: the object which received the signal
507 * Notify of a new SSRC that entered @session.
509 gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
510 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
511 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_new_ssrc),
512 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
514 * GstRtpSession::on-ssrc_collision:
515 * @sess: the object which received the signal
518 * Notify when we have an SSRC collision
520 gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
521 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
522 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
523 on_ssrc_collision), NULL, NULL, g_cclosure_marshal_VOID__UINT,
524 G_TYPE_NONE, 1, G_TYPE_UINT);
526 * GstRtpSession::on-ssrc_validated:
527 * @sess: the object which received the signal
530 * Notify of a new SSRC that became validated.
532 gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
533 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
534 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
535 on_ssrc_validated), NULL, NULL, g_cclosure_marshal_VOID__UINT,
536 G_TYPE_NONE, 1, G_TYPE_UINT);
538 * GstRtpSession::on-ssrc_active:
539 * @sess: the object which received the signal
542 * Notify of a SSRC that is active, i.e., sending RTCP.
544 gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
545 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
546 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
547 on_ssrc_active), NULL, NULL, g_cclosure_marshal_VOID__UINT,
548 G_TYPE_NONE, 1, G_TYPE_UINT);
550 * GstRtpSession::on-ssrc-sdes:
551 * @session: the object which received the signal
554 * Notify that a new SDES was received for SSRC.
556 gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
557 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
558 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_ssrc_sdes),
559 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
562 * GstRtpSession::on-bye-ssrc:
563 * @sess: the object which received the signal
566 * Notify of an SSRC that became inactive because of a BYE packet.
568 gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
569 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
570 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_ssrc),
571 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
573 * GstRtpSession::on-bye-timeout:
574 * @sess: the object which received the signal
577 * Notify of an SSRC that has timed out because of BYE
579 gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
580 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
581 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_timeout),
582 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
584 * GstRtpSession::on-timeout:
585 * @sess: the object which received the signal
588 * Notify of an SSRC that has timed out
590 gst_rtp_session_signals[SIGNAL_ON_TIMEOUT] =
591 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
592 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_timeout),
593 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
595 g_object_class_install_property (gobject_class, PROP_NTP_NS_BASE,
596 g_param_spec_uint64 ("ntp-ns-base", "NTP base time",
597 "The NTP base time corresponding to running_time 0", 0,
598 G_MAXUINT64, DEFAULT_NTP_NS_BASE, G_PARAM_READWRITE));
600 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
601 g_param_spec_double ("bandwidth", "Bandwidth",
602 "The bandwidth of the session",
603 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH, G_PARAM_READWRITE));
605 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
606 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
607 "The fraction of the bandwidth used for RTCP",
608 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION, G_PARAM_READWRITE));
610 g_object_class_install_property (gobject_class, PROP_SDES_CNAME,
611 g_param_spec_string ("sdes-cname", "SDES CNAME",
612 "The CNAME to put in SDES messages of this session",
613 DEFAULT_SDES_CNAME, G_PARAM_READWRITE));
615 g_object_class_install_property (gobject_class, PROP_SDES_NAME,
616 g_param_spec_string ("sdes-name", "SDES NAME",
617 "The NAME to put in SDES messages of this session",
618 DEFAULT_SDES_NAME, G_PARAM_READWRITE));
620 g_object_class_install_property (gobject_class, PROP_SDES_EMAIL,
621 g_param_spec_string ("sdes-email", "SDES EMAIL",
622 "The EMAIL to put in SDES messages of this session",
623 DEFAULT_SDES_EMAIL, G_PARAM_READWRITE));
625 g_object_class_install_property (gobject_class, PROP_SDES_PHONE,
626 g_param_spec_string ("sdes-phone", "SDES PHONE",
627 "The PHONE to put in SDES messages of this session",
628 DEFAULT_SDES_PHONE, G_PARAM_READWRITE));
630 g_object_class_install_property (gobject_class, PROP_SDES_LOCATION,
631 g_param_spec_string ("sdes-location", "SDES LOCATION",
632 "The LOCATION to put in SDES messages of this session",
633 DEFAULT_SDES_LOCATION, G_PARAM_READWRITE));
635 g_object_class_install_property (gobject_class, PROP_SDES_TOOL,
636 g_param_spec_string ("sdes-tool", "SDES TOOL",
637 "The TOOL to put in SDES messages of this session",
638 DEFAULT_SDES_TOOL, G_PARAM_READWRITE));
640 g_object_class_install_property (gobject_class, PROP_SDES_NOTE,
641 g_param_spec_string ("sdes-note", "SDES NOTE",
642 "The NOTE to put in SDES messages of this session",
643 DEFAULT_SDES_NOTE, G_PARAM_READWRITE));
645 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
646 g_param_spec_uint ("num-sources", "Num Sources",
647 "The number of sources in the session", 0, G_MAXUINT,
648 DEFAULT_NUM_SOURCES, G_PARAM_READABLE));
650 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
651 g_param_spec_uint ("num-active-sources", "Num Active Sources",
652 "The number of active sources in the session", 0, G_MAXUINT,
653 DEFAULT_NUM_ACTIVE_SOURCES, G_PARAM_READABLE));
655 gstelement_class->change_state =
656 GST_DEBUG_FUNCPTR (gst_rtp_session_change_state);
657 gstelement_class->request_new_pad =
658 GST_DEBUG_FUNCPTR (gst_rtp_session_request_new_pad);
659 gstelement_class->release_pad =
660 GST_DEBUG_FUNCPTR (gst_rtp_session_release_pad);
662 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_session_clear_pt_map);
664 GST_DEBUG_CATEGORY_INIT (gst_rtp_session_debug,
665 "rtpsession", 0, "RTP Session");
669 gst_rtp_session_init (GstRtpSession * rtpsession, GstRtpSessionClass * klass)
671 rtpsession->priv = GST_RTP_SESSION_GET_PRIVATE (rtpsession);
672 rtpsession->priv->lock = g_mutex_new ();
673 rtpsession->priv->session = rtp_session_new ();
674 /* configure callbacks */
675 rtp_session_set_callbacks (rtpsession->priv->session, &callbacks, rtpsession);
676 /* configure signals */
677 g_signal_connect (rtpsession->priv->session, "on-new-ssrc",
678 (GCallback) on_new_ssrc, rtpsession);
679 g_signal_connect (rtpsession->priv->session, "on-ssrc-collision",
680 (GCallback) on_ssrc_collision, rtpsession);
681 g_signal_connect (rtpsession->priv->session, "on-ssrc-validated",
682 (GCallback) on_ssrc_validated, rtpsession);
683 g_signal_connect (rtpsession->priv->session, "on-ssrc-active",
684 (GCallback) on_ssrc_active, rtpsession);
685 g_signal_connect (rtpsession->priv->session, "on-ssrc-sdes",
686 (GCallback) on_ssrc_sdes, rtpsession);
687 g_signal_connect (rtpsession->priv->session, "on-bye-ssrc",
688 (GCallback) on_bye_ssrc, rtpsession);
689 g_signal_connect (rtpsession->priv->session, "on-bye-timeout",
690 (GCallback) on_bye_timeout, rtpsession);
691 g_signal_connect (rtpsession->priv->session, "on-timeout",
692 (GCallback) on_timeout, rtpsession);
693 rtpsession->priv->ptmap = g_hash_table_new (NULL, NULL);
695 gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
696 gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
698 rtpsession->priv->thread_stopped = TRUE;
702 gst_rtp_session_finalize (GObject * object)
704 GstRtpSession *rtpsession;
706 rtpsession = GST_RTP_SESSION (object);
708 g_hash_table_destroy (rtpsession->priv->ptmap);
709 g_mutex_free (rtpsession->priv->lock);
710 g_object_unref (rtpsession->priv->session);
712 G_OBJECT_CLASS (parent_class)->finalize (object);
716 gst_rtp_session_set_property (GObject * object, guint prop_id,
717 const GValue * value, GParamSpec * pspec)
719 GstRtpSession *rtpsession;
720 GstRtpSessionPrivate *priv;
722 rtpsession = GST_RTP_SESSION (object);
723 priv = rtpsession->priv;
726 case PROP_NTP_NS_BASE:
727 GST_OBJECT_LOCK (rtpsession);
728 priv->ntpnsbase = g_value_get_uint64 (value);
729 GST_DEBUG_OBJECT (rtpsession, "setting NTP base to %" GST_TIME_FORMAT,
730 GST_TIME_ARGS (priv->ntpnsbase));
731 GST_OBJECT_UNLOCK (rtpsession);
734 rtp_session_set_bandwidth (priv->session, g_value_get_double (value));
736 case PROP_RTCP_FRACTION:
737 rtp_session_set_rtcp_fraction (priv->session, g_value_get_double (value));
739 case PROP_SDES_CNAME:
740 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_CNAME,
741 g_value_get_string (value));
744 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_NAME,
745 g_value_get_string (value));
747 case PROP_SDES_EMAIL:
748 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_EMAIL,
749 g_value_get_string (value));
751 case PROP_SDES_PHONE:
752 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_PHONE,
753 g_value_get_string (value));
755 case PROP_SDES_LOCATION:
756 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_LOC,
757 g_value_get_string (value));
760 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_TOOL,
761 g_value_get_string (value));
764 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_NOTE,
765 g_value_get_string (value));
768 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
774 gst_rtp_session_get_property (GObject * object, guint prop_id,
775 GValue * value, GParamSpec * pspec)
777 GstRtpSession *rtpsession;
778 GstRtpSessionPrivate *priv;
780 rtpsession = GST_RTP_SESSION (object);
781 priv = rtpsession->priv;
784 case PROP_NTP_NS_BASE:
785 GST_OBJECT_LOCK (rtpsession);
786 g_value_set_uint64 (value, priv->ntpnsbase);
787 GST_OBJECT_UNLOCK (rtpsession);
790 g_value_set_double (value, rtp_session_get_bandwidth (priv->session));
792 case PROP_RTCP_FRACTION:
793 g_value_set_double (value, rtp_session_get_rtcp_fraction (priv->session));
795 case PROP_SDES_CNAME:
796 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
797 GST_RTCP_SDES_CNAME));
800 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
801 GST_RTCP_SDES_NAME));
803 case PROP_SDES_EMAIL:
804 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
805 GST_RTCP_SDES_EMAIL));
807 case PROP_SDES_PHONE:
808 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
809 GST_RTCP_SDES_PHONE));
811 case PROP_SDES_LOCATION:
812 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
816 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
817 GST_RTCP_SDES_TOOL));
820 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
821 GST_RTCP_SDES_NOTE));
823 case PROP_NUM_SOURCES:
824 g_value_set_uint (value, rtp_session_get_num_sources (priv->session));
826 case PROP_NUM_ACTIVE_SOURCES:
827 g_value_set_uint (value,
828 rtp_session_get_num_active_sources (priv->session));
831 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
837 get_current_ntp_ns_time (GstRtpSession * rtpsession)
841 GstClockTime base_time, ntpnsbase;
843 GST_OBJECT_LOCK (rtpsession);
844 if ((clock = GST_ELEMENT_CLOCK (rtpsession))) {
845 base_time = GST_ELEMENT_CAST (rtpsession)->base_time;
846 ntpnsbase = rtpsession->priv->ntpnsbase;
847 gst_object_ref (clock);
848 GST_OBJECT_UNLOCK (rtpsession);
850 /* get current NTP time */
851 ntpnstime = gst_clock_get_time (clock);
852 /* convert to running time */
853 ntpnstime -= base_time;
854 /* add NTP base offset */
855 ntpnstime += ntpnsbase;
857 gst_object_unref (clock);
859 GST_OBJECT_UNLOCK (rtpsession);
867 rtcp_thread (GstRtpSession * rtpsession)
871 GstClockTime current_time;
872 GstClockTime next_timeout;
875 /* for RTCP timeouts we use the system clock */
876 sysclock = gst_system_clock_obtain ();
877 if (sysclock == NULL)
880 current_time = gst_clock_get_time (sysclock);
882 GST_DEBUG_OBJECT (rtpsession, "entering RTCP thread");
884 GST_RTP_SESSION_LOCK (rtpsession);
886 while (!rtpsession->priv->stop_thread) {
889 /* get initial estimate */
891 rtp_session_next_timeout (rtpsession->priv->session, current_time);
893 GST_DEBUG_OBJECT (rtpsession, "next check time %" GST_TIME_FORMAT,
894 GST_TIME_ARGS (next_timeout));
896 /* leave if no more timeouts, the session ended */
897 if (next_timeout == GST_CLOCK_TIME_NONE)
900 id = rtpsession->priv->id =
901 gst_clock_new_single_shot_id (sysclock, next_timeout);
902 GST_RTP_SESSION_UNLOCK (rtpsession);
904 res = gst_clock_id_wait (id, NULL);
906 GST_RTP_SESSION_LOCK (rtpsession);
907 gst_clock_id_unref (id);
908 rtpsession->priv->id = NULL;
910 if (rtpsession->priv->stop_thread)
913 /* update current time */
914 current_time = gst_clock_get_time (sysclock);
916 /* get current NTP time */
917 ntpnstime = get_current_ntp_ns_time (rtpsession);
919 /* we get unlocked because we need to perform reconsideration, don't perform
920 * the timeout but get a new reporting estimate. */
921 GST_DEBUG_OBJECT (rtpsession, "unlocked %d, current %" GST_TIME_FORMAT,
922 res, GST_TIME_ARGS (current_time));
924 /* perform actions, we ignore result. Release lock because it might push. */
925 GST_RTP_SESSION_UNLOCK (rtpsession);
926 rtp_session_on_timeout (rtpsession->priv->session, current_time, ntpnstime);
927 GST_RTP_SESSION_LOCK (rtpsession);
929 /* mark the thread as stopped now */
930 rtpsession->priv->thread_stopped = TRUE;
931 GST_RTP_SESSION_UNLOCK (rtpsession);
933 gst_object_unref (sysclock);
935 GST_DEBUG_OBJECT (rtpsession, "leaving RTCP thread");
941 GST_ELEMENT_ERROR (rtpsession, CORE, CLOCK, (NULL),
942 ("Could not get system clock"));
948 start_rtcp_thread (GstRtpSession * rtpsession)
950 GError *error = NULL;
953 GST_DEBUG_OBJECT (rtpsession, "starting RTCP thread");
955 GST_RTP_SESSION_LOCK (rtpsession);
956 rtpsession->priv->stop_thread = FALSE;
957 if (rtpsession->priv->thread_stopped) {
958 /* only create a new thread if the old one was stopped. Otherwise we can
959 * just reuse the currently running one. */
960 rtpsession->priv->thread =
961 g_thread_create ((GThreadFunc) rtcp_thread, rtpsession, TRUE, &error);
962 rtpsession->priv->thread_stopped = FALSE;
964 GST_RTP_SESSION_UNLOCK (rtpsession);
968 GST_DEBUG_OBJECT (rtpsession, "failed to start thread, %s", error->message);
969 g_error_free (error);
977 stop_rtcp_thread (GstRtpSession * rtpsession)
979 GST_DEBUG_OBJECT (rtpsession, "stopping RTCP thread");
981 GST_RTP_SESSION_LOCK (rtpsession);
982 rtpsession->priv->stop_thread = TRUE;
983 if (rtpsession->priv->id)
984 gst_clock_id_unschedule (rtpsession->priv->id);
985 GST_RTP_SESSION_UNLOCK (rtpsession);
989 join_rtcp_thread (GstRtpSession * rtpsession)
991 GST_RTP_SESSION_LOCK (rtpsession);
992 /* don't try to join when we have no thread */
993 if (rtpsession->priv->thread != NULL) {
994 GST_DEBUG_OBJECT (rtpsession, "joining RTCP thread");
995 GST_RTP_SESSION_UNLOCK (rtpsession);
997 g_thread_join (rtpsession->priv->thread);
999 GST_RTP_SESSION_LOCK (rtpsession);
1000 /* after the join, take the lock and clear the thread structure. The caller
1001 * is supposed to not concurrently call start and join. */
1002 rtpsession->priv->thread = NULL;
1004 GST_RTP_SESSION_UNLOCK (rtpsession);
1007 static GstStateChangeReturn
1008 gst_rtp_session_change_state (GstElement * element, GstStateChange transition)
1010 GstStateChangeReturn res;
1011 GstRtpSession *rtpsession;
1012 GstRtpSessionPrivate *priv;
1014 rtpsession = GST_RTP_SESSION (element);
1015 priv = rtpsession->priv;
1017 switch (transition) {
1018 case GST_STATE_CHANGE_NULL_TO_READY:
1020 case GST_STATE_CHANGE_READY_TO_PAUSED:
1022 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1024 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1025 case GST_STATE_CHANGE_PAUSED_TO_READY:
1026 /* no need to join yet, we might want to continue later. Also, the
1027 * dataflow could block downstream so that a join could just block
1029 stop_rtcp_thread (rtpsession);
1035 res = parent_class->change_state (element, transition);
1037 switch (transition) {
1038 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1039 if (!start_rtcp_thread (rtpsession))
1042 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1044 case GST_STATE_CHANGE_PAUSED_TO_READY:
1045 /* downstream is now releasing the dataflow and we can join. */
1046 join_rtcp_thread (rtpsession);
1048 case GST_STATE_CHANGE_READY_TO_NULL:
1058 return GST_STATE_CHANGE_FAILURE;
1063 gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession)
1065 /* FIXME, do something */
1068 /* called when the session manager has an RTP packet ready for further
1070 static GstFlowReturn
1071 gst_rtp_session_process_rtp (RTPSession * sess, RTPSource * src,
1072 GstBuffer * buffer, gpointer user_data)
1074 GstFlowReturn result;
1075 GstRtpSession *rtpsession;
1076 GstRtpSessionPrivate *priv;
1078 rtpsession = GST_RTP_SESSION (user_data);
1079 priv = rtpsession->priv;
1081 if (rtpsession->recv_rtp_src) {
1082 GST_DEBUG_OBJECT (rtpsession, "pushing received RTP packet");
1083 result = gst_pad_push (rtpsession->recv_rtp_src, buffer);
1085 GST_DEBUG_OBJECT (rtpsession, "dropping received RTP packet");
1086 gst_buffer_unref (buffer);
1087 result = GST_FLOW_OK;
1092 /* called when the session manager has an RTP packet ready for further
1094 static GstFlowReturn
1095 gst_rtp_session_send_rtp (RTPSession * sess, RTPSource * src,
1096 GstBuffer * buffer, gpointer user_data)
1098 GstFlowReturn result;
1099 GstRtpSession *rtpsession;
1100 GstRtpSessionPrivate *priv;
1102 rtpsession = GST_RTP_SESSION (user_data);
1103 priv = rtpsession->priv;
1105 GST_DEBUG_OBJECT (rtpsession, "sending RTP packet");
1107 if (rtpsession->send_rtp_src) {
1108 result = gst_pad_push (rtpsession->send_rtp_src, buffer);
1110 gst_buffer_unref (buffer);
1111 result = GST_FLOW_OK;
1116 /* called when the session manager has an RTCP packet ready for further
1118 static GstFlowReturn
1119 gst_rtp_session_send_rtcp (RTPSession * sess, RTPSource * src,
1120 GstBuffer * buffer, gpointer user_data)
1122 GstFlowReturn result;
1123 GstRtpSession *rtpsession;
1124 GstRtpSessionPrivate *priv;
1126 rtpsession = GST_RTP_SESSION (user_data);
1127 priv = rtpsession->priv;
1129 if (rtpsession->send_rtcp_src) {
1132 /* set rtcp caps on output pad */
1133 if (!(caps = GST_PAD_CAPS (rtpsession->send_rtcp_src))) {
1134 caps = gst_caps_new_simple ("application/x-rtcp", NULL);
1135 gst_pad_set_caps (rtpsession->send_rtcp_src, caps);
1136 gst_caps_unref (caps);
1138 gst_buffer_set_caps (buffer, caps);
1139 GST_DEBUG_OBJECT (rtpsession, "sending RTCP");
1140 result = gst_pad_push (rtpsession->send_rtcp_src, buffer);
1142 GST_DEBUG_OBJECT (rtpsession, "not sending RTCP, no output pad");
1143 gst_buffer_unref (buffer);
1144 result = GST_FLOW_OK;
1149 /* called when the session manager has an SR RTCP packet ready for handling
1150 * inter stream synchronisation */
1151 static GstFlowReturn
1152 gst_rtp_session_sync_rtcp (RTPSession * sess,
1153 RTPSource * src, GstBuffer * buffer, gpointer user_data)
1155 GstFlowReturn result;
1156 GstRtpSession *rtpsession;
1157 GstRtpSessionPrivate *priv;
1159 rtpsession = GST_RTP_SESSION (user_data);
1160 priv = rtpsession->priv;
1162 if (rtpsession->sync_src) {
1165 /* set rtcp caps on output pad */
1166 if (!(caps = GST_PAD_CAPS (rtpsession->sync_src))) {
1167 caps = gst_caps_new_simple ("application/x-rtcp", NULL);
1168 gst_pad_set_caps (rtpsession->sync_src, caps);
1169 gst_caps_unref (caps);
1171 gst_buffer_set_caps (buffer, caps);
1172 GST_DEBUG_OBJECT (rtpsession, "sending Sync RTCP");
1173 result = gst_pad_push (rtpsession->sync_src, buffer);
1175 GST_DEBUG_OBJECT (rtpsession, "not sending Sync RTCP, no output pad");
1176 gst_buffer_unref (buffer);
1177 result = GST_FLOW_OK;
1183 gst_rtp_session_cache_caps (GstRtpSession * rtpsession, GstCaps * caps)
1185 GstRtpSessionPrivate *priv;
1186 const GstStructure *s;
1189 priv = rtpsession->priv;
1191 GST_DEBUG_OBJECT (rtpsession, "parsing caps");
1193 s = gst_caps_get_structure (caps, 0);
1194 if (!gst_structure_get_int (s, "payload", &payload))
1197 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (payload));
1201 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (payload), caps);
1204 /* called when the session manager needs the clock rate */
1206 gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
1209 gint ipayload, result = -1;
1210 GstRtpSession *rtpsession;
1211 GstRtpSessionPrivate *priv;
1213 GValue args[2] = { {0}, {0} };
1215 const GstStructure *s;
1217 rtpsession = GST_RTP_SESSION_CAST (user_data);
1218 priv = rtpsession->priv;
1220 GST_RTP_SESSION_LOCK (rtpsession);
1221 ipayload = payload; /* make compiler happy */
1222 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (ipayload));
1226 /* not found in the cache, try to get it with a signal */
1227 g_value_init (&args[0], GST_TYPE_ELEMENT);
1228 g_value_set_object (&args[0], rtpsession);
1229 g_value_init (&args[1], G_TYPE_UINT);
1230 g_value_set_uint (&args[1], payload);
1232 g_value_init (&ret, GST_TYPE_CAPS);
1233 g_value_set_boxed (&ret, NULL);
1235 g_signal_emitv (args, gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP], 0,
1238 caps = (GstCaps *) g_value_get_boxed (&ret);
1242 gst_rtp_session_cache_caps (rtpsession, caps);
1245 s = gst_caps_get_structure (caps, 0);
1246 if (!gst_structure_get_int (s, "clock-rate", &result))
1249 GST_DEBUG_OBJECT (rtpsession, "parsed clock-rate %d", result);
1252 GST_RTP_SESSION_UNLOCK (rtpsession);
1259 GST_DEBUG_OBJECT (rtpsession, "could not get caps");
1264 GST_DEBUG_OBJECT (rtpsession, "No clock-rate in caps!");
1269 /* called when the session manager asks us to reconsider the timeout */
1271 gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data)
1273 GstRtpSession *rtpsession;
1275 rtpsession = GST_RTP_SESSION_CAST (user_data);
1277 GST_RTP_SESSION_LOCK (rtpsession);
1278 GST_DEBUG_OBJECT (rtpsession, "unlock timer for reconsideration");
1279 if (rtpsession->priv->id)
1280 gst_clock_id_unschedule (rtpsession->priv->id);
1281 GST_RTP_SESSION_UNLOCK (rtpsession);
1285 gst_rtp_session_event_recv_rtp_sink (GstPad * pad, GstEvent * event)
1287 GstRtpSession *rtpsession;
1288 GstRtpSessionPrivate *priv;
1289 gboolean ret = FALSE;
1291 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1292 priv = rtpsession->priv;
1294 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1295 GST_EVENT_TYPE_NAME (event));
1297 switch (GST_EVENT_TYPE (event)) {
1298 case GST_EVENT_FLUSH_STOP:
1299 gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
1300 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1302 case GST_EVENT_NEWSEGMENT:
1305 gdouble rate, arate;
1307 gint64 start, stop, time;
1308 GstSegment *segment;
1310 segment = &rtpsession->recv_rtp_seg;
1312 /* the newsegment event is needed to convert the RTP timestamp to
1313 * running_time, which is needed to generate a mapping from RTP to NTP
1314 * timestamps in SR reports */
1315 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1316 &start, &stop, &time);
1318 GST_DEBUG_OBJECT (rtpsession,
1319 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1320 "format GST_FORMAT_TIME, "
1321 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1322 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1323 update, rate, arate, GST_TIME_ARGS (segment->start),
1324 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1325 GST_TIME_ARGS (segment->accum));
1327 gst_segment_set_newsegment_full (segment, update, rate,
1328 arate, format, start, stop, time);
1330 /* push event forward */
1331 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1335 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1338 gst_object_unref (rtpsession);
1344 gst_rtp_session_internal_links (GstPad * pad)
1346 GstRtpSession *rtpsession;
1347 GstRtpSessionPrivate *priv;
1350 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1351 priv = rtpsession->priv;
1353 if (pad == rtpsession->recv_rtp_src) {
1354 res = g_list_prepend (res, rtpsession->recv_rtp_sink);
1355 } else if (pad == rtpsession->recv_rtp_sink) {
1356 res = g_list_prepend (res, rtpsession->recv_rtp_src);
1357 } else if (pad == rtpsession->send_rtp_src) {
1358 res = g_list_prepend (res, rtpsession->send_rtp_sink);
1359 } else if (pad == rtpsession->send_rtp_sink) {
1360 res = g_list_prepend (res, rtpsession->send_rtp_src);
1363 gst_object_unref (rtpsession);
1369 gst_rtp_session_sink_setcaps (GstPad * pad, GstCaps * caps)
1371 GstRtpSession *rtpsession;
1372 GstRtpSessionPrivate *priv;
1374 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1375 priv = rtpsession->priv;
1377 GST_RTP_SESSION_LOCK (rtpsession);
1378 gst_rtp_session_cache_caps (rtpsession, caps);
1379 GST_RTP_SESSION_UNLOCK (rtpsession);
1381 gst_object_unref (rtpsession);
1386 /* receive a packet from a sender, send it to the RTP session manager and
1387 * forward the packet on the rtp_src pad
1389 static GstFlowReturn
1390 gst_rtp_session_chain_recv_rtp (GstPad * pad, GstBuffer * buffer)
1392 GstRtpSession *rtpsession;
1393 GstRtpSessionPrivate *priv;
1396 GstClockTime timestamp;
1398 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1399 priv = rtpsession->priv;
1401 GST_DEBUG_OBJECT (rtpsession, "received RTP packet");
1403 /* get NTP time when this packet was captured, this depends on the timestamp. */
1404 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1405 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1406 /* convert to running time using the segment values */
1408 gst_segment_to_running_time (&rtpsession->recv_rtp_seg, GST_FORMAT_TIME,
1410 /* add constant to convert running time to NTP time */
1411 ntpnstime += priv->ntpnsbase;
1413 ntpnstime = get_current_ntp_ns_time (rtpsession);
1416 ret = rtp_session_process_rtp (priv->session, buffer, ntpnstime);
1417 if (ret != GST_FLOW_OK)
1422 gst_object_unref (rtpsession);
1429 GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1430 gst_flow_get_name (ret));
1436 gst_rtp_session_event_recv_rtcp_sink (GstPad * pad, GstEvent * event)
1438 GstRtpSession *rtpsession;
1439 GstRtpSessionPrivate *priv;
1440 gboolean ret = FALSE;
1442 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1443 priv = rtpsession->priv;
1445 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1446 GST_EVENT_TYPE_NAME (event));
1448 switch (GST_EVENT_TYPE (event)) {
1450 if (rtpsession->send_rtcp_src) {
1451 gst_event_ref (event);
1452 ret = gst_pad_push_event (rtpsession->send_rtcp_src, event);
1454 ret = gst_pad_push_event (rtpsession->sync_src, event);
1457 gst_object_unref (rtpsession);
1462 /* Receive an RTCP packet from a sender, send it to the RTP session manager and
1463 * forward the SR packets to the sync_src pad.
1465 static GstFlowReturn
1466 gst_rtp_session_chain_recv_rtcp (GstPad * pad, GstBuffer * buffer)
1468 GstRtpSession *rtpsession;
1469 GstRtpSessionPrivate *priv;
1472 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1473 priv = rtpsession->priv;
1475 GST_DEBUG_OBJECT (rtpsession, "received RTCP packet");
1477 ret = rtp_session_process_rtcp (priv->session, buffer);
1479 gst_object_unref (rtpsession);
1485 gst_rtp_session_event_send_rtp_sink (GstPad * pad, GstEvent * event)
1487 GstRtpSession *rtpsession;
1488 GstRtpSessionPrivate *priv;
1489 gboolean ret = FALSE;
1491 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1492 priv = rtpsession->priv;
1494 GST_DEBUG_OBJECT (rtpsession, "received event");
1496 switch (GST_EVENT_TYPE (event)) {
1497 case GST_EVENT_FLUSH_STOP:
1498 gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
1499 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1501 case GST_EVENT_NEWSEGMENT:
1504 gdouble rate, arate;
1506 gint64 start, stop, time;
1507 GstSegment *segment;
1509 segment = &rtpsession->send_rtp_seg;
1511 /* the newsegment event is needed to convert the RTP timestamp to
1512 * running_time, which is needed to generate a mapping from RTP to NTP
1513 * timestamps in SR reports */
1514 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1515 &start, &stop, &time);
1517 GST_DEBUG_OBJECT (rtpsession,
1518 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1519 "format GST_FORMAT_TIME, "
1520 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1521 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1522 update, rate, arate, GST_TIME_ARGS (segment->start),
1523 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1524 GST_TIME_ARGS (segment->accum));
1526 gst_segment_set_newsegment_full (segment, update, rate,
1527 arate, format, start, stop, time);
1529 /* push event forward */
1530 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1534 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1537 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1540 gst_object_unref (rtpsession);
1546 gst_rtp_session_getcaps_send_rtp (GstPad * pad)
1548 GstRtpSession *rtpsession;
1549 GstRtpSessionPrivate *priv;
1551 GstStructure *s1, *s2;
1553 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1554 priv = rtpsession->priv;
1556 /* we can basically accept anything but we prefer to receive packets with our
1557 * internal SSRC so that we don't have to patch it. Create a structure with
1558 * the SSRC and another one without. */
1559 s1 = gst_structure_new ("application/x-rtp",
1560 "ssrc", G_TYPE_UINT, priv->session->source->ssrc, NULL);
1561 s2 = gst_structure_new ("application/x-rtp", NULL);
1563 result = gst_caps_new_full (s1, s2, NULL);
1565 GST_DEBUG_OBJECT (rtpsession, "getting caps %" GST_PTR_FORMAT, result);
1567 gst_object_unref (rtpsession);
1572 /* Recieve an RTP packet to be send to the receivers, send to RTP session
1573 * manager and forward to send_rtp_src.
1575 static GstFlowReturn
1576 gst_rtp_session_chain_send_rtp (GstPad * pad, GstBuffer * buffer)
1578 GstRtpSession *rtpsession;
1579 GstRtpSessionPrivate *priv;
1581 GstClockTime timestamp;
1584 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1585 priv = rtpsession->priv;
1587 GST_DEBUG_OBJECT (rtpsession, "received RTP packet");
1589 /* get NTP time when this packet was captured, this depends on the timestamp. */
1590 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1591 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1592 /* convert to running time using the segment start value. */
1594 gst_segment_to_running_time (&rtpsession->send_rtp_seg, GST_FORMAT_TIME,
1596 /* convert to NTP time by adding the NTP base */
1597 ntpnstime += priv->ntpnsbase;
1599 /* no timestamp, we could take the current running_time and convert it to
1604 ret = rtp_session_send_rtp (priv->session, buffer, ntpnstime);
1605 if (ret != GST_FLOW_OK)
1609 gst_object_unref (rtpsession);
1616 GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1617 gst_flow_get_name (ret));
1622 /* Create sinkpad to receive RTP packets from senders. This will also create a
1623 * srcpad for the RTP packets.
1626 create_recv_rtp_sink (GstRtpSession * rtpsession)
1628 GST_DEBUG_OBJECT (rtpsession, "creating RTP sink pad");
1630 rtpsession->recv_rtp_sink =
1631 gst_pad_new_from_static_template (&rtpsession_recv_rtp_sink_template,
1633 gst_pad_set_chain_function (rtpsession->recv_rtp_sink,
1634 gst_rtp_session_chain_recv_rtp);
1635 gst_pad_set_event_function (rtpsession->recv_rtp_sink,
1636 (GstPadEventFunction) gst_rtp_session_event_recv_rtp_sink);
1637 gst_pad_set_setcaps_function (rtpsession->recv_rtp_sink,
1638 gst_rtp_session_sink_setcaps);
1639 gst_pad_set_internal_link_function (rtpsession->recv_rtp_sink,
1640 gst_rtp_session_internal_links);
1641 gst_pad_set_active (rtpsession->recv_rtp_sink, TRUE);
1642 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1643 rtpsession->recv_rtp_sink);
1645 GST_DEBUG_OBJECT (rtpsession, "creating RTP src pad");
1646 rtpsession->recv_rtp_src =
1647 gst_pad_new_from_static_template (&rtpsession_recv_rtp_src_template,
1649 gst_pad_set_internal_link_function (rtpsession->recv_rtp_src,
1650 gst_rtp_session_internal_links);
1651 gst_pad_use_fixed_caps (rtpsession->recv_rtp_src);
1652 gst_pad_set_active (rtpsession->recv_rtp_src, TRUE);
1653 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->recv_rtp_src);
1655 return rtpsession->recv_rtp_sink;
1658 /* Create a sinkpad to receive RTCP messages from senders, this will also create a
1659 * sync_src pad for the SR packets.
1662 create_recv_rtcp_sink (GstRtpSession * rtpsession)
1664 GST_DEBUG_OBJECT (rtpsession, "creating RTCP sink pad");
1666 rtpsession->recv_rtcp_sink =
1667 gst_pad_new_from_static_template (&rtpsession_recv_rtcp_sink_template,
1669 gst_pad_set_chain_function (rtpsession->recv_rtcp_sink,
1670 gst_rtp_session_chain_recv_rtcp);
1671 gst_pad_set_event_function (rtpsession->recv_rtcp_sink,
1672 (GstPadEventFunction) gst_rtp_session_event_recv_rtcp_sink);
1673 gst_pad_set_active (rtpsession->recv_rtcp_sink, TRUE);
1674 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1675 rtpsession->recv_rtcp_sink);
1677 GST_DEBUG_OBJECT (rtpsession, "creating sync src pad");
1678 rtpsession->sync_src =
1679 gst_pad_new_from_static_template (&rtpsession_sync_src_template,
1681 gst_pad_use_fixed_caps (rtpsession->sync_src);
1682 gst_pad_set_active (rtpsession->sync_src, TRUE);
1683 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
1685 return rtpsession->recv_rtcp_sink;
1688 /* Create a sinkpad to receive RTP packets for receivers. This will also create a
1692 create_send_rtp_sink (GstRtpSession * rtpsession)
1694 GST_DEBUG_OBJECT (rtpsession, "creating pad");
1696 rtpsession->send_rtp_sink =
1697 gst_pad_new_from_static_template (&rtpsession_send_rtp_sink_template,
1699 gst_pad_set_chain_function (rtpsession->send_rtp_sink,
1700 gst_rtp_session_chain_send_rtp);
1701 gst_pad_set_getcaps_function (rtpsession->send_rtp_sink,
1702 gst_rtp_session_getcaps_send_rtp);
1703 gst_pad_set_event_function (rtpsession->send_rtp_sink,
1704 (GstPadEventFunction) gst_rtp_session_event_send_rtp_sink);
1705 gst_pad_set_internal_link_function (rtpsession->send_rtp_sink,
1706 gst_rtp_session_internal_links);
1707 gst_pad_set_active (rtpsession->send_rtp_sink, TRUE);
1708 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1709 rtpsession->send_rtp_sink);
1711 rtpsession->send_rtp_src =
1712 gst_pad_new_from_static_template (&rtpsession_send_rtp_src_template,
1714 gst_pad_set_internal_link_function (rtpsession->send_rtp_src,
1715 gst_rtp_session_internal_links);
1716 gst_pad_set_active (rtpsession->send_rtp_src, TRUE);
1717 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->send_rtp_src);
1719 return rtpsession->send_rtp_sink;
1722 /* Create a srcpad with the RTCP packets to send out.
1723 * This pad will be driven by the RTP session manager when it wants to send out
1727 create_send_rtcp_src (GstRtpSession * rtpsession)
1729 GST_DEBUG_OBJECT (rtpsession, "creating pad");
1731 rtpsession->send_rtcp_src =
1732 gst_pad_new_from_static_template (&rtpsession_send_rtcp_src_template,
1734 gst_pad_use_fixed_caps (rtpsession->send_rtcp_src);
1735 gst_pad_set_active (rtpsession->send_rtcp_src, TRUE);
1736 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1737 rtpsession->send_rtcp_src);
1739 return rtpsession->send_rtcp_src;
1743 gst_rtp_session_request_new_pad (GstElement * element,
1744 GstPadTemplate * templ, const gchar * name)
1746 GstRtpSession *rtpsession;
1747 GstElementClass *klass;
1750 g_return_val_if_fail (templ != NULL, NULL);
1751 g_return_val_if_fail (GST_IS_RTP_SESSION (element), NULL);
1753 rtpsession = GST_RTP_SESSION (element);
1754 klass = GST_ELEMENT_GET_CLASS (element);
1756 GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
1758 GST_RTP_SESSION_LOCK (rtpsession);
1760 /* figure out the template */
1761 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink")) {
1762 if (rtpsession->recv_rtp_sink != NULL)
1765 result = create_recv_rtp_sink (rtpsession);
1766 } else if (templ == gst_element_class_get_pad_template (klass,
1767 "recv_rtcp_sink")) {
1768 if (rtpsession->recv_rtcp_sink != NULL)
1771 result = create_recv_rtcp_sink (rtpsession);
1772 } else if (templ == gst_element_class_get_pad_template (klass,
1774 if (rtpsession->send_rtp_sink != NULL)
1777 result = create_send_rtp_sink (rtpsession);
1778 } else if (templ == gst_element_class_get_pad_template (klass,
1780 if (rtpsession->send_rtcp_src != NULL)
1783 result = create_send_rtcp_src (rtpsession);
1785 goto wrong_template;
1787 GST_RTP_SESSION_UNLOCK (rtpsession);
1794 GST_RTP_SESSION_UNLOCK (rtpsession);
1795 g_warning ("gstrtpsession: this is not our template");
1800 GST_RTP_SESSION_UNLOCK (rtpsession);
1801 g_warning ("gstrtpsession: pad already requested");
1807 gst_rtp_session_release_pad (GstElement * element, GstPad * pad)