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 <gst/glib-compat-private.h>
119 #include "gstrtpbin-marshal.h"
120 #include "gstrtpsession.h"
121 #include "rtpsession.h"
123 GST_DEBUG_CATEGORY_STATIC (gst_rtp_session_debug);
124 #define GST_CAT_DEFAULT gst_rtp_session_debug
127 static GstStaticPadTemplate rtpsession_recv_rtp_sink_template =
128 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink",
131 GST_STATIC_CAPS ("application/x-rtp")
134 static GstStaticPadTemplate rtpsession_recv_rtcp_sink_template =
135 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink",
138 GST_STATIC_CAPS ("application/x-rtcp")
141 static GstStaticPadTemplate rtpsession_send_rtp_sink_template =
142 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink",
145 GST_STATIC_CAPS ("application/x-rtp")
149 static GstStaticPadTemplate rtpsession_recv_rtp_src_template =
150 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src",
153 GST_STATIC_CAPS ("application/x-rtp")
156 static GstStaticPadTemplate rtpsession_sync_src_template =
157 GST_STATIC_PAD_TEMPLATE ("sync_src",
160 GST_STATIC_CAPS ("application/x-rtcp")
163 static GstStaticPadTemplate rtpsession_send_rtp_src_template =
164 GST_STATIC_PAD_TEMPLATE ("send_rtp_src",
167 GST_STATIC_CAPS ("application/x-rtp")
170 static GstStaticPadTemplate rtpsession_send_rtcp_src_template =
171 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src",
174 GST_STATIC_CAPS ("application/x-rtcp")
177 /* signals and args */
180 SIGNAL_REQUEST_PT_MAP,
184 SIGNAL_ON_SSRC_COLLISION,
185 SIGNAL_ON_SSRC_VALIDATED,
186 SIGNAL_ON_SSRC_ACTIVE,
189 SIGNAL_ON_BYE_TIMEOUT,
191 SIGNAL_ON_SENDER_TIMEOUT,
195 #define DEFAULT_NTP_NS_BASE 0
196 #define DEFAULT_BANDWIDTH RTP_STATS_BANDWIDTH
197 #define DEFAULT_RTCP_FRACTION (RTP_STATS_BANDWIDTH * RTP_STATS_RTCP_FRACTION)
198 #define DEFAULT_RTCP_RR_BANDWIDTH -1
199 #define DEFAULT_RTCP_RS_BANDWIDTH -1
200 #define DEFAULT_SDES NULL
201 #define DEFAULT_NUM_SOURCES 0
202 #define DEFAULT_NUM_ACTIVE_SOURCES 0
203 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
204 #define DEFAULT_RTCP_MIN_INTERVAL (RTP_STATS_MIN_INTERVAL * GST_SECOND)
212 PROP_RTCP_RR_BANDWIDTH,
213 PROP_RTCP_RS_BANDWIDTH,
216 PROP_NUM_ACTIVE_SOURCES,
217 PROP_INTERNAL_SESSION,
218 PROP_USE_PIPELINE_CLOCK,
219 PROP_RTCP_MIN_INTERVAL,
223 #define GST_RTP_SESSION_GET_PRIVATE(obj) \
224 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_SESSION, GstRtpSessionPrivate))
226 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock (&(sess)->priv->lock)
227 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock (&(sess)->priv->lock)
229 struct _GstRtpSessionPrivate
236 /* thread for sending out RTCP */
238 gboolean stop_thread;
240 gboolean thread_stopped;
247 gboolean use_pipeline_clock;
250 /* callbacks to handle actions from the session manager */
251 static GstFlowReturn gst_rtp_session_process_rtp (RTPSession * sess,
252 RTPSource * src, GstBuffer * buffer, gpointer user_data);
253 static GstFlowReturn gst_rtp_session_send_rtp (RTPSession * sess,
254 RTPSource * src, gpointer data, gpointer user_data);
255 static GstFlowReturn gst_rtp_session_send_rtcp (RTPSession * sess,
256 RTPSource * src, GstBuffer * buffer, gboolean eos, gpointer user_data);
257 static GstFlowReturn gst_rtp_session_sync_rtcp (RTPSession * sess,
258 RTPSource * src, GstBuffer * buffer, gpointer user_data);
259 static gint gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
261 static void gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data);
262 static void gst_rtp_session_request_key_unit (RTPSession * sess,
263 gboolean all_headers, gpointer user_data);
264 static GstClockTime gst_rtp_session_request_time (RTPSession * session,
267 static RTPSessionCallbacks callbacks = {
268 gst_rtp_session_process_rtp,
269 gst_rtp_session_send_rtp,
270 gst_rtp_session_sync_rtcp,
271 gst_rtp_session_send_rtcp,
272 gst_rtp_session_clock_rate,
273 gst_rtp_session_reconsider,
274 gst_rtp_session_request_key_unit,
275 gst_rtp_session_request_time
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, const GstCaps * caps);
290 static void gst_rtp_session_release_pad (GstElement * element, GstPad * pad);
292 static gboolean gst_rtp_session_sink_setcaps (GstPad * pad,
293 GstRtpSession * rtpsession, GstCaps * caps);
294 static gboolean gst_rtp_session_setcaps_send_rtp (GstPad * pad,
295 GstRtpSession * rtpsession, GstCaps * caps);
297 static void gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession);
299 static guint gst_rtp_session_signals[LAST_SIGNAL] = { 0 };
302 on_new_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
304 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0,
309 on_ssrc_collision (RTPSession * session, RTPSource * src, GstRtpSession * sess)
311 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
316 on_ssrc_validated (RTPSession * session, RTPSource * src, GstRtpSession * sess)
318 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
323 on_ssrc_active (RTPSession * session, RTPSource * src, GstRtpSession * sess)
325 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
330 on_ssrc_sdes (RTPSession * session, RTPSource * src, GstRtpSession * sess)
335 /* convert the new SDES info into a message */
336 RTP_SESSION_LOCK (session);
337 g_object_get (src, "sdes", &s, NULL);
338 RTP_SESSION_UNLOCK (session);
340 m = gst_message_new_custom (GST_MESSAGE_ELEMENT, GST_OBJECT (sess), s);
341 gst_element_post_message (GST_ELEMENT_CAST (sess), m);
343 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0,
348 on_bye_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
350 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0,
355 on_bye_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
357 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
362 on_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
364 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_TIMEOUT], 0,
369 on_sender_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
371 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
375 #define gst_rtp_session_parent_class parent_class
376 G_DEFINE_TYPE (GstRtpSession, gst_rtp_session, GST_TYPE_ELEMENT);
379 gst_rtp_session_class_init (GstRtpSessionClass * klass)
381 GObjectClass *gobject_class;
382 GstElementClass *gstelement_class;
384 gobject_class = (GObjectClass *) klass;
385 gstelement_class = (GstElementClass *) klass;
387 g_type_class_add_private (klass, sizeof (GstRtpSessionPrivate));
389 gobject_class->finalize = gst_rtp_session_finalize;
390 gobject_class->set_property = gst_rtp_session_set_property;
391 gobject_class->get_property = gst_rtp_session_get_property;
394 * GstRtpSession::request-pt-map:
395 * @sess: the object which received the signal
398 * Request the payload type as #GstCaps for @pt.
400 gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP] =
401 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
402 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, request_pt_map),
403 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT, GST_TYPE_CAPS, 1,
406 * GstRtpSession::clear-pt-map:
407 * @sess: the object which received the signal
409 * Clear the cached pt-maps requested with #GstRtpSession::request-pt-map.
411 gst_rtp_session_signals[SIGNAL_CLEAR_PT_MAP] =
412 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
413 G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpSessionClass, clear_pt_map),
414 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
417 * GstRtpSession::on-new-ssrc:
418 * @sess: the object which received the signal
421 * Notify of a new SSRC that entered @session.
423 gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
424 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
425 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_new_ssrc),
426 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
428 * GstRtpSession::on-ssrc_collision:
429 * @sess: the object which received the signal
432 * Notify when we have an SSRC collision
434 gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
435 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
436 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
437 on_ssrc_collision), NULL, NULL, g_cclosure_marshal_VOID__UINT,
438 G_TYPE_NONE, 1, G_TYPE_UINT);
440 * GstRtpSession::on-ssrc_validated:
441 * @sess: the object which received the signal
444 * Notify of a new SSRC that became validated.
446 gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
447 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
448 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
449 on_ssrc_validated), NULL, NULL, g_cclosure_marshal_VOID__UINT,
450 G_TYPE_NONE, 1, G_TYPE_UINT);
452 * GstRtpSession::on-ssrc_active:
453 * @sess: the object which received the signal
456 * Notify of a SSRC that is active, i.e., sending RTCP.
458 gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
459 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
460 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
461 on_ssrc_active), NULL, NULL, g_cclosure_marshal_VOID__UINT,
462 G_TYPE_NONE, 1, G_TYPE_UINT);
464 * GstRtpSession::on-ssrc-sdes:
465 * @session: the object which received the signal
468 * Notify that a new SDES was received for SSRC.
470 gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
471 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
472 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_ssrc_sdes),
473 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
476 * GstRtpSession::on-bye-ssrc:
477 * @sess: the object which received the signal
480 * Notify of an SSRC that became inactive because of a BYE packet.
482 gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
483 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
484 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_ssrc),
485 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
487 * GstRtpSession::on-bye-timeout:
488 * @sess: the object which received the signal
491 * Notify of an SSRC that has timed out because of BYE
493 gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
494 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
495 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_timeout),
496 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
498 * GstRtpSession::on-timeout:
499 * @sess: the object which received the signal
502 * Notify of an SSRC that has timed out
504 gst_rtp_session_signals[SIGNAL_ON_TIMEOUT] =
505 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
506 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_timeout),
507 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
509 * GstRtpSession::on-sender-timeout:
510 * @sess: the object which received the signal
513 * Notify of a sender SSRC that has timed out and became a receiver
515 gst_rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
516 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
517 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
518 on_sender_timeout), NULL, NULL, g_cclosure_marshal_VOID__UINT,
519 G_TYPE_NONE, 1, G_TYPE_UINT);
521 g_object_class_install_property (gobject_class, PROP_NTP_NS_BASE,
522 g_param_spec_uint64 ("ntp-ns-base", "NTP base time",
523 "The NTP base time corresponding to running_time 0 (deprecated)", 0,
524 G_MAXUINT64, DEFAULT_NTP_NS_BASE,
525 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
527 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
528 g_param_spec_double ("bandwidth", "Bandwidth",
529 "The bandwidth of the session in bytes per second (0 for auto-discover)",
530 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
531 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
533 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
534 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
535 "The RTCP bandwidth of the session in bytes per second "
536 "(or as a real fraction of the RTP bandwidth if < 1.0)",
537 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
538 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
540 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
541 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
542 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
543 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
544 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
546 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
547 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
548 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
549 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
550 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
552 g_object_class_install_property (gobject_class, PROP_SDES,
553 g_param_spec_boxed ("sdes", "SDES",
554 "The SDES items of this session",
555 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
557 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
558 g_param_spec_uint ("num-sources", "Num Sources",
559 "The number of sources in the session", 0, G_MAXUINT,
560 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
562 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
563 g_param_spec_uint ("num-active-sources", "Num Active Sources",
564 "The number of active sources in the session", 0, G_MAXUINT,
565 DEFAULT_NUM_ACTIVE_SOURCES,
566 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
568 g_object_class_install_property (gobject_class, PROP_INTERNAL_SESSION,
569 g_param_spec_object ("internal-session", "Internal Session",
570 "The internal RTPSession object", RTP_TYPE_SESSION,
571 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
573 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
574 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
575 "Use the pipeline clock to set the NTP time in the RTCP SR messages",
576 DEFAULT_USE_PIPELINE_CLOCK,
577 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
579 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
580 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
581 "Minimum interval between Regular RTCP packet (in ns)",
582 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
583 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
585 gstelement_class->change_state =
586 GST_DEBUG_FUNCPTR (gst_rtp_session_change_state);
587 gstelement_class->request_new_pad =
588 GST_DEBUG_FUNCPTR (gst_rtp_session_request_new_pad);
589 gstelement_class->release_pad =
590 GST_DEBUG_FUNCPTR (gst_rtp_session_release_pad);
592 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_session_clear_pt_map);
595 gst_element_class_add_pad_template (gstelement_class,
596 gst_static_pad_template_get (&rtpsession_recv_rtp_sink_template));
597 gst_element_class_add_pad_template (gstelement_class,
598 gst_static_pad_template_get (&rtpsession_recv_rtcp_sink_template));
599 gst_element_class_add_pad_template (gstelement_class,
600 gst_static_pad_template_get (&rtpsession_send_rtp_sink_template));
603 gst_element_class_add_pad_template (gstelement_class,
604 gst_static_pad_template_get (&rtpsession_recv_rtp_src_template));
605 gst_element_class_add_pad_template (gstelement_class,
606 gst_static_pad_template_get (&rtpsession_sync_src_template));
607 gst_element_class_add_pad_template (gstelement_class,
608 gst_static_pad_template_get (&rtpsession_send_rtp_src_template));
609 gst_element_class_add_pad_template (gstelement_class,
610 gst_static_pad_template_get (&rtpsession_send_rtcp_src_template));
612 gst_element_class_set_static_metadata (gstelement_class, "RTP Session",
613 "Filter/Network/RTP",
614 "Implement an RTP session", "Wim Taymans <wim.taymans@gmail.com>");
616 GST_DEBUG_CATEGORY_INIT (gst_rtp_session_debug,
617 "rtpsession", 0, "RTP Session");
621 gst_rtp_session_init (GstRtpSession * rtpsession)
623 rtpsession->priv = GST_RTP_SESSION_GET_PRIVATE (rtpsession);
624 g_mutex_init (&rtpsession->priv->lock);
625 rtpsession->priv->sysclock = gst_system_clock_obtain ();
626 rtpsession->priv->session = rtp_session_new ();
627 rtpsession->priv->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
629 /* configure callbacks */
630 rtp_session_set_callbacks (rtpsession->priv->session, &callbacks, rtpsession);
631 /* configure signals */
632 g_signal_connect (rtpsession->priv->session, "on-new-ssrc",
633 (GCallback) on_new_ssrc, rtpsession);
634 g_signal_connect (rtpsession->priv->session, "on-ssrc-collision",
635 (GCallback) on_ssrc_collision, rtpsession);
636 g_signal_connect (rtpsession->priv->session, "on-ssrc-validated",
637 (GCallback) on_ssrc_validated, rtpsession);
638 g_signal_connect (rtpsession->priv->session, "on-ssrc-active",
639 (GCallback) on_ssrc_active, rtpsession);
640 g_signal_connect (rtpsession->priv->session, "on-ssrc-sdes",
641 (GCallback) on_ssrc_sdes, rtpsession);
642 g_signal_connect (rtpsession->priv->session, "on-bye-ssrc",
643 (GCallback) on_bye_ssrc, rtpsession);
644 g_signal_connect (rtpsession->priv->session, "on-bye-timeout",
645 (GCallback) on_bye_timeout, rtpsession);
646 g_signal_connect (rtpsession->priv->session, "on-timeout",
647 (GCallback) on_timeout, rtpsession);
648 g_signal_connect (rtpsession->priv->session, "on-sender-timeout",
649 (GCallback) on_sender_timeout, rtpsession);
650 rtpsession->priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
651 (GDestroyNotify) gst_caps_unref);
653 gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
654 gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
656 rtpsession->priv->thread_stopped = TRUE;
660 gst_rtp_session_finalize (GObject * object)
662 GstRtpSession *rtpsession;
664 rtpsession = GST_RTP_SESSION (object);
666 g_hash_table_destroy (rtpsession->priv->ptmap);
667 g_mutex_clear (&rtpsession->priv->lock);
668 g_object_unref (rtpsession->priv->sysclock);
669 g_object_unref (rtpsession->priv->session);
671 G_OBJECT_CLASS (parent_class)->finalize (object);
675 gst_rtp_session_set_property (GObject * object, guint prop_id,
676 const GValue * value, GParamSpec * pspec)
678 GstRtpSession *rtpsession;
679 GstRtpSessionPrivate *priv;
681 rtpsession = GST_RTP_SESSION (object);
682 priv = rtpsession->priv;
685 case PROP_NTP_NS_BASE:
686 GST_OBJECT_LOCK (rtpsession);
687 priv->ntpnsbase = g_value_get_uint64 (value);
688 GST_DEBUG_OBJECT (rtpsession, "setting NTP base to %" GST_TIME_FORMAT,
689 GST_TIME_ARGS (priv->ntpnsbase));
690 GST_OBJECT_UNLOCK (rtpsession);
693 g_object_set_property (G_OBJECT (priv->session), "bandwidth", value);
695 case PROP_RTCP_FRACTION:
696 g_object_set_property (G_OBJECT (priv->session), "rtcp-fraction", value);
698 case PROP_RTCP_RR_BANDWIDTH:
699 g_object_set_property (G_OBJECT (priv->session), "rtcp-rr-bandwidth",
702 case PROP_RTCP_RS_BANDWIDTH:
703 g_object_set_property (G_OBJECT (priv->session), "rtcp-rs-bandwidth",
707 rtp_session_set_sdes_struct (priv->session, g_value_get_boxed (value));
709 case PROP_USE_PIPELINE_CLOCK:
710 priv->use_pipeline_clock = g_value_get_boolean (value);
712 case PROP_RTCP_MIN_INTERVAL:
713 g_object_set_property (G_OBJECT (priv->session), "rtcp-min-interval",
717 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
723 gst_rtp_session_get_property (GObject * object, guint prop_id,
724 GValue * value, GParamSpec * pspec)
726 GstRtpSession *rtpsession;
727 GstRtpSessionPrivate *priv;
729 rtpsession = GST_RTP_SESSION (object);
730 priv = rtpsession->priv;
733 case PROP_NTP_NS_BASE:
734 GST_OBJECT_LOCK (rtpsession);
735 g_value_set_uint64 (value, priv->ntpnsbase);
736 GST_OBJECT_UNLOCK (rtpsession);
739 g_object_get_property (G_OBJECT (priv->session), "bandwidth", value);
741 case PROP_RTCP_FRACTION:
742 g_object_get_property (G_OBJECT (priv->session), "rtcp-fraction", value);
744 case PROP_RTCP_RR_BANDWIDTH:
745 g_object_get_property (G_OBJECT (priv->session), "rtcp-rr-bandwidth",
748 case PROP_RTCP_RS_BANDWIDTH:
749 g_object_get_property (G_OBJECT (priv->session), "rtcp-rs-bandwidth",
753 g_value_take_boxed (value, rtp_session_get_sdes_struct (priv->session));
755 case PROP_NUM_SOURCES:
756 g_value_set_uint (value, rtp_session_get_num_sources (priv->session));
758 case PROP_NUM_ACTIVE_SOURCES:
759 g_value_set_uint (value,
760 rtp_session_get_num_active_sources (priv->session));
762 case PROP_INTERNAL_SESSION:
763 g_value_set_object (value, priv->session);
765 case PROP_USE_PIPELINE_CLOCK:
766 g_value_set_boolean (value, priv->use_pipeline_clock);
768 case PROP_RTCP_MIN_INTERVAL:
769 g_object_get_property (G_OBJECT (priv->session), "rtcp-min-interval",
773 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
779 get_current_times (GstRtpSession * rtpsession, GstClockTime * running_time,
784 GstClockTime base_time, rt, clock_time;
786 GST_OBJECT_LOCK (rtpsession);
787 if ((clock = GST_ELEMENT_CLOCK (rtpsession))) {
788 base_time = GST_ELEMENT_CAST (rtpsession)->base_time;
789 gst_object_ref (clock);
790 GST_OBJECT_UNLOCK (rtpsession);
792 clock_time = gst_clock_get_time (clock);
794 if (rtpsession->priv->use_pipeline_clock) {
799 /* get current NTP time */
800 g_get_current_time (¤t);
801 ntpns = GST_TIMEVAL_TO_TIME (current);
804 /* add constant to convert from 1970 based time to 1900 based time */
805 ntpns += (2208988800LL * GST_SECOND);
807 /* get current clock time and convert to running time */
808 rt = clock_time - base_time;
810 gst_object_unref (clock);
812 GST_OBJECT_UNLOCK (rtpsession);
823 rtcp_thread (GstRtpSession * rtpsession)
826 GstClockTime current_time;
827 GstClockTime next_timeout;
829 GstClockTime running_time;
833 GST_DEBUG_OBJECT (rtpsession, "entering RTCP thread");
835 GST_RTP_SESSION_LOCK (rtpsession);
837 sysclock = rtpsession->priv->sysclock;
838 current_time = gst_clock_get_time (sysclock);
840 session = rtpsession->priv->session;
842 GST_DEBUG_OBJECT (rtpsession, "starting at %" GST_TIME_FORMAT,
843 GST_TIME_ARGS (current_time));
844 session->start_time = current_time;
846 while (!rtpsession->priv->stop_thread) {
849 /* get initial estimate */
850 next_timeout = rtp_session_next_timeout (session, current_time);
852 GST_DEBUG_OBJECT (rtpsession, "next check time %" GST_TIME_FORMAT,
853 GST_TIME_ARGS (next_timeout));
855 /* leave if no more timeouts, the session ended */
856 if (next_timeout == GST_CLOCK_TIME_NONE)
859 id = rtpsession->priv->id =
860 gst_clock_new_single_shot_id (sysclock, next_timeout);
861 GST_RTP_SESSION_UNLOCK (rtpsession);
863 res = gst_clock_id_wait (id, NULL);
865 GST_RTP_SESSION_LOCK (rtpsession);
866 gst_clock_id_unref (id);
867 rtpsession->priv->id = NULL;
869 if (rtpsession->priv->stop_thread)
872 /* update current time */
873 current_time = gst_clock_get_time (sysclock);
875 /* get current NTP time */
876 get_current_times (rtpsession, &running_time, &ntpnstime);
878 /* we get unlocked because we need to perform reconsideration, don't perform
879 * the timeout but get a new reporting estimate. */
880 GST_DEBUG_OBJECT (rtpsession, "unlocked %d, current %" GST_TIME_FORMAT,
881 res, GST_TIME_ARGS (current_time));
883 /* perform actions, we ignore result. Release lock because it might push. */
884 GST_RTP_SESSION_UNLOCK (rtpsession);
885 rtp_session_on_timeout (session, current_time, ntpnstime, running_time);
886 GST_RTP_SESSION_LOCK (rtpsession);
888 /* mark the thread as stopped now */
889 rtpsession->priv->thread_stopped = TRUE;
890 GST_RTP_SESSION_UNLOCK (rtpsession);
892 GST_DEBUG_OBJECT (rtpsession, "leaving RTCP thread");
896 start_rtcp_thread (GstRtpSession * rtpsession)
898 GError *error = NULL;
901 GST_DEBUG_OBJECT (rtpsession, "starting RTCP thread");
903 GST_RTP_SESSION_LOCK (rtpsession);
904 rtpsession->priv->stop_thread = FALSE;
905 if (rtpsession->priv->thread_stopped) {
906 /* if the thread stopped, and we still have a handle to the thread, join it
907 * now. We can safely join with the lock held, the thread will not take it
909 if (rtpsession->priv->thread)
910 g_thread_join (rtpsession->priv->thread);
911 /* only create a new thread if the old one was stopped. Otherwise we can
912 * just reuse the currently running one. */
913 rtpsession->priv->thread = g_thread_try_new ("rtpsession-rtcp-thread",
914 (GThreadFunc) rtcp_thread, rtpsession, &error);
915 rtpsession->priv->thread_stopped = FALSE;
917 GST_RTP_SESSION_UNLOCK (rtpsession);
921 GST_DEBUG_OBJECT (rtpsession, "failed to start thread, %s", error->message);
922 g_error_free (error);
930 stop_rtcp_thread (GstRtpSession * rtpsession)
932 GST_DEBUG_OBJECT (rtpsession, "stopping RTCP thread");
934 GST_RTP_SESSION_LOCK (rtpsession);
935 rtpsession->priv->stop_thread = TRUE;
936 if (rtpsession->priv->id)
937 gst_clock_id_unschedule (rtpsession->priv->id);
938 GST_RTP_SESSION_UNLOCK (rtpsession);
942 join_rtcp_thread (GstRtpSession * rtpsession)
944 GST_RTP_SESSION_LOCK (rtpsession);
945 /* don't try to join when we have no thread */
946 if (rtpsession->priv->thread != NULL) {
947 GST_DEBUG_OBJECT (rtpsession, "joining RTCP thread");
948 GST_RTP_SESSION_UNLOCK (rtpsession);
950 g_thread_join (rtpsession->priv->thread);
952 GST_RTP_SESSION_LOCK (rtpsession);
953 /* after the join, take the lock and clear the thread structure. The caller
954 * is supposed to not concurrently call start and join. */
955 rtpsession->priv->thread = NULL;
957 GST_RTP_SESSION_UNLOCK (rtpsession);
960 static GstStateChangeReturn
961 gst_rtp_session_change_state (GstElement * element, GstStateChange transition)
963 GstStateChangeReturn res;
964 GstRtpSession *rtpsession;
966 rtpsession = GST_RTP_SESSION (element);
968 switch (transition) {
969 case GST_STATE_CHANGE_NULL_TO_READY:
971 case GST_STATE_CHANGE_READY_TO_PAUSED:
973 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
975 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
976 case GST_STATE_CHANGE_PAUSED_TO_READY:
977 /* no need to join yet, we might want to continue later. Also, the
978 * dataflow could block downstream so that a join could just block
980 stop_rtcp_thread (rtpsession);
986 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
988 switch (transition) {
989 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
990 if (!start_rtcp_thread (rtpsession))
993 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
995 case GST_STATE_CHANGE_PAUSED_TO_READY:
996 /* downstream is now releasing the dataflow and we can join. */
997 join_rtcp_thread (rtpsession);
999 case GST_STATE_CHANGE_READY_TO_NULL:
1009 return GST_STATE_CHANGE_FAILURE;
1014 return_true (gpointer key, gpointer value, gpointer user_data)
1020 gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession)
1022 g_hash_table_foreach_remove (rtpsession->priv->ptmap, return_true, NULL);
1025 /* called when the session manager has an RTP packet or a list of packets
1026 * ready for further processing */
1027 static GstFlowReturn
1028 gst_rtp_session_process_rtp (RTPSession * sess, RTPSource * src,
1029 GstBuffer * buffer, gpointer user_data)
1031 GstFlowReturn result;
1032 GstRtpSession *rtpsession;
1035 rtpsession = GST_RTP_SESSION (user_data);
1037 GST_RTP_SESSION_LOCK (rtpsession);
1038 if ((rtp_src = rtpsession->recv_rtp_src))
1039 gst_object_ref (rtp_src);
1040 GST_RTP_SESSION_UNLOCK (rtpsession);
1043 GST_LOG_OBJECT (rtpsession, "pushing received RTP packet");
1044 result = gst_pad_push (rtp_src, buffer);
1045 gst_object_unref (rtp_src);
1047 GST_DEBUG_OBJECT (rtpsession, "dropping received RTP packet");
1048 gst_buffer_unref (buffer);
1049 result = GST_FLOW_OK;
1054 /* called when the session manager has an RTP packet ready for further
1056 static GstFlowReturn
1057 gst_rtp_session_send_rtp (RTPSession * sess, RTPSource * src,
1058 gpointer data, gpointer user_data)
1060 GstFlowReturn result;
1061 GstRtpSession *rtpsession;
1064 rtpsession = GST_RTP_SESSION (user_data);
1066 GST_RTP_SESSION_LOCK (rtpsession);
1067 if ((rtp_src = rtpsession->send_rtp_src))
1068 gst_object_ref (rtp_src);
1069 GST_RTP_SESSION_UNLOCK (rtpsession);
1072 if (GST_IS_BUFFER (data)) {
1073 GST_LOG_OBJECT (rtpsession, "sending RTP packet");
1074 result = gst_pad_push (rtp_src, GST_BUFFER_CAST (data));
1076 GST_LOG_OBJECT (rtpsession, "sending RTP list");
1077 result = gst_pad_push_list (rtp_src, GST_BUFFER_LIST_CAST (data));
1079 gst_object_unref (rtp_src);
1081 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1082 result = GST_FLOW_OK;
1087 /* called when the session manager has an RTCP packet ready for further
1088 * sending. The eos flag is set when an EOS event should be sent downstream as
1090 static GstFlowReturn
1091 gst_rtp_session_send_rtcp (RTPSession * sess, RTPSource * src,
1092 GstBuffer * buffer, gboolean eos, gpointer user_data)
1094 GstFlowReturn result;
1095 GstRtpSession *rtpsession;
1098 rtpsession = GST_RTP_SESSION (user_data);
1100 GST_RTP_SESSION_LOCK (rtpsession);
1101 if (rtpsession->priv->stop_thread)
1104 if ((rtcp_src = rtpsession->send_rtcp_src)) {
1107 gst_object_ref (rtcp_src);
1108 GST_RTP_SESSION_UNLOCK (rtpsession);
1110 /* set rtcp caps on output pad */
1111 if (!(caps = gst_pad_get_current_caps (rtcp_src))) {
1112 caps = gst_caps_new_empty_simple ("application/x-rtcp");
1113 gst_pad_set_caps (rtcp_src, caps);
1115 gst_caps_unref (caps);
1117 GST_LOG_OBJECT (rtpsession, "sending RTCP");
1118 result = gst_pad_push (rtcp_src, buffer);
1120 /* we have to send EOS after this packet */
1122 GST_LOG_OBJECT (rtpsession, "sending EOS");
1123 gst_pad_push_event (rtcp_src, gst_event_new_eos ());
1125 gst_object_unref (rtcp_src);
1127 GST_RTP_SESSION_UNLOCK (rtpsession);
1129 GST_DEBUG_OBJECT (rtpsession, "not sending RTCP, no output pad");
1130 gst_buffer_unref (buffer);
1131 result = GST_FLOW_OK;
1138 GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1139 gst_buffer_unref (buffer);
1140 GST_RTP_SESSION_UNLOCK (rtpsession);
1145 /* called when the session manager has an SR RTCP packet ready for handling
1146 * inter stream synchronisation */
1147 static GstFlowReturn
1148 gst_rtp_session_sync_rtcp (RTPSession * sess, RTPSource * src,
1149 GstBuffer * buffer, gpointer user_data)
1151 GstFlowReturn result;
1152 GstRtpSession *rtpsession;
1155 rtpsession = GST_RTP_SESSION (user_data);
1157 GST_RTP_SESSION_LOCK (rtpsession);
1158 if (rtpsession->priv->stop_thread)
1161 if ((sync_src = rtpsession->sync_src)) {
1164 gst_object_ref (sync_src);
1165 GST_RTP_SESSION_UNLOCK (rtpsession);
1167 /* set rtcp caps on output pad */
1168 if (!(caps = gst_pad_get_current_caps (sync_src))) {
1169 caps = gst_caps_new_empty_simple ("application/x-rtcp");
1170 gst_pad_set_caps (sync_src, caps);
1172 gst_caps_unref (caps);
1174 GST_LOG_OBJECT (rtpsession, "sending Sync RTCP");
1175 result = gst_pad_push (sync_src, buffer);
1176 gst_object_unref (sync_src);
1178 GST_RTP_SESSION_UNLOCK (rtpsession);
1180 GST_DEBUG_OBJECT (rtpsession, "not sending Sync RTCP, no output pad");
1181 gst_buffer_unref (buffer);
1182 result = GST_FLOW_OK;
1189 GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1190 gst_buffer_unref (buffer);
1191 GST_RTP_SESSION_UNLOCK (rtpsession);
1197 gst_rtp_session_cache_caps (GstRtpSession * rtpsession, GstCaps * caps)
1199 GstRtpSessionPrivate *priv;
1200 const GstStructure *s;
1203 priv = rtpsession->priv;
1205 GST_DEBUG_OBJECT (rtpsession, "parsing caps");
1207 s = gst_caps_get_structure (caps, 0);
1208 if (!gst_structure_get_int (s, "payload", &payload))
1211 if (g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (payload)))
1214 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (payload),
1215 gst_caps_ref (caps));
1219 gst_rtp_session_get_caps_for_pt (GstRtpSession * rtpsession, guint payload)
1221 GstCaps *caps = NULL;
1222 GValue args[2] = { {0}, {0} };
1225 GST_RTP_SESSION_LOCK (rtpsession);
1226 caps = g_hash_table_lookup (rtpsession->priv->ptmap,
1227 GINT_TO_POINTER (payload));
1229 gst_caps_ref (caps);
1233 /* not found in the cache, try to get it with a signal */
1234 g_value_init (&args[0], GST_TYPE_ELEMENT);
1235 g_value_set_object (&args[0], rtpsession);
1236 g_value_init (&args[1], G_TYPE_UINT);
1237 g_value_set_uint (&args[1], payload);
1239 g_value_init (&ret, GST_TYPE_CAPS);
1240 g_value_set_boxed (&ret, NULL);
1242 GST_RTP_SESSION_UNLOCK (rtpsession);
1244 g_signal_emitv (args, gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP], 0,
1247 GST_RTP_SESSION_LOCK (rtpsession);
1249 g_value_unset (&args[0]);
1250 g_value_unset (&args[1]);
1251 caps = (GstCaps *) g_value_dup_boxed (&ret);
1252 g_value_unset (&ret);
1256 gst_rtp_session_cache_caps (rtpsession, caps);
1259 GST_RTP_SESSION_UNLOCK (rtpsession);
1265 GST_DEBUG_OBJECT (rtpsession, "could not get caps");
1270 /* called when the session manager needs the clock rate */
1272 gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
1276 GstRtpSession *rtpsession;
1278 const GstStructure *s;
1280 rtpsession = GST_RTP_SESSION_CAST (user_data);
1282 caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1287 s = gst_caps_get_structure (caps, 0);
1288 if (!gst_structure_get_int (s, "clock-rate", &result))
1291 gst_caps_unref (caps);
1293 GST_DEBUG_OBJECT (rtpsession, "parsed clock-rate %d", result);
1302 gst_caps_unref (caps);
1303 GST_DEBUG_OBJECT (rtpsession, "No clock-rate in caps!");
1308 /* called when the session manager asks us to reconsider the timeout */
1310 gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data)
1312 GstRtpSession *rtpsession;
1314 rtpsession = GST_RTP_SESSION_CAST (user_data);
1316 GST_RTP_SESSION_LOCK (rtpsession);
1317 GST_DEBUG_OBJECT (rtpsession, "unlock timer for reconsideration");
1318 if (rtpsession->priv->id)
1319 gst_clock_id_unschedule (rtpsession->priv->id);
1320 GST_RTP_SESSION_UNLOCK (rtpsession);
1324 gst_rtp_session_event_recv_rtp_sink (GstPad * pad, GstObject * parent,
1327 GstRtpSession *rtpsession;
1328 gboolean ret = FALSE;
1330 rtpsession = GST_RTP_SESSION (parent);
1332 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1333 GST_EVENT_TYPE_NAME (event));
1335 switch (GST_EVENT_TYPE (event)) {
1336 case GST_EVENT_CAPS:
1341 gst_event_parse_caps (event, &caps);
1342 gst_rtp_session_sink_setcaps (pad, rtpsession, caps);
1343 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1346 case GST_EVENT_FLUSH_STOP:
1347 gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
1348 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1350 case GST_EVENT_SEGMENT:
1352 GstSegment *segment, in_segment;
1354 segment = &rtpsession->recv_rtp_seg;
1356 /* the newsegment event is needed to convert the RTP timestamp to
1357 * running_time, which is needed to generate a mapping from RTP to NTP
1358 * timestamps in SR reports */
1359 gst_event_copy_segment (event, &in_segment);
1360 GST_DEBUG_OBJECT (rtpsession, "received segment %" GST_SEGMENT_FORMAT,
1363 /* accept upstream */
1364 gst_segment_copy_into (&in_segment, segment);
1366 /* push event forward */
1367 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1371 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1380 gst_rtp_session_request_remote_key_unit (GstRtpSession * rtpsession,
1381 guint32 ssrc, guint payload, gboolean all_headers, gint count)
1385 caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1388 const GstStructure *s = gst_caps_get_structure (caps, 0);
1392 pli = gst_structure_has_field (s, "rtcp-fb-nack-pli");
1393 fir = gst_structure_has_field (s, "rtcp-fb-ccm-fir") && all_headers;
1395 /* Google Talk uses FIR for repair, so send it even if we just want a
1398 gst_structure_has_field (s, "rtcp-fb-x-gstreamer-fir-as-repair"))
1401 gst_caps_unref (caps);
1404 return rtp_session_request_key_unit (rtpsession->priv->session, ssrc,
1405 gst_clock_get_time (rtpsession->priv->sysclock), fir, count);
1412 gst_rtp_session_event_recv_rtp_src (GstPad * pad, GstObject * parent,
1415 GstRtpSession *rtpsession;
1416 gboolean forward = TRUE;
1417 gboolean ret = TRUE;
1418 const GstStructure *s;
1422 rtpsession = GST_RTP_SESSION (parent);
1424 switch (GST_EVENT_TYPE (event)) {
1425 case GST_EVENT_CUSTOM_UPSTREAM:
1426 s = gst_event_get_structure (event);
1427 if (gst_structure_has_name (s, "GstForceKeyUnit") &&
1428 gst_structure_get_uint (s, "ssrc", &ssrc) &&
1429 gst_structure_get_uint (s, "payload", &pt)) {
1430 gboolean all_headers = FALSE;
1433 gst_structure_get_boolean (s, "all-headers", &all_headers);
1434 if (gst_structure_get_int (s, "count", &count) && count < 0)
1435 count += G_MAXINT; /* Make sure count is positive if present */
1436 if (gst_rtp_session_request_remote_key_unit (rtpsession, ssrc, pt,
1437 all_headers, count))
1446 ret = gst_pad_push_event (rtpsession->recv_rtp_sink, event);
1452 static GstIterator *
1453 gst_rtp_session_iterate_internal_links (GstPad * pad, GstObject * parent)
1455 GstRtpSession *rtpsession;
1456 GstPad *otherpad = NULL;
1457 GstIterator *it = NULL;
1459 rtpsession = GST_RTP_SESSION (parent);
1461 GST_RTP_SESSION_LOCK (rtpsession);
1462 if (pad == rtpsession->recv_rtp_src) {
1463 otherpad = gst_object_ref (rtpsession->recv_rtp_sink);
1464 } else if (pad == rtpsession->recv_rtp_sink) {
1465 otherpad = gst_object_ref (rtpsession->recv_rtp_src);
1466 } else if (pad == rtpsession->send_rtp_src) {
1467 otherpad = gst_object_ref (rtpsession->send_rtp_sink);
1468 } else if (pad == rtpsession->send_rtp_sink) {
1469 otherpad = gst_object_ref (rtpsession->send_rtp_src);
1471 GST_RTP_SESSION_UNLOCK (rtpsession);
1474 GValue val = { 0, };
1476 g_value_init (&val, GST_TYPE_PAD);
1477 g_value_set_object (&val, otherpad);
1478 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
1479 g_value_unset (&val);
1480 gst_object_unref (otherpad);
1487 gst_rtp_session_sink_setcaps (GstPad * pad, GstRtpSession * rtpsession,
1490 GST_RTP_SESSION_LOCK (rtpsession);
1491 gst_rtp_session_cache_caps (rtpsession, caps);
1492 GST_RTP_SESSION_UNLOCK (rtpsession);
1497 /* receive a packet from a sender, send it to the RTP session manager and
1498 * forward the packet on the rtp_src pad
1500 static GstFlowReturn
1501 gst_rtp_session_chain_recv_rtp (GstPad * pad, GstObject * parent,
1504 GstRtpSession *rtpsession;
1505 GstRtpSessionPrivate *priv;
1507 GstClockTime current_time, running_time;
1508 GstClockTime timestamp;
1510 rtpsession = GST_RTP_SESSION (parent);
1511 priv = rtpsession->priv;
1513 GST_LOG_OBJECT (rtpsession, "received RTP packet");
1515 /* get NTP time when this packet was captured, this depends on the timestamp. */
1516 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1517 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1518 /* convert to running time using the segment values */
1520 gst_segment_to_running_time (&rtpsession->recv_rtp_seg, GST_FORMAT_TIME,
1523 get_current_times (rtpsession, &running_time, NULL);
1525 current_time = gst_clock_get_time (priv->sysclock);
1527 ret = rtp_session_process_rtp (priv->session, buffer, current_time,
1529 if (ret != GST_FLOW_OK)
1539 GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1540 gst_flow_get_name (ret));
1546 gst_rtp_session_event_recv_rtcp_sink (GstPad * pad, GstObject * parent,
1549 GstRtpSession *rtpsession;
1550 gboolean ret = FALSE;
1552 rtpsession = GST_RTP_SESSION (parent);
1554 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1555 GST_EVENT_TYPE_NAME (event));
1557 switch (GST_EVENT_TYPE (event)) {
1559 ret = gst_pad_push_event (rtpsession->sync_src, event);
1566 /* Receive an RTCP packet from a sender, send it to the RTP session manager and
1567 * forward the SR packets to the sync_src pad.
1569 static GstFlowReturn
1570 gst_rtp_session_chain_recv_rtcp (GstPad * pad, GstObject * parent,
1573 GstRtpSession *rtpsession;
1574 GstRtpSessionPrivate *priv;
1575 GstClockTime current_time;
1578 rtpsession = GST_RTP_SESSION (parent);
1579 priv = rtpsession->priv;
1581 GST_LOG_OBJECT (rtpsession, "received RTCP packet");
1583 current_time = gst_clock_get_time (priv->sysclock);
1584 get_current_times (rtpsession, NULL, &ntpnstime);
1586 rtp_session_process_rtcp (priv->session, buffer, current_time, ntpnstime);
1588 return GST_FLOW_OK; /* always return OK */
1592 gst_rtp_session_query_send_rtcp_src (GstPad * pad, GstObject * parent,
1595 GstRtpSession *rtpsession;
1596 gboolean ret = FALSE;
1598 rtpsession = GST_RTP_SESSION (parent);
1600 GST_DEBUG_OBJECT (rtpsession, "received QUERY");
1602 switch (GST_QUERY_TYPE (query)) {
1603 case GST_QUERY_LATENCY:
1605 /* use the defaults for the latency query. */
1606 gst_query_set_latency (query, FALSE, 0, -1);
1609 /* other queries simply fail for now */
1617 gst_rtp_session_event_send_rtcp_src (GstPad * pad, GstObject * parent,
1620 GstRtpSession *rtpsession;
1621 gboolean ret = TRUE;
1623 rtpsession = GST_RTP_SESSION (parent);
1624 GST_DEBUG_OBJECT (rtpsession, "received EVENT");
1626 switch (GST_EVENT_TYPE (event)) {
1627 case GST_EVENT_SEEK:
1628 case GST_EVENT_LATENCY:
1629 gst_event_unref (event);
1633 /* other events simply fail for now */
1634 gst_event_unref (event);
1644 gst_rtp_session_event_send_rtp_sink (GstPad * pad, GstObject * parent,
1647 GstRtpSession *rtpsession;
1648 gboolean ret = FALSE;
1650 rtpsession = GST_RTP_SESSION (parent);
1652 GST_DEBUG_OBJECT (rtpsession, "received event");
1654 switch (GST_EVENT_TYPE (event)) {
1655 case GST_EVENT_CAPS:
1660 gst_event_parse_caps (event, &caps);
1661 gst_rtp_session_setcaps_send_rtp (pad, rtpsession, caps);
1662 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1665 case GST_EVENT_FLUSH_STOP:
1666 gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
1667 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1669 case GST_EVENT_SEGMENT:{
1670 GstSegment *segment, in_segment;
1672 segment = &rtpsession->send_rtp_seg;
1674 /* the newsegment event is needed to convert the RTP timestamp to
1675 * running_time, which is needed to generate a mapping from RTP to NTP
1676 * timestamps in SR reports */
1677 gst_event_copy_segment (event, &in_segment);
1678 GST_DEBUG_OBJECT (rtpsession, "received segment %" GST_SEGMENT_FORMAT,
1681 /* accept upstream */
1682 gst_segment_copy_into (&in_segment, segment);
1684 /* push event forward */
1685 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1688 case GST_EVENT_EOS:{
1689 GstClockTime current_time;
1691 /* push downstream FIXME, we are not supposed to leave the session just
1692 * because we stop sending. */
1693 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1694 current_time = gst_clock_get_time (rtpsession->priv->sysclock);
1695 GST_DEBUG_OBJECT (rtpsession, "scheduling BYE message");
1696 rtp_session_schedule_bye (rtpsession->priv->session, "End of stream",
1701 GstPad *send_rtp_src = NULL;
1702 GST_RTP_SESSION_LOCK (rtpsession);
1703 if (rtpsession->send_rtp_src)
1704 send_rtp_src = gst_object_ref (rtpsession->send_rtp_src);
1705 GST_RTP_SESSION_UNLOCK (rtpsession);
1708 ret = gst_pad_push_event (send_rtp_src, event);
1709 gst_object_unref (send_rtp_src);
1711 gst_event_unref (event);
1721 gst_rtp_session_getcaps_send_rtp (GstPad * pad, GstRtpSession * rtpsession,
1724 GstRtpSessionPrivate *priv;
1726 GstStructure *s1, *s2;
1729 priv = rtpsession->priv;
1731 ssrc = rtp_session_get_internal_ssrc (priv->session);
1733 /* we can basically accept anything but we prefer to receive packets with our
1734 * internal SSRC so that we don't have to patch it. Create a structure with
1735 * the SSRC and another one without. */
1736 s1 = gst_structure_new ("application/x-rtp", "ssrc", G_TYPE_UINT, ssrc, NULL);
1737 s2 = gst_structure_new_empty ("application/x-rtp");
1739 result = gst_caps_new_full (s1, s2, NULL);
1742 GstCaps *caps = result;
1744 result = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
1745 gst_caps_unref (caps);
1748 GST_DEBUG_OBJECT (rtpsession, "getting caps %" GST_PTR_FORMAT, result);
1754 gst_rtp_session_query_send_rtp (GstPad * pad, GstObject * parent,
1757 gboolean res = FALSE;
1758 GstRtpSession *rtpsession;
1760 rtpsession = GST_RTP_SESSION (parent);
1762 switch (GST_QUERY_TYPE (query)) {
1763 case GST_QUERY_CAPS:
1765 GstCaps *filter, *caps;
1767 gst_query_parse_caps (query, &filter);
1768 caps = gst_rtp_session_getcaps_send_rtp (pad, rtpsession, filter);
1769 gst_query_set_caps_result (query, caps);
1770 gst_caps_unref (caps);
1775 res = gst_pad_query_default (pad, parent, query);
1783 gst_rtp_session_setcaps_send_rtp (GstPad * pad, GstRtpSession * rtpsession,
1786 GstRtpSessionPrivate *priv;
1787 GstStructure *s = gst_caps_get_structure (caps, 0);
1790 priv = rtpsession->priv;
1792 if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
1793 GST_DEBUG_OBJECT (rtpsession, "setting internal SSRC to %08x", ssrc);
1794 rtp_session_set_internal_ssrc (priv->session, ssrc);
1799 /* Recieve an RTP packet or a list of packets to be send to the receivers,
1800 * send to RTP session manager and forward to send_rtp_src.
1802 static GstFlowReturn
1803 gst_rtp_session_chain_send_rtp_common (GstRtpSession * rtpsession,
1804 gpointer data, gboolean is_list)
1806 GstRtpSessionPrivate *priv;
1808 GstClockTime timestamp, running_time;
1809 GstClockTime current_time;
1811 priv = rtpsession->priv;
1813 GST_LOG_OBJECT (rtpsession, "received RTP %s", is_list ? "list" : "packet");
1815 /* get NTP time when this packet was captured, this depends on the timestamp. */
1817 GstBuffer *buffer = NULL;
1819 /* All groups in an list have the same timestamp.
1820 * So, just take it from the first group. */
1821 buffer = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0);
1823 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1827 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (data));
1830 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1831 /* convert to running time using the segment start value. */
1833 gst_segment_to_running_time (&rtpsession->send_rtp_seg, GST_FORMAT_TIME,
1840 current_time = gst_clock_get_time (priv->sysclock);
1841 ret = rtp_session_send_rtp (priv->session, data, is_list, current_time,
1843 if (ret != GST_FLOW_OK)
1853 GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1854 gst_flow_get_name (ret));
1859 static GstFlowReturn
1860 gst_rtp_session_chain_send_rtp (GstPad * pad, GstObject * parent,
1863 GstRtpSession *rtpsession = GST_RTP_SESSION (parent);
1865 return gst_rtp_session_chain_send_rtp_common (rtpsession, buffer, FALSE);
1868 static GstFlowReturn
1869 gst_rtp_session_chain_send_rtp_list (GstPad * pad, GstObject * parent,
1870 GstBufferList * list)
1872 GstRtpSession *rtpsession = GST_RTP_SESSION (parent);
1874 return gst_rtp_session_chain_send_rtp_common (rtpsession, list, TRUE);
1877 /* Create sinkpad to receive RTP packets from senders. This will also create a
1878 * srcpad for the RTP packets.
1881 create_recv_rtp_sink (GstRtpSession * rtpsession)
1883 GST_DEBUG_OBJECT (rtpsession, "creating RTP sink pad");
1885 rtpsession->recv_rtp_sink =
1886 gst_pad_new_from_static_template (&rtpsession_recv_rtp_sink_template,
1888 gst_pad_set_chain_function (rtpsession->recv_rtp_sink,
1889 gst_rtp_session_chain_recv_rtp);
1890 gst_pad_set_event_function (rtpsession->recv_rtp_sink,
1891 (GstPadEventFunction) gst_rtp_session_event_recv_rtp_sink);
1892 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_sink,
1893 gst_rtp_session_iterate_internal_links);
1894 gst_pad_set_active (rtpsession->recv_rtp_sink, TRUE);
1895 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1896 rtpsession->recv_rtp_sink);
1898 GST_DEBUG_OBJECT (rtpsession, "creating RTP src pad");
1899 rtpsession->recv_rtp_src =
1900 gst_pad_new_from_static_template (&rtpsession_recv_rtp_src_template,
1902 gst_pad_set_event_function (rtpsession->recv_rtp_src,
1903 (GstPadEventFunction) gst_rtp_session_event_recv_rtp_src);
1904 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_src,
1905 gst_rtp_session_iterate_internal_links);
1906 gst_pad_use_fixed_caps (rtpsession->recv_rtp_src);
1907 gst_pad_set_active (rtpsession->recv_rtp_src, TRUE);
1908 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->recv_rtp_src);
1910 return rtpsession->recv_rtp_sink;
1913 /* Remove sinkpad to receive RTP packets from senders. This will also remove
1914 * the srcpad for the RTP packets.
1917 remove_recv_rtp_sink (GstRtpSession * rtpsession)
1919 GST_DEBUG_OBJECT (rtpsession, "removing RTP sink pad");
1921 /* deactivate from source to sink */
1922 gst_pad_set_active (rtpsession->recv_rtp_src, FALSE);
1923 gst_pad_set_active (rtpsession->recv_rtp_sink, FALSE);
1926 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1927 rtpsession->recv_rtp_sink);
1928 rtpsession->recv_rtp_sink = NULL;
1930 GST_DEBUG_OBJECT (rtpsession, "removing RTP src pad");
1931 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1932 rtpsession->recv_rtp_src);
1933 rtpsession->recv_rtp_src = NULL;
1936 /* Create a sinkpad to receive RTCP messages from senders, this will also create a
1937 * sync_src pad for the SR packets.
1940 create_recv_rtcp_sink (GstRtpSession * rtpsession)
1942 GST_DEBUG_OBJECT (rtpsession, "creating RTCP sink pad");
1944 rtpsession->recv_rtcp_sink =
1945 gst_pad_new_from_static_template (&rtpsession_recv_rtcp_sink_template,
1947 gst_pad_set_chain_function (rtpsession->recv_rtcp_sink,
1948 gst_rtp_session_chain_recv_rtcp);
1949 gst_pad_set_event_function (rtpsession->recv_rtcp_sink,
1950 (GstPadEventFunction) gst_rtp_session_event_recv_rtcp_sink);
1951 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtcp_sink,
1952 gst_rtp_session_iterate_internal_links);
1953 gst_pad_set_active (rtpsession->recv_rtcp_sink, TRUE);
1954 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1955 rtpsession->recv_rtcp_sink);
1957 GST_DEBUG_OBJECT (rtpsession, "creating sync src pad");
1958 rtpsession->sync_src =
1959 gst_pad_new_from_static_template (&rtpsession_sync_src_template,
1961 gst_pad_set_iterate_internal_links_function (rtpsession->sync_src,
1962 gst_rtp_session_iterate_internal_links);
1963 gst_pad_use_fixed_caps (rtpsession->sync_src);
1964 gst_pad_set_active (rtpsession->sync_src, TRUE);
1965 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
1967 return rtpsession->recv_rtcp_sink;
1971 remove_recv_rtcp_sink (GstRtpSession * rtpsession)
1973 GST_DEBUG_OBJECT (rtpsession, "removing RTCP sink pad");
1975 gst_pad_set_active (rtpsession->sync_src, FALSE);
1976 gst_pad_set_active (rtpsession->recv_rtcp_sink, FALSE);
1978 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1979 rtpsession->recv_rtcp_sink);
1980 rtpsession->recv_rtcp_sink = NULL;
1982 GST_DEBUG_OBJECT (rtpsession, "removing sync src pad");
1983 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
1984 rtpsession->sync_src = NULL;
1987 /* Create a sinkpad to receive RTP packets for receivers. This will also create a
1991 create_send_rtp_sink (GstRtpSession * rtpsession)
1993 GST_DEBUG_OBJECT (rtpsession, "creating pad");
1995 rtpsession->send_rtp_sink =
1996 gst_pad_new_from_static_template (&rtpsession_send_rtp_sink_template,
1998 gst_pad_set_chain_function (rtpsession->send_rtp_sink,
1999 gst_rtp_session_chain_send_rtp);
2000 gst_pad_set_chain_list_function (rtpsession->send_rtp_sink,
2001 gst_rtp_session_chain_send_rtp_list);
2002 gst_pad_set_query_function (rtpsession->send_rtp_sink,
2003 gst_rtp_session_query_send_rtp);
2004 gst_pad_set_event_function (rtpsession->send_rtp_sink,
2005 (GstPadEventFunction) gst_rtp_session_event_send_rtp_sink);
2006 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_sink,
2007 gst_rtp_session_iterate_internal_links);
2008 gst_pad_set_active (rtpsession->send_rtp_sink, TRUE);
2009 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2010 rtpsession->send_rtp_sink);
2012 rtpsession->send_rtp_src =
2013 gst_pad_new_from_static_template (&rtpsession_send_rtp_src_template,
2015 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_src,
2016 gst_rtp_session_iterate_internal_links);
2017 gst_pad_set_active (rtpsession->send_rtp_src, TRUE);
2018 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->send_rtp_src);
2020 return rtpsession->send_rtp_sink;
2024 remove_send_rtp_sink (GstRtpSession * rtpsession)
2026 GST_DEBUG_OBJECT (rtpsession, "removing pad");
2028 gst_pad_set_active (rtpsession->send_rtp_src, FALSE);
2029 gst_pad_set_active (rtpsession->send_rtp_sink, FALSE);
2031 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2032 rtpsession->send_rtp_sink);
2033 rtpsession->send_rtp_sink = NULL;
2035 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2036 rtpsession->send_rtp_src);
2037 rtpsession->send_rtp_src = NULL;
2040 /* Create a srcpad with the RTCP packets to send out.
2041 * This pad will be driven by the RTP session manager when it wants to send out
2045 create_send_rtcp_src (GstRtpSession * rtpsession)
2047 GST_DEBUG_OBJECT (rtpsession, "creating pad");
2049 rtpsession->send_rtcp_src =
2050 gst_pad_new_from_static_template (&rtpsession_send_rtcp_src_template,
2052 gst_pad_use_fixed_caps (rtpsession->send_rtcp_src);
2053 gst_pad_set_active (rtpsession->send_rtcp_src, TRUE);
2054 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtcp_src,
2055 gst_rtp_session_iterate_internal_links);
2056 gst_pad_set_query_function (rtpsession->send_rtcp_src,
2057 gst_rtp_session_query_send_rtcp_src);
2058 gst_pad_set_event_function (rtpsession->send_rtcp_src,
2059 gst_rtp_session_event_send_rtcp_src);
2060 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2061 rtpsession->send_rtcp_src);
2063 return rtpsession->send_rtcp_src;
2067 remove_send_rtcp_src (GstRtpSession * rtpsession)
2069 GST_DEBUG_OBJECT (rtpsession, "removing pad");
2071 gst_pad_set_active (rtpsession->send_rtcp_src, FALSE);
2073 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2074 rtpsession->send_rtcp_src);
2075 rtpsession->send_rtcp_src = NULL;
2079 gst_rtp_session_request_new_pad (GstElement * element,
2080 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
2082 GstRtpSession *rtpsession;
2083 GstElementClass *klass;
2086 g_return_val_if_fail (templ != NULL, NULL);
2087 g_return_val_if_fail (GST_IS_RTP_SESSION (element), NULL);
2089 rtpsession = GST_RTP_SESSION (element);
2090 klass = GST_ELEMENT_GET_CLASS (element);
2092 GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
2094 GST_RTP_SESSION_LOCK (rtpsession);
2096 /* figure out the template */
2097 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink")) {
2098 if (rtpsession->recv_rtp_sink != NULL)
2101 result = create_recv_rtp_sink (rtpsession);
2102 } else if (templ == gst_element_class_get_pad_template (klass,
2103 "recv_rtcp_sink")) {
2104 if (rtpsession->recv_rtcp_sink != NULL)
2107 result = create_recv_rtcp_sink (rtpsession);
2108 } else if (templ == gst_element_class_get_pad_template (klass,
2110 if (rtpsession->send_rtp_sink != NULL)
2113 result = create_send_rtp_sink (rtpsession);
2114 } else if (templ == gst_element_class_get_pad_template (klass,
2116 if (rtpsession->send_rtcp_src != NULL)
2119 result = create_send_rtcp_src (rtpsession);
2121 goto wrong_template;
2123 GST_RTP_SESSION_UNLOCK (rtpsession);
2130 GST_RTP_SESSION_UNLOCK (rtpsession);
2131 g_warning ("gstrtpsession: this is not our template");
2136 GST_RTP_SESSION_UNLOCK (rtpsession);
2137 g_warning ("gstrtpsession: pad already requested");
2143 gst_rtp_session_release_pad (GstElement * element, GstPad * pad)
2145 GstRtpSession *rtpsession;
2147 g_return_if_fail (GST_IS_RTP_SESSION (element));
2148 g_return_if_fail (GST_IS_PAD (pad));
2150 rtpsession = GST_RTP_SESSION (element);
2152 GST_DEBUG_OBJECT (element, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2154 GST_RTP_SESSION_LOCK (rtpsession);
2156 if (rtpsession->recv_rtp_sink == pad) {
2157 remove_recv_rtp_sink (rtpsession);
2158 } else if (rtpsession->recv_rtcp_sink == pad) {
2159 remove_recv_rtcp_sink (rtpsession);
2160 } else if (rtpsession->send_rtp_sink == pad) {
2161 remove_send_rtp_sink (rtpsession);
2162 } else if (rtpsession->send_rtcp_src == pad) {
2163 remove_send_rtcp_src (rtpsession);
2167 GST_RTP_SESSION_UNLOCK (rtpsession);
2174 GST_RTP_SESSION_UNLOCK (rtpsession);
2175 g_warning ("gstrtpsession: asked to release an unknown pad");
2181 gst_rtp_session_request_key_unit (RTPSession * sess,
2182 gboolean all_headers, gpointer user_data)
2184 GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2187 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
2188 gst_structure_new ("GstForceKeyUnit",
2189 "all-headers", G_TYPE_BOOLEAN, all_headers, NULL));
2190 gst_pad_push_event (rtpsession->send_rtp_sink, event);
2194 gst_rtp_session_request_time (RTPSession * session, gpointer user_data)
2196 GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2198 return gst_clock_get_time (rtpsession->priv->sysclock);