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 * @see_also: gstrtpjitterbuffer, gstrtpbin, gstrtpptdemux, gstrtpssrcdemux
24 * The RTP session manager models one participant with a unique SSRC in an RTP
25 * session. This session can be used to send and receive RTP and RTCP packets.
26 * Based on what REQUEST pads are requested from the session manager, specific
27 * functionality can be activated.
29 * The session manager currently implements RFC 3550 including:
32 * <para>RTP packet validation based on consecutive sequence numbers.</para>
35 * <para>Maintainance of the SSRC participant database.</para>
38 * <para>Keeping per participant statistics based on received RTCP packets.</para>
41 * <para>Scheduling of RR/SR RTCP packets.</para>
45 * The gstrtpsession will not demux packets based on SSRC or payload type, nor will
46 * it correct for packet reordering and jitter. Use #GstRtpsSrcDemux,
47 * #GstRtpPtDemux and GstRtpJitterBuffer in addition to #GstRtpSession to
48 * perform these tasks. It is usually a good idea to use #GstRtpBin, which
49 * combines all these features in one element.
51 * To use #GstRtpSession as an RTP receiver, request a recv_rtp_sink pad, which will
52 * automatically create recv_rtp_src pad. Data received on the recv_rtp_sink pad
53 * will be processed in the session and after being validated forwarded on the
56 * To also use #GstRtpSession as an RTCP receiver, request a recv_rtcp_sink pad,
57 * which will automatically create a sync_src pad. Packets received on the RTCP
58 * pad will be used by the session manager to update the stats and database of
59 * the other participants. SR packets will be forwarded on the sync_src pad
60 * so that they can be used to perform inter-stream synchronisation when needed.
62 * If you want the session manager to generate and send RTCP packets, request
63 * the send_rtcp_src pad. Packet pushed on this pad contain SR/RR RTCP reports
64 * that should be sent to all participants in the session.
66 * To use #GstRtpSession as a sender, request a send_rtp_sink pad, which will
67 * automatically create a send_rtp_src pad. The session manager will modify the
68 * SSRC in the RTP packets to its own SSRC and wil forward the packets on the
69 * send_rtp_src pad after updating its internal state.
71 * The session manager needs the clock-rate of the payload types it is handling
72 * and will signal the #GstRtpSession::request-pt-map signal when it needs such a
73 * mapping. One can clear the cached values with the #GstRtpSession::clear-pt-map
77 * <title>Example pipelines</title>
79 * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink gstrtpsession .recv_rtp_src ! rtptheoradepay ! theoradec ! xvimagesink
80 * ]| Receive theora RTP packets from port 5000 and send them to the depayloader,
81 * decoder and display. Note that the application/x-rtp caps on udpsrc should be
82 * configured based on some negotiation process such as RTSP for this pipeline
85 * gst-launch udpsrc port=5000 caps="application/x-rtp, ..." ! .recv_rtp_sink gstrtpsession name=session \
86 * .recv_rtp_src ! rtptheoradepay ! theoradec ! xvimagesink \
87 * udpsrc port=5001 caps="application/x-rtcp" ! session.recv_rtcp_sink
88 * ]| Receive theora RTP packets from port 5000 and send them to the depayloader,
89 * decoder and display. Receive RTCP packets from port 5001 and process them in
90 * the session manager.
91 * Note that the application/x-rtp caps on udpsrc should be
92 * configured based on some negotiation process such as RTSP for this pipeline
95 * gst-launch videotestsrc ! theoraenc ! rtptheorapay ! .send_rtp_sink gstrtpsession .send_rtp_src ! udpsink port=5000
96 * ]| Send theora RTP packets through the session manager and out on UDP port
99 * gst-launch videotestsrc ! theoraenc ! rtptheorapay ! .send_rtp_sink gstrtpsession name=session .send_rtp_src \
100 * ! udpsink port=5000 session.send_rtcp_src ! udpsink port=5001
101 * ]| Send theora RTP packets through the session manager and out on UDP port
102 * 5000. Send RTCP packets on port 5001. Note that this pipeline will not preroll
103 * correctly because the second udpsink will not preroll correctly (no RTCP
104 * packets are sent in the PAUSED state). Applications should manually set and
105 * keep (see gst_element_set_locked_state()) the RTCP udpsink to the PLAYING state.
108 * Last reviewed on 2007-05-28 (0.10.5)
115 #include <gst/rtp/gstrtpbuffer.h>
117 #include "gstrtpbin-marshal.h"
118 #include "gstrtpsession.h"
119 #include "rtpsession.h"
121 GST_DEBUG_CATEGORY_STATIC (gst_rtp_session_debug);
122 #define GST_CAT_DEFAULT gst_rtp_session_debug
124 /* elementfactory information */
125 static const GstElementDetails rtpsession_details =
126 GST_ELEMENT_DETAILS ("RTP Session",
127 "Filter/Network/RTP",
128 "Implement an RTP session",
129 "Wim Taymans <wim.taymans@gmail.com>");
132 static GstStaticPadTemplate rtpsession_recv_rtp_sink_template =
133 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink",
136 GST_STATIC_CAPS ("application/x-rtp")
139 static GstStaticPadTemplate rtpsession_recv_rtcp_sink_template =
140 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink",
143 GST_STATIC_CAPS ("application/x-rtcp")
146 static GstStaticPadTemplate rtpsession_send_rtp_sink_template =
147 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink",
150 GST_STATIC_CAPS ("application/x-rtp")
154 static GstStaticPadTemplate rtpsession_recv_rtp_src_template =
155 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src",
158 GST_STATIC_CAPS ("application/x-rtp")
161 static GstStaticPadTemplate rtpsession_sync_src_template =
162 GST_STATIC_PAD_TEMPLATE ("sync_src",
165 GST_STATIC_CAPS ("application/x-rtcp")
168 static GstStaticPadTemplate rtpsession_send_rtp_src_template =
169 GST_STATIC_PAD_TEMPLATE ("send_rtp_src",
172 GST_STATIC_CAPS ("application/x-rtp")
175 static GstStaticPadTemplate rtpsession_send_rtcp_src_template =
176 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src",
179 GST_STATIC_CAPS ("application/x-rtcp")
182 /* signals and args */
185 SIGNAL_REQUEST_PT_MAP,
189 SIGNAL_ON_SSRC_COLLISION,
190 SIGNAL_ON_SSRC_VALIDATED,
191 SIGNAL_ON_SSRC_ACTIVE,
194 SIGNAL_ON_BYE_TIMEOUT,
199 #define DEFAULT_NTP_NS_BASE 0
200 #define DEFAULT_BANDWIDTH RTP_STATS_BANDWIDTH
201 #define DEFAULT_RTCP_FRACTION RTP_STATS_RTCP_BANDWIDTH
202 #define DEFAULT_SDES_CNAME NULL
203 #define DEFAULT_SDES_NAME NULL
204 #define DEFAULT_SDES_EMAIL NULL
205 #define DEFAULT_SDES_PHONE NULL
206 #define DEFAULT_SDES_LOCATION NULL
207 #define DEFAULT_SDES_TOOL NULL
208 #define DEFAULT_SDES_NOTE NULL
209 #define DEFAULT_NUM_SOURCES 0
210 #define DEFAULT_NUM_ACTIVE_SOURCES 0
226 PROP_NUM_ACTIVE_SOURCES,
230 #define GST_RTP_SESSION_GET_PRIVATE(obj) \
231 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_SESSION, GstRtpSessionPrivate))
233 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock ((sess)->priv->lock)
234 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock ((sess)->priv->lock)
236 struct _GstRtpSessionPrivate
243 /* thread for sending out RTCP */
245 gboolean stop_thread;
247 gboolean thread_stopped;
256 /* callbacks to handle actions from the session manager */
257 static GstFlowReturn gst_rtp_session_process_rtp (RTPSession * sess,
258 RTPSource * src, GstBuffer * buffer, gpointer user_data);
259 static GstFlowReturn gst_rtp_session_send_rtp (RTPSession * sess,
260 RTPSource * src, GstBuffer * buffer, gpointer user_data);
261 static GstFlowReturn gst_rtp_session_send_rtcp (RTPSession * sess,
262 RTPSource * src, GstBuffer * buffer, gboolean eos, gpointer user_data);
263 static GstFlowReturn gst_rtp_session_sync_rtcp (RTPSession * sess,
264 RTPSource * src, GstBuffer * buffer, gpointer user_data);
265 static gint gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
267 static void gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data);
269 static RTPSessionCallbacks callbacks = {
270 gst_rtp_session_process_rtp,
271 gst_rtp_session_send_rtp,
272 gst_rtp_session_sync_rtcp,
273 gst_rtp_session_send_rtcp,
274 gst_rtp_session_clock_rate,
275 gst_rtp_session_reconsider
278 /* GObject vmethods */
279 static void gst_rtp_session_finalize (GObject * object);
280 static void gst_rtp_session_set_property (GObject * object, guint prop_id,
281 const GValue * value, GParamSpec * pspec);
282 static void gst_rtp_session_get_property (GObject * object, guint prop_id,
283 GValue * value, GParamSpec * pspec);
285 /* GstElement vmethods */
286 static GstStateChangeReturn gst_rtp_session_change_state (GstElement * element,
287 GstStateChange transition);
288 static GstPad *gst_rtp_session_request_new_pad (GstElement * element,
289 GstPadTemplate * templ, const gchar * name);
290 static void gst_rtp_session_release_pad (GstElement * element, GstPad * pad);
292 static void gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession);
294 static guint gst_rtp_session_signals[LAST_SIGNAL] = { 0 };
297 on_new_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
299 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0,
304 on_ssrc_collision (RTPSession * session, RTPSource * src, GstRtpSession * sess)
306 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
311 on_ssrc_validated (RTPSession * session, RTPSource * src, GstRtpSession * sess)
313 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
318 on_ssrc_active (RTPSession * session, RTPSource * src, GstRtpSession * sess)
320 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
324 static GstStructure *
325 source_get_sdes_structure (RTPSource * src)
327 GstStructure *result;
331 result = gst_structure_empty_new ("GstRTPSessionSDES");
333 gst_structure_set (result, "ssrc", G_TYPE_UINT, src->ssrc, NULL);
335 g_value_init (&val, G_TYPE_STRING);
336 str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_CNAME);
338 g_value_take_string (&val, str);
339 gst_structure_set_value (result, "cname", &val);
341 str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_NAME);
343 g_value_take_string (&val, str);
344 gst_structure_set_value (result, "name", &val);
346 str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_EMAIL);
348 g_value_take_string (&val, str);
349 gst_structure_set_value (result, "email", &val);
351 str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_PHONE);
353 g_value_take_string (&val, str);
354 gst_structure_set_value (result, "phone", &val);
356 str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_LOC);
358 g_value_take_string (&val, str);
359 gst_structure_set_value (result, "location", &val);
361 str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_TOOL);
363 g_value_take_string (&val, str);
364 gst_structure_set_value (result, "tool", &val);
366 str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_NOTE);
368 g_value_take_string (&val, str);
369 gst_structure_set_value (result, "note", &val);
371 str = rtp_source_get_sdes_string (src, GST_RTCP_SDES_PRIV);
373 g_value_take_string (&val, str);
374 gst_structure_set_value (result, "priv", &val);
376 g_value_unset (&val);
382 on_ssrc_sdes (RTPSession * session, RTPSource * src, GstRtpSession * sess)
387 /* convert the new SDES info into a message */
388 RTP_SESSION_LOCK (session);
389 s = source_get_sdes_structure (src);
390 RTP_SESSION_UNLOCK (session);
391 m = gst_message_new_custom (GST_MESSAGE_ELEMENT, GST_OBJECT (sess), s);
392 gst_element_post_message (GST_ELEMENT_CAST (sess), m);
394 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0,
399 on_bye_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
401 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0,
406 on_bye_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
408 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
413 on_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
415 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_TIMEOUT], 0,
419 GST_BOILERPLATE (GstRtpSession, gst_rtp_session, GstElement, GST_TYPE_ELEMENT);
422 gst_rtp_session_base_init (gpointer klass)
424 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
427 gst_element_class_add_pad_template (element_class,
428 gst_static_pad_template_get (&rtpsession_recv_rtp_sink_template));
429 gst_element_class_add_pad_template (element_class,
430 gst_static_pad_template_get (&rtpsession_recv_rtcp_sink_template));
431 gst_element_class_add_pad_template (element_class,
432 gst_static_pad_template_get (&rtpsession_send_rtp_sink_template));
435 gst_element_class_add_pad_template (element_class,
436 gst_static_pad_template_get (&rtpsession_recv_rtp_src_template));
437 gst_element_class_add_pad_template (element_class,
438 gst_static_pad_template_get (&rtpsession_sync_src_template));
439 gst_element_class_add_pad_template (element_class,
440 gst_static_pad_template_get (&rtpsession_send_rtp_src_template));
441 gst_element_class_add_pad_template (element_class,
442 gst_static_pad_template_get (&rtpsession_send_rtcp_src_template));
444 gst_element_class_set_details (element_class, &rtpsession_details);
448 gst_rtp_session_class_init (GstRtpSessionClass * klass)
450 GObjectClass *gobject_class;
451 GstElementClass *gstelement_class;
453 gobject_class = (GObjectClass *) klass;
454 gstelement_class = (GstElementClass *) klass;
456 g_type_class_add_private (klass, sizeof (GstRtpSessionPrivate));
458 gobject_class->finalize = gst_rtp_session_finalize;
459 gobject_class->set_property = gst_rtp_session_set_property;
460 gobject_class->get_property = gst_rtp_session_get_property;
463 * GstRtpSession::request-pt-map:
464 * @sess: the object which received the signal
467 * Request the payload type as #GstCaps for @pt.
469 gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP] =
470 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
471 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, request_pt_map),
472 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT, GST_TYPE_CAPS, 1,
475 * GstRtpSession::clear-pt-map:
476 * @sess: the object which received the signal
478 * Clear the cached pt-maps requested with #GstRtpSession::request-pt-map.
480 gst_rtp_session_signals[SIGNAL_CLEAR_PT_MAP] =
481 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
482 G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpSessionClass, clear_pt_map),
483 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
486 * GstRtpSession::on-new-ssrc:
487 * @sess: the object which received the signal
490 * Notify of a new SSRC that entered @session.
492 gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
493 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
494 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_new_ssrc),
495 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
497 * GstRtpSession::on-ssrc_collision:
498 * @sess: the object which received the signal
501 * Notify when we have an SSRC collision
503 gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
504 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
505 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
506 on_ssrc_collision), NULL, NULL, g_cclosure_marshal_VOID__UINT,
507 G_TYPE_NONE, 1, G_TYPE_UINT);
509 * GstRtpSession::on-ssrc_validated:
510 * @sess: the object which received the signal
513 * Notify of a new SSRC that became validated.
515 gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
516 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
517 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
518 on_ssrc_validated), NULL, NULL, g_cclosure_marshal_VOID__UINT,
519 G_TYPE_NONE, 1, G_TYPE_UINT);
521 * GstRtpSession::on-ssrc_active:
522 * @sess: the object which received the signal
525 * Notify of a SSRC that is active, i.e., sending RTCP.
527 gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
528 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
529 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
530 on_ssrc_active), NULL, NULL, g_cclosure_marshal_VOID__UINT,
531 G_TYPE_NONE, 1, G_TYPE_UINT);
533 * GstRtpSession::on-ssrc-sdes:
534 * @session: the object which received the signal
537 * Notify that a new SDES was received for SSRC.
539 gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
540 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
541 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_ssrc_sdes),
542 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
545 * GstRtpSession::on-bye-ssrc:
546 * @sess: the object which received the signal
549 * Notify of an SSRC that became inactive because of a BYE packet.
551 gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
552 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
553 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_ssrc),
554 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
556 * GstRtpSession::on-bye-timeout:
557 * @sess: the object which received the signal
560 * Notify of an SSRC that has timed out because of BYE
562 gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
563 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
564 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_timeout),
565 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
567 * GstRtpSession::on-timeout:
568 * @sess: the object which received the signal
571 * Notify of an SSRC that has timed out
573 gst_rtp_session_signals[SIGNAL_ON_TIMEOUT] =
574 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
575 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_timeout),
576 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
578 g_object_class_install_property (gobject_class, PROP_NTP_NS_BASE,
579 g_param_spec_uint64 ("ntp-ns-base", "NTP base time",
580 "The NTP base time corresponding to running_time 0", 0,
581 G_MAXUINT64, DEFAULT_NTP_NS_BASE, G_PARAM_READWRITE));
583 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
584 g_param_spec_double ("bandwidth", "Bandwidth",
585 "The bandwidth of the session",
586 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH, G_PARAM_READWRITE));
588 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
589 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
590 "The fraction of the bandwidth used for RTCP",
591 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION, G_PARAM_READWRITE));
593 g_object_class_install_property (gobject_class, PROP_SDES_CNAME,
594 g_param_spec_string ("sdes-cname", "SDES CNAME",
595 "The CNAME to put in SDES messages of this session",
596 DEFAULT_SDES_CNAME, G_PARAM_READWRITE));
598 g_object_class_install_property (gobject_class, PROP_SDES_NAME,
599 g_param_spec_string ("sdes-name", "SDES NAME",
600 "The NAME to put in SDES messages of this session",
601 DEFAULT_SDES_NAME, G_PARAM_READWRITE));
603 g_object_class_install_property (gobject_class, PROP_SDES_EMAIL,
604 g_param_spec_string ("sdes-email", "SDES EMAIL",
605 "The EMAIL to put in SDES messages of this session",
606 DEFAULT_SDES_EMAIL, G_PARAM_READWRITE));
608 g_object_class_install_property (gobject_class, PROP_SDES_PHONE,
609 g_param_spec_string ("sdes-phone", "SDES PHONE",
610 "The PHONE to put in SDES messages of this session",
611 DEFAULT_SDES_PHONE, G_PARAM_READWRITE));
613 g_object_class_install_property (gobject_class, PROP_SDES_LOCATION,
614 g_param_spec_string ("sdes-location", "SDES LOCATION",
615 "The LOCATION to put in SDES messages of this session",
616 DEFAULT_SDES_LOCATION, G_PARAM_READWRITE));
618 g_object_class_install_property (gobject_class, PROP_SDES_TOOL,
619 g_param_spec_string ("sdes-tool", "SDES TOOL",
620 "The TOOL to put in SDES messages of this session",
621 DEFAULT_SDES_TOOL, G_PARAM_READWRITE));
623 g_object_class_install_property (gobject_class, PROP_SDES_NOTE,
624 g_param_spec_string ("sdes-note", "SDES NOTE",
625 "The NOTE to put in SDES messages of this session",
626 DEFAULT_SDES_NOTE, G_PARAM_READWRITE));
628 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
629 g_param_spec_uint ("num-sources", "Num Sources",
630 "The number of sources in the session", 0, G_MAXUINT,
631 DEFAULT_NUM_SOURCES, G_PARAM_READABLE));
633 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
634 g_param_spec_uint ("num-active-sources", "Num Active Sources",
635 "The number of active sources in the session", 0, G_MAXUINT,
636 DEFAULT_NUM_ACTIVE_SOURCES, G_PARAM_READABLE));
638 gstelement_class->change_state =
639 GST_DEBUG_FUNCPTR (gst_rtp_session_change_state);
640 gstelement_class->request_new_pad =
641 GST_DEBUG_FUNCPTR (gst_rtp_session_request_new_pad);
642 gstelement_class->release_pad =
643 GST_DEBUG_FUNCPTR (gst_rtp_session_release_pad);
645 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_session_clear_pt_map);
647 GST_DEBUG_CATEGORY_INIT (gst_rtp_session_debug,
648 "rtpsession", 0, "RTP Session");
652 gst_rtp_session_init (GstRtpSession * rtpsession, GstRtpSessionClass * klass)
654 rtpsession->priv = GST_RTP_SESSION_GET_PRIVATE (rtpsession);
655 rtpsession->priv->lock = g_mutex_new ();
656 rtpsession->priv->sysclock = gst_system_clock_obtain ();
657 rtpsession->priv->session = rtp_session_new ();
658 /* configure callbacks */
659 rtp_session_set_callbacks (rtpsession->priv->session, &callbacks, rtpsession);
660 /* configure signals */
661 g_signal_connect (rtpsession->priv->session, "on-new-ssrc",
662 (GCallback) on_new_ssrc, rtpsession);
663 g_signal_connect (rtpsession->priv->session, "on-ssrc-collision",
664 (GCallback) on_ssrc_collision, rtpsession);
665 g_signal_connect (rtpsession->priv->session, "on-ssrc-validated",
666 (GCallback) on_ssrc_validated, rtpsession);
667 g_signal_connect (rtpsession->priv->session, "on-ssrc-active",
668 (GCallback) on_ssrc_active, rtpsession);
669 g_signal_connect (rtpsession->priv->session, "on-ssrc-sdes",
670 (GCallback) on_ssrc_sdes, rtpsession);
671 g_signal_connect (rtpsession->priv->session, "on-bye-ssrc",
672 (GCallback) on_bye_ssrc, rtpsession);
673 g_signal_connect (rtpsession->priv->session, "on-bye-timeout",
674 (GCallback) on_bye_timeout, rtpsession);
675 g_signal_connect (rtpsession->priv->session, "on-timeout",
676 (GCallback) on_timeout, rtpsession);
677 rtpsession->priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
678 (GDestroyNotify) gst_caps_unref);
680 gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
681 gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
683 rtpsession->priv->thread_stopped = TRUE;
687 gst_rtp_session_finalize (GObject * object)
689 GstRtpSession *rtpsession;
691 rtpsession = GST_RTP_SESSION (object);
693 if (rtpsession->recv_rtp_sink != NULL)
694 gst_object_unref (rtpsession->recv_rtp_sink);
695 if (rtpsession->recv_rtcp_sink != NULL)
696 gst_object_unref (rtpsession->recv_rtcp_sink);
697 if (rtpsession->send_rtp_sink != NULL)
698 gst_object_unref (rtpsession->send_rtp_sink);
699 if (rtpsession->send_rtcp_src != NULL)
700 gst_object_unref (rtpsession->send_rtcp_src);
702 g_hash_table_destroy (rtpsession->priv->ptmap);
703 g_mutex_free (rtpsession->priv->lock);
704 g_object_unref (rtpsession->priv->sysclock);
705 g_object_unref (rtpsession->priv->session);
707 G_OBJECT_CLASS (parent_class)->finalize (object);
711 gst_rtp_session_set_property (GObject * object, guint prop_id,
712 const GValue * value, GParamSpec * pspec)
714 GstRtpSession *rtpsession;
715 GstRtpSessionPrivate *priv;
717 rtpsession = GST_RTP_SESSION (object);
718 priv = rtpsession->priv;
721 case PROP_NTP_NS_BASE:
722 GST_OBJECT_LOCK (rtpsession);
723 priv->ntpnsbase = g_value_get_uint64 (value);
724 GST_DEBUG_OBJECT (rtpsession, "setting NTP base to %" GST_TIME_FORMAT,
725 GST_TIME_ARGS (priv->ntpnsbase));
726 GST_OBJECT_UNLOCK (rtpsession);
729 rtp_session_set_bandwidth (priv->session, g_value_get_double (value));
731 case PROP_RTCP_FRACTION:
732 rtp_session_set_rtcp_fraction (priv->session, g_value_get_double (value));
734 case PROP_SDES_CNAME:
735 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_CNAME,
736 g_value_get_string (value));
739 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_NAME,
740 g_value_get_string (value));
742 case PROP_SDES_EMAIL:
743 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_EMAIL,
744 g_value_get_string (value));
746 case PROP_SDES_PHONE:
747 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_PHONE,
748 g_value_get_string (value));
750 case PROP_SDES_LOCATION:
751 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_LOC,
752 g_value_get_string (value));
755 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_TOOL,
756 g_value_get_string (value));
759 rtp_session_set_sdes_string (priv->session, GST_RTCP_SDES_NOTE,
760 g_value_get_string (value));
763 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
769 gst_rtp_session_get_property (GObject * object, guint prop_id,
770 GValue * value, GParamSpec * pspec)
772 GstRtpSession *rtpsession;
773 GstRtpSessionPrivate *priv;
775 rtpsession = GST_RTP_SESSION (object);
776 priv = rtpsession->priv;
779 case PROP_NTP_NS_BASE:
780 GST_OBJECT_LOCK (rtpsession);
781 g_value_set_uint64 (value, priv->ntpnsbase);
782 GST_OBJECT_UNLOCK (rtpsession);
785 g_value_set_double (value, rtp_session_get_bandwidth (priv->session));
787 case PROP_RTCP_FRACTION:
788 g_value_set_double (value, rtp_session_get_rtcp_fraction (priv->session));
790 case PROP_SDES_CNAME:
791 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
792 GST_RTCP_SDES_CNAME));
795 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
796 GST_RTCP_SDES_NAME));
798 case PROP_SDES_EMAIL:
799 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
800 GST_RTCP_SDES_EMAIL));
802 case PROP_SDES_PHONE:
803 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
804 GST_RTCP_SDES_PHONE));
806 case PROP_SDES_LOCATION:
807 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
811 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
812 GST_RTCP_SDES_TOOL));
815 g_value_take_string (value, rtp_session_get_sdes_string (priv->session,
816 GST_RTCP_SDES_NOTE));
818 case PROP_NUM_SOURCES:
819 g_value_set_uint (value, rtp_session_get_num_sources (priv->session));
821 case PROP_NUM_ACTIVE_SOURCES:
822 g_value_set_uint (value,
823 rtp_session_get_num_active_sources (priv->session));
826 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
832 get_current_ntp_ns_time (GstRtpSession * rtpsession)
836 GstClockTime base_time, ntpnsbase;
838 GST_OBJECT_LOCK (rtpsession);
839 if ((clock = GST_ELEMENT_CLOCK (rtpsession))) {
840 base_time = GST_ELEMENT_CAST (rtpsession)->base_time;
841 ntpnsbase = rtpsession->priv->ntpnsbase;
842 gst_object_ref (clock);
843 GST_OBJECT_UNLOCK (rtpsession);
845 /* get current NTP time */
846 ntpnstime = gst_clock_get_time (clock);
847 /* convert to running time */
848 ntpnstime -= base_time;
849 /* add NTP base offset */
850 ntpnstime += ntpnsbase;
852 gst_object_unref (clock);
854 GST_OBJECT_UNLOCK (rtpsession);
862 rtcp_thread (GstRtpSession * rtpsession)
865 GstClockTime current_time;
866 GstClockTime next_timeout;
869 GST_DEBUG_OBJECT (rtpsession, "entering RTCP thread");
871 GST_RTP_SESSION_LOCK (rtpsession);
873 current_time = gst_clock_get_time (rtpsession->priv->sysclock);
875 while (!rtpsession->priv->stop_thread) {
878 /* get initial estimate */
880 rtp_session_next_timeout (rtpsession->priv->session, current_time);
882 GST_DEBUG_OBJECT (rtpsession, "next check time %" GST_TIME_FORMAT,
883 GST_TIME_ARGS (next_timeout));
885 /* leave if no more timeouts, the session ended */
886 if (next_timeout == GST_CLOCK_TIME_NONE)
889 id = rtpsession->priv->id =
890 gst_clock_new_single_shot_id (rtpsession->priv->sysclock, next_timeout);
891 GST_RTP_SESSION_UNLOCK (rtpsession);
893 res = gst_clock_id_wait (id, NULL);
895 GST_RTP_SESSION_LOCK (rtpsession);
896 gst_clock_id_unref (id);
897 rtpsession->priv->id = NULL;
899 if (rtpsession->priv->stop_thread)
902 /* update current time */
903 current_time = gst_clock_get_time (rtpsession->priv->sysclock);
905 /* get current NTP time */
906 ntpnstime = get_current_ntp_ns_time (rtpsession);
908 /* we get unlocked because we need to perform reconsideration, don't perform
909 * the timeout but get a new reporting estimate. */
910 GST_DEBUG_OBJECT (rtpsession, "unlocked %d, current %" GST_TIME_FORMAT,
911 res, GST_TIME_ARGS (current_time));
913 /* perform actions, we ignore result. Release lock because it might push. */
914 GST_RTP_SESSION_UNLOCK (rtpsession);
915 rtp_session_on_timeout (rtpsession->priv->session, current_time, ntpnstime);
916 GST_RTP_SESSION_LOCK (rtpsession);
918 /* mark the thread as stopped now */
919 rtpsession->priv->thread_stopped = TRUE;
920 GST_RTP_SESSION_UNLOCK (rtpsession);
922 GST_DEBUG_OBJECT (rtpsession, "leaving RTCP thread");
926 start_rtcp_thread (GstRtpSession * rtpsession)
928 GError *error = NULL;
931 GST_DEBUG_OBJECT (rtpsession, "starting RTCP thread");
933 GST_RTP_SESSION_LOCK (rtpsession);
934 rtpsession->priv->stop_thread = FALSE;
935 if (rtpsession->priv->thread_stopped) {
936 /* only create a new thread if the old one was stopped. Otherwise we can
937 * just reuse the currently running one. */
938 rtpsession->priv->thread =
939 g_thread_create ((GThreadFunc) rtcp_thread, rtpsession, TRUE, &error);
940 rtpsession->priv->thread_stopped = FALSE;
942 GST_RTP_SESSION_UNLOCK (rtpsession);
946 GST_DEBUG_OBJECT (rtpsession, "failed to start thread, %s", error->message);
947 g_error_free (error);
955 stop_rtcp_thread (GstRtpSession * rtpsession)
957 GST_DEBUG_OBJECT (rtpsession, "stopping RTCP thread");
959 GST_RTP_SESSION_LOCK (rtpsession);
960 rtpsession->priv->stop_thread = TRUE;
961 if (rtpsession->priv->id)
962 gst_clock_id_unschedule (rtpsession->priv->id);
963 GST_RTP_SESSION_UNLOCK (rtpsession);
967 join_rtcp_thread (GstRtpSession * rtpsession)
969 GST_RTP_SESSION_LOCK (rtpsession);
970 /* don't try to join when we have no thread */
971 if (rtpsession->priv->thread != NULL) {
972 GST_DEBUG_OBJECT (rtpsession, "joining RTCP thread");
973 GST_RTP_SESSION_UNLOCK (rtpsession);
975 g_thread_join (rtpsession->priv->thread);
977 GST_RTP_SESSION_LOCK (rtpsession);
978 /* after the join, take the lock and clear the thread structure. The caller
979 * is supposed to not concurrently call start and join. */
980 rtpsession->priv->thread = NULL;
982 GST_RTP_SESSION_UNLOCK (rtpsession);
985 static GstStateChangeReturn
986 gst_rtp_session_change_state (GstElement * element, GstStateChange transition)
988 GstStateChangeReturn res;
989 GstRtpSession *rtpsession;
990 GstRtpSessionPrivate *priv;
992 rtpsession = GST_RTP_SESSION (element);
993 priv = rtpsession->priv;
995 switch (transition) {
996 case GST_STATE_CHANGE_NULL_TO_READY:
998 case GST_STATE_CHANGE_READY_TO_PAUSED:
1000 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1002 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1003 case GST_STATE_CHANGE_PAUSED_TO_READY:
1004 /* no need to join yet, we might want to continue later. Also, the
1005 * dataflow could block downstream so that a join could just block
1007 stop_rtcp_thread (rtpsession);
1013 res = parent_class->change_state (element, transition);
1015 switch (transition) {
1016 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1017 if (!start_rtcp_thread (rtpsession))
1020 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1022 case GST_STATE_CHANGE_PAUSED_TO_READY:
1023 /* downstream is now releasing the dataflow and we can join. */
1024 join_rtcp_thread (rtpsession);
1026 case GST_STATE_CHANGE_READY_TO_NULL:
1036 return GST_STATE_CHANGE_FAILURE;
1041 return_true (gpointer key, gpointer value, gpointer user_data)
1047 gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession)
1049 g_hash_table_foreach_remove (rtpsession->priv->ptmap, return_true, NULL);
1052 /* called when the session manager has an RTP packet ready for further
1054 static GstFlowReturn
1055 gst_rtp_session_process_rtp (RTPSession * sess, RTPSource * src,
1056 GstBuffer * buffer, gpointer user_data)
1058 GstFlowReturn result;
1059 GstRtpSession *rtpsession;
1060 GstRtpSessionPrivate *priv;
1062 rtpsession = GST_RTP_SESSION (user_data);
1063 priv = rtpsession->priv;
1065 if (rtpsession->recv_rtp_src) {
1066 GST_LOG_OBJECT (rtpsession, "pushing received RTP packet");
1067 result = gst_pad_push (rtpsession->recv_rtp_src, buffer);
1069 GST_DEBUG_OBJECT (rtpsession, "dropping received RTP packet");
1070 gst_buffer_unref (buffer);
1071 result = GST_FLOW_OK;
1076 /* called when the session manager has an RTP packet ready for further
1078 static GstFlowReturn
1079 gst_rtp_session_send_rtp (RTPSession * sess, RTPSource * src,
1080 GstBuffer * buffer, gpointer user_data)
1082 GstFlowReturn result;
1083 GstRtpSession *rtpsession;
1084 GstRtpSessionPrivate *priv;
1086 rtpsession = GST_RTP_SESSION (user_data);
1087 priv = rtpsession->priv;
1089 GST_LOG_OBJECT (rtpsession, "sending RTP packet");
1091 if (rtpsession->send_rtp_src) {
1092 result = gst_pad_push (rtpsession->send_rtp_src, buffer);
1094 gst_buffer_unref (buffer);
1095 result = GST_FLOW_OK;
1100 /* called when the session manager has an RTCP packet ready for further
1101 * sending. The eos flag is set when an EOS event should be sent downstream as
1103 static GstFlowReturn
1104 gst_rtp_session_send_rtcp (RTPSession * sess, RTPSource * src,
1105 GstBuffer * buffer, gboolean eos, gpointer user_data)
1107 GstFlowReturn result;
1108 GstRtpSession *rtpsession;
1109 GstRtpSessionPrivate *priv;
1111 rtpsession = GST_RTP_SESSION (user_data);
1112 priv = rtpsession->priv;
1114 if (rtpsession->send_rtcp_src) {
1117 /* set rtcp caps on output pad */
1118 if (!(caps = GST_PAD_CAPS (rtpsession->send_rtcp_src))) {
1119 caps = gst_caps_new_simple ("application/x-rtcp", NULL);
1120 gst_pad_set_caps (rtpsession->send_rtcp_src, caps);
1121 gst_caps_unref (caps);
1123 gst_buffer_set_caps (buffer, caps);
1124 GST_LOG_OBJECT (rtpsession, "sending RTCP");
1125 result = gst_pad_push (rtpsession->send_rtcp_src, buffer);
1127 /* we have to send EOS after this packet */
1129 GST_LOG_OBJECT (rtpsession, "sending EOS");
1130 gst_pad_push_event (rtpsession->send_rtcp_src, gst_event_new_eos ());
1133 GST_DEBUG_OBJECT (rtpsession, "not sending RTCP, no output pad");
1134 gst_buffer_unref (buffer);
1135 result = GST_FLOW_OK;
1140 /* called when the session manager has an SR RTCP packet ready for handling
1141 * inter stream synchronisation */
1142 static GstFlowReturn
1143 gst_rtp_session_sync_rtcp (RTPSession * sess,
1144 RTPSource * src, GstBuffer * buffer, gpointer user_data)
1146 GstFlowReturn result;
1147 GstRtpSession *rtpsession;
1148 GstRtpSessionPrivate *priv;
1150 rtpsession = GST_RTP_SESSION (user_data);
1151 priv = rtpsession->priv;
1153 if (rtpsession->sync_src) {
1156 /* set rtcp caps on output pad */
1157 if (!(caps = GST_PAD_CAPS (rtpsession->sync_src))) {
1158 caps = gst_caps_new_simple ("application/x-rtcp", NULL);
1159 gst_pad_set_caps (rtpsession->sync_src, caps);
1160 gst_caps_unref (caps);
1162 gst_buffer_set_caps (buffer, caps);
1163 GST_LOG_OBJECT (rtpsession, "sending Sync RTCP");
1164 result = gst_pad_push (rtpsession->sync_src, buffer);
1166 GST_DEBUG_OBJECT (rtpsession, "not sending Sync RTCP, no output pad");
1167 gst_buffer_unref (buffer);
1168 result = GST_FLOW_OK;
1174 gst_rtp_session_cache_caps (GstRtpSession * rtpsession, GstCaps * caps)
1176 GstRtpSessionPrivate *priv;
1177 const GstStructure *s;
1180 priv = rtpsession->priv;
1182 GST_DEBUG_OBJECT (rtpsession, "parsing caps");
1184 s = gst_caps_get_structure (caps, 0);
1185 if (!gst_structure_get_int (s, "payload", &payload))
1188 if (g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (payload)))
1191 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (payload),
1192 gst_caps_ref (caps));
1195 /* called when the session manager needs the clock rate */
1197 gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
1200 gint ipayload, result = -1;
1201 GstRtpSession *rtpsession;
1202 GstRtpSessionPrivate *priv;
1204 GValue args[2] = { {0}, {0} };
1206 const GstStructure *s;
1208 rtpsession = GST_RTP_SESSION_CAST (user_data);
1209 priv = rtpsession->priv;
1211 GST_RTP_SESSION_LOCK (rtpsession);
1212 ipayload = payload; /* make compiler happy */
1213 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (ipayload));
1215 gst_caps_ref (caps);
1219 /* not found in the cache, try to get it with a signal */
1220 g_value_init (&args[0], GST_TYPE_ELEMENT);
1221 g_value_set_object (&args[0], rtpsession);
1222 g_value_init (&args[1], G_TYPE_UINT);
1223 g_value_set_uint (&args[1], payload);
1225 g_value_init (&ret, GST_TYPE_CAPS);
1226 g_value_set_boxed (&ret, NULL);
1228 g_signal_emitv (args, gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP], 0,
1231 g_value_unset (&args[0]);
1232 g_value_unset (&args[1]);
1233 caps = (GstCaps *) g_value_dup_boxed (&ret);
1234 g_value_unset (&ret);
1238 gst_rtp_session_cache_caps (rtpsession, caps);
1241 s = gst_caps_get_structure (caps, 0);
1242 if (!gst_structure_get_int (s, "clock-rate", &result))
1245 gst_caps_unref (caps);
1247 GST_DEBUG_OBJECT (rtpsession, "parsed clock-rate %d", result);
1250 GST_RTP_SESSION_UNLOCK (rtpsession);
1257 GST_DEBUG_OBJECT (rtpsession, "could not get caps");
1262 gst_caps_unref (caps);
1263 GST_DEBUG_OBJECT (rtpsession, "No clock-rate in caps!");
1268 /* called when the session manager asks us to reconsider the timeout */
1270 gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data)
1272 GstRtpSession *rtpsession;
1274 rtpsession = GST_RTP_SESSION_CAST (user_data);
1276 GST_RTP_SESSION_LOCK (rtpsession);
1277 GST_DEBUG_OBJECT (rtpsession, "unlock timer for reconsideration");
1278 if (rtpsession->priv->id)
1279 gst_clock_id_unschedule (rtpsession->priv->id);
1280 GST_RTP_SESSION_UNLOCK (rtpsession);
1284 gst_rtp_session_event_recv_rtp_sink (GstPad * pad, GstEvent * event)
1286 GstRtpSession *rtpsession;
1287 GstRtpSessionPrivate *priv;
1288 gboolean ret = FALSE;
1290 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1291 priv = rtpsession->priv;
1293 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1294 GST_EVENT_TYPE_NAME (event));
1296 switch (GST_EVENT_TYPE (event)) {
1297 case GST_EVENT_FLUSH_STOP:
1298 gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
1299 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1301 case GST_EVENT_NEWSEGMENT:
1304 gdouble rate, arate;
1306 gint64 start, stop, time;
1307 GstSegment *segment;
1309 segment = &rtpsession->recv_rtp_seg;
1311 /* the newsegment event is needed to convert the RTP timestamp to
1312 * running_time, which is needed to generate a mapping from RTP to NTP
1313 * timestamps in SR reports */
1314 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1315 &start, &stop, &time);
1317 GST_DEBUG_OBJECT (rtpsession,
1318 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1319 "format GST_FORMAT_TIME, "
1320 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1321 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1322 update, rate, arate, GST_TIME_ARGS (segment->start),
1323 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1324 GST_TIME_ARGS (segment->accum));
1326 gst_segment_set_newsegment_full (segment, update, rate,
1327 arate, format, start, stop, time);
1329 /* push event forward */
1330 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1334 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1337 gst_object_unref (rtpsession);
1343 gst_rtp_session_internal_links (GstPad * pad)
1345 GstRtpSession *rtpsession;
1346 GstRtpSessionPrivate *priv;
1349 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1350 priv = rtpsession->priv;
1352 if (pad == rtpsession->recv_rtp_src) {
1353 res = g_list_prepend (res, rtpsession->recv_rtp_sink);
1354 } else if (pad == rtpsession->recv_rtp_sink) {
1355 res = g_list_prepend (res, rtpsession->recv_rtp_src);
1356 } else if (pad == rtpsession->send_rtp_src) {
1357 res = g_list_prepend (res, rtpsession->send_rtp_sink);
1358 } else if (pad == rtpsession->send_rtp_sink) {
1359 res = g_list_prepend (res, rtpsession->send_rtp_src);
1362 gst_object_unref (rtpsession);
1368 gst_rtp_session_sink_setcaps (GstPad * pad, GstCaps * caps)
1370 GstRtpSession *rtpsession;
1371 GstRtpSessionPrivate *priv;
1373 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1374 priv = rtpsession->priv;
1376 GST_RTP_SESSION_LOCK (rtpsession);
1377 gst_rtp_session_cache_caps (rtpsession, caps);
1378 GST_RTP_SESSION_UNLOCK (rtpsession);
1380 gst_object_unref (rtpsession);
1385 /* receive a packet from a sender, send it to the RTP session manager and
1386 * forward the packet on the rtp_src pad
1388 static GstFlowReturn
1389 gst_rtp_session_chain_recv_rtp (GstPad * pad, GstBuffer * buffer)
1391 GstRtpSession *rtpsession;
1392 GstRtpSessionPrivate *priv;
1394 GstClockTime current_time;
1396 GstClockTime timestamp;
1398 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1399 priv = rtpsession->priv;
1401 GST_LOG_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 current_time = gst_clock_get_time (priv->sysclock);
1417 ret = rtp_session_process_rtp (priv->session, buffer, current_time,
1419 if (ret != GST_FLOW_OK)
1424 gst_object_unref (rtpsession);
1431 GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1432 gst_flow_get_name (ret));
1438 gst_rtp_session_event_recv_rtcp_sink (GstPad * pad, GstEvent * event)
1440 GstRtpSession *rtpsession;
1441 GstRtpSessionPrivate *priv;
1442 gboolean ret = FALSE;
1444 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1445 priv = rtpsession->priv;
1447 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1448 GST_EVENT_TYPE_NAME (event));
1450 switch (GST_EVENT_TYPE (event)) {
1452 if (rtpsession->send_rtcp_src) {
1453 gst_event_ref (event);
1454 ret = gst_pad_push_event (rtpsession->send_rtcp_src, event);
1456 ret = gst_pad_push_event (rtpsession->sync_src, event);
1459 gst_object_unref (rtpsession);
1464 /* Receive an RTCP packet from a sender, send it to the RTP session manager and
1465 * forward the SR packets to the sync_src pad.
1467 static GstFlowReturn
1468 gst_rtp_session_chain_recv_rtcp (GstPad * pad, GstBuffer * buffer)
1470 GstRtpSession *rtpsession;
1471 GstRtpSessionPrivate *priv;
1472 GstClockTime current_time;
1475 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1476 priv = rtpsession->priv;
1478 GST_LOG_OBJECT (rtpsession, "received RTCP packet");
1480 current_time = gst_clock_get_time (priv->sysclock);
1481 ret = rtp_session_process_rtcp (priv->session, buffer, current_time);
1483 gst_object_unref (rtpsession);
1489 gst_rtp_session_query_send_rtcp_src (GstPad * pad, GstQuery * query)
1491 GstRtpSession *rtpsession;
1492 GstRtpSessionPrivate *priv;
1493 gboolean ret = FALSE;
1495 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1496 priv = rtpsession->priv;
1498 GST_DEBUG_OBJECT (rtpsession, "received QUERY");
1500 switch (GST_QUERY_TYPE (query)) {
1501 case GST_QUERY_LATENCY:
1503 /* use the defaults for the latency query. */
1504 gst_query_set_latency (query, FALSE, 0, -1);
1507 /* other queries simply fail for now */
1511 gst_object_unref (rtpsession);
1517 gst_rtp_session_event_send_rtp_sink (GstPad * pad, GstEvent * event)
1519 GstRtpSession *rtpsession;
1520 GstRtpSessionPrivate *priv;
1521 gboolean ret = FALSE;
1523 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1524 priv = rtpsession->priv;
1526 GST_DEBUG_OBJECT (rtpsession, "received event");
1528 switch (GST_EVENT_TYPE (event)) {
1529 case GST_EVENT_FLUSH_STOP:
1530 gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
1531 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1533 case GST_EVENT_NEWSEGMENT:{
1535 gdouble rate, arate;
1537 gint64 start, stop, time;
1538 GstSegment *segment;
1540 segment = &rtpsession->send_rtp_seg;
1542 /* the newsegment event is needed to convert the RTP timestamp to
1543 * running_time, which is needed to generate a mapping from RTP to NTP
1544 * timestamps in SR reports */
1545 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1546 &start, &stop, &time);
1548 GST_DEBUG_OBJECT (rtpsession,
1549 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1550 "format GST_FORMAT_TIME, "
1551 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1552 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1553 update, rate, arate, GST_TIME_ARGS (segment->start),
1554 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1555 GST_TIME_ARGS (segment->accum));
1557 gst_segment_set_newsegment_full (segment, update, rate,
1558 arate, format, start, stop, time);
1560 /* push event forward */
1561 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1564 case GST_EVENT_EOS:{
1565 GstClockTime current_time;
1567 /* push downstream FIXME, we are not supposed to leave the session just
1568 * because we stop sending. */
1569 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1570 current_time = gst_clock_get_time (rtpsession->priv->sysclock);
1571 GST_DEBUG_OBJECT (rtpsession, "scheduling BYE message");
1572 rtp_session_send_bye (rtpsession->priv->session, "End of stream",
1577 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1580 gst_object_unref (rtpsession);
1586 gst_rtp_session_getcaps_send_rtp (GstPad * pad)
1588 GstRtpSession *rtpsession;
1589 GstRtpSessionPrivate *priv;
1591 GstStructure *s1, *s2;
1593 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1594 priv = rtpsession->priv;
1596 /* we can basically accept anything but we prefer to receive packets with our
1597 * internal SSRC so that we don't have to patch it. Create a structure with
1598 * the SSRC and another one without. */
1599 s1 = gst_structure_new ("application/x-rtp",
1600 "ssrc", G_TYPE_UINT, priv->session->source->ssrc, NULL);
1601 s2 = gst_structure_new ("application/x-rtp", NULL);
1603 result = gst_caps_new_full (s1, s2, NULL);
1605 GST_DEBUG_OBJECT (rtpsession, "getting caps %" GST_PTR_FORMAT, result);
1607 gst_object_unref (rtpsession);
1612 /* Recieve an RTP packet to be send to the receivers, send to RTP session
1613 * manager and forward to send_rtp_src.
1615 static GstFlowReturn
1616 gst_rtp_session_chain_send_rtp (GstPad * pad, GstBuffer * buffer)
1618 GstRtpSession *rtpsession;
1619 GstRtpSessionPrivate *priv;
1621 GstClockTime timestamp;
1622 GstClockTime current_time;
1625 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1626 priv = rtpsession->priv;
1628 GST_LOG_OBJECT (rtpsession, "received RTP packet");
1630 /* get NTP time when this packet was captured, this depends on the timestamp. */
1631 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1632 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1633 /* convert to running time using the segment start value. */
1635 gst_segment_to_running_time (&rtpsession->send_rtp_seg, GST_FORMAT_TIME,
1637 /* convert to NTP time by adding the NTP base */
1638 ntpnstime += priv->ntpnsbase;
1640 /* no timestamp, we could take the current running_time and convert it to
1645 current_time = gst_clock_get_time (priv->sysclock);
1646 ret = rtp_session_send_rtp (priv->session, buffer, current_time, ntpnstime);
1647 if (ret != GST_FLOW_OK)
1651 gst_object_unref (rtpsession);
1658 GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1659 gst_flow_get_name (ret));
1664 /* Create sinkpad to receive RTP packets from senders. This will also create a
1665 * srcpad for the RTP packets.
1668 create_recv_rtp_sink (GstRtpSession * rtpsession)
1670 GST_DEBUG_OBJECT (rtpsession, "creating RTP sink pad");
1672 rtpsession->recv_rtp_sink =
1673 gst_pad_new_from_static_template (&rtpsession_recv_rtp_sink_template,
1675 gst_pad_set_chain_function (rtpsession->recv_rtp_sink,
1676 gst_rtp_session_chain_recv_rtp);
1677 gst_pad_set_event_function (rtpsession->recv_rtp_sink,
1678 (GstPadEventFunction) gst_rtp_session_event_recv_rtp_sink);
1679 gst_pad_set_setcaps_function (rtpsession->recv_rtp_sink,
1680 gst_rtp_session_sink_setcaps);
1681 gst_pad_set_internal_link_function (rtpsession->recv_rtp_sink,
1682 gst_rtp_session_internal_links);
1683 gst_pad_set_active (rtpsession->recv_rtp_sink, TRUE);
1684 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1685 rtpsession->recv_rtp_sink);
1687 GST_DEBUG_OBJECT (rtpsession, "creating RTP src pad");
1688 rtpsession->recv_rtp_src =
1689 gst_pad_new_from_static_template (&rtpsession_recv_rtp_src_template,
1691 gst_pad_set_internal_link_function (rtpsession->recv_rtp_src,
1692 gst_rtp_session_internal_links);
1693 gst_pad_use_fixed_caps (rtpsession->recv_rtp_src);
1694 gst_pad_set_active (rtpsession->recv_rtp_src, TRUE);
1695 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->recv_rtp_src);
1697 return rtpsession->recv_rtp_sink;
1700 /* Create a sinkpad to receive RTCP messages from senders, this will also create a
1701 * sync_src pad for the SR packets.
1704 create_recv_rtcp_sink (GstRtpSession * rtpsession)
1706 GST_DEBUG_OBJECT (rtpsession, "creating RTCP sink pad");
1708 rtpsession->recv_rtcp_sink =
1709 gst_pad_new_from_static_template (&rtpsession_recv_rtcp_sink_template,
1711 gst_pad_set_chain_function (rtpsession->recv_rtcp_sink,
1712 gst_rtp_session_chain_recv_rtcp);
1713 gst_pad_set_event_function (rtpsession->recv_rtcp_sink,
1714 (GstPadEventFunction) gst_rtp_session_event_recv_rtcp_sink);
1715 gst_pad_set_internal_link_function (rtpsession->recv_rtcp_sink,
1716 gst_rtp_session_internal_links);
1717 gst_pad_set_active (rtpsession->recv_rtcp_sink, TRUE);
1718 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1719 rtpsession->recv_rtcp_sink);
1721 GST_DEBUG_OBJECT (rtpsession, "creating sync src pad");
1722 rtpsession->sync_src =
1723 gst_pad_new_from_static_template (&rtpsession_sync_src_template,
1725 gst_pad_set_internal_link_function (rtpsession->sync_src,
1726 gst_rtp_session_internal_links);
1727 gst_pad_use_fixed_caps (rtpsession->sync_src);
1728 gst_pad_set_active (rtpsession->sync_src, TRUE);
1729 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
1731 return rtpsession->recv_rtcp_sink;
1734 /* Create a sinkpad to receive RTP packets for receivers. This will also create a
1738 create_send_rtp_sink (GstRtpSession * rtpsession)
1740 GST_DEBUG_OBJECT (rtpsession, "creating pad");
1742 rtpsession->send_rtp_sink =
1743 gst_pad_new_from_static_template (&rtpsession_send_rtp_sink_template,
1745 gst_pad_set_chain_function (rtpsession->send_rtp_sink,
1746 gst_rtp_session_chain_send_rtp);
1747 gst_pad_set_getcaps_function (rtpsession->send_rtp_sink,
1748 gst_rtp_session_getcaps_send_rtp);
1749 gst_pad_set_event_function (rtpsession->send_rtp_sink,
1750 (GstPadEventFunction) gst_rtp_session_event_send_rtp_sink);
1751 gst_pad_set_internal_link_function (rtpsession->send_rtp_sink,
1752 gst_rtp_session_internal_links);
1753 gst_pad_set_active (rtpsession->send_rtp_sink, TRUE);
1754 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1755 rtpsession->send_rtp_sink);
1757 rtpsession->send_rtp_src =
1758 gst_pad_new_from_static_template (&rtpsession_send_rtp_src_template,
1760 gst_pad_set_internal_link_function (rtpsession->send_rtp_src,
1761 gst_rtp_session_internal_links);
1762 gst_pad_set_active (rtpsession->send_rtp_src, TRUE);
1763 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->send_rtp_src);
1765 return rtpsession->send_rtp_sink;
1768 /* Create a srcpad with the RTCP packets to send out.
1769 * This pad will be driven by the RTP session manager when it wants to send out
1773 create_send_rtcp_src (GstRtpSession * rtpsession)
1775 GST_DEBUG_OBJECT (rtpsession, "creating pad");
1777 rtpsession->send_rtcp_src =
1778 gst_pad_new_from_static_template (&rtpsession_send_rtcp_src_template,
1780 gst_pad_use_fixed_caps (rtpsession->send_rtcp_src);
1781 gst_pad_set_active (rtpsession->send_rtcp_src, TRUE);
1782 gst_pad_set_internal_link_function (rtpsession->send_rtcp_src,
1783 gst_rtp_session_internal_links);
1784 gst_pad_set_query_function (rtpsession->send_rtcp_src,
1785 gst_rtp_session_query_send_rtcp_src);
1786 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1787 rtpsession->send_rtcp_src);
1789 return rtpsession->send_rtcp_src;
1793 gst_rtp_session_request_new_pad (GstElement * element,
1794 GstPadTemplate * templ, const gchar * name)
1796 GstRtpSession *rtpsession;
1797 GstElementClass *klass;
1800 g_return_val_if_fail (templ != NULL, NULL);
1801 g_return_val_if_fail (GST_IS_RTP_SESSION (element), NULL);
1803 rtpsession = GST_RTP_SESSION (element);
1804 klass = GST_ELEMENT_GET_CLASS (element);
1806 GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
1808 GST_RTP_SESSION_LOCK (rtpsession);
1810 /* figure out the template */
1811 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink")) {
1812 if (rtpsession->recv_rtp_sink != NULL)
1815 result = create_recv_rtp_sink (rtpsession);
1816 } else if (templ == gst_element_class_get_pad_template (klass,
1817 "recv_rtcp_sink")) {
1818 if (rtpsession->recv_rtcp_sink != NULL)
1821 result = create_recv_rtcp_sink (rtpsession);
1822 } else if (templ == gst_element_class_get_pad_template (klass,
1824 if (rtpsession->send_rtp_sink != NULL)
1827 result = create_send_rtp_sink (rtpsession);
1828 } else if (templ == gst_element_class_get_pad_template (klass,
1830 if (rtpsession->send_rtcp_src != NULL)
1833 result = create_send_rtcp_src (rtpsession);
1835 goto wrong_template;
1837 GST_RTP_SESSION_UNLOCK (rtpsession);
1844 GST_RTP_SESSION_UNLOCK (rtpsession);
1845 g_warning ("gstrtpsession: this is not our template");
1850 GST_RTP_SESSION_UNLOCK (rtpsession);
1851 g_warning ("gstrtpsession: pad already requested");
1857 gst_rtp_session_release_pad (GstElement * element, GstPad * pad)
1862 gst_rtp_session_set_ssrc (GstRtpSession * sess, guint32 ssrc)
1864 rtp_session_set_internal_ssrc (sess->priv->session, ssrc);