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
125 static GstStaticPadTemplate rtpsession_recv_rtp_sink_template =
126 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink",
129 GST_STATIC_CAPS ("application/x-rtp")
132 static GstStaticPadTemplate rtpsession_recv_rtcp_sink_template =
133 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink",
136 GST_STATIC_CAPS ("application/x-rtcp")
139 static GstStaticPadTemplate rtpsession_send_rtp_sink_template =
140 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink",
143 GST_STATIC_CAPS ("application/x-rtp")
147 static GstStaticPadTemplate rtpsession_recv_rtp_src_template =
148 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src",
151 GST_STATIC_CAPS ("application/x-rtp")
154 static GstStaticPadTemplate rtpsession_sync_src_template =
155 GST_STATIC_PAD_TEMPLATE ("sync_src",
158 GST_STATIC_CAPS ("application/x-rtcp")
161 static GstStaticPadTemplate rtpsession_send_rtp_src_template =
162 GST_STATIC_PAD_TEMPLATE ("send_rtp_src",
165 GST_STATIC_CAPS ("application/x-rtp")
168 static GstStaticPadTemplate rtpsession_send_rtcp_src_template =
169 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src",
172 GST_STATIC_CAPS ("application/x-rtcp")
175 /* signals and args */
178 SIGNAL_REQUEST_PT_MAP,
182 SIGNAL_ON_SSRC_COLLISION,
183 SIGNAL_ON_SSRC_VALIDATED,
184 SIGNAL_ON_SSRC_ACTIVE,
187 SIGNAL_ON_BYE_TIMEOUT,
189 SIGNAL_ON_SENDER_TIMEOUT,
193 #define DEFAULT_NTP_NS_BASE 0
194 #define DEFAULT_BANDWIDTH RTP_STATS_BANDWIDTH
195 #define DEFAULT_RTCP_FRACTION (RTP_STATS_BANDWIDTH * RTP_STATS_RTCP_FRACTION)
196 #define DEFAULT_RTCP_RR_BANDWIDTH -1
197 #define DEFAULT_RTCP_RS_BANDWIDTH -1
198 #define DEFAULT_SDES NULL
199 #define DEFAULT_NUM_SOURCES 0
200 #define DEFAULT_NUM_ACTIVE_SOURCES 0
201 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
202 #define DEFAULT_RTCP_MIN_INTERVAL (RTP_STATS_MIN_INTERVAL * GST_SECOND)
210 PROP_RTCP_RR_BANDWIDTH,
211 PROP_RTCP_RS_BANDWIDTH,
214 PROP_NUM_ACTIVE_SOURCES,
215 PROP_INTERNAL_SESSION,
216 PROP_USE_PIPELINE_CLOCK,
217 PROP_RTCP_MIN_INTERVAL,
221 #define GST_RTP_SESSION_GET_PRIVATE(obj) \
222 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_SESSION, GstRtpSessionPrivate))
224 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock ((sess)->priv->lock)
225 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock ((sess)->priv->lock)
227 struct _GstRtpSessionPrivate
234 /* thread for sending out RTCP */
236 gboolean stop_thread;
238 gboolean thread_stopped;
245 gboolean use_pipeline_clock;
248 /* callbacks to handle actions from the session manager */
249 static GstFlowReturn gst_rtp_session_process_rtp (RTPSession * sess,
250 RTPSource * src, GstBuffer * buffer, gpointer user_data);
251 static GstFlowReturn gst_rtp_session_send_rtp (RTPSession * sess,
252 RTPSource * src, gpointer data, gpointer user_data);
253 static GstFlowReturn gst_rtp_session_send_rtcp (RTPSession * sess,
254 RTPSource * src, GstBuffer * buffer, gboolean eos, gpointer user_data);
255 static GstFlowReturn gst_rtp_session_sync_rtcp (RTPSession * sess,
256 RTPSource * src, GstBuffer * buffer, gpointer user_data);
257 static gint gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
259 static void gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data);
260 static void gst_rtp_session_request_key_unit (RTPSession * sess,
261 gboolean all_headers, gpointer user_data);
262 static GstClockTime gst_rtp_session_request_time (RTPSession * session,
265 static RTPSessionCallbacks callbacks = {
266 gst_rtp_session_process_rtp,
267 gst_rtp_session_send_rtp,
268 gst_rtp_session_sync_rtcp,
269 gst_rtp_session_send_rtcp,
270 gst_rtp_session_clock_rate,
271 gst_rtp_session_reconsider,
272 gst_rtp_session_request_key_unit,
273 gst_rtp_session_request_time
276 /* GObject vmethods */
277 static void gst_rtp_session_finalize (GObject * object);
278 static void gst_rtp_session_set_property (GObject * object, guint prop_id,
279 const GValue * value, GParamSpec * pspec);
280 static void gst_rtp_session_get_property (GObject * object, guint prop_id,
281 GValue * value, GParamSpec * pspec);
283 /* GstElement vmethods */
284 static GstStateChangeReturn gst_rtp_session_change_state (GstElement * element,
285 GstStateChange transition);
286 static GstPad *gst_rtp_session_request_new_pad (GstElement * element,
287 GstPadTemplate * templ, const gchar * name);
288 static void gst_rtp_session_release_pad (GstElement * element, GstPad * pad);
290 static void gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession);
292 static guint gst_rtp_session_signals[LAST_SIGNAL] = { 0 };
295 on_new_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
297 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0,
302 on_ssrc_collision (RTPSession * session, RTPSource * src, GstRtpSession * sess)
304 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
309 on_ssrc_validated (RTPSession * session, RTPSource * src, GstRtpSession * sess)
311 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
316 on_ssrc_active (RTPSession * session, RTPSource * src, GstRtpSession * sess)
318 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
323 on_ssrc_sdes (RTPSession * session, RTPSource * src, GstRtpSession * sess)
328 /* convert the new SDES info into a message */
329 RTP_SESSION_LOCK (session);
330 g_object_get (src, "sdes", &s, NULL);
331 RTP_SESSION_UNLOCK (session);
333 m = gst_message_new_custom (GST_MESSAGE_ELEMENT, GST_OBJECT (sess), s);
334 gst_element_post_message (GST_ELEMENT_CAST (sess), m);
336 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0,
341 on_bye_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
343 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0,
348 on_bye_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
350 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
355 on_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
357 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_TIMEOUT], 0,
362 on_sender_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
364 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
368 GST_BOILERPLATE (GstRtpSession, gst_rtp_session, GstElement, GST_TYPE_ELEMENT);
371 gst_rtp_session_base_init (gpointer klass)
373 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
376 gst_element_class_add_pad_template (element_class,
377 gst_static_pad_template_get (&rtpsession_recv_rtp_sink_template));
378 gst_element_class_add_pad_template (element_class,
379 gst_static_pad_template_get (&rtpsession_recv_rtcp_sink_template));
380 gst_element_class_add_pad_template (element_class,
381 gst_static_pad_template_get (&rtpsession_send_rtp_sink_template));
384 gst_element_class_add_pad_template (element_class,
385 gst_static_pad_template_get (&rtpsession_recv_rtp_src_template));
386 gst_element_class_add_pad_template (element_class,
387 gst_static_pad_template_get (&rtpsession_sync_src_template));
388 gst_element_class_add_pad_template (element_class,
389 gst_static_pad_template_get (&rtpsession_send_rtp_src_template));
390 gst_element_class_add_pad_template (element_class,
391 gst_static_pad_template_get (&rtpsession_send_rtcp_src_template));
393 gst_element_class_set_details_simple (element_class, "RTP Session",
394 "Filter/Network/RTP",
395 "Implement an RTP session", "Wim Taymans <wim.taymans@gmail.com>");
399 gst_rtp_session_class_init (GstRtpSessionClass * klass)
401 GObjectClass *gobject_class;
402 GstElementClass *gstelement_class;
404 gobject_class = (GObjectClass *) klass;
405 gstelement_class = (GstElementClass *) klass;
407 g_type_class_add_private (klass, sizeof (GstRtpSessionPrivate));
409 gobject_class->finalize = gst_rtp_session_finalize;
410 gobject_class->set_property = gst_rtp_session_set_property;
411 gobject_class->get_property = gst_rtp_session_get_property;
414 * GstRtpSession::request-pt-map:
415 * @sess: the object which received the signal
418 * Request the payload type as #GstCaps for @pt.
420 gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP] =
421 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
422 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, request_pt_map),
423 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT, GST_TYPE_CAPS, 1,
426 * GstRtpSession::clear-pt-map:
427 * @sess: the object which received the signal
429 * Clear the cached pt-maps requested with #GstRtpSession::request-pt-map.
431 gst_rtp_session_signals[SIGNAL_CLEAR_PT_MAP] =
432 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
433 G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpSessionClass, clear_pt_map),
434 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
437 * GstRtpSession::on-new-ssrc:
438 * @sess: the object which received the signal
441 * Notify of a new SSRC that entered @session.
443 gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
444 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
445 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_new_ssrc),
446 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
448 * GstRtpSession::on-ssrc_collision:
449 * @sess: the object which received the signal
452 * Notify when we have an SSRC collision
454 gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
455 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
456 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
457 on_ssrc_collision), NULL, NULL, g_cclosure_marshal_VOID__UINT,
458 G_TYPE_NONE, 1, G_TYPE_UINT);
460 * GstRtpSession::on-ssrc_validated:
461 * @sess: the object which received the signal
464 * Notify of a new SSRC that became validated.
466 gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
467 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
468 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
469 on_ssrc_validated), NULL, NULL, g_cclosure_marshal_VOID__UINT,
470 G_TYPE_NONE, 1, G_TYPE_UINT);
472 * GstRtpSession::on-ssrc_active:
473 * @sess: the object which received the signal
476 * Notify of a SSRC that is active, i.e., sending RTCP.
478 gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
479 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
480 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
481 on_ssrc_active), NULL, NULL, g_cclosure_marshal_VOID__UINT,
482 G_TYPE_NONE, 1, G_TYPE_UINT);
484 * GstRtpSession::on-ssrc-sdes:
485 * @session: the object which received the signal
488 * Notify that a new SDES was received for SSRC.
490 gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
491 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
492 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_ssrc_sdes),
493 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
496 * GstRtpSession::on-bye-ssrc:
497 * @sess: the object which received the signal
500 * Notify of an SSRC that became inactive because of a BYE packet.
502 gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
503 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
504 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_ssrc),
505 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
507 * GstRtpSession::on-bye-timeout:
508 * @sess: the object which received the signal
511 * Notify of an SSRC that has timed out because of BYE
513 gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
514 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
515 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_timeout),
516 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
518 * GstRtpSession::on-timeout:
519 * @sess: the object which received the signal
522 * Notify of an SSRC that has timed out
524 gst_rtp_session_signals[SIGNAL_ON_TIMEOUT] =
525 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
526 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_timeout),
527 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
529 * GstRtpSession::on-sender-timeout:
530 * @sess: the object which received the signal
533 * Notify of a sender SSRC that has timed out and became a receiver
535 gst_rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
536 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
537 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
538 on_sender_timeout), NULL, NULL, g_cclosure_marshal_VOID__UINT,
539 G_TYPE_NONE, 1, G_TYPE_UINT);
541 g_object_class_install_property (gobject_class, PROP_NTP_NS_BASE,
542 g_param_spec_uint64 ("ntp-ns-base", "NTP base time",
543 "The NTP base time corresponding to running_time 0 (deprecated)", 0,
544 G_MAXUINT64, DEFAULT_NTP_NS_BASE,
545 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
547 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
548 g_param_spec_double ("bandwidth", "Bandwidth",
549 "The bandwidth of the session in bytes per second (0 for auto-discover)",
550 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
551 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
553 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
554 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
555 "The RTCP bandwidth of the session in bytes per second "
556 "(or as a real fraction of the RTP bandwidth if < 1.0)",
557 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
558 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
560 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
561 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
562 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
563 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
564 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
566 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
567 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
568 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
569 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
570 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
572 g_object_class_install_property (gobject_class, PROP_SDES,
573 g_param_spec_boxed ("sdes", "SDES",
574 "The SDES items of this session",
575 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
577 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
578 g_param_spec_uint ("num-sources", "Num Sources",
579 "The number of sources in the session", 0, G_MAXUINT,
580 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
582 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
583 g_param_spec_uint ("num-active-sources", "Num Active Sources",
584 "The number of active sources in the session", 0, G_MAXUINT,
585 DEFAULT_NUM_ACTIVE_SOURCES,
586 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
588 g_object_class_install_property (gobject_class, PROP_INTERNAL_SESSION,
589 g_param_spec_object ("internal-session", "Internal Session",
590 "The internal RTPSession object", RTP_TYPE_SESSION,
591 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
593 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
594 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
595 "Use the pipeline clock to set the NTP time in the RTCP SR messages",
596 DEFAULT_USE_PIPELINE_CLOCK,
597 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
599 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
600 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
601 "Minimum interval between Regular RTCP packet (in ns)",
602 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
603 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
605 gstelement_class->change_state =
606 GST_DEBUG_FUNCPTR (gst_rtp_session_change_state);
607 gstelement_class->request_new_pad =
608 GST_DEBUG_FUNCPTR (gst_rtp_session_request_new_pad);
609 gstelement_class->release_pad =
610 GST_DEBUG_FUNCPTR (gst_rtp_session_release_pad);
612 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_session_clear_pt_map);
614 GST_DEBUG_CATEGORY_INIT (gst_rtp_session_debug,
615 "rtpsession", 0, "RTP Session");
619 gst_rtp_session_init (GstRtpSession * rtpsession, GstRtpSessionClass * klass)
621 rtpsession->priv = GST_RTP_SESSION_GET_PRIVATE (rtpsession);
622 rtpsession->priv->lock = g_mutex_new ();
623 rtpsession->priv->sysclock = gst_system_clock_obtain ();
624 rtpsession->priv->session = rtp_session_new ();
625 rtpsession->priv->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
627 /* configure callbacks */
628 rtp_session_set_callbacks (rtpsession->priv->session, &callbacks, rtpsession);
629 /* configure signals */
630 g_signal_connect (rtpsession->priv->session, "on-new-ssrc",
631 (GCallback) on_new_ssrc, rtpsession);
632 g_signal_connect (rtpsession->priv->session, "on-ssrc-collision",
633 (GCallback) on_ssrc_collision, rtpsession);
634 g_signal_connect (rtpsession->priv->session, "on-ssrc-validated",
635 (GCallback) on_ssrc_validated, rtpsession);
636 g_signal_connect (rtpsession->priv->session, "on-ssrc-active",
637 (GCallback) on_ssrc_active, rtpsession);
638 g_signal_connect (rtpsession->priv->session, "on-ssrc-sdes",
639 (GCallback) on_ssrc_sdes, rtpsession);
640 g_signal_connect (rtpsession->priv->session, "on-bye-ssrc",
641 (GCallback) on_bye_ssrc, rtpsession);
642 g_signal_connect (rtpsession->priv->session, "on-bye-timeout",
643 (GCallback) on_bye_timeout, rtpsession);
644 g_signal_connect (rtpsession->priv->session, "on-timeout",
645 (GCallback) on_timeout, rtpsession);
646 g_signal_connect (rtpsession->priv->session, "on-sender-timeout",
647 (GCallback) on_sender_timeout, rtpsession);
648 rtpsession->priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
649 (GDestroyNotify) gst_caps_unref);
651 gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
652 gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
654 rtpsession->priv->thread_stopped = TRUE;
658 gst_rtp_session_finalize (GObject * object)
660 GstRtpSession *rtpsession;
662 rtpsession = GST_RTP_SESSION (object);
664 g_hash_table_destroy (rtpsession->priv->ptmap);
665 g_mutex_free (rtpsession->priv->lock);
666 g_object_unref (rtpsession->priv->sysclock);
667 g_object_unref (rtpsession->priv->session);
669 G_OBJECT_CLASS (parent_class)->finalize (object);
673 gst_rtp_session_set_property (GObject * object, guint prop_id,
674 const GValue * value, GParamSpec * pspec)
676 GstRtpSession *rtpsession;
677 GstRtpSessionPrivate *priv;
679 rtpsession = GST_RTP_SESSION (object);
680 priv = rtpsession->priv;
683 case PROP_NTP_NS_BASE:
684 GST_OBJECT_LOCK (rtpsession);
685 priv->ntpnsbase = g_value_get_uint64 (value);
686 GST_DEBUG_OBJECT (rtpsession, "setting NTP base to %" GST_TIME_FORMAT,
687 GST_TIME_ARGS (priv->ntpnsbase));
688 GST_OBJECT_UNLOCK (rtpsession);
691 g_object_set_property (G_OBJECT (priv->session), "bandwidth", value);
693 case PROP_RTCP_FRACTION:
694 g_object_set_property (G_OBJECT (priv->session), "rtcp-fraction", value);
696 case PROP_RTCP_RR_BANDWIDTH:
697 g_object_set_property (G_OBJECT (priv->session), "rtcp-rr-bandwidth",
700 case PROP_RTCP_RS_BANDWIDTH:
701 g_object_set_property (G_OBJECT (priv->session), "rtcp-rs-bandwidth",
705 rtp_session_set_sdes_struct (priv->session, g_value_get_boxed (value));
707 case PROP_USE_PIPELINE_CLOCK:
708 priv->use_pipeline_clock = g_value_get_boolean (value);
710 case PROP_RTCP_MIN_INTERVAL:
711 g_object_set_property (G_OBJECT (priv->session), "rtcp-min-interval",
715 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
721 gst_rtp_session_get_property (GObject * object, guint prop_id,
722 GValue * value, GParamSpec * pspec)
724 GstRtpSession *rtpsession;
725 GstRtpSessionPrivate *priv;
727 rtpsession = GST_RTP_SESSION (object);
728 priv = rtpsession->priv;
731 case PROP_NTP_NS_BASE:
732 GST_OBJECT_LOCK (rtpsession);
733 g_value_set_uint64 (value, priv->ntpnsbase);
734 GST_OBJECT_UNLOCK (rtpsession);
737 g_object_get_property (G_OBJECT (priv->session), "bandwidth", value);
739 case PROP_RTCP_FRACTION:
740 g_object_get_property (G_OBJECT (priv->session), "rtcp-fraction", value);
742 case PROP_RTCP_RR_BANDWIDTH:
743 g_object_get_property (G_OBJECT (priv->session), "rtcp-rr-bandwidth",
746 case PROP_RTCP_RS_BANDWIDTH:
747 g_object_get_property (G_OBJECT (priv->session), "rtcp-rs-bandwidth",
751 g_value_take_boxed (value, rtp_session_get_sdes_struct (priv->session));
753 case PROP_NUM_SOURCES:
754 g_value_set_uint (value, rtp_session_get_num_sources (priv->session));
756 case PROP_NUM_ACTIVE_SOURCES:
757 g_value_set_uint (value,
758 rtp_session_get_num_active_sources (priv->session));
760 case PROP_INTERNAL_SESSION:
761 g_value_set_object (value, priv->session);
763 case PROP_USE_PIPELINE_CLOCK:
764 g_value_set_boolean (value, priv->use_pipeline_clock);
766 case PROP_RTCP_MIN_INTERVAL:
767 g_object_get_property (G_OBJECT (priv->session), "rtcp-min-interval",
771 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
777 get_current_times (GstRtpSession * rtpsession, GstClockTime * running_time,
782 GstClockTime base_time, rt, clock_time;
784 GST_OBJECT_LOCK (rtpsession);
785 if ((clock = GST_ELEMENT_CLOCK (rtpsession))) {
786 base_time = GST_ELEMENT_CAST (rtpsession)->base_time;
787 gst_object_ref (clock);
788 GST_OBJECT_UNLOCK (rtpsession);
790 clock_time = gst_clock_get_time (clock);
792 if (rtpsession->priv->use_pipeline_clock) {
797 /* get current NTP time */
798 g_get_current_time (¤t);
799 ntpns = GST_TIMEVAL_TO_TIME (current);
802 /* add constant to convert from 1970 based time to 1900 based time */
803 ntpns += (2208988800LL * GST_SECOND);
805 /* get current clock time and convert to running time */
806 rt = clock_time - base_time;
808 gst_object_unref (clock);
810 GST_OBJECT_UNLOCK (rtpsession);
821 rtcp_thread (GstRtpSession * rtpsession)
824 GstClockTime current_time;
825 GstClockTime next_timeout;
827 GstClockTime running_time;
831 GST_DEBUG_OBJECT (rtpsession, "entering RTCP thread");
833 GST_RTP_SESSION_LOCK (rtpsession);
835 sysclock = rtpsession->priv->sysclock;
836 current_time = gst_clock_get_time (sysclock);
838 session = rtpsession->priv->session;
840 while (!rtpsession->priv->stop_thread) {
843 /* get initial estimate */
844 next_timeout = rtp_session_next_timeout (session, current_time);
846 GST_DEBUG_OBJECT (rtpsession, "next check time %" GST_TIME_FORMAT,
847 GST_TIME_ARGS (next_timeout));
849 /* leave if no more timeouts, the session ended */
850 if (next_timeout == GST_CLOCK_TIME_NONE)
853 id = rtpsession->priv->id =
854 gst_clock_new_single_shot_id (sysclock, next_timeout);
855 GST_RTP_SESSION_UNLOCK (rtpsession);
857 res = gst_clock_id_wait (id, NULL);
859 GST_RTP_SESSION_LOCK (rtpsession);
860 gst_clock_id_unref (id);
861 rtpsession->priv->id = NULL;
863 if (rtpsession->priv->stop_thread)
866 /* update current time */
867 current_time = gst_clock_get_time (sysclock);
869 /* get current NTP time */
870 get_current_times (rtpsession, &running_time, &ntpnstime);
872 /* we get unlocked because we need to perform reconsideration, don't perform
873 * the timeout but get a new reporting estimate. */
874 GST_DEBUG_OBJECT (rtpsession, "unlocked %d, current %" GST_TIME_FORMAT,
875 res, GST_TIME_ARGS (current_time));
877 /* perform actions, we ignore result. Release lock because it might push. */
878 GST_RTP_SESSION_UNLOCK (rtpsession);
879 rtp_session_on_timeout (session, current_time, ntpnstime, running_time);
880 GST_RTP_SESSION_LOCK (rtpsession);
882 /* mark the thread as stopped now */
883 rtpsession->priv->thread_stopped = TRUE;
884 GST_RTP_SESSION_UNLOCK (rtpsession);
886 GST_DEBUG_OBJECT (rtpsession, "leaving RTCP thread");
890 start_rtcp_thread (GstRtpSession * rtpsession)
892 GError *error = NULL;
895 GST_DEBUG_OBJECT (rtpsession, "starting RTCP thread");
897 GST_RTP_SESSION_LOCK (rtpsession);
898 rtpsession->priv->stop_thread = FALSE;
899 if (rtpsession->priv->thread_stopped) {
900 /* if the thread stopped, and we still have a handle to the thread, join it
901 * now. We can safely join with the lock held, the thread will not take it
903 if (rtpsession->priv->thread)
904 g_thread_join (rtpsession->priv->thread);
905 /* only create a new thread if the old one was stopped. Otherwise we can
906 * just reuse the currently running one. */
907 rtpsession->priv->thread =
908 g_thread_create ((GThreadFunc) rtcp_thread, rtpsession, TRUE, &error);
909 rtpsession->priv->thread_stopped = FALSE;
911 GST_RTP_SESSION_UNLOCK (rtpsession);
915 GST_DEBUG_OBJECT (rtpsession, "failed to start thread, %s", error->message);
916 g_error_free (error);
924 stop_rtcp_thread (GstRtpSession * rtpsession)
926 GST_DEBUG_OBJECT (rtpsession, "stopping RTCP thread");
928 GST_RTP_SESSION_LOCK (rtpsession);
929 rtpsession->priv->stop_thread = TRUE;
930 if (rtpsession->priv->id)
931 gst_clock_id_unschedule (rtpsession->priv->id);
932 GST_RTP_SESSION_UNLOCK (rtpsession);
936 join_rtcp_thread (GstRtpSession * rtpsession)
938 GST_RTP_SESSION_LOCK (rtpsession);
939 /* don't try to join when we have no thread */
940 if (rtpsession->priv->thread != NULL) {
941 GST_DEBUG_OBJECT (rtpsession, "joining RTCP thread");
942 GST_RTP_SESSION_UNLOCK (rtpsession);
944 g_thread_join (rtpsession->priv->thread);
946 GST_RTP_SESSION_LOCK (rtpsession);
947 /* after the join, take the lock and clear the thread structure. The caller
948 * is supposed to not concurrently call start and join. */
949 rtpsession->priv->thread = NULL;
951 GST_RTP_SESSION_UNLOCK (rtpsession);
954 static GstStateChangeReturn
955 gst_rtp_session_change_state (GstElement * element, GstStateChange transition)
957 GstStateChangeReturn res;
958 GstRtpSession *rtpsession;
960 rtpsession = GST_RTP_SESSION (element);
962 switch (transition) {
963 case GST_STATE_CHANGE_NULL_TO_READY:
965 case GST_STATE_CHANGE_READY_TO_PAUSED:
967 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
969 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
970 case GST_STATE_CHANGE_PAUSED_TO_READY:
971 /* no need to join yet, we might want to continue later. Also, the
972 * dataflow could block downstream so that a join could just block
974 stop_rtcp_thread (rtpsession);
980 res = parent_class->change_state (element, transition);
982 switch (transition) {
983 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
984 if (!start_rtcp_thread (rtpsession))
987 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
989 case GST_STATE_CHANGE_PAUSED_TO_READY:
990 /* downstream is now releasing the dataflow and we can join. */
991 join_rtcp_thread (rtpsession);
993 case GST_STATE_CHANGE_READY_TO_NULL:
1003 return GST_STATE_CHANGE_FAILURE;
1008 return_true (gpointer key, gpointer value, gpointer user_data)
1014 gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession)
1016 g_hash_table_foreach_remove (rtpsession->priv->ptmap, return_true, NULL);
1019 /* called when the session manager has an RTP packet or a list of packets
1020 * ready for further processing */
1021 static GstFlowReturn
1022 gst_rtp_session_process_rtp (RTPSession * sess, RTPSource * src,
1023 GstBuffer * buffer, gpointer user_data)
1025 GstFlowReturn result;
1026 GstRtpSession *rtpsession;
1029 rtpsession = GST_RTP_SESSION (user_data);
1031 GST_RTP_SESSION_LOCK (rtpsession);
1032 if ((rtp_src = rtpsession->recv_rtp_src))
1033 gst_object_ref (rtp_src);
1034 GST_RTP_SESSION_UNLOCK (rtpsession);
1037 GST_LOG_OBJECT (rtpsession, "pushing received RTP packet");
1038 result = gst_pad_push (rtp_src, buffer);
1039 gst_object_unref (rtp_src);
1041 GST_DEBUG_OBJECT (rtpsession, "dropping received RTP packet");
1042 gst_buffer_unref (buffer);
1043 result = GST_FLOW_OK;
1048 /* called when the session manager has an RTP packet ready for further
1050 static GstFlowReturn
1051 gst_rtp_session_send_rtp (RTPSession * sess, RTPSource * src,
1052 gpointer data, gpointer user_data)
1054 GstFlowReturn result;
1055 GstRtpSession *rtpsession;
1058 rtpsession = GST_RTP_SESSION (user_data);
1060 GST_RTP_SESSION_LOCK (rtpsession);
1061 if ((rtp_src = rtpsession->send_rtp_src))
1062 gst_object_ref (rtp_src);
1063 GST_RTP_SESSION_UNLOCK (rtpsession);
1066 if (GST_IS_BUFFER (data)) {
1067 GST_LOG_OBJECT (rtpsession, "sending RTP packet");
1068 result = gst_pad_push (rtp_src, GST_BUFFER_CAST (data));
1070 GST_LOG_OBJECT (rtpsession, "sending RTP list");
1071 result = gst_pad_push_list (rtp_src, GST_BUFFER_LIST_CAST (data));
1073 gst_object_unref (rtp_src);
1075 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1076 result = GST_FLOW_OK;
1081 /* called when the session manager has an RTCP packet ready for further
1082 * sending. The eos flag is set when an EOS event should be sent downstream as
1084 static GstFlowReturn
1085 gst_rtp_session_send_rtcp (RTPSession * sess, RTPSource * src,
1086 GstBuffer * buffer, gboolean eos, gpointer user_data)
1088 GstFlowReturn result;
1089 GstRtpSession *rtpsession;
1092 rtpsession = GST_RTP_SESSION (user_data);
1094 GST_RTP_SESSION_LOCK (rtpsession);
1095 if (rtpsession->priv->stop_thread)
1098 if ((rtcp_src = rtpsession->send_rtcp_src)) {
1101 /* set rtcp caps on output pad */
1102 if (!(caps = GST_PAD_CAPS (rtcp_src))) {
1103 caps = gst_caps_new_simple ("application/x-rtcp", NULL);
1104 gst_pad_set_caps (rtcp_src, caps);
1106 gst_caps_ref (caps);
1107 gst_buffer_set_caps (buffer, caps);
1108 gst_caps_unref (caps);
1110 gst_object_ref (rtcp_src);
1111 GST_RTP_SESSION_UNLOCK (rtpsession);
1113 GST_LOG_OBJECT (rtpsession, "sending RTCP");
1114 result = gst_pad_push (rtcp_src, buffer);
1116 /* we have to send EOS after this packet */
1118 GST_LOG_OBJECT (rtpsession, "sending EOS");
1119 gst_pad_push_event (rtcp_src, gst_event_new_eos ());
1121 gst_object_unref (rtcp_src);
1123 GST_RTP_SESSION_UNLOCK (rtpsession);
1125 GST_DEBUG_OBJECT (rtpsession, "not sending RTCP, no output pad");
1126 gst_buffer_unref (buffer);
1127 result = GST_FLOW_OK;
1134 GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1135 gst_buffer_unref (buffer);
1136 GST_RTP_SESSION_UNLOCK (rtpsession);
1141 /* called when the session manager has an SR RTCP packet ready for handling
1142 * inter stream synchronisation */
1143 static GstFlowReturn
1144 gst_rtp_session_sync_rtcp (RTPSession * sess, RTPSource * src,
1145 GstBuffer * buffer, gpointer user_data)
1147 GstFlowReturn result;
1148 GstRtpSession *rtpsession;
1151 rtpsession = GST_RTP_SESSION (user_data);
1153 GST_RTP_SESSION_LOCK (rtpsession);
1154 if (rtpsession->priv->stop_thread)
1157 if ((sync_src = rtpsession->sync_src)) {
1160 /* set rtcp caps on output pad */
1161 if (!(caps = GST_PAD_CAPS (sync_src))) {
1162 caps = gst_caps_new_simple ("application/x-rtcp", NULL);
1163 gst_pad_set_caps (sync_src, caps);
1165 gst_caps_ref (caps);
1166 gst_buffer_set_caps (buffer, caps);
1167 gst_caps_unref (caps);
1169 gst_object_ref (sync_src);
1170 GST_RTP_SESSION_UNLOCK (rtpsession);
1172 GST_LOG_OBJECT (rtpsession, "sending Sync RTCP");
1173 result = gst_pad_push (sync_src, buffer);
1174 gst_object_unref (sync_src);
1176 GST_RTP_SESSION_UNLOCK (rtpsession);
1178 GST_DEBUG_OBJECT (rtpsession, "not sending Sync RTCP, no output pad");
1179 gst_buffer_unref (buffer);
1180 result = GST_FLOW_OK;
1187 GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1188 gst_buffer_unref (buffer);
1189 GST_RTP_SESSION_UNLOCK (rtpsession);
1195 gst_rtp_session_cache_caps (GstRtpSession * rtpsession, GstCaps * caps)
1197 GstRtpSessionPrivate *priv;
1198 const GstStructure *s;
1201 priv = rtpsession->priv;
1203 GST_DEBUG_OBJECT (rtpsession, "parsing caps");
1205 s = gst_caps_get_structure (caps, 0);
1206 if (!gst_structure_get_int (s, "payload", &payload))
1209 if (g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (payload)))
1212 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (payload),
1213 gst_caps_ref (caps));
1217 gst_rtp_session_get_caps_for_pt (GstRtpSession * rtpsession, guint payload)
1219 GstCaps *caps = NULL;
1220 GValue args[2] = { {0}, {0} };
1223 GST_RTP_SESSION_LOCK (rtpsession);
1224 caps = g_hash_table_lookup (rtpsession->priv->ptmap,
1225 GINT_TO_POINTER (payload));
1227 gst_caps_ref (caps);
1231 /* not found in the cache, try to get it with a signal */
1232 g_value_init (&args[0], GST_TYPE_ELEMENT);
1233 g_value_set_object (&args[0], rtpsession);
1234 g_value_init (&args[1], G_TYPE_UINT);
1235 g_value_set_uint (&args[1], payload);
1237 g_value_init (&ret, GST_TYPE_CAPS);
1238 g_value_set_boxed (&ret, NULL);
1240 GST_RTP_SESSION_UNLOCK (rtpsession);
1242 g_signal_emitv (args, gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP], 0,
1245 GST_RTP_SESSION_LOCK (rtpsession);
1247 g_value_unset (&args[0]);
1248 g_value_unset (&args[1]);
1249 caps = (GstCaps *) g_value_dup_boxed (&ret);
1250 g_value_unset (&ret);
1254 gst_rtp_session_cache_caps (rtpsession, caps);
1257 GST_RTP_SESSION_UNLOCK (rtpsession);
1263 GST_DEBUG_OBJECT (rtpsession, "could not get caps");
1268 /* called when the session manager needs the clock rate */
1270 gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
1274 GstRtpSession *rtpsession;
1276 const GstStructure *s;
1278 rtpsession = GST_RTP_SESSION_CAST (user_data);
1280 caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1285 s = gst_caps_get_structure (caps, 0);
1286 if (!gst_structure_get_int (s, "clock-rate", &result))
1289 gst_caps_unref (caps);
1291 GST_DEBUG_OBJECT (rtpsession, "parsed clock-rate %d", result);
1300 gst_caps_unref (caps);
1301 GST_DEBUG_OBJECT (rtpsession, "No clock-rate in caps!");
1306 /* called when the session manager asks us to reconsider the timeout */
1308 gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data)
1310 GstRtpSession *rtpsession;
1312 rtpsession = GST_RTP_SESSION_CAST (user_data);
1314 GST_RTP_SESSION_LOCK (rtpsession);
1315 GST_DEBUG_OBJECT (rtpsession, "unlock timer for reconsideration");
1316 if (rtpsession->priv->id)
1317 gst_clock_id_unschedule (rtpsession->priv->id);
1318 GST_RTP_SESSION_UNLOCK (rtpsession);
1322 gst_rtp_session_event_recv_rtp_sink (GstPad * pad, GstEvent * event)
1324 GstRtpSession *rtpsession;
1325 gboolean ret = FALSE;
1327 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1328 if (G_UNLIKELY (rtpsession == NULL)) {
1329 gst_event_unref (event);
1333 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1334 GST_EVENT_TYPE_NAME (event));
1336 switch (GST_EVENT_TYPE (event)) {
1337 case GST_EVENT_FLUSH_STOP:
1338 gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
1339 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1341 case GST_EVENT_NEWSEGMENT:
1344 gdouble rate, arate;
1346 gint64 start, stop, time;
1347 GstSegment *segment;
1349 segment = &rtpsession->recv_rtp_seg;
1351 /* the newsegment event is needed to convert the RTP timestamp to
1352 * running_time, which is needed to generate a mapping from RTP to NTP
1353 * timestamps in SR reports */
1354 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1355 &start, &stop, &time);
1357 GST_DEBUG_OBJECT (rtpsession,
1358 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1359 "format GST_FORMAT_TIME, "
1360 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1361 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1362 update, rate, arate, GST_TIME_ARGS (segment->start),
1363 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1364 GST_TIME_ARGS (segment->accum));
1366 gst_segment_set_newsegment_full (segment, update, rate,
1367 arate, format, start, stop, time);
1369 /* push event forward */
1370 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1374 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1377 gst_object_unref (rtpsession);
1384 gst_rtp_session_request_remote_key_unit (GstRtpSession * rtpsession,
1385 guint32 ssrc, guint payload, gboolean all_headers)
1388 gboolean requested = FALSE;
1390 caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1393 const GstStructure *s = gst_caps_get_structure (caps, 0);
1396 pli = gst_structure_has_field (s, "rtcp-fb-nack-pli");
1398 gst_caps_unref (caps);
1401 rtp_session_request_key_unit (rtpsession->priv->session, ssrc);
1402 rtp_session_request_early_rtcp (rtpsession->priv->session,
1403 gst_clock_get_time (rtpsession->priv->sysclock), 200 * GST_MSECOND);
1412 gst_rtp_session_event_recv_rtp_src (GstPad * pad, GstEvent * event)
1414 GstRtpSession *rtpsession;
1415 gboolean forward = TRUE;
1416 gboolean ret = TRUE;
1417 const GstStructure *s;
1421 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1422 if (G_UNLIKELY (rtpsession == NULL)) {
1423 gst_event_unref (event);
1427 switch (GST_EVENT_TYPE (event)) {
1428 case GST_EVENT_CUSTOM_UPSTREAM:
1429 s = gst_event_get_structure (event);
1430 if (gst_structure_has_name (s, "GstForceKeyUnit") &&
1431 gst_structure_get_uint (s, "ssrc", &ssrc) &&
1432 gst_structure_get_uint (s, "payload", &pt)) {
1433 gboolean all_headers = FALSE;
1435 gst_structure_get_boolean (s, "all-headers", &all_headers);
1436 if (gst_rtp_session_request_remote_key_unit (rtpsession, ssrc, pt,
1446 ret = gst_pad_push_event (rtpsession->recv_rtp_sink, event);
1448 gst_object_unref (rtpsession);
1454 static GstIterator *
1455 gst_rtp_session_iterate_internal_links (GstPad * pad)
1457 GstRtpSession *rtpsession;
1458 GstPad *otherpad = NULL;
1459 GstIterator *it = NULL;
1461 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1462 if (G_UNLIKELY (rtpsession == NULL))
1465 GST_RTP_SESSION_LOCK (rtpsession);
1466 if (pad == rtpsession->recv_rtp_src) {
1467 otherpad = gst_object_ref (rtpsession->recv_rtp_sink);
1468 } else if (pad == rtpsession->recv_rtp_sink) {
1469 otherpad = gst_object_ref (rtpsession->recv_rtp_src);
1470 } else if (pad == rtpsession->send_rtp_src) {
1471 otherpad = gst_object_ref (rtpsession->send_rtp_sink);
1472 } else if (pad == rtpsession->send_rtp_sink) {
1473 otherpad = gst_object_ref (rtpsession->send_rtp_src);
1475 GST_RTP_SESSION_UNLOCK (rtpsession);
1478 it = gst_iterator_new_single (GST_TYPE_PAD, otherpad,
1479 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
1480 gst_object_unref (otherpad);
1483 gst_object_unref (rtpsession);
1489 gst_rtp_session_sink_setcaps (GstPad * pad, GstCaps * caps)
1491 GstRtpSession *rtpsession;
1493 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1495 GST_RTP_SESSION_LOCK (rtpsession);
1496 gst_rtp_session_cache_caps (rtpsession, caps);
1497 GST_RTP_SESSION_UNLOCK (rtpsession);
1499 gst_object_unref (rtpsession);
1504 /* receive a packet from a sender, send it to the RTP session manager and
1505 * forward the packet on the rtp_src pad
1507 static GstFlowReturn
1508 gst_rtp_session_chain_recv_rtp (GstPad * pad, GstBuffer * buffer)
1510 GstRtpSession *rtpsession;
1511 GstRtpSessionPrivate *priv;
1513 GstClockTime current_time, running_time;
1514 GstClockTime timestamp;
1516 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1517 priv = rtpsession->priv;
1519 GST_LOG_OBJECT (rtpsession, "received RTP packet");
1521 /* get NTP time when this packet was captured, this depends on the timestamp. */
1522 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1523 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1524 /* convert to running time using the segment values */
1526 gst_segment_to_running_time (&rtpsession->recv_rtp_seg, GST_FORMAT_TIME,
1529 get_current_times (rtpsession, &running_time, NULL);
1531 current_time = gst_clock_get_time (priv->sysclock);
1533 ret = rtp_session_process_rtp (priv->session, buffer, current_time,
1535 if (ret != GST_FLOW_OK)
1539 gst_object_unref (rtpsession);
1546 GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1547 gst_flow_get_name (ret));
1553 gst_rtp_session_event_recv_rtcp_sink (GstPad * pad, GstEvent * event)
1555 GstRtpSession *rtpsession;
1556 gboolean ret = FALSE;
1558 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1560 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1561 GST_EVENT_TYPE_NAME (event));
1563 switch (GST_EVENT_TYPE (event)) {
1565 ret = gst_pad_push_event (rtpsession->sync_src, event);
1568 gst_object_unref (rtpsession);
1573 /* Receive an RTCP packet from a sender, send it to the RTP session manager and
1574 * forward the SR packets to the sync_src pad.
1576 static GstFlowReturn
1577 gst_rtp_session_chain_recv_rtcp (GstPad * pad, GstBuffer * buffer)
1579 GstRtpSession *rtpsession;
1580 GstRtpSessionPrivate *priv;
1581 GstClockTime current_time;
1584 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1585 priv = rtpsession->priv;
1587 GST_LOG_OBJECT (rtpsession, "received RTCP packet");
1589 current_time = gst_clock_get_time (priv->sysclock);
1590 get_current_times (rtpsession, NULL, &ntpnstime);
1592 rtp_session_process_rtcp (priv->session, buffer, current_time, ntpnstime);
1594 gst_object_unref (rtpsession);
1596 return GST_FLOW_OK; /* always return OK */
1600 gst_rtp_session_query_send_rtcp_src (GstPad * pad, GstQuery * query)
1602 GstRtpSession *rtpsession;
1603 gboolean ret = FALSE;
1605 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1607 GST_DEBUG_OBJECT (rtpsession, "received QUERY");
1609 switch (GST_QUERY_TYPE (query)) {
1610 case GST_QUERY_LATENCY:
1612 /* use the defaults for the latency query. */
1613 gst_query_set_latency (query, FALSE, 0, -1);
1616 /* other queries simply fail for now */
1620 gst_object_unref (rtpsession);
1626 gst_rtp_session_event_send_rtcp_src (GstPad * pad, GstEvent * event)
1628 GstRtpSession *rtpsession;
1629 gboolean ret = TRUE;
1631 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1632 if (G_UNLIKELY (rtpsession == NULL)) {
1633 gst_event_unref (event);
1636 GST_DEBUG_OBJECT (rtpsession, "received EVENT");
1638 switch (GST_EVENT_TYPE (event)) {
1639 case GST_EVENT_SEEK:
1640 case GST_EVENT_LATENCY:
1641 gst_event_unref (event);
1645 /* other events simply fail for now */
1646 gst_event_unref (event);
1651 gst_object_unref (rtpsession);
1657 gst_rtp_session_event_send_rtp_sink (GstPad * pad, GstEvent * event)
1659 GstRtpSession *rtpsession;
1660 gboolean ret = FALSE;
1662 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1664 GST_DEBUG_OBJECT (rtpsession, "received event");
1666 switch (GST_EVENT_TYPE (event)) {
1667 case GST_EVENT_FLUSH_STOP:
1668 gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
1669 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1671 case GST_EVENT_NEWSEGMENT:{
1673 gdouble rate, arate;
1675 gint64 start, stop, time;
1676 GstSegment *segment;
1678 segment = &rtpsession->send_rtp_seg;
1680 /* the newsegment event is needed to convert the RTP timestamp to
1681 * running_time, which is needed to generate a mapping from RTP to NTP
1682 * timestamps in SR reports */
1683 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1684 &start, &stop, &time);
1686 GST_DEBUG_OBJECT (rtpsession,
1687 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1688 "format GST_FORMAT_TIME, "
1689 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1690 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1691 update, rate, arate, GST_TIME_ARGS (segment->start),
1692 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1693 GST_TIME_ARGS (segment->accum));
1695 gst_segment_set_newsegment_full (segment, update, rate,
1696 arate, format, start, stop, time);
1698 /* push event forward */
1699 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1702 case GST_EVENT_EOS:{
1703 GstClockTime current_time;
1705 /* push downstream FIXME, we are not supposed to leave the session just
1706 * because we stop sending. */
1707 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1708 current_time = gst_clock_get_time (rtpsession->priv->sysclock);
1709 GST_DEBUG_OBJECT (rtpsession, "scheduling BYE message");
1710 rtp_session_schedule_bye (rtpsession->priv->session, "End of stream",
1715 GstPad *send_rtp_src = NULL;
1716 GST_RTP_SESSION_LOCK (rtpsession);
1717 if (rtpsession->send_rtp_src)
1718 send_rtp_src = gst_object_ref (rtpsession->send_rtp_src);
1719 GST_RTP_SESSION_UNLOCK (rtpsession);
1722 ret = gst_pad_push_event (send_rtp_src, event);
1723 gst_object_unref (send_rtp_src);
1725 gst_event_unref (event);
1730 gst_object_unref (rtpsession);
1736 gst_rtp_session_getcaps_send_rtp (GstPad * pad)
1738 GstRtpSession *rtpsession;
1739 GstRtpSessionPrivate *priv;
1741 GstStructure *s1, *s2;
1744 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1745 priv = rtpsession->priv;
1747 ssrc = rtp_session_get_internal_ssrc (priv->session);
1749 /* we can basically accept anything but we prefer to receive packets with our
1750 * internal SSRC so that we don't have to patch it. Create a structure with
1751 * the SSRC and another one without. */
1752 s1 = gst_structure_new ("application/x-rtp", "ssrc", G_TYPE_UINT, ssrc, NULL);
1753 s2 = gst_structure_new ("application/x-rtp", NULL);
1755 result = gst_caps_new_full (s1, s2, NULL);
1757 GST_DEBUG_OBJECT (rtpsession, "getting caps %" GST_PTR_FORMAT, result);
1759 gst_object_unref (rtpsession);
1765 gst_rtp_session_setcaps_send_rtp (GstPad * pad, GstCaps * caps)
1767 GstRtpSession *rtpsession;
1768 GstRtpSessionPrivate *priv;
1769 GstStructure *s = gst_caps_get_structure (caps, 0);
1772 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1773 priv = rtpsession->priv;
1775 if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
1776 GST_DEBUG_OBJECT (rtpsession, "setting internal SSRC to %08x", ssrc);
1777 rtp_session_set_internal_ssrc (priv->session, ssrc);
1780 gst_object_unref (rtpsession);
1785 /* Recieve an RTP packet or a list of packets to be send to the receivers,
1786 * send to RTP session manager and forward to send_rtp_src.
1788 static GstFlowReturn
1789 gst_rtp_session_chain_send_rtp_common (GstPad * pad, gpointer data,
1792 GstRtpSession *rtpsession;
1793 GstRtpSessionPrivate *priv;
1795 GstClockTime timestamp, running_time;
1796 GstClockTime current_time;
1798 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1799 priv = rtpsession->priv;
1801 GST_LOG_OBJECT (rtpsession, "received RTP %s", is_list ? "list" : "packet");
1803 /* get NTP time when this packet was captured, this depends on the timestamp. */
1805 GstBuffer *buffer = NULL;
1807 /* All groups in an list have the same timestamp.
1808 * So, just take it from the first group. */
1809 buffer = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0, 0);
1811 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1815 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (data));
1818 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1819 /* convert to running time using the segment start value. */
1821 gst_segment_to_running_time (&rtpsession->send_rtp_seg, GST_FORMAT_TIME,
1828 current_time = gst_clock_get_time (priv->sysclock);
1829 ret = rtp_session_send_rtp (priv->session, data, is_list, current_time,
1831 if (ret != GST_FLOW_OK)
1835 gst_object_unref (rtpsession);
1842 GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1843 gst_flow_get_name (ret));
1848 static GstFlowReturn
1849 gst_rtp_session_chain_send_rtp (GstPad * pad, GstBuffer * buffer)
1851 return gst_rtp_session_chain_send_rtp_common (pad, buffer, FALSE);
1854 static GstFlowReturn
1855 gst_rtp_session_chain_send_rtp_list (GstPad * pad, GstBufferList * list)
1857 return gst_rtp_session_chain_send_rtp_common (pad, list, TRUE);
1860 /* Create sinkpad to receive RTP packets from senders. This will also create a
1861 * srcpad for the RTP packets.
1864 create_recv_rtp_sink (GstRtpSession * rtpsession)
1866 GST_DEBUG_OBJECT (rtpsession, "creating RTP sink pad");
1868 rtpsession->recv_rtp_sink =
1869 gst_pad_new_from_static_template (&rtpsession_recv_rtp_sink_template,
1871 gst_pad_set_chain_function (rtpsession->recv_rtp_sink,
1872 gst_rtp_session_chain_recv_rtp);
1873 gst_pad_set_event_function (rtpsession->recv_rtp_sink,
1874 (GstPadEventFunction) gst_rtp_session_event_recv_rtp_sink);
1875 gst_pad_set_setcaps_function (rtpsession->recv_rtp_sink,
1876 gst_rtp_session_sink_setcaps);
1877 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_sink,
1878 gst_rtp_session_iterate_internal_links);
1879 gst_pad_set_active (rtpsession->recv_rtp_sink, TRUE);
1880 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1881 rtpsession->recv_rtp_sink);
1883 GST_DEBUG_OBJECT (rtpsession, "creating RTP src pad");
1884 rtpsession->recv_rtp_src =
1885 gst_pad_new_from_static_template (&rtpsession_recv_rtp_src_template,
1887 gst_pad_set_event_function (rtpsession->recv_rtp_src,
1888 (GstPadEventFunction) gst_rtp_session_event_recv_rtp_src);
1889 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_src,
1890 gst_rtp_session_iterate_internal_links);
1891 gst_pad_use_fixed_caps (rtpsession->recv_rtp_src);
1892 gst_pad_set_active (rtpsession->recv_rtp_src, TRUE);
1893 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->recv_rtp_src);
1895 return rtpsession->recv_rtp_sink;
1898 /* Remove sinkpad to receive RTP packets from senders. This will also remove
1899 * the srcpad for the RTP packets.
1902 remove_recv_rtp_sink (GstRtpSession * rtpsession)
1904 GST_DEBUG_OBJECT (rtpsession, "removing RTP sink pad");
1906 /* deactivate from source to sink */
1907 gst_pad_set_active (rtpsession->recv_rtp_src, FALSE);
1908 gst_pad_set_active (rtpsession->recv_rtp_sink, FALSE);
1911 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1912 rtpsession->recv_rtp_sink);
1913 rtpsession->recv_rtp_sink = NULL;
1915 GST_DEBUG_OBJECT (rtpsession, "removing RTP src pad");
1916 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1917 rtpsession->recv_rtp_src);
1918 rtpsession->recv_rtp_src = NULL;
1921 /* Create a sinkpad to receive RTCP messages from senders, this will also create a
1922 * sync_src pad for the SR packets.
1925 create_recv_rtcp_sink (GstRtpSession * rtpsession)
1927 GST_DEBUG_OBJECT (rtpsession, "creating RTCP sink pad");
1929 rtpsession->recv_rtcp_sink =
1930 gst_pad_new_from_static_template (&rtpsession_recv_rtcp_sink_template,
1932 gst_pad_set_chain_function (rtpsession->recv_rtcp_sink,
1933 gst_rtp_session_chain_recv_rtcp);
1934 gst_pad_set_event_function (rtpsession->recv_rtcp_sink,
1935 (GstPadEventFunction) gst_rtp_session_event_recv_rtcp_sink);
1936 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtcp_sink,
1937 gst_rtp_session_iterate_internal_links);
1938 gst_pad_set_active (rtpsession->recv_rtcp_sink, TRUE);
1939 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1940 rtpsession->recv_rtcp_sink);
1942 GST_DEBUG_OBJECT (rtpsession, "creating sync src pad");
1943 rtpsession->sync_src =
1944 gst_pad_new_from_static_template (&rtpsession_sync_src_template,
1946 gst_pad_set_iterate_internal_links_function (rtpsession->sync_src,
1947 gst_rtp_session_iterate_internal_links);
1948 gst_pad_use_fixed_caps (rtpsession->sync_src);
1949 gst_pad_set_active (rtpsession->sync_src, TRUE);
1950 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
1952 return rtpsession->recv_rtcp_sink;
1956 remove_recv_rtcp_sink (GstRtpSession * rtpsession)
1958 GST_DEBUG_OBJECT (rtpsession, "removing RTCP sink pad");
1960 gst_pad_set_active (rtpsession->sync_src, FALSE);
1961 gst_pad_set_active (rtpsession->recv_rtcp_sink, FALSE);
1963 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1964 rtpsession->recv_rtcp_sink);
1965 rtpsession->recv_rtcp_sink = NULL;
1967 GST_DEBUG_OBJECT (rtpsession, "removing sync src pad");
1968 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
1969 rtpsession->sync_src = NULL;
1972 /* Create a sinkpad to receive RTP packets for receivers. This will also create a
1976 create_send_rtp_sink (GstRtpSession * rtpsession)
1978 GST_DEBUG_OBJECT (rtpsession, "creating pad");
1980 rtpsession->send_rtp_sink =
1981 gst_pad_new_from_static_template (&rtpsession_send_rtp_sink_template,
1983 gst_pad_set_chain_function (rtpsession->send_rtp_sink,
1984 gst_rtp_session_chain_send_rtp);
1985 gst_pad_set_chain_list_function (rtpsession->send_rtp_sink,
1986 gst_rtp_session_chain_send_rtp_list);
1987 gst_pad_set_getcaps_function (rtpsession->send_rtp_sink,
1988 gst_rtp_session_getcaps_send_rtp);
1989 gst_pad_set_setcaps_function (rtpsession->send_rtp_sink,
1990 gst_rtp_session_setcaps_send_rtp);
1991 gst_pad_set_event_function (rtpsession->send_rtp_sink,
1992 (GstPadEventFunction) gst_rtp_session_event_send_rtp_sink);
1993 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_sink,
1994 gst_rtp_session_iterate_internal_links);
1995 gst_pad_set_active (rtpsession->send_rtp_sink, TRUE);
1996 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1997 rtpsession->send_rtp_sink);
1999 rtpsession->send_rtp_src =
2000 gst_pad_new_from_static_template (&rtpsession_send_rtp_src_template,
2002 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_src,
2003 gst_rtp_session_iterate_internal_links);
2004 gst_pad_set_active (rtpsession->send_rtp_src, TRUE);
2005 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->send_rtp_src);
2007 return rtpsession->send_rtp_sink;
2011 remove_send_rtp_sink (GstRtpSession * rtpsession)
2013 GST_DEBUG_OBJECT (rtpsession, "removing pad");
2015 gst_pad_set_active (rtpsession->send_rtp_src, FALSE);
2016 gst_pad_set_active (rtpsession->send_rtp_sink, FALSE);
2018 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2019 rtpsession->send_rtp_sink);
2020 rtpsession->send_rtp_sink = NULL;
2022 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2023 rtpsession->send_rtp_src);
2024 rtpsession->send_rtp_src = NULL;
2027 /* Create a srcpad with the RTCP packets to send out.
2028 * This pad will be driven by the RTP session manager when it wants to send out
2032 create_send_rtcp_src (GstRtpSession * rtpsession)
2034 GST_DEBUG_OBJECT (rtpsession, "creating pad");
2036 rtpsession->send_rtcp_src =
2037 gst_pad_new_from_static_template (&rtpsession_send_rtcp_src_template,
2039 gst_pad_use_fixed_caps (rtpsession->send_rtcp_src);
2040 gst_pad_set_active (rtpsession->send_rtcp_src, TRUE);
2041 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtcp_src,
2042 gst_rtp_session_iterate_internal_links);
2043 gst_pad_set_query_function (rtpsession->send_rtcp_src,
2044 gst_rtp_session_query_send_rtcp_src);
2045 gst_pad_set_event_function (rtpsession->send_rtcp_src,
2046 gst_rtp_session_event_send_rtcp_src);
2047 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2048 rtpsession->send_rtcp_src);
2050 return rtpsession->send_rtcp_src;
2054 remove_send_rtcp_src (GstRtpSession * rtpsession)
2056 GST_DEBUG_OBJECT (rtpsession, "removing pad");
2058 gst_pad_set_active (rtpsession->send_rtcp_src, FALSE);
2060 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2061 rtpsession->send_rtcp_src);
2062 rtpsession->send_rtcp_src = NULL;
2066 gst_rtp_session_request_new_pad (GstElement * element,
2067 GstPadTemplate * templ, const gchar * name)
2069 GstRtpSession *rtpsession;
2070 GstElementClass *klass;
2073 g_return_val_if_fail (templ != NULL, NULL);
2074 g_return_val_if_fail (GST_IS_RTP_SESSION (element), NULL);
2076 rtpsession = GST_RTP_SESSION (element);
2077 klass = GST_ELEMENT_GET_CLASS (element);
2079 GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
2081 GST_RTP_SESSION_LOCK (rtpsession);
2083 /* figure out the template */
2084 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink")) {
2085 if (rtpsession->recv_rtp_sink != NULL)
2088 result = create_recv_rtp_sink (rtpsession);
2089 } else if (templ == gst_element_class_get_pad_template (klass,
2090 "recv_rtcp_sink")) {
2091 if (rtpsession->recv_rtcp_sink != NULL)
2094 result = create_recv_rtcp_sink (rtpsession);
2095 } else if (templ == gst_element_class_get_pad_template (klass,
2097 if (rtpsession->send_rtp_sink != NULL)
2100 result = create_send_rtp_sink (rtpsession);
2101 } else if (templ == gst_element_class_get_pad_template (klass,
2103 if (rtpsession->send_rtcp_src != NULL)
2106 result = create_send_rtcp_src (rtpsession);
2108 goto wrong_template;
2110 GST_RTP_SESSION_UNLOCK (rtpsession);
2117 GST_RTP_SESSION_UNLOCK (rtpsession);
2118 g_warning ("gstrtpsession: this is not our template");
2123 GST_RTP_SESSION_UNLOCK (rtpsession);
2124 g_warning ("gstrtpsession: pad already requested");
2130 gst_rtp_session_release_pad (GstElement * element, GstPad * pad)
2132 GstRtpSession *rtpsession;
2134 g_return_if_fail (GST_IS_RTP_SESSION (element));
2135 g_return_if_fail (GST_IS_PAD (pad));
2137 rtpsession = GST_RTP_SESSION (element);
2139 GST_DEBUG_OBJECT (element, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2141 GST_RTP_SESSION_LOCK (rtpsession);
2143 if (rtpsession->recv_rtp_sink == pad) {
2144 remove_recv_rtp_sink (rtpsession);
2145 } else if (rtpsession->recv_rtcp_sink == pad) {
2146 remove_recv_rtcp_sink (rtpsession);
2147 } else if (rtpsession->send_rtp_sink == pad) {
2148 remove_send_rtp_sink (rtpsession);
2149 } else if (rtpsession->send_rtcp_src == pad) {
2150 remove_send_rtcp_src (rtpsession);
2154 GST_RTP_SESSION_UNLOCK (rtpsession);
2161 GST_RTP_SESSION_UNLOCK (rtpsession);
2162 g_warning ("gstrtpsession: asked to release an unknown pad");
2168 gst_rtp_session_request_key_unit (RTPSession * sess,
2169 gboolean all_headers, gpointer user_data)
2171 GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2174 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
2175 gst_structure_new ("GstForceKeyUnit",
2176 "all-headers", G_TYPE_BOOLEAN, all_headers, NULL));
2177 gst_pad_push_event (rtpsession->send_rtp_sink, event);
2181 gst_rtp_session_request_time (RTPSession * session, gpointer user_data)
2183 GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2185 return gst_clock_get_time (rtpsession->priv->sysclock);