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);
394 g_value_unset (&val);
400 on_ssrc_sdes (RTPSession * session, RTPSource * src, GstRtpSession * sess)
405 /* convert the new SDES info into a message */
406 RTP_SESSION_LOCK (session);
407 s = source_get_sdes_structure (src);
408 RTP_SESSION_UNLOCK (session);
409 m = gst_message_new_custom (GST_MESSAGE_ELEMENT, GST_OBJECT (sess), s);
410 gst_element_post_message (GST_ELEMENT_CAST (sess), m);
412 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0,
417 on_bye_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
419 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0,
424 on_bye_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
426 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
431 on_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
433 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_TIMEOUT], 0,
437 GST_BOILERPLATE (GstRtpSession, gst_rtp_session, GstElement, GST_TYPE_ELEMENT);
440 gst_rtp_session_base_init (gpointer klass)
442 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
445 gst_element_class_add_pad_template (element_class,
446 gst_static_pad_template_get (&rtpsession_recv_rtp_sink_template));
447 gst_element_class_add_pad_template (element_class,
448 gst_static_pad_template_get (&rtpsession_recv_rtcp_sink_template));
449 gst_element_class_add_pad_template (element_class,
450 gst_static_pad_template_get (&rtpsession_send_rtp_sink_template));
453 gst_element_class_add_pad_template (element_class,
454 gst_static_pad_template_get (&rtpsession_recv_rtp_src_template));
455 gst_element_class_add_pad_template (element_class,
456 gst_static_pad_template_get (&rtpsession_sync_src_template));
457 gst_element_class_add_pad_template (element_class,
458 gst_static_pad_template_get (&rtpsession_send_rtp_src_template));
459 gst_element_class_add_pad_template (element_class,
460 gst_static_pad_template_get (&rtpsession_send_rtcp_src_template));
462 gst_element_class_set_details (element_class, &rtpsession_details);
466 gst_rtp_session_class_init (GstRtpSessionClass * klass)
468 GObjectClass *gobject_class;
469 GstElementClass *gstelement_class;
471 gobject_class = (GObjectClass *) klass;
472 gstelement_class = (GstElementClass *) klass;
474 g_type_class_add_private (klass, sizeof (GstRtpSessionPrivate));
476 gobject_class->finalize = gst_rtp_session_finalize;
477 gobject_class->set_property = gst_rtp_session_set_property;
478 gobject_class->get_property = gst_rtp_session_get_property;
481 * GstRtpSession::request-pt-map:
482 * @sess: the object which received the signal
485 * Request the payload type as #GstCaps for @pt.
487 gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP] =
488 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
489 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, request_pt_map),
490 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT, GST_TYPE_CAPS, 1,
493 * GstRtpSession::clear-pt-map:
494 * @sess: the object which received the signal
496 * Clear the cached pt-maps requested with GstRtpSession::request-pt-map.
498 gst_rtp_session_signals[SIGNAL_CLEAR_PT_MAP] =
499 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
500 G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpSessionClass, clear_pt_map),
501 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
504 * GstRtpSession::on-new-ssrc:
505 * @sess: the object which received the signal
508 * Notify of a new SSRC that entered @session.
510 gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
511 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
512 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_new_ssrc),
513 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
515 * GstRtpSession::on-ssrc_collision:
516 * @sess: the object which received the signal
519 * Notify when we have an SSRC collision
521 gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
522 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
523 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
524 on_ssrc_collision), NULL, NULL, g_cclosure_marshal_VOID__UINT,
525 G_TYPE_NONE, 1, G_TYPE_UINT);
527 * GstRtpSession::on-ssrc_validated:
528 * @sess: the object which received the signal
531 * Notify of a new SSRC that became validated.
533 gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
534 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
535 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
536 on_ssrc_validated), NULL, NULL, g_cclosure_marshal_VOID__UINT,
537 G_TYPE_NONE, 1, G_TYPE_UINT);
539 * GstRtpSession::on-ssrc_active:
540 * @sess: the object which received the signal
543 * Notify of a SSRC that is active, i.e., sending RTCP.
545 gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
546 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
547 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
548 on_ssrc_active), NULL, NULL, g_cclosure_marshal_VOID__UINT,
549 G_TYPE_NONE, 1, G_TYPE_UINT);
551 * GstRtpSession::on-ssrc-sdes:
552 * @session: the object which received the signal
555 * Notify that a new SDES was received for SSRC.
557 gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
558 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
559 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_ssrc_sdes),
560 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
563 * GstRtpSession::on-bye-ssrc:
564 * @sess: the object which received the signal
567 * Notify of an SSRC that became inactive because of a BYE packet.
569 gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
570 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
571 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_ssrc),
572 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
574 * GstRtpSession::on-bye-timeout:
575 * @sess: the object which received the signal
578 * Notify of an SSRC that has timed out because of BYE
580 gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
581 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
582 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_timeout),
583 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
585 * GstRtpSession::on-timeout:
586 * @sess: the object which received the signal
589 * Notify of an SSRC that has timed out
591 gst_rtp_session_signals[SIGNAL_ON_TIMEOUT] =
592 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
593 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_timeout),
594 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
596 g_object_class_install_property (gobject_class, PROP_NTP_NS_BASE,
597 g_param_spec_uint64 ("ntp-ns-base", "NTP base time",
598 "The NTP base time corresponding to running_time 0", 0,
599 G_MAXUINT64, DEFAULT_NTP_NS_BASE, G_PARAM_READWRITE));
601 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
602 g_param_spec_double ("bandwidth", "Bandwidth",
603 "The bandwidth of the session",
604 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH, G_PARAM_READWRITE));
606 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
607 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
608 "The fraction of the bandwidth used for RTCP",
609 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION, G_PARAM_READWRITE));
611 g_object_class_install_property (gobject_class, PROP_SDES_CNAME,
612 g_param_spec_string ("sdes-cname", "SDES CNAME",
613 "The CNAME to put in SDES messages of this session",
614 DEFAULT_SDES_CNAME, G_PARAM_READWRITE));
616 g_object_class_install_property (gobject_class, PROP_SDES_NAME,
617 g_param_spec_string ("sdes-name", "SDES NAME",
618 "The NAME to put in SDES messages of this session",
619 DEFAULT_SDES_NAME, G_PARAM_READWRITE));
621 g_object_class_install_property (gobject_class, PROP_SDES_EMAIL,
622 g_param_spec_string ("sdes-email", "SDES EMAIL",
623 "The EMAIL to put in SDES messages of this session",
624 DEFAULT_SDES_EMAIL, G_PARAM_READWRITE));
626 g_object_class_install_property (gobject_class, PROP_SDES_PHONE,
627 g_param_spec_string ("sdes-phone", "SDES PHONE",
628 "The PHONE to put in SDES messages of this session",
629 DEFAULT_SDES_PHONE, G_PARAM_READWRITE));
631 g_object_class_install_property (gobject_class, PROP_SDES_LOCATION,
632 g_param_spec_string ("sdes-location", "SDES LOCATION",
633 "The LOCATION to put in SDES messages of this session",
634 DEFAULT_SDES_LOCATION, G_PARAM_READWRITE));
636 g_object_class_install_property (gobject_class, PROP_SDES_TOOL,
637 g_param_spec_string ("sdes-tool", "SDES TOOL",
638 "The TOOL to put in SDES messages of this session",
639 DEFAULT_SDES_TOOL, G_PARAM_READWRITE));
641 g_object_class_install_property (gobject_class, PROP_SDES_NOTE,
642 g_param_spec_string ("sdes-note", "SDES NOTE",
643 "The NOTE to put in SDES messages of this session",
644 DEFAULT_SDES_NOTE, G_PARAM_READWRITE));
646 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
647 g_param_spec_uint ("num-sources", "Num Sources",
648 "The number of sources in the session", 0, G_MAXUINT,
649 DEFAULT_NUM_SOURCES, G_PARAM_READABLE));
651 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
652 g_param_spec_uint ("num-active-sources", "Num Active Sources",
653 "The number of active sources in the session", 0, G_MAXUINT,
654 DEFAULT_NUM_ACTIVE_SOURCES, G_PARAM_READABLE));
656 gstelement_class->change_state =
657 GST_DEBUG_FUNCPTR (gst_rtp_session_change_state);
658 gstelement_class->request_new_pad =
659 GST_DEBUG_FUNCPTR (gst_rtp_session_request_new_pad);
660 gstelement_class->release_pad =
661 GST_DEBUG_FUNCPTR (gst_rtp_session_release_pad);
663 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_session_clear_pt_map);
665 GST_DEBUG_CATEGORY_INIT (gst_rtp_session_debug,
666 "rtpsession", 0, "RTP Session");
670 gst_rtp_session_init (GstRtpSession * rtpsession, GstRtpSessionClass * klass)
672 rtpsession->priv = GST_RTP_SESSION_GET_PRIVATE (rtpsession);
673 rtpsession->priv->lock = g_mutex_new ();
674 rtpsession->priv->session = rtp_session_new ();
675 /* configure callbacks */
676 rtp_session_set_callbacks (rtpsession->priv->session, &callbacks, rtpsession);
677 /* configure signals */
678 g_signal_connect (rtpsession->priv->session, "on-new-ssrc",
679 (GCallback) on_new_ssrc, rtpsession);
680 g_signal_connect (rtpsession->priv->session, "on-ssrc-collision",
681 (GCallback) on_ssrc_collision, rtpsession);
682 g_signal_connect (rtpsession->priv->session, "on-ssrc-validated",
683 (GCallback) on_ssrc_validated, rtpsession);
684 g_signal_connect (rtpsession->priv->session, "on-ssrc-active",
685 (GCallback) on_ssrc_active, rtpsession);
686 g_signal_connect (rtpsession->priv->session, "on-ssrc-sdes",
687 (GCallback) on_ssrc_sdes, rtpsession);
688 g_signal_connect (rtpsession->priv->session, "on-bye-ssrc",
689 (GCallback) on_bye_ssrc, rtpsession);
690 g_signal_connect (rtpsession->priv->session, "on-bye-timeout",
691 (GCallback) on_bye_timeout, rtpsession);
692 g_signal_connect (rtpsession->priv->session, "on-timeout",
693 (GCallback) on_timeout, rtpsession);
694 rtpsession->priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
695 (GDestroyNotify) gst_caps_unref);
697 gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
698 gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
700 rtpsession->priv->thread_stopped = TRUE;
704 gst_rtp_session_finalize (GObject * object)
706 GstRtpSession *rtpsession;
708 rtpsession = GST_RTP_SESSION (object);
710 if (rtpsession->recv_rtp_sink != NULL)
711 gst_object_unref (rtpsession->recv_rtp_sink);
712 if (rtpsession->recv_rtcp_sink != NULL)
713 gst_object_unref (rtpsession->recv_rtcp_sink);
714 if (rtpsession->send_rtp_sink != NULL)
715 gst_object_unref (rtpsession->send_rtp_sink);
716 if (rtpsession->send_rtcp_src != NULL)
717 gst_object_unref (rtpsession->send_rtcp_src);
719 g_hash_table_destroy (rtpsession->priv->ptmap);
720 g_mutex_free (rtpsession->priv->lock);
721 g_object_unref (rtpsession->priv->session);
723 G_OBJECT_CLASS (parent_class)->finalize (object);
727 gst_rtp_session_set_property (GObject * object, guint prop_id,
728 const GValue * value, GParamSpec * pspec)
730 GstRtpSession *rtpsession;
731 GstRtpSessionPrivate *priv;
733 rtpsession = GST_RTP_SESSION (object);
734 priv = rtpsession->priv;
737 case PROP_NTP_NS_BASE:
738 GST_OBJECT_LOCK (rtpsession);
739 priv->ntpnsbase = g_value_get_uint64 (value);
740 GST_DEBUG_OBJECT (rtpsession, "setting NTP base to %" GST_TIME_FORMAT,
741 GST_TIME_ARGS (priv->ntpnsbase));
742 GST_OBJECT_UNLOCK (rtpsession);
745 rtp_session_set_bandwidth (priv->session, g_value_get_double (value));
747 case PROP_RTCP_FRACTION:
748 rtp_session_set_rtcp_fraction (priv->session, g_value_get_double (value));
750 case PROP_SDES_CNAME:
751 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_CNAME,
752 g_value_get_string (value));
755 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_NAME,
756 g_value_get_string (value));
758 case PROP_SDES_EMAIL:
759 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_EMAIL,
760 g_value_get_string (value));
762 case PROP_SDES_PHONE:
763 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_PHONE,
764 g_value_get_string (value));
766 case PROP_SDES_LOCATION:
767 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_LOC,
768 g_value_get_string (value));
771 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_TOOL,
772 g_value_get_string (value));
775 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_NOTE,
776 g_value_get_string (value));
779 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
785 gst_rtp_session_get_property (GObject * object, guint prop_id,
786 GValue * value, GParamSpec * pspec)
788 GstRtpSession *rtpsession;
789 GstRtpSessionPrivate *priv;
791 rtpsession = GST_RTP_SESSION (object);
792 priv = rtpsession->priv;
795 case PROP_NTP_NS_BASE:
796 GST_OBJECT_LOCK (rtpsession);
797 g_value_set_uint64 (value, priv->ntpnsbase);
798 GST_OBJECT_UNLOCK (rtpsession);
801 g_value_set_double (value, rtp_session_get_bandwidth (priv->session));
803 case PROP_RTCP_FRACTION:
804 g_value_set_double (value, rtp_session_get_rtcp_fraction (priv->session));
806 case PROP_SDES_CNAME:
807 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
808 GST_RTCP_SDES_CNAME));
811 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
812 GST_RTCP_SDES_NAME));
814 case PROP_SDES_EMAIL:
815 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
816 GST_RTCP_SDES_EMAIL));
818 case PROP_SDES_PHONE:
819 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
820 GST_RTCP_SDES_PHONE));
822 case PROP_SDES_LOCATION:
823 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
827 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
828 GST_RTCP_SDES_TOOL));
831 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
832 GST_RTCP_SDES_NOTE));
834 case PROP_NUM_SOURCES:
835 g_value_set_uint (value, rtp_session_get_num_sources (priv->session));
837 case PROP_NUM_ACTIVE_SOURCES:
838 g_value_set_uint (value,
839 rtp_session_get_num_active_sources (priv->session));
842 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
848 get_current_ntp_ns_time (GstRtpSession * rtpsession)
852 GstClockTime base_time, ntpnsbase;
854 GST_OBJECT_LOCK (rtpsession);
855 if ((clock = GST_ELEMENT_CLOCK (rtpsession))) {
856 base_time = GST_ELEMENT_CAST (rtpsession)->base_time;
857 ntpnsbase = rtpsession->priv->ntpnsbase;
858 gst_object_ref (clock);
859 GST_OBJECT_UNLOCK (rtpsession);
861 /* get current NTP time */
862 ntpnstime = gst_clock_get_time (clock);
863 /* convert to running time */
864 ntpnstime -= base_time;
865 /* add NTP base offset */
866 ntpnstime += ntpnsbase;
868 gst_object_unref (clock);
870 GST_OBJECT_UNLOCK (rtpsession);
878 rtcp_thread (GstRtpSession * rtpsession)
882 GstClockTime current_time;
883 GstClockTime next_timeout;
886 /* for RTCP timeouts we use the system clock */
887 sysclock = gst_system_clock_obtain ();
888 if (sysclock == NULL)
891 current_time = gst_clock_get_time (sysclock);
893 GST_DEBUG_OBJECT (rtpsession, "entering RTCP thread");
895 GST_RTP_SESSION_LOCK (rtpsession);
897 while (!rtpsession->priv->stop_thread) {
900 /* get initial estimate */
902 rtp_session_next_timeout (rtpsession->priv->session, current_time);
904 GST_DEBUG_OBJECT (rtpsession, "next check time %" GST_TIME_FORMAT,
905 GST_TIME_ARGS (next_timeout));
907 /* leave if no more timeouts, the session ended */
908 if (next_timeout == GST_CLOCK_TIME_NONE)
911 id = rtpsession->priv->id =
912 gst_clock_new_single_shot_id (sysclock, next_timeout);
913 GST_RTP_SESSION_UNLOCK (rtpsession);
915 res = gst_clock_id_wait (id, NULL);
917 GST_RTP_SESSION_LOCK (rtpsession);
918 gst_clock_id_unref (id);
919 rtpsession->priv->id = NULL;
921 if (rtpsession->priv->stop_thread)
924 /* update current time */
925 current_time = gst_clock_get_time (sysclock);
927 /* get current NTP time */
928 ntpnstime = get_current_ntp_ns_time (rtpsession);
930 /* we get unlocked because we need to perform reconsideration, don't perform
931 * the timeout but get a new reporting estimate. */
932 GST_DEBUG_OBJECT (rtpsession, "unlocked %d, current %" GST_TIME_FORMAT,
933 res, GST_TIME_ARGS (current_time));
935 /* perform actions, we ignore result. Release lock because it might push. */
936 GST_RTP_SESSION_UNLOCK (rtpsession);
937 rtp_session_on_timeout (rtpsession->priv->session, current_time, ntpnstime);
938 GST_RTP_SESSION_LOCK (rtpsession);
940 /* mark the thread as stopped now */
941 rtpsession->priv->thread_stopped = TRUE;
942 GST_RTP_SESSION_UNLOCK (rtpsession);
944 gst_object_unref (sysclock);
946 GST_DEBUG_OBJECT (rtpsession, "leaving RTCP thread");
952 GST_ELEMENT_ERROR (rtpsession, CORE, CLOCK, (NULL),
953 ("Could not get system clock"));
959 start_rtcp_thread (GstRtpSession * rtpsession)
961 GError *error = NULL;
964 GST_DEBUG_OBJECT (rtpsession, "starting RTCP thread");
966 GST_RTP_SESSION_LOCK (rtpsession);
967 rtpsession->priv->stop_thread = FALSE;
968 if (rtpsession->priv->thread_stopped) {
969 /* only create a new thread if the old one was stopped. Otherwise we can
970 * just reuse the currently running one. */
971 rtpsession->priv->thread =
972 g_thread_create ((GThreadFunc) rtcp_thread, rtpsession, TRUE, &error);
973 rtpsession->priv->thread_stopped = FALSE;
975 GST_RTP_SESSION_UNLOCK (rtpsession);
979 GST_DEBUG_OBJECT (rtpsession, "failed to start thread, %s", error->message);
980 g_error_free (error);
988 stop_rtcp_thread (GstRtpSession * rtpsession)
990 GST_DEBUG_OBJECT (rtpsession, "stopping RTCP thread");
992 GST_RTP_SESSION_LOCK (rtpsession);
993 rtpsession->priv->stop_thread = TRUE;
994 if (rtpsession->priv->id)
995 gst_clock_id_unschedule (rtpsession->priv->id);
996 GST_RTP_SESSION_UNLOCK (rtpsession);
1000 join_rtcp_thread (GstRtpSession * rtpsession)
1002 GST_RTP_SESSION_LOCK (rtpsession);
1003 /* don't try to join when we have no thread */
1004 if (rtpsession->priv->thread != NULL) {
1005 GST_DEBUG_OBJECT (rtpsession, "joining RTCP thread");
1006 GST_RTP_SESSION_UNLOCK (rtpsession);
1008 g_thread_join (rtpsession->priv->thread);
1010 GST_RTP_SESSION_LOCK (rtpsession);
1011 /* after the join, take the lock and clear the thread structure. The caller
1012 * is supposed to not concurrently call start and join. */
1013 rtpsession->priv->thread = NULL;
1015 GST_RTP_SESSION_UNLOCK (rtpsession);
1018 static GstStateChangeReturn
1019 gst_rtp_session_change_state (GstElement * element, GstStateChange transition)
1021 GstStateChangeReturn res;
1022 GstRtpSession *rtpsession;
1023 GstRtpSessionPrivate *priv;
1025 rtpsession = GST_RTP_SESSION (element);
1026 priv = rtpsession->priv;
1028 switch (transition) {
1029 case GST_STATE_CHANGE_NULL_TO_READY:
1031 case GST_STATE_CHANGE_READY_TO_PAUSED:
1033 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1035 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1036 case GST_STATE_CHANGE_PAUSED_TO_READY:
1037 /* no need to join yet, we might want to continue later. Also, the
1038 * dataflow could block downstream so that a join could just block
1040 stop_rtcp_thread (rtpsession);
1046 res = parent_class->change_state (element, transition);
1048 switch (transition) {
1049 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1050 if (!start_rtcp_thread (rtpsession))
1053 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1055 case GST_STATE_CHANGE_PAUSED_TO_READY:
1056 /* downstream is now releasing the dataflow and we can join. */
1057 join_rtcp_thread (rtpsession);
1059 case GST_STATE_CHANGE_READY_TO_NULL:
1069 return GST_STATE_CHANGE_FAILURE;
1074 return_true (gpointer key, gpointer value, gpointer user_data)
1080 gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession)
1082 g_hash_table_foreach_remove (rtpsession->priv->ptmap, return_true, NULL);
1085 /* called when the session manager has an RTP packet ready for further
1087 static GstFlowReturn
1088 gst_rtp_session_process_rtp (RTPSession * sess, RTPSource * src,
1089 GstBuffer * buffer, gpointer user_data)
1091 GstFlowReturn result;
1092 GstRtpSession *rtpsession;
1093 GstRtpSessionPrivate *priv;
1095 rtpsession = GST_RTP_SESSION (user_data);
1096 priv = rtpsession->priv;
1098 if (rtpsession->recv_rtp_src) {
1099 GST_DEBUG_OBJECT (rtpsession, "pushing received RTP packet");
1100 result = gst_pad_push (rtpsession->recv_rtp_src, buffer);
1102 GST_DEBUG_OBJECT (rtpsession, "dropping received RTP packet");
1103 gst_buffer_unref (buffer);
1104 result = GST_FLOW_OK;
1109 /* called when the session manager has an RTP packet ready for further
1111 static GstFlowReturn
1112 gst_rtp_session_send_rtp (RTPSession * sess, RTPSource * src,
1113 GstBuffer * buffer, gpointer user_data)
1115 GstFlowReturn result;
1116 GstRtpSession *rtpsession;
1117 GstRtpSessionPrivate *priv;
1119 rtpsession = GST_RTP_SESSION (user_data);
1120 priv = rtpsession->priv;
1122 GST_DEBUG_OBJECT (rtpsession, "sending RTP packet");
1124 if (rtpsession->send_rtp_src) {
1125 result = gst_pad_push (rtpsession->send_rtp_src, buffer);
1127 gst_buffer_unref (buffer);
1128 result = GST_FLOW_OK;
1133 /* called when the session manager has an RTCP packet ready for further
1135 static GstFlowReturn
1136 gst_rtp_session_send_rtcp (RTPSession * sess, RTPSource * src,
1137 GstBuffer * buffer, gpointer user_data)
1139 GstFlowReturn result;
1140 GstRtpSession *rtpsession;
1141 GstRtpSessionPrivate *priv;
1143 rtpsession = GST_RTP_SESSION (user_data);
1144 priv = rtpsession->priv;
1146 if (rtpsession->send_rtcp_src) {
1149 /* set rtcp caps on output pad */
1150 if (!(caps = GST_PAD_CAPS (rtpsession->send_rtcp_src))) {
1151 caps = gst_caps_new_simple ("application/x-rtcp", NULL);
1152 gst_pad_set_caps (rtpsession->send_rtcp_src, caps);
1153 gst_caps_unref (caps);
1155 gst_buffer_set_caps (buffer, caps);
1156 GST_DEBUG_OBJECT (rtpsession, "sending RTCP");
1157 result = gst_pad_push (rtpsession->send_rtcp_src, buffer);
1159 GST_DEBUG_OBJECT (rtpsession, "not sending RTCP, no output pad");
1160 gst_buffer_unref (buffer);
1161 result = GST_FLOW_OK;
1166 /* called when the session manager has an SR RTCP packet ready for handling
1167 * inter stream synchronisation */
1168 static GstFlowReturn
1169 gst_rtp_session_sync_rtcp (RTPSession * sess,
1170 RTPSource * src, GstBuffer * buffer, gpointer user_data)
1172 GstFlowReturn result;
1173 GstRtpSession *rtpsession;
1174 GstRtpSessionPrivate *priv;
1176 rtpsession = GST_RTP_SESSION (user_data);
1177 priv = rtpsession->priv;
1179 if (rtpsession->sync_src) {
1182 /* set rtcp caps on output pad */
1183 if (!(caps = GST_PAD_CAPS (rtpsession->sync_src))) {
1184 caps = gst_caps_new_simple ("application/x-rtcp", NULL);
1185 gst_pad_set_caps (rtpsession->sync_src, caps);
1186 gst_caps_unref (caps);
1188 gst_buffer_set_caps (buffer, caps);
1189 GST_DEBUG_OBJECT (rtpsession, "sending Sync RTCP");
1190 result = gst_pad_push (rtpsession->sync_src, buffer);
1192 GST_DEBUG_OBJECT (rtpsession, "not sending Sync RTCP, no output pad");
1193 gst_buffer_unref (buffer);
1194 result = GST_FLOW_OK;
1200 gst_rtp_session_cache_caps (GstRtpSession * rtpsession, GstCaps * caps)
1202 GstRtpSessionPrivate *priv;
1203 const GstStructure *s;
1206 priv = rtpsession->priv;
1208 GST_DEBUG_OBJECT (rtpsession, "parsing caps");
1210 s = gst_caps_get_structure (caps, 0);
1211 if (!gst_structure_get_int (s, "payload", &payload))
1214 if (g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (payload)))
1217 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (payload),
1218 gst_caps_ref (caps));
1221 /* called when the session manager needs the clock rate */
1223 gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
1226 gint ipayload, result = -1;
1227 GstRtpSession *rtpsession;
1228 GstRtpSessionPrivate *priv;
1230 GValue args[2] = { {0}, {0} };
1232 const GstStructure *s;
1234 rtpsession = GST_RTP_SESSION_CAST (user_data);
1235 priv = rtpsession->priv;
1237 GST_RTP_SESSION_LOCK (rtpsession);
1238 ipayload = payload; /* make compiler happy */
1239 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (ipayload));
1241 gst_caps_ref (caps);
1245 /* not found in the cache, try to get it with a signal */
1246 g_value_init (&args[0], GST_TYPE_ELEMENT);
1247 g_value_set_object (&args[0], rtpsession);
1248 g_value_init (&args[1], G_TYPE_UINT);
1249 g_value_set_uint (&args[1], payload);
1251 g_value_init (&ret, GST_TYPE_CAPS);
1252 g_value_set_boxed (&ret, NULL);
1254 g_signal_emitv (args, gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP], 0,
1257 g_value_unset (&args[0]);
1258 g_value_unset (&args[1]);
1259 caps = (GstCaps *) g_value_dup_boxed (&ret);
1260 g_value_unset (&ret);
1264 gst_rtp_session_cache_caps (rtpsession, caps);
1267 s = gst_caps_get_structure (caps, 0);
1268 if (!gst_structure_get_int (s, "clock-rate", &result))
1271 gst_caps_unref (caps);
1273 GST_DEBUG_OBJECT (rtpsession, "parsed clock-rate %d", result);
1276 GST_RTP_SESSION_UNLOCK (rtpsession);
1283 GST_DEBUG_OBJECT (rtpsession, "could not get caps");
1288 gst_caps_unref (caps);
1289 GST_DEBUG_OBJECT (rtpsession, "No clock-rate in caps!");
1294 /* called when the session manager asks us to reconsider the timeout */
1296 gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data)
1298 GstRtpSession *rtpsession;
1300 rtpsession = GST_RTP_SESSION_CAST (user_data);
1302 GST_RTP_SESSION_LOCK (rtpsession);
1303 GST_DEBUG_OBJECT (rtpsession, "unlock timer for reconsideration");
1304 if (rtpsession->priv->id)
1305 gst_clock_id_unschedule (rtpsession->priv->id);
1306 GST_RTP_SESSION_UNLOCK (rtpsession);
1310 gst_rtp_session_event_recv_rtp_sink (GstPad * pad, GstEvent * event)
1312 GstRtpSession *rtpsession;
1313 GstRtpSessionPrivate *priv;
1314 gboolean ret = FALSE;
1316 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1317 priv = rtpsession->priv;
1319 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1320 GST_EVENT_TYPE_NAME (event));
1322 switch (GST_EVENT_TYPE (event)) {
1323 case GST_EVENT_FLUSH_STOP:
1324 gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
1325 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1327 case GST_EVENT_NEWSEGMENT:
1330 gdouble rate, arate;
1332 gint64 start, stop, time;
1333 GstSegment *segment;
1335 segment = &rtpsession->recv_rtp_seg;
1337 /* the newsegment event is needed to convert the RTP timestamp to
1338 * running_time, which is needed to generate a mapping from RTP to NTP
1339 * timestamps in SR reports */
1340 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1341 &start, &stop, &time);
1343 GST_DEBUG_OBJECT (rtpsession,
1344 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1345 "format GST_FORMAT_TIME, "
1346 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1347 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1348 update, rate, arate, GST_TIME_ARGS (segment->start),
1349 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1350 GST_TIME_ARGS (segment->accum));
1352 gst_segment_set_newsegment_full (segment, update, rate,
1353 arate, format, start, stop, time);
1355 /* push event forward */
1356 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1360 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1363 gst_object_unref (rtpsession);
1369 gst_rtp_session_internal_links (GstPad * pad)
1371 GstRtpSession *rtpsession;
1372 GstRtpSessionPrivate *priv;
1375 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1376 priv = rtpsession->priv;
1378 if (pad == rtpsession->recv_rtp_src) {
1379 res = g_list_prepend (res, rtpsession->recv_rtp_sink);
1380 } else if (pad == rtpsession->recv_rtp_sink) {
1381 res = g_list_prepend (res, rtpsession->recv_rtp_src);
1382 } else if (pad == rtpsession->send_rtp_src) {
1383 res = g_list_prepend (res, rtpsession->send_rtp_sink);
1384 } else if (pad == rtpsession->send_rtp_sink) {
1385 res = g_list_prepend (res, rtpsession->send_rtp_src);
1388 gst_object_unref (rtpsession);
1394 gst_rtp_session_sink_setcaps (GstPad * pad, GstCaps * caps)
1396 GstRtpSession *rtpsession;
1397 GstRtpSessionPrivate *priv;
1399 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1400 priv = rtpsession->priv;
1402 GST_RTP_SESSION_LOCK (rtpsession);
1403 gst_rtp_session_cache_caps (rtpsession, caps);
1404 GST_RTP_SESSION_UNLOCK (rtpsession);
1406 gst_object_unref (rtpsession);
1411 /* receive a packet from a sender, send it to the RTP session manager and
1412 * forward the packet on the rtp_src pad
1414 static GstFlowReturn
1415 gst_rtp_session_chain_recv_rtp (GstPad * pad, GstBuffer * buffer)
1417 GstRtpSession *rtpsession;
1418 GstRtpSessionPrivate *priv;
1421 GstClockTime timestamp;
1423 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1424 priv = rtpsession->priv;
1426 GST_DEBUG_OBJECT (rtpsession, "received RTP packet");
1428 /* get NTP time when this packet was captured, this depends on the timestamp. */
1429 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1430 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1431 /* convert to running time using the segment values */
1433 gst_segment_to_running_time (&rtpsession->recv_rtp_seg, GST_FORMAT_TIME,
1435 /* add constant to convert running time to NTP time */
1436 ntpnstime += priv->ntpnsbase;
1438 ntpnstime = get_current_ntp_ns_time (rtpsession);
1441 ret = rtp_session_process_rtp (priv->session, buffer, ntpnstime);
1442 if (ret != GST_FLOW_OK)
1447 gst_object_unref (rtpsession);
1454 GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1455 gst_flow_get_name (ret));
1461 gst_rtp_session_event_recv_rtcp_sink (GstPad * pad, GstEvent * event)
1463 GstRtpSession *rtpsession;
1464 GstRtpSessionPrivate *priv;
1465 gboolean ret = FALSE;
1467 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1468 priv = rtpsession->priv;
1470 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1471 GST_EVENT_TYPE_NAME (event));
1473 switch (GST_EVENT_TYPE (event)) {
1475 if (rtpsession->send_rtcp_src) {
1476 gst_event_ref (event);
1477 ret = gst_pad_push_event (rtpsession->send_rtcp_src, event);
1479 ret = gst_pad_push_event (rtpsession->sync_src, event);
1482 gst_object_unref (rtpsession);
1487 /* Receive an RTCP packet from a sender, send it to the RTP session manager and
1488 * forward the SR packets to the sync_src pad.
1490 static GstFlowReturn
1491 gst_rtp_session_chain_recv_rtcp (GstPad * pad, GstBuffer * buffer)
1493 GstRtpSession *rtpsession;
1494 GstRtpSessionPrivate *priv;
1497 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1498 priv = rtpsession->priv;
1500 GST_DEBUG_OBJECT (rtpsession, "received RTCP packet");
1502 ret = rtp_session_process_rtcp (priv->session, buffer);
1504 gst_object_unref (rtpsession);
1510 gst_rtp_session_event_send_rtp_sink (GstPad * pad, GstEvent * event)
1512 GstRtpSession *rtpsession;
1513 GstRtpSessionPrivate *priv;
1514 gboolean ret = FALSE;
1516 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1517 priv = rtpsession->priv;
1519 GST_DEBUG_OBJECT (rtpsession, "received event");
1521 switch (GST_EVENT_TYPE (event)) {
1522 case GST_EVENT_FLUSH_STOP:
1523 gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
1524 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1526 case GST_EVENT_NEWSEGMENT:
1529 gdouble rate, arate;
1531 gint64 start, stop, time;
1532 GstSegment *segment;
1534 segment = &rtpsession->send_rtp_seg;
1536 /* the newsegment event is needed to convert the RTP timestamp to
1537 * running_time, which is needed to generate a mapping from RTP to NTP
1538 * timestamps in SR reports */
1539 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1540 &start, &stop, &time);
1542 GST_DEBUG_OBJECT (rtpsession,
1543 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1544 "format GST_FORMAT_TIME, "
1545 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1546 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1547 update, rate, arate, GST_TIME_ARGS (segment->start),
1548 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1549 GST_TIME_ARGS (segment->accum));
1551 gst_segment_set_newsegment_full (segment, update, rate,
1552 arate, format, start, stop, time);
1554 /* push event forward */
1555 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1559 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1562 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1565 gst_object_unref (rtpsession);
1571 gst_rtp_session_getcaps_send_rtp (GstPad * pad)
1573 GstRtpSession *rtpsession;
1574 GstRtpSessionPrivate *priv;
1576 GstStructure *s1, *s2;
1578 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1579 priv = rtpsession->priv;
1581 /* we can basically accept anything but we prefer to receive packets with our
1582 * internal SSRC so that we don't have to patch it. Create a structure with
1583 * the SSRC and another one without. */
1584 s1 = gst_structure_new ("application/x-rtp",
1585 "ssrc", G_TYPE_UINT, priv->session->source->ssrc, NULL);
1586 s2 = gst_structure_new ("application/x-rtp", NULL);
1588 result = gst_caps_new_full (s1, s2, NULL);
1590 GST_DEBUG_OBJECT (rtpsession, "getting caps %" GST_PTR_FORMAT, result);
1592 gst_object_unref (rtpsession);
1597 /* Recieve an RTP packet to be send to the receivers, send to RTP session
1598 * manager and forward to send_rtp_src.
1600 static GstFlowReturn
1601 gst_rtp_session_chain_send_rtp (GstPad * pad, GstBuffer * buffer)
1603 GstRtpSession *rtpsession;
1604 GstRtpSessionPrivate *priv;
1606 GstClockTime timestamp;
1609 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1610 priv = rtpsession->priv;
1612 GST_DEBUG_OBJECT (rtpsession, "received RTP packet");
1614 /* get NTP time when this packet was captured, this depends on the timestamp. */
1615 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1616 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1617 /* convert to running time using the segment start value. */
1619 gst_segment_to_running_time (&rtpsession->send_rtp_seg, GST_FORMAT_TIME,
1621 /* convert to NTP time by adding the NTP base */
1622 ntpnstime += priv->ntpnsbase;
1624 /* no timestamp, we could take the current running_time and convert it to
1629 ret = rtp_session_send_rtp (priv->session, buffer, ntpnstime);
1630 if (ret != GST_FLOW_OK)
1634 gst_object_unref (rtpsession);
1641 GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1642 gst_flow_get_name (ret));
1647 /* Create sinkpad to receive RTP packets from senders. This will also create a
1648 * srcpad for the RTP packets.
1651 create_recv_rtp_sink (GstRtpSession * rtpsession)
1653 GST_DEBUG_OBJECT (rtpsession, "creating RTP sink pad");
1655 rtpsession->recv_rtp_sink =
1656 gst_pad_new_from_static_template (&rtpsession_recv_rtp_sink_template,
1658 gst_pad_set_chain_function (rtpsession->recv_rtp_sink,
1659 gst_rtp_session_chain_recv_rtp);
1660 gst_pad_set_event_function (rtpsession->recv_rtp_sink,
1661 (GstPadEventFunction) gst_rtp_session_event_recv_rtp_sink);
1662 gst_pad_set_setcaps_function (rtpsession->recv_rtp_sink,
1663 gst_rtp_session_sink_setcaps);
1664 gst_pad_set_internal_link_function (rtpsession->recv_rtp_sink,
1665 gst_rtp_session_internal_links);
1666 gst_pad_set_active (rtpsession->recv_rtp_sink, TRUE);
1667 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1668 rtpsession->recv_rtp_sink);
1670 GST_DEBUG_OBJECT (rtpsession, "creating RTP src pad");
1671 rtpsession->recv_rtp_src =
1672 gst_pad_new_from_static_template (&rtpsession_recv_rtp_src_template,
1674 gst_pad_set_internal_link_function (rtpsession->recv_rtp_src,
1675 gst_rtp_session_internal_links);
1676 gst_pad_use_fixed_caps (rtpsession->recv_rtp_src);
1677 gst_pad_set_active (rtpsession->recv_rtp_src, TRUE);
1678 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->recv_rtp_src);
1680 return rtpsession->recv_rtp_sink;
1683 /* Create a sinkpad to receive RTCP messages from senders, this will also create a
1684 * sync_src pad for the SR packets.
1687 create_recv_rtcp_sink (GstRtpSession * rtpsession)
1689 GST_DEBUG_OBJECT (rtpsession, "creating RTCP sink pad");
1691 rtpsession->recv_rtcp_sink =
1692 gst_pad_new_from_static_template (&rtpsession_recv_rtcp_sink_template,
1694 gst_pad_set_chain_function (rtpsession->recv_rtcp_sink,
1695 gst_rtp_session_chain_recv_rtcp);
1696 gst_pad_set_event_function (rtpsession->recv_rtcp_sink,
1697 (GstPadEventFunction) gst_rtp_session_event_recv_rtcp_sink);
1698 gst_pad_set_active (rtpsession->recv_rtcp_sink, TRUE);
1699 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1700 rtpsession->recv_rtcp_sink);
1702 GST_DEBUG_OBJECT (rtpsession, "creating sync src pad");
1703 rtpsession->sync_src =
1704 gst_pad_new_from_static_template (&rtpsession_sync_src_template,
1706 gst_pad_use_fixed_caps (rtpsession->sync_src);
1707 gst_pad_set_active (rtpsession->sync_src, TRUE);
1708 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
1710 return rtpsession->recv_rtcp_sink;
1713 /* Create a sinkpad to receive RTP packets for receivers. This will also create a
1717 create_send_rtp_sink (GstRtpSession * rtpsession)
1719 GST_DEBUG_OBJECT (rtpsession, "creating pad");
1721 rtpsession->send_rtp_sink =
1722 gst_pad_new_from_static_template (&rtpsession_send_rtp_sink_template,
1724 gst_pad_set_chain_function (rtpsession->send_rtp_sink,
1725 gst_rtp_session_chain_send_rtp);
1726 gst_pad_set_getcaps_function (rtpsession->send_rtp_sink,
1727 gst_rtp_session_getcaps_send_rtp);
1728 gst_pad_set_event_function (rtpsession->send_rtp_sink,
1729 (GstPadEventFunction) gst_rtp_session_event_send_rtp_sink);
1730 gst_pad_set_internal_link_function (rtpsession->send_rtp_sink,
1731 gst_rtp_session_internal_links);
1732 gst_pad_set_active (rtpsession->send_rtp_sink, TRUE);
1733 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1734 rtpsession->send_rtp_sink);
1736 rtpsession->send_rtp_src =
1737 gst_pad_new_from_static_template (&rtpsession_send_rtp_src_template,
1739 gst_pad_set_internal_link_function (rtpsession->send_rtp_src,
1740 gst_rtp_session_internal_links);
1741 gst_pad_set_active (rtpsession->send_rtp_src, TRUE);
1742 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->send_rtp_src);
1744 return rtpsession->send_rtp_sink;
1747 /* Create a srcpad with the RTCP packets to send out.
1748 * This pad will be driven by the RTP session manager when it wants to send out
1752 create_send_rtcp_src (GstRtpSession * rtpsession)
1754 GST_DEBUG_OBJECT (rtpsession, "creating pad");
1756 rtpsession->send_rtcp_src =
1757 gst_pad_new_from_static_template (&rtpsession_send_rtcp_src_template,
1759 gst_pad_use_fixed_caps (rtpsession->send_rtcp_src);
1760 gst_pad_set_active (rtpsession->send_rtcp_src, TRUE);
1761 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1762 rtpsession->send_rtcp_src);
1764 return rtpsession->send_rtcp_src;
1768 gst_rtp_session_request_new_pad (GstElement * element,
1769 GstPadTemplate * templ, const gchar * name)
1771 GstRtpSession *rtpsession;
1772 GstElementClass *klass;
1775 g_return_val_if_fail (templ != NULL, NULL);
1776 g_return_val_if_fail (GST_IS_RTP_SESSION (element), NULL);
1778 rtpsession = GST_RTP_SESSION (element);
1779 klass = GST_ELEMENT_GET_CLASS (element);
1781 GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
1783 GST_RTP_SESSION_LOCK (rtpsession);
1785 /* figure out the template */
1786 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink")) {
1787 if (rtpsession->recv_rtp_sink != NULL)
1790 result = create_recv_rtp_sink (rtpsession);
1791 } else if (templ == gst_element_class_get_pad_template (klass,
1792 "recv_rtcp_sink")) {
1793 if (rtpsession->recv_rtcp_sink != NULL)
1796 result = create_recv_rtcp_sink (rtpsession);
1797 } else if (templ == gst_element_class_get_pad_template (klass,
1799 if (rtpsession->send_rtp_sink != NULL)
1802 result = create_send_rtp_sink (rtpsession);
1803 } else if (templ == gst_element_class_get_pad_template (klass,
1805 if (rtpsession->send_rtcp_src != NULL)
1808 result = create_send_rtcp_src (rtpsession);
1810 goto wrong_template;
1812 GST_RTP_SESSION_UNLOCK (rtpsession);
1819 GST_RTP_SESSION_UNLOCK (rtpsession);
1820 g_warning ("gstrtpsession: this is not our template");
1825 GST_RTP_SESSION_UNLOCK (rtpsession);
1826 g_warning ("gstrtpsession: pad already requested");
1832 gst_rtp_session_release_pad (GstElement * element, GstPad * pad)