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_static_pad_template (element_class,
377 &rtpsession_recv_rtp_sink_template);
378 gst_element_class_add_static_pad_template (element_class,
379 &rtpsession_recv_rtcp_sink_template);
380 gst_element_class_add_static_pad_template (element_class,
381 &rtpsession_send_rtp_sink_template);
384 gst_element_class_add_static_pad_template (element_class,
385 &rtpsession_recv_rtp_src_template);
386 gst_element_class_add_static_pad_template (element_class,
387 &rtpsession_sync_src_template);
388 gst_element_class_add_static_pad_template (element_class,
389 &rtpsession_send_rtp_src_template);
390 gst_element_class_add_static_pad_template (element_class,
391 &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 GST_DEBUG_OBJECT (rtpsession, "starting at %" GST_TIME_FORMAT,
841 GST_TIME_ARGS (current_time));
842 session->start_time = current_time;
844 while (!rtpsession->priv->stop_thread) {
847 /* get initial estimate */
848 next_timeout = rtp_session_next_timeout (session, current_time);
850 GST_DEBUG_OBJECT (rtpsession, "next check time %" GST_TIME_FORMAT,
851 GST_TIME_ARGS (next_timeout));
853 /* leave if no more timeouts, the session ended */
854 if (next_timeout == GST_CLOCK_TIME_NONE)
857 id = rtpsession->priv->id =
858 gst_clock_new_single_shot_id (sysclock, next_timeout);
859 GST_RTP_SESSION_UNLOCK (rtpsession);
861 res = gst_clock_id_wait (id, NULL);
863 GST_RTP_SESSION_LOCK (rtpsession);
864 gst_clock_id_unref (id);
865 rtpsession->priv->id = NULL;
867 if (rtpsession->priv->stop_thread)
870 /* update current time */
871 current_time = gst_clock_get_time (sysclock);
873 /* get current NTP time */
874 get_current_times (rtpsession, &running_time, &ntpnstime);
876 /* we get unlocked because we need to perform reconsideration, don't perform
877 * the timeout but get a new reporting estimate. */
878 GST_DEBUG_OBJECT (rtpsession, "unlocked %d, current %" GST_TIME_FORMAT,
879 res, GST_TIME_ARGS (current_time));
881 /* perform actions, we ignore result. Release lock because it might push. */
882 GST_RTP_SESSION_UNLOCK (rtpsession);
883 rtp_session_on_timeout (session, current_time, ntpnstime, running_time);
884 GST_RTP_SESSION_LOCK (rtpsession);
886 /* mark the thread as stopped now */
887 rtpsession->priv->thread_stopped = TRUE;
888 GST_RTP_SESSION_UNLOCK (rtpsession);
890 GST_DEBUG_OBJECT (rtpsession, "leaving RTCP thread");
894 start_rtcp_thread (GstRtpSession * rtpsession)
896 GError *error = NULL;
899 GST_DEBUG_OBJECT (rtpsession, "starting RTCP thread");
901 GST_RTP_SESSION_LOCK (rtpsession);
902 rtpsession->priv->stop_thread = FALSE;
903 if (rtpsession->priv->thread_stopped) {
904 /* if the thread stopped, and we still have a handle to the thread, join it
905 * now. We can safely join with the lock held, the thread will not take it
907 if (rtpsession->priv->thread)
908 g_thread_join (rtpsession->priv->thread);
909 /* only create a new thread if the old one was stopped. Otherwise we can
910 * just reuse the currently running one. */
911 rtpsession->priv->thread =
912 g_thread_create ((GThreadFunc) rtcp_thread, rtpsession, TRUE, &error);
913 rtpsession->priv->thread_stopped = FALSE;
915 GST_RTP_SESSION_UNLOCK (rtpsession);
919 GST_DEBUG_OBJECT (rtpsession, "failed to start thread, %s", error->message);
920 g_error_free (error);
928 stop_rtcp_thread (GstRtpSession * rtpsession)
930 GST_DEBUG_OBJECT (rtpsession, "stopping RTCP thread");
932 GST_RTP_SESSION_LOCK (rtpsession);
933 rtpsession->priv->stop_thread = TRUE;
934 if (rtpsession->priv->id)
935 gst_clock_id_unschedule (rtpsession->priv->id);
936 GST_RTP_SESSION_UNLOCK (rtpsession);
940 join_rtcp_thread (GstRtpSession * rtpsession)
942 GST_RTP_SESSION_LOCK (rtpsession);
943 /* don't try to join when we have no thread */
944 if (rtpsession->priv->thread != NULL) {
945 GST_DEBUG_OBJECT (rtpsession, "joining RTCP thread");
946 GST_RTP_SESSION_UNLOCK (rtpsession);
948 g_thread_join (rtpsession->priv->thread);
950 GST_RTP_SESSION_LOCK (rtpsession);
951 /* after the join, take the lock and clear the thread structure. The caller
952 * is supposed to not concurrently call start and join. */
953 rtpsession->priv->thread = NULL;
955 GST_RTP_SESSION_UNLOCK (rtpsession);
958 static GstStateChangeReturn
959 gst_rtp_session_change_state (GstElement * element, GstStateChange transition)
961 GstStateChangeReturn res;
962 GstRtpSession *rtpsession;
964 rtpsession = GST_RTP_SESSION (element);
966 switch (transition) {
967 case GST_STATE_CHANGE_NULL_TO_READY:
969 case GST_STATE_CHANGE_READY_TO_PAUSED:
971 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
973 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
974 case GST_STATE_CHANGE_PAUSED_TO_READY:
975 /* no need to join yet, we might want to continue later. Also, the
976 * dataflow could block downstream so that a join could just block
978 stop_rtcp_thread (rtpsession);
984 res = parent_class->change_state (element, transition);
986 switch (transition) {
987 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
988 if (!start_rtcp_thread (rtpsession))
991 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
993 case GST_STATE_CHANGE_PAUSED_TO_READY:
994 /* downstream is now releasing the dataflow and we can join. */
995 join_rtcp_thread (rtpsession);
997 case GST_STATE_CHANGE_READY_TO_NULL:
1007 return GST_STATE_CHANGE_FAILURE;
1012 return_true (gpointer key, gpointer value, gpointer user_data)
1018 gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession)
1020 g_hash_table_foreach_remove (rtpsession->priv->ptmap, return_true, NULL);
1023 /* called when the session manager has an RTP packet or a list of packets
1024 * ready for further processing */
1025 static GstFlowReturn
1026 gst_rtp_session_process_rtp (RTPSession * sess, RTPSource * src,
1027 GstBuffer * buffer, gpointer user_data)
1029 GstFlowReturn result;
1030 GstRtpSession *rtpsession;
1033 rtpsession = GST_RTP_SESSION (user_data);
1035 GST_RTP_SESSION_LOCK (rtpsession);
1036 if ((rtp_src = rtpsession->recv_rtp_src))
1037 gst_object_ref (rtp_src);
1038 GST_RTP_SESSION_UNLOCK (rtpsession);
1041 GST_LOG_OBJECT (rtpsession, "pushing received RTP packet");
1042 result = gst_pad_push (rtp_src, buffer);
1043 gst_object_unref (rtp_src);
1045 GST_DEBUG_OBJECT (rtpsession, "dropping received RTP packet");
1046 gst_buffer_unref (buffer);
1047 result = GST_FLOW_OK;
1052 /* called when the session manager has an RTP packet ready for further
1054 static GstFlowReturn
1055 gst_rtp_session_send_rtp (RTPSession * sess, RTPSource * src,
1056 gpointer data, gpointer user_data)
1058 GstFlowReturn result;
1059 GstRtpSession *rtpsession;
1062 rtpsession = GST_RTP_SESSION (user_data);
1064 GST_RTP_SESSION_LOCK (rtpsession);
1065 if ((rtp_src = rtpsession->send_rtp_src))
1066 gst_object_ref (rtp_src);
1067 GST_RTP_SESSION_UNLOCK (rtpsession);
1070 if (GST_IS_BUFFER (data)) {
1071 GST_LOG_OBJECT (rtpsession, "sending RTP packet");
1072 result = gst_pad_push (rtp_src, GST_BUFFER_CAST (data));
1074 GST_LOG_OBJECT (rtpsession, "sending RTP list");
1075 result = gst_pad_push_list (rtp_src, GST_BUFFER_LIST_CAST (data));
1077 gst_object_unref (rtp_src);
1079 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1080 result = GST_FLOW_OK;
1085 /* called when the session manager has an RTCP packet ready for further
1086 * sending. The eos flag is set when an EOS event should be sent downstream as
1088 static GstFlowReturn
1089 gst_rtp_session_send_rtcp (RTPSession * sess, RTPSource * src,
1090 GstBuffer * buffer, gboolean eos, gpointer user_data)
1092 GstFlowReturn result;
1093 GstRtpSession *rtpsession;
1096 rtpsession = GST_RTP_SESSION (user_data);
1098 GST_RTP_SESSION_LOCK (rtpsession);
1099 if (rtpsession->priv->stop_thread)
1102 if ((rtcp_src = rtpsession->send_rtcp_src)) {
1105 /* set rtcp caps on output pad */
1106 if (!(caps = GST_PAD_CAPS (rtcp_src))) {
1107 caps = gst_caps_new_simple ("application/x-rtcp", NULL);
1108 gst_pad_set_caps (rtcp_src, caps);
1110 gst_caps_ref (caps);
1111 gst_buffer_set_caps (buffer, caps);
1112 gst_caps_unref (caps);
1114 gst_object_ref (rtcp_src);
1115 GST_RTP_SESSION_UNLOCK (rtpsession);
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 /* set rtcp caps on output pad */
1165 if (!(caps = GST_PAD_CAPS (sync_src))) {
1166 caps = gst_caps_new_simple ("application/x-rtcp", NULL);
1167 gst_pad_set_caps (sync_src, caps);
1169 gst_caps_ref (caps);
1170 gst_buffer_set_caps (buffer, caps);
1171 gst_caps_unref (caps);
1173 gst_object_ref (sync_src);
1174 GST_RTP_SESSION_UNLOCK (rtpsession);
1176 GST_LOG_OBJECT (rtpsession, "sending Sync RTCP");
1177 result = gst_pad_push (sync_src, buffer);
1178 gst_object_unref (sync_src);
1180 GST_RTP_SESSION_UNLOCK (rtpsession);
1182 GST_DEBUG_OBJECT (rtpsession, "not sending Sync RTCP, no output pad");
1183 gst_buffer_unref (buffer);
1184 result = GST_FLOW_OK;
1191 GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1192 gst_buffer_unref (buffer);
1193 GST_RTP_SESSION_UNLOCK (rtpsession);
1199 gst_rtp_session_cache_caps (GstRtpSession * rtpsession, GstCaps * caps)
1201 GstRtpSessionPrivate *priv;
1202 const GstStructure *s;
1205 priv = rtpsession->priv;
1207 GST_DEBUG_OBJECT (rtpsession, "parsing caps");
1209 s = gst_caps_get_structure (caps, 0);
1210 if (!gst_structure_get_int (s, "payload", &payload))
1213 if (g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (payload)))
1216 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (payload),
1217 gst_caps_ref (caps));
1221 gst_rtp_session_get_caps_for_pt (GstRtpSession * rtpsession, guint payload)
1223 GstCaps *caps = NULL;
1224 GValue args[2] = { {0}, {0} };
1227 GST_RTP_SESSION_LOCK (rtpsession);
1228 caps = g_hash_table_lookup (rtpsession->priv->ptmap,
1229 GINT_TO_POINTER (payload));
1231 gst_caps_ref (caps);
1235 /* not found in the cache, try to get it with a signal */
1236 g_value_init (&args[0], GST_TYPE_ELEMENT);
1237 g_value_set_object (&args[0], rtpsession);
1238 g_value_init (&args[1], G_TYPE_UINT);
1239 g_value_set_uint (&args[1], payload);
1241 g_value_init (&ret, GST_TYPE_CAPS);
1242 g_value_set_boxed (&ret, NULL);
1244 GST_RTP_SESSION_UNLOCK (rtpsession);
1246 g_signal_emitv (args, gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP], 0,
1249 GST_RTP_SESSION_LOCK (rtpsession);
1251 g_value_unset (&args[0]);
1252 g_value_unset (&args[1]);
1253 caps = (GstCaps *) g_value_dup_boxed (&ret);
1254 g_value_unset (&ret);
1258 gst_rtp_session_cache_caps (rtpsession, caps);
1261 GST_RTP_SESSION_UNLOCK (rtpsession);
1267 GST_DEBUG_OBJECT (rtpsession, "could not get caps");
1272 /* called when the session manager needs the clock rate */
1274 gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
1278 GstRtpSession *rtpsession;
1280 const GstStructure *s;
1282 rtpsession = GST_RTP_SESSION_CAST (user_data);
1284 caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1289 s = gst_caps_get_structure (caps, 0);
1290 if (!gst_structure_get_int (s, "clock-rate", &result))
1293 gst_caps_unref (caps);
1295 GST_DEBUG_OBJECT (rtpsession, "parsed clock-rate %d", result);
1304 gst_caps_unref (caps);
1305 GST_DEBUG_OBJECT (rtpsession, "No clock-rate in caps!");
1310 /* called when the session manager asks us to reconsider the timeout */
1312 gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data)
1314 GstRtpSession *rtpsession;
1316 rtpsession = GST_RTP_SESSION_CAST (user_data);
1318 GST_RTP_SESSION_LOCK (rtpsession);
1319 GST_DEBUG_OBJECT (rtpsession, "unlock timer for reconsideration");
1320 if (rtpsession->priv->id)
1321 gst_clock_id_unschedule (rtpsession->priv->id);
1322 GST_RTP_SESSION_UNLOCK (rtpsession);
1326 gst_rtp_session_event_recv_rtp_sink (GstPad * pad, GstEvent * event)
1328 GstRtpSession *rtpsession;
1329 gboolean ret = FALSE;
1331 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1332 if (G_UNLIKELY (rtpsession == NULL)) {
1333 gst_event_unref (event);
1337 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1338 GST_EVENT_TYPE_NAME (event));
1340 switch (GST_EVENT_TYPE (event)) {
1341 case GST_EVENT_FLUSH_STOP:
1342 gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
1343 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1345 case GST_EVENT_NEWSEGMENT:
1348 gdouble rate, arate;
1350 gint64 start, stop, time;
1351 GstSegment *segment;
1353 segment = &rtpsession->recv_rtp_seg;
1355 /* the newsegment event is needed to convert the RTP timestamp to
1356 * running_time, which is needed to generate a mapping from RTP to NTP
1357 * timestamps in SR reports */
1358 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1359 &start, &stop, &time);
1361 GST_DEBUG_OBJECT (rtpsession,
1362 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1363 "format GST_FORMAT_TIME, "
1364 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1365 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1366 update, rate, arate, GST_TIME_ARGS (segment->start),
1367 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1368 GST_TIME_ARGS (segment->accum));
1370 gst_segment_set_newsegment_full (segment, update, rate,
1371 arate, format, start, stop, time);
1373 /* push event forward */
1374 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1378 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1381 gst_object_unref (rtpsession);
1388 gst_rtp_session_request_remote_key_unit (GstRtpSession * rtpsession,
1389 guint32 ssrc, guint payload, gboolean all_headers, gint count)
1393 caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1396 const GstStructure *s = gst_caps_get_structure (caps, 0);
1400 pli = gst_structure_has_field (s, "rtcp-fb-nack-pli");
1401 fir = gst_structure_has_field (s, "rtcp-fb-ccm-fir") && all_headers;
1403 /* Google Talk uses FIR for repair, so send it even if we just want a
1406 gst_structure_has_field (s, "rtcp-fb-x-gstreamer-fir-as-repair"))
1409 gst_caps_unref (caps);
1412 return rtp_session_request_key_unit (rtpsession->priv->session, ssrc,
1413 gst_clock_get_time (rtpsession->priv->sysclock), fir, count);
1420 gst_rtp_session_event_recv_rtp_src (GstPad * pad, GstEvent * event)
1422 GstRtpSession *rtpsession;
1423 gboolean forward = TRUE;
1424 gboolean ret = TRUE;
1425 const GstStructure *s;
1429 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1430 if (G_UNLIKELY (rtpsession == NULL)) {
1431 gst_event_unref (event);
1435 switch (GST_EVENT_TYPE (event)) {
1436 case GST_EVENT_CUSTOM_UPSTREAM:
1437 s = gst_event_get_structure (event);
1438 if (gst_structure_has_name (s, "GstForceKeyUnit") &&
1439 gst_structure_get_uint (s, "ssrc", &ssrc) &&
1440 gst_structure_get_uint (s, "payload", &pt)) {
1441 gboolean all_headers = FALSE;
1444 gst_structure_get_boolean (s, "all-headers", &all_headers);
1445 if (gst_structure_get_int (s, "count", &count) && count < 0)
1446 count += G_MAXINT; /* Make sure count is positive if present */
1447 if (gst_rtp_session_request_remote_key_unit (rtpsession, ssrc, pt,
1448 all_headers, count))
1457 ret = gst_pad_push_event (rtpsession->recv_rtp_sink, event);
1459 gst_object_unref (rtpsession);
1465 static GstIterator *
1466 gst_rtp_session_iterate_internal_links (GstPad * pad)
1468 GstRtpSession *rtpsession;
1469 GstPad *otherpad = NULL;
1470 GstIterator *it = NULL;
1472 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1473 if (G_UNLIKELY (rtpsession == NULL))
1476 GST_RTP_SESSION_LOCK (rtpsession);
1477 if (pad == rtpsession->recv_rtp_src) {
1478 otherpad = gst_object_ref (rtpsession->recv_rtp_sink);
1479 } else if (pad == rtpsession->recv_rtp_sink) {
1480 otherpad = gst_object_ref (rtpsession->recv_rtp_src);
1481 } else if (pad == rtpsession->send_rtp_src) {
1482 otherpad = gst_object_ref (rtpsession->send_rtp_sink);
1483 } else if (pad == rtpsession->send_rtp_sink) {
1484 otherpad = gst_object_ref (rtpsession->send_rtp_src);
1486 GST_RTP_SESSION_UNLOCK (rtpsession);
1489 it = gst_iterator_new_single (GST_TYPE_PAD, otherpad,
1490 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
1491 gst_object_unref (otherpad);
1494 gst_object_unref (rtpsession);
1500 gst_rtp_session_sink_setcaps (GstPad * pad, GstCaps * caps)
1502 GstRtpSession *rtpsession;
1504 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1506 GST_RTP_SESSION_LOCK (rtpsession);
1507 gst_rtp_session_cache_caps (rtpsession, caps);
1508 GST_RTP_SESSION_UNLOCK (rtpsession);
1510 gst_object_unref (rtpsession);
1515 /* receive a packet from a sender, send it to the RTP session manager and
1516 * forward the packet on the rtp_src pad
1518 static GstFlowReturn
1519 gst_rtp_session_chain_recv_rtp (GstPad * pad, GstBuffer * buffer)
1521 GstRtpSession *rtpsession;
1522 GstRtpSessionPrivate *priv;
1524 GstClockTime current_time, running_time;
1525 GstClockTime timestamp;
1527 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1528 priv = rtpsession->priv;
1530 GST_LOG_OBJECT (rtpsession, "received RTP packet");
1532 /* get NTP time when this packet was captured, this depends on the timestamp. */
1533 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1534 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1535 /* convert to running time using the segment values */
1537 gst_segment_to_running_time (&rtpsession->recv_rtp_seg, GST_FORMAT_TIME,
1540 get_current_times (rtpsession, &running_time, NULL);
1542 current_time = gst_clock_get_time (priv->sysclock);
1544 ret = rtp_session_process_rtp (priv->session, buffer, current_time,
1546 if (ret != GST_FLOW_OK)
1550 gst_object_unref (rtpsession);
1557 GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1558 gst_flow_get_name (ret));
1564 gst_rtp_session_event_recv_rtcp_sink (GstPad * pad, GstEvent * event)
1566 GstRtpSession *rtpsession;
1567 gboolean ret = FALSE;
1569 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1571 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1572 GST_EVENT_TYPE_NAME (event));
1574 switch (GST_EVENT_TYPE (event)) {
1576 ret = gst_pad_push_event (rtpsession->sync_src, event);
1579 gst_object_unref (rtpsession);
1584 /* Receive an RTCP packet from a sender, send it to the RTP session manager and
1585 * forward the SR packets to the sync_src pad.
1587 static GstFlowReturn
1588 gst_rtp_session_chain_recv_rtcp (GstPad * pad, GstBuffer * buffer)
1590 GstRtpSession *rtpsession;
1591 GstRtpSessionPrivate *priv;
1592 GstClockTime current_time;
1595 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1596 priv = rtpsession->priv;
1598 GST_LOG_OBJECT (rtpsession, "received RTCP packet");
1600 current_time = gst_clock_get_time (priv->sysclock);
1601 get_current_times (rtpsession, NULL, &ntpnstime);
1603 rtp_session_process_rtcp (priv->session, buffer, current_time, ntpnstime);
1605 gst_object_unref (rtpsession);
1607 return GST_FLOW_OK; /* always return OK */
1611 gst_rtp_session_query_send_rtcp_src (GstPad * pad, GstQuery * query)
1613 GstRtpSession *rtpsession;
1614 gboolean ret = FALSE;
1616 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1618 GST_DEBUG_OBJECT (rtpsession, "received QUERY");
1620 switch (GST_QUERY_TYPE (query)) {
1621 case GST_QUERY_LATENCY:
1623 /* use the defaults for the latency query. */
1624 gst_query_set_latency (query, FALSE, 0, -1);
1627 /* other queries simply fail for now */
1631 gst_object_unref (rtpsession);
1637 gst_rtp_session_event_send_rtcp_src (GstPad * pad, GstEvent * event)
1639 GstRtpSession *rtpsession;
1640 gboolean ret = TRUE;
1642 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1643 if (G_UNLIKELY (rtpsession == NULL)) {
1644 gst_event_unref (event);
1647 GST_DEBUG_OBJECT (rtpsession, "received EVENT");
1649 switch (GST_EVENT_TYPE (event)) {
1650 case GST_EVENT_SEEK:
1651 case GST_EVENT_LATENCY:
1652 gst_event_unref (event);
1656 /* other events simply fail for now */
1657 gst_event_unref (event);
1662 gst_object_unref (rtpsession);
1668 gst_rtp_session_event_send_rtp_sink (GstPad * pad, GstEvent * event)
1670 GstRtpSession *rtpsession;
1671 gboolean ret = FALSE;
1673 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1675 GST_DEBUG_OBJECT (rtpsession, "received event");
1677 switch (GST_EVENT_TYPE (event)) {
1678 case GST_EVENT_FLUSH_STOP:
1679 gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
1680 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1682 case GST_EVENT_NEWSEGMENT:{
1684 gdouble rate, arate;
1686 gint64 start, stop, time;
1687 GstSegment *segment;
1689 segment = &rtpsession->send_rtp_seg;
1691 /* the newsegment event is needed to convert the RTP timestamp to
1692 * running_time, which is needed to generate a mapping from RTP to NTP
1693 * timestamps in SR reports */
1694 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1695 &start, &stop, &time);
1697 GST_DEBUG_OBJECT (rtpsession,
1698 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1699 "format GST_FORMAT_TIME, "
1700 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1701 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1702 update, rate, arate, GST_TIME_ARGS (segment->start),
1703 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1704 GST_TIME_ARGS (segment->accum));
1706 gst_segment_set_newsegment_full (segment, update, rate,
1707 arate, format, start, stop, time);
1709 /* push event forward */
1710 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1713 case GST_EVENT_EOS:{
1714 GstClockTime current_time;
1716 /* push downstream FIXME, we are not supposed to leave the session just
1717 * because we stop sending. */
1718 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1719 current_time = gst_clock_get_time (rtpsession->priv->sysclock);
1720 GST_DEBUG_OBJECT (rtpsession, "scheduling BYE message");
1721 rtp_session_schedule_bye (rtpsession->priv->session, "End of stream",
1726 GstPad *send_rtp_src = NULL;
1727 GST_RTP_SESSION_LOCK (rtpsession);
1728 if (rtpsession->send_rtp_src)
1729 send_rtp_src = gst_object_ref (rtpsession->send_rtp_src);
1730 GST_RTP_SESSION_UNLOCK (rtpsession);
1733 ret = gst_pad_push_event (send_rtp_src, event);
1734 gst_object_unref (send_rtp_src);
1736 gst_event_unref (event);
1741 gst_object_unref (rtpsession);
1747 gst_rtp_session_getcaps_send_rtp (GstPad * pad)
1749 GstRtpSession *rtpsession;
1750 GstRtpSessionPrivate *priv;
1752 GstStructure *s1, *s2;
1755 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1756 priv = rtpsession->priv;
1758 ssrc = rtp_session_get_internal_ssrc (priv->session);
1760 /* we can basically accept anything but we prefer to receive packets with our
1761 * internal SSRC so that we don't have to patch it. Create a structure with
1762 * the SSRC and another one without. */
1763 s1 = gst_structure_new ("application/x-rtp", "ssrc", G_TYPE_UINT, ssrc, NULL);
1764 s2 = gst_structure_new ("application/x-rtp", NULL);
1766 result = gst_caps_new_full (s1, s2, NULL);
1768 GST_DEBUG_OBJECT (rtpsession, "getting caps %" GST_PTR_FORMAT, result);
1770 gst_object_unref (rtpsession);
1776 gst_rtp_session_setcaps_send_rtp (GstPad * pad, GstCaps * caps)
1778 GstRtpSession *rtpsession;
1779 GstRtpSessionPrivate *priv;
1780 GstStructure *s = gst_caps_get_structure (caps, 0);
1783 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1784 priv = rtpsession->priv;
1786 if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
1787 GST_DEBUG_OBJECT (rtpsession, "setting internal SSRC to %08x", ssrc);
1788 rtp_session_set_internal_ssrc (priv->session, ssrc);
1791 gst_object_unref (rtpsession);
1796 /* Recieve an RTP packet or a list of packets to be send to the receivers,
1797 * send to RTP session manager and forward to send_rtp_src.
1799 static GstFlowReturn
1800 gst_rtp_session_chain_send_rtp_common (GstPad * pad, gpointer data,
1803 GstRtpSession *rtpsession;
1804 GstRtpSessionPrivate *priv;
1806 GstClockTime timestamp, running_time;
1807 GstClockTime current_time;
1809 rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
1810 priv = rtpsession->priv;
1812 GST_LOG_OBJECT (rtpsession, "received RTP %s", is_list ? "list" : "packet");
1814 /* get NTP time when this packet was captured, this depends on the timestamp. */
1816 GstBuffer *buffer = NULL;
1818 /* All groups in an list have the same timestamp.
1819 * So, just take it from the first group. */
1820 buffer = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0, 0);
1822 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1826 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (data));
1829 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1830 /* convert to running time using the segment start value. */
1832 gst_segment_to_running_time (&rtpsession->send_rtp_seg, GST_FORMAT_TIME,
1839 current_time = gst_clock_get_time (priv->sysclock);
1840 ret = rtp_session_send_rtp (priv->session, data, is_list, current_time,
1842 if (ret != GST_FLOW_OK)
1846 gst_object_unref (rtpsession);
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, GstBuffer * buffer)
1862 return gst_rtp_session_chain_send_rtp_common (pad, buffer, FALSE);
1865 static GstFlowReturn
1866 gst_rtp_session_chain_send_rtp_list (GstPad * pad, GstBufferList * list)
1868 return gst_rtp_session_chain_send_rtp_common (pad, list, TRUE);
1871 /* Create sinkpad to receive RTP packets from senders. This will also create a
1872 * srcpad for the RTP packets.
1875 create_recv_rtp_sink (GstRtpSession * rtpsession)
1877 GST_DEBUG_OBJECT (rtpsession, "creating RTP sink pad");
1879 rtpsession->recv_rtp_sink =
1880 gst_pad_new_from_static_template (&rtpsession_recv_rtp_sink_template,
1882 gst_pad_set_chain_function (rtpsession->recv_rtp_sink,
1883 gst_rtp_session_chain_recv_rtp);
1884 gst_pad_set_event_function (rtpsession->recv_rtp_sink,
1885 (GstPadEventFunction) gst_rtp_session_event_recv_rtp_sink);
1886 gst_pad_set_setcaps_function (rtpsession->recv_rtp_sink,
1887 gst_rtp_session_sink_setcaps);
1888 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_sink,
1889 gst_rtp_session_iterate_internal_links);
1890 gst_pad_set_active (rtpsession->recv_rtp_sink, TRUE);
1891 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1892 rtpsession->recv_rtp_sink);
1894 GST_DEBUG_OBJECT (rtpsession, "creating RTP src pad");
1895 rtpsession->recv_rtp_src =
1896 gst_pad_new_from_static_template (&rtpsession_recv_rtp_src_template,
1898 gst_pad_set_event_function (rtpsession->recv_rtp_src,
1899 (GstPadEventFunction) gst_rtp_session_event_recv_rtp_src);
1900 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_src,
1901 gst_rtp_session_iterate_internal_links);
1902 gst_pad_use_fixed_caps (rtpsession->recv_rtp_src);
1903 gst_pad_set_active (rtpsession->recv_rtp_src, TRUE);
1904 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->recv_rtp_src);
1906 return rtpsession->recv_rtp_sink;
1909 /* Remove sinkpad to receive RTP packets from senders. This will also remove
1910 * the srcpad for the RTP packets.
1913 remove_recv_rtp_sink (GstRtpSession * rtpsession)
1915 GST_DEBUG_OBJECT (rtpsession, "removing RTP sink pad");
1917 /* deactivate from source to sink */
1918 gst_pad_set_active (rtpsession->recv_rtp_src, FALSE);
1919 gst_pad_set_active (rtpsession->recv_rtp_sink, FALSE);
1922 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1923 rtpsession->recv_rtp_sink);
1924 rtpsession->recv_rtp_sink = NULL;
1926 GST_DEBUG_OBJECT (rtpsession, "removing RTP src pad");
1927 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1928 rtpsession->recv_rtp_src);
1929 rtpsession->recv_rtp_src = NULL;
1932 /* Create a sinkpad to receive RTCP messages from senders, this will also create a
1933 * sync_src pad for the SR packets.
1936 create_recv_rtcp_sink (GstRtpSession * rtpsession)
1938 GST_DEBUG_OBJECT (rtpsession, "creating RTCP sink pad");
1940 rtpsession->recv_rtcp_sink =
1941 gst_pad_new_from_static_template (&rtpsession_recv_rtcp_sink_template,
1943 gst_pad_set_chain_function (rtpsession->recv_rtcp_sink,
1944 gst_rtp_session_chain_recv_rtcp);
1945 gst_pad_set_event_function (rtpsession->recv_rtcp_sink,
1946 (GstPadEventFunction) gst_rtp_session_event_recv_rtcp_sink);
1947 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtcp_sink,
1948 gst_rtp_session_iterate_internal_links);
1949 gst_pad_set_active (rtpsession->recv_rtcp_sink, TRUE);
1950 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1951 rtpsession->recv_rtcp_sink);
1953 GST_DEBUG_OBJECT (rtpsession, "creating sync src pad");
1954 rtpsession->sync_src =
1955 gst_pad_new_from_static_template (&rtpsession_sync_src_template,
1957 gst_pad_set_iterate_internal_links_function (rtpsession->sync_src,
1958 gst_rtp_session_iterate_internal_links);
1959 gst_pad_use_fixed_caps (rtpsession->sync_src);
1960 gst_pad_set_active (rtpsession->sync_src, TRUE);
1961 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
1963 return rtpsession->recv_rtcp_sink;
1967 remove_recv_rtcp_sink (GstRtpSession * rtpsession)
1969 GST_DEBUG_OBJECT (rtpsession, "removing RTCP sink pad");
1971 gst_pad_set_active (rtpsession->sync_src, FALSE);
1972 gst_pad_set_active (rtpsession->recv_rtcp_sink, FALSE);
1974 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1975 rtpsession->recv_rtcp_sink);
1976 rtpsession->recv_rtcp_sink = NULL;
1978 GST_DEBUG_OBJECT (rtpsession, "removing sync src pad");
1979 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
1980 rtpsession->sync_src = NULL;
1983 /* Create a sinkpad to receive RTP packets for receivers. This will also create a
1987 create_send_rtp_sink (GstRtpSession * rtpsession)
1989 GST_DEBUG_OBJECT (rtpsession, "creating pad");
1991 rtpsession->send_rtp_sink =
1992 gst_pad_new_from_static_template (&rtpsession_send_rtp_sink_template,
1994 gst_pad_set_chain_function (rtpsession->send_rtp_sink,
1995 gst_rtp_session_chain_send_rtp);
1996 gst_pad_set_chain_list_function (rtpsession->send_rtp_sink,
1997 gst_rtp_session_chain_send_rtp_list);
1998 gst_pad_set_getcaps_function (rtpsession->send_rtp_sink,
1999 gst_rtp_session_getcaps_send_rtp);
2000 gst_pad_set_setcaps_function (rtpsession->send_rtp_sink,
2001 gst_rtp_session_setcaps_send_rtp);
2002 gst_pad_set_event_function (rtpsession->send_rtp_sink,
2003 (GstPadEventFunction) gst_rtp_session_event_send_rtp_sink);
2004 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_sink,
2005 gst_rtp_session_iterate_internal_links);
2006 gst_pad_set_active (rtpsession->send_rtp_sink, TRUE);
2007 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2008 rtpsession->send_rtp_sink);
2010 rtpsession->send_rtp_src =
2011 gst_pad_new_from_static_template (&rtpsession_send_rtp_src_template,
2013 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_src,
2014 gst_rtp_session_iterate_internal_links);
2015 gst_pad_set_active (rtpsession->send_rtp_src, TRUE);
2016 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->send_rtp_src);
2018 return rtpsession->send_rtp_sink;
2022 remove_send_rtp_sink (GstRtpSession * rtpsession)
2024 GST_DEBUG_OBJECT (rtpsession, "removing pad");
2026 gst_pad_set_active (rtpsession->send_rtp_src, FALSE);
2027 gst_pad_set_active (rtpsession->send_rtp_sink, FALSE);
2029 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2030 rtpsession->send_rtp_sink);
2031 rtpsession->send_rtp_sink = NULL;
2033 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2034 rtpsession->send_rtp_src);
2035 rtpsession->send_rtp_src = NULL;
2038 /* Create a srcpad with the RTCP packets to send out.
2039 * This pad will be driven by the RTP session manager when it wants to send out
2043 create_send_rtcp_src (GstRtpSession * rtpsession)
2045 GST_DEBUG_OBJECT (rtpsession, "creating pad");
2047 rtpsession->send_rtcp_src =
2048 gst_pad_new_from_static_template (&rtpsession_send_rtcp_src_template,
2050 gst_pad_use_fixed_caps (rtpsession->send_rtcp_src);
2051 gst_pad_set_active (rtpsession->send_rtcp_src, TRUE);
2052 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtcp_src,
2053 gst_rtp_session_iterate_internal_links);
2054 gst_pad_set_query_function (rtpsession->send_rtcp_src,
2055 gst_rtp_session_query_send_rtcp_src);
2056 gst_pad_set_event_function (rtpsession->send_rtcp_src,
2057 gst_rtp_session_event_send_rtcp_src);
2058 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2059 rtpsession->send_rtcp_src);
2061 return rtpsession->send_rtcp_src;
2065 remove_send_rtcp_src (GstRtpSession * rtpsession)
2067 GST_DEBUG_OBJECT (rtpsession, "removing pad");
2069 gst_pad_set_active (rtpsession->send_rtcp_src, FALSE);
2071 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2072 rtpsession->send_rtcp_src);
2073 rtpsession->send_rtcp_src = NULL;
2077 gst_rtp_session_request_new_pad (GstElement * element,
2078 GstPadTemplate * templ, const gchar * name)
2080 GstRtpSession *rtpsession;
2081 GstElementClass *klass;
2084 g_return_val_if_fail (templ != NULL, NULL);
2085 g_return_val_if_fail (GST_IS_RTP_SESSION (element), NULL);
2087 rtpsession = GST_RTP_SESSION (element);
2088 klass = GST_ELEMENT_GET_CLASS (element);
2090 GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
2092 GST_RTP_SESSION_LOCK (rtpsession);
2094 /* figure out the template */
2095 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink")) {
2096 if (rtpsession->recv_rtp_sink != NULL)
2099 result = create_recv_rtp_sink (rtpsession);
2100 } else if (templ == gst_element_class_get_pad_template (klass,
2101 "recv_rtcp_sink")) {
2102 if (rtpsession->recv_rtcp_sink != NULL)
2105 result = create_recv_rtcp_sink (rtpsession);
2106 } else if (templ == gst_element_class_get_pad_template (klass,
2108 if (rtpsession->send_rtp_sink != NULL)
2111 result = create_send_rtp_sink (rtpsession);
2112 } else if (templ == gst_element_class_get_pad_template (klass,
2114 if (rtpsession->send_rtcp_src != NULL)
2117 result = create_send_rtcp_src (rtpsession);
2119 goto wrong_template;
2121 GST_RTP_SESSION_UNLOCK (rtpsession);
2128 GST_RTP_SESSION_UNLOCK (rtpsession);
2129 g_warning ("gstrtpsession: this is not our template");
2134 GST_RTP_SESSION_UNLOCK (rtpsession);
2135 g_warning ("gstrtpsession: pad already requested");
2141 gst_rtp_session_release_pad (GstElement * element, GstPad * pad)
2143 GstRtpSession *rtpsession;
2145 g_return_if_fail (GST_IS_RTP_SESSION (element));
2146 g_return_if_fail (GST_IS_PAD (pad));
2148 rtpsession = GST_RTP_SESSION (element);
2150 GST_DEBUG_OBJECT (element, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2152 GST_RTP_SESSION_LOCK (rtpsession);
2154 if (rtpsession->recv_rtp_sink == pad) {
2155 remove_recv_rtp_sink (rtpsession);
2156 } else if (rtpsession->recv_rtcp_sink == pad) {
2157 remove_recv_rtcp_sink (rtpsession);
2158 } else if (rtpsession->send_rtp_sink == pad) {
2159 remove_send_rtp_sink (rtpsession);
2160 } else if (rtpsession->send_rtcp_src == pad) {
2161 remove_send_rtcp_src (rtpsession);
2165 GST_RTP_SESSION_UNLOCK (rtpsession);
2172 GST_RTP_SESSION_UNLOCK (rtpsession);
2173 g_warning ("gstrtpsession: asked to release an unknown pad");
2179 gst_rtp_session_request_key_unit (RTPSession * sess,
2180 gboolean all_headers, gpointer user_data)
2182 GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2185 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
2186 gst_structure_new ("GstForceKeyUnit",
2187 "all-headers", G_TYPE_BOOLEAN, all_headers, NULL));
2188 gst_pad_push_event (rtpsession->send_rtp_sink, event);
2192 gst_rtp_session_request_time (RTPSession * session, gpointer user_data)
2194 GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2196 return gst_clock_get_time (rtpsession->priv->sysclock);