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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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-1.0 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-1.0 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-1.0 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-1.0 videotestsrc ! theoraenc ! rtptheorapay ! .send_rtp_sink gstrtpsession name=session .send_rtp_src \
100 * ! udpsink port=5000 session.send_rtcp_src ! udpsink port=5001
101 * ]| Send theora RTP packets through the session manager and out on UDP port
102 * 5000. Send RTCP packets on port 5001. Note that this pipeline will not preroll
103 * correctly because the second udpsink will not preroll correctly (no RTCP
104 * packets are sent in the PAUSED state). Applications should manually set and
105 * keep (see gst_element_set_locked_state()) the RTCP udpsink to the PLAYING state.
108 * Last reviewed on 2007-05-28 (0.10.5)
115 #include <gst/rtp/gstrtpbuffer.h>
117 #include <gst/glib-compat-private.h>
119 #include "gstrtpbin-marshal.h"
120 #include "gstrtpsession.h"
121 #include "rtpsession.h"
123 GST_DEBUG_CATEGORY_STATIC (gst_rtp_session_debug);
124 #define GST_CAT_DEFAULT gst_rtp_session_debug
127 static GstStaticPadTemplate rtpsession_recv_rtp_sink_template =
128 GST_STATIC_PAD_TEMPLATE ("recv_rtp_sink",
131 GST_STATIC_CAPS ("application/x-rtp")
134 static GstStaticPadTemplate rtpsession_recv_rtcp_sink_template =
135 GST_STATIC_PAD_TEMPLATE ("recv_rtcp_sink",
138 GST_STATIC_CAPS ("application/x-rtcp")
141 static GstStaticPadTemplate rtpsession_send_rtp_sink_template =
142 GST_STATIC_PAD_TEMPLATE ("send_rtp_sink",
145 GST_STATIC_CAPS ("application/x-rtp")
149 static GstStaticPadTemplate rtpsession_recv_rtp_src_template =
150 GST_STATIC_PAD_TEMPLATE ("recv_rtp_src",
153 GST_STATIC_CAPS ("application/x-rtp")
156 static GstStaticPadTemplate rtpsession_sync_src_template =
157 GST_STATIC_PAD_TEMPLATE ("sync_src",
160 GST_STATIC_CAPS ("application/x-rtcp")
163 static GstStaticPadTemplate rtpsession_send_rtp_src_template =
164 GST_STATIC_PAD_TEMPLATE ("send_rtp_src",
167 GST_STATIC_CAPS ("application/x-rtp")
170 static GstStaticPadTemplate rtpsession_send_rtcp_src_template =
171 GST_STATIC_PAD_TEMPLATE ("send_rtcp_src",
174 GST_STATIC_CAPS ("application/x-rtcp")
177 /* signals and args */
180 SIGNAL_REQUEST_PT_MAP,
184 SIGNAL_ON_SSRC_COLLISION,
185 SIGNAL_ON_SSRC_VALIDATED,
186 SIGNAL_ON_SSRC_ACTIVE,
189 SIGNAL_ON_BYE_TIMEOUT,
191 SIGNAL_ON_SENDER_TIMEOUT,
195 #define DEFAULT_BANDWIDTH RTP_STATS_BANDWIDTH
196 #define DEFAULT_RTCP_FRACTION (RTP_STATS_BANDWIDTH * RTP_STATS_RTCP_FRACTION)
197 #define DEFAULT_RTCP_RR_BANDWIDTH -1
198 #define DEFAULT_RTCP_RS_BANDWIDTH -1
199 #define DEFAULT_SDES NULL
200 #define DEFAULT_NUM_SOURCES 0
201 #define DEFAULT_NUM_ACTIVE_SOURCES 0
202 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
203 #define DEFAULT_RTCP_MIN_INTERVAL (RTP_STATS_MIN_INTERVAL * GST_SECOND)
204 #define DEFAULT_PROBATION RTP_DEFAULT_PROBATION
211 PROP_RTCP_RR_BANDWIDTH,
212 PROP_RTCP_RS_BANDWIDTH,
215 PROP_NUM_ACTIVE_SOURCES,
216 PROP_INTERNAL_SESSION,
217 PROP_USE_PIPELINE_CLOCK,
218 PROP_RTCP_MIN_INTERVAL,
223 #define GST_RTP_SESSION_GET_PRIVATE(obj) \
224 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_SESSION, GstRtpSessionPrivate))
226 #define GST_RTP_SESSION_LOCK(sess) g_mutex_lock (&(sess)->priv->lock)
227 #define GST_RTP_SESSION_UNLOCK(sess) g_mutex_unlock (&(sess)->priv->lock)
229 #define GST_RTP_SESSION_WAIT(sess) g_cond_wait (&(sess)->priv->cond, &(sess)->priv->lock)
230 #define GST_RTP_SESSION_SIGNAL(sess) g_cond_signal (&(sess)->priv->cond)
232 struct _GstRtpSessionPrivate
240 /* thread for sending out RTCP */
242 gboolean stop_thread;
244 gboolean thread_stopped;
250 GstClockTime send_latency;
252 gboolean use_pipeline_clock;
255 /* callbacks to handle actions from the session manager */
256 static GstFlowReturn gst_rtp_session_process_rtp (RTPSession * sess,
257 RTPSource * src, GstBuffer * buffer, gpointer user_data);
258 static GstFlowReturn gst_rtp_session_send_rtp (RTPSession * sess,
259 RTPSource * src, gpointer data, gpointer user_data);
260 static GstFlowReturn gst_rtp_session_send_rtcp (RTPSession * sess,
261 RTPSource * src, GstBuffer * buffer, gboolean eos, gpointer user_data);
262 static GstFlowReturn gst_rtp_session_sync_rtcp (RTPSession * sess,
263 GstBuffer * buffer, gpointer user_data);
264 static gint gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
266 static void gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data);
267 static void gst_rtp_session_request_key_unit (RTPSession * sess,
268 gboolean all_headers, gpointer user_data);
269 static GstClockTime gst_rtp_session_request_time (RTPSession * session,
272 static RTPSessionCallbacks callbacks = {
273 gst_rtp_session_process_rtp,
274 gst_rtp_session_send_rtp,
275 gst_rtp_session_sync_rtcp,
276 gst_rtp_session_send_rtcp,
277 gst_rtp_session_clock_rate,
278 gst_rtp_session_reconsider,
279 gst_rtp_session_request_key_unit,
280 gst_rtp_session_request_time
283 /* GObject vmethods */
284 static void gst_rtp_session_finalize (GObject * object);
285 static void gst_rtp_session_set_property (GObject * object, guint prop_id,
286 const GValue * value, GParamSpec * pspec);
287 static void gst_rtp_session_get_property (GObject * object, guint prop_id,
288 GValue * value, GParamSpec * pspec);
290 /* GstElement vmethods */
291 static GstStateChangeReturn gst_rtp_session_change_state (GstElement * element,
292 GstStateChange transition);
293 static GstPad *gst_rtp_session_request_new_pad (GstElement * element,
294 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
295 static void gst_rtp_session_release_pad (GstElement * element, GstPad * pad);
297 static gboolean gst_rtp_session_sink_setcaps (GstPad * pad,
298 GstRtpSession * rtpsession, GstCaps * caps);
299 static gboolean gst_rtp_session_setcaps_send_rtp (GstPad * pad,
300 GstRtpSession * rtpsession, GstCaps * caps);
302 static void gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession);
304 static guint gst_rtp_session_signals[LAST_SIGNAL] = { 0 };
307 on_new_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
309 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0,
314 on_ssrc_collision (RTPSession * session, RTPSource * src, GstRtpSession * sess)
316 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
321 on_ssrc_validated (RTPSession * session, RTPSource * src, GstRtpSession * sess)
323 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
328 on_ssrc_active (RTPSession * session, RTPSource * src, GstRtpSession * sess)
330 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
335 on_ssrc_sdes (RTPSession * session, RTPSource * src, GstRtpSession * sess)
340 /* convert the new SDES info into a message */
341 RTP_SESSION_LOCK (session);
342 g_object_get (src, "sdes", &s, NULL);
343 RTP_SESSION_UNLOCK (session);
345 m = gst_message_new_custom (GST_MESSAGE_ELEMENT, GST_OBJECT (sess), s);
346 gst_element_post_message (GST_ELEMENT_CAST (sess), m);
348 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0,
353 on_bye_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
355 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0,
360 on_bye_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
362 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
367 on_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
369 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_TIMEOUT], 0,
374 on_sender_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
376 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
380 #define gst_rtp_session_parent_class parent_class
381 G_DEFINE_TYPE (GstRtpSession, gst_rtp_session, GST_TYPE_ELEMENT);
384 gst_rtp_session_class_init (GstRtpSessionClass * klass)
386 GObjectClass *gobject_class;
387 GstElementClass *gstelement_class;
389 gobject_class = (GObjectClass *) klass;
390 gstelement_class = (GstElementClass *) klass;
392 g_type_class_add_private (klass, sizeof (GstRtpSessionPrivate));
394 gobject_class->finalize = gst_rtp_session_finalize;
395 gobject_class->set_property = gst_rtp_session_set_property;
396 gobject_class->get_property = gst_rtp_session_get_property;
399 * GstRtpSession::request-pt-map:
400 * @sess: the object which received the signal
403 * Request the payload type as #GstCaps for @pt.
405 gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP] =
406 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
407 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, request_pt_map),
408 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT, GST_TYPE_CAPS, 1,
411 * GstRtpSession::clear-pt-map:
412 * @sess: the object which received the signal
414 * Clear the cached pt-maps requested with #GstRtpSession::request-pt-map.
416 gst_rtp_session_signals[SIGNAL_CLEAR_PT_MAP] =
417 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
418 G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpSessionClass, clear_pt_map),
419 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
422 * GstRtpSession::on-new-ssrc:
423 * @sess: the object which received the signal
426 * Notify of a new SSRC that entered @session.
428 gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
429 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
430 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_new_ssrc),
431 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
433 * GstRtpSession::on-ssrc_collision:
434 * @sess: the object which received the signal
437 * Notify when we have an SSRC collision
439 gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
440 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
441 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
442 on_ssrc_collision), NULL, NULL, g_cclosure_marshal_VOID__UINT,
443 G_TYPE_NONE, 1, G_TYPE_UINT);
445 * GstRtpSession::on-ssrc_validated:
446 * @sess: the object which received the signal
449 * Notify of a new SSRC that became validated.
451 gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
452 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
453 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
454 on_ssrc_validated), NULL, NULL, g_cclosure_marshal_VOID__UINT,
455 G_TYPE_NONE, 1, G_TYPE_UINT);
457 * GstRtpSession::on-ssrc_active:
458 * @sess: the object which received the signal
461 * Notify of a SSRC that is active, i.e., sending RTCP.
463 gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
464 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
465 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
466 on_ssrc_active), NULL, NULL, g_cclosure_marshal_VOID__UINT,
467 G_TYPE_NONE, 1, G_TYPE_UINT);
469 * GstRtpSession::on-ssrc-sdes:
470 * @session: the object which received the signal
473 * Notify that a new SDES was received for SSRC.
475 gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
476 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
477 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_ssrc_sdes),
478 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
481 * GstRtpSession::on-bye-ssrc:
482 * @sess: the object which received the signal
485 * Notify of an SSRC that became inactive because of a BYE packet.
487 gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
488 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
489 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_ssrc),
490 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
492 * GstRtpSession::on-bye-timeout:
493 * @sess: the object which received the signal
496 * Notify of an SSRC that has timed out because of BYE
498 gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
499 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
500 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_timeout),
501 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
503 * GstRtpSession::on-timeout:
504 * @sess: the object which received the signal
507 * Notify of an SSRC that has timed out
509 gst_rtp_session_signals[SIGNAL_ON_TIMEOUT] =
510 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
511 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_timeout),
512 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
514 * GstRtpSession::on-sender-timeout:
515 * @sess: the object which received the signal
518 * Notify of a sender SSRC that has timed out and became a receiver
520 gst_rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
521 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
522 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
523 on_sender_timeout), NULL, NULL, g_cclosure_marshal_VOID__UINT,
524 G_TYPE_NONE, 1, G_TYPE_UINT);
526 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
527 g_param_spec_double ("bandwidth", "Bandwidth",
528 "The bandwidth of the session in bytes per second (0 for auto-discover)",
529 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
530 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
532 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
533 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
534 "The RTCP bandwidth of the session in bytes per second "
535 "(or as a real fraction of the RTP bandwidth if < 1.0)",
536 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
537 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
539 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
540 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
541 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
542 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
543 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
545 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
546 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
547 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
548 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
549 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
551 g_object_class_install_property (gobject_class, PROP_SDES,
552 g_param_spec_boxed ("sdes", "SDES",
553 "The SDES items of this session",
554 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
556 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
557 g_param_spec_uint ("num-sources", "Num Sources",
558 "The number of sources in the session", 0, G_MAXUINT,
559 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
561 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
562 g_param_spec_uint ("num-active-sources", "Num Active Sources",
563 "The number of active sources in the session", 0, G_MAXUINT,
564 DEFAULT_NUM_ACTIVE_SOURCES,
565 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
567 g_object_class_install_property (gobject_class, PROP_INTERNAL_SESSION,
568 g_param_spec_object ("internal-session", "Internal Session",
569 "The internal RTPSession object", RTP_TYPE_SESSION,
570 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
572 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
573 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
574 "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
575 DEFAULT_USE_PIPELINE_CLOCK,
576 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
578 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
579 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
580 "Minimum interval between Regular RTCP packet (in ns)",
581 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
582 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
584 g_object_class_install_property (gobject_class, PROP_PROBATION,
585 g_param_spec_uint ("probation", "Number of probations",
586 "Consecutive packet sequence numbers to accept the source",
587 0, G_MAXUINT, DEFAULT_PROBATION,
588 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
590 gstelement_class->change_state =
591 GST_DEBUG_FUNCPTR (gst_rtp_session_change_state);
592 gstelement_class->request_new_pad =
593 GST_DEBUG_FUNCPTR (gst_rtp_session_request_new_pad);
594 gstelement_class->release_pad =
595 GST_DEBUG_FUNCPTR (gst_rtp_session_release_pad);
597 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_session_clear_pt_map);
600 gst_element_class_add_pad_template (gstelement_class,
601 gst_static_pad_template_get (&rtpsession_recv_rtp_sink_template));
602 gst_element_class_add_pad_template (gstelement_class,
603 gst_static_pad_template_get (&rtpsession_recv_rtcp_sink_template));
604 gst_element_class_add_pad_template (gstelement_class,
605 gst_static_pad_template_get (&rtpsession_send_rtp_sink_template));
608 gst_element_class_add_pad_template (gstelement_class,
609 gst_static_pad_template_get (&rtpsession_recv_rtp_src_template));
610 gst_element_class_add_pad_template (gstelement_class,
611 gst_static_pad_template_get (&rtpsession_sync_src_template));
612 gst_element_class_add_pad_template (gstelement_class,
613 gst_static_pad_template_get (&rtpsession_send_rtp_src_template));
614 gst_element_class_add_pad_template (gstelement_class,
615 gst_static_pad_template_get (&rtpsession_send_rtcp_src_template));
617 gst_element_class_set_static_metadata (gstelement_class, "RTP Session",
618 "Filter/Network/RTP",
619 "Implement an RTP session", "Wim Taymans <wim.taymans@gmail.com>");
621 GST_DEBUG_CATEGORY_INIT (gst_rtp_session_debug,
622 "rtpsession", 0, "RTP Session");
626 gst_rtp_session_init (GstRtpSession * rtpsession)
628 rtpsession->priv = GST_RTP_SESSION_GET_PRIVATE (rtpsession);
629 g_mutex_init (&rtpsession->priv->lock);
630 g_cond_init (&rtpsession->priv->cond);
631 rtpsession->priv->sysclock = gst_system_clock_obtain ();
632 rtpsession->priv->session = rtp_session_new ();
633 rtpsession->priv->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
635 /* configure callbacks */
636 rtp_session_set_callbacks (rtpsession->priv->session, &callbacks, rtpsession);
637 /* configure signals */
638 g_signal_connect (rtpsession->priv->session, "on-new-ssrc",
639 (GCallback) on_new_ssrc, rtpsession);
640 g_signal_connect (rtpsession->priv->session, "on-ssrc-collision",
641 (GCallback) on_ssrc_collision, rtpsession);
642 g_signal_connect (rtpsession->priv->session, "on-ssrc-validated",
643 (GCallback) on_ssrc_validated, rtpsession);
644 g_signal_connect (rtpsession->priv->session, "on-ssrc-active",
645 (GCallback) on_ssrc_active, rtpsession);
646 g_signal_connect (rtpsession->priv->session, "on-ssrc-sdes",
647 (GCallback) on_ssrc_sdes, rtpsession);
648 g_signal_connect (rtpsession->priv->session, "on-bye-ssrc",
649 (GCallback) on_bye_ssrc, rtpsession);
650 g_signal_connect (rtpsession->priv->session, "on-bye-timeout",
651 (GCallback) on_bye_timeout, rtpsession);
652 g_signal_connect (rtpsession->priv->session, "on-timeout",
653 (GCallback) on_timeout, rtpsession);
654 g_signal_connect (rtpsession->priv->session, "on-sender-timeout",
655 (GCallback) on_sender_timeout, rtpsession);
656 rtpsession->priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
657 (GDestroyNotify) gst_caps_unref);
659 gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
660 gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
662 rtpsession->priv->thread_stopped = TRUE;
666 gst_rtp_session_finalize (GObject * object)
668 GstRtpSession *rtpsession;
670 rtpsession = GST_RTP_SESSION (object);
672 g_hash_table_destroy (rtpsession->priv->ptmap);
673 g_mutex_clear (&rtpsession->priv->lock);
674 g_cond_clear (&rtpsession->priv->cond);
675 g_object_unref (rtpsession->priv->sysclock);
676 g_object_unref (rtpsession->priv->session);
678 G_OBJECT_CLASS (parent_class)->finalize (object);
682 gst_rtp_session_set_property (GObject * object, guint prop_id,
683 const GValue * value, GParamSpec * pspec)
685 GstRtpSession *rtpsession;
686 GstRtpSessionPrivate *priv;
688 rtpsession = GST_RTP_SESSION (object);
689 priv = rtpsession->priv;
693 g_object_set_property (G_OBJECT (priv->session), "bandwidth", value);
695 case PROP_RTCP_FRACTION:
696 g_object_set_property (G_OBJECT (priv->session), "rtcp-fraction", value);
698 case PROP_RTCP_RR_BANDWIDTH:
699 g_object_set_property (G_OBJECT (priv->session), "rtcp-rr-bandwidth",
702 case PROP_RTCP_RS_BANDWIDTH:
703 g_object_set_property (G_OBJECT (priv->session), "rtcp-rs-bandwidth",
707 rtp_session_set_sdes_struct (priv->session, g_value_get_boxed (value));
709 case PROP_USE_PIPELINE_CLOCK:
710 priv->use_pipeline_clock = g_value_get_boolean (value);
712 case PROP_RTCP_MIN_INTERVAL:
713 g_object_set_property (G_OBJECT (priv->session), "rtcp-min-interval",
717 g_object_set_property (G_OBJECT (priv->session), "probation", value);
720 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
726 gst_rtp_session_get_property (GObject * object, guint prop_id,
727 GValue * value, GParamSpec * pspec)
729 GstRtpSession *rtpsession;
730 GstRtpSessionPrivate *priv;
732 rtpsession = GST_RTP_SESSION (object);
733 priv = rtpsession->priv;
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_get_property (G_OBJECT (priv->session), "probation", value);
774 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
780 get_current_times (GstRtpSession * rtpsession, GstClockTime * running_time,
785 GstClockTime base_time, rt, clock_time;
787 GST_OBJECT_LOCK (rtpsession);
788 if ((clock = GST_ELEMENT_CLOCK (rtpsession))) {
789 base_time = GST_ELEMENT_CAST (rtpsession)->base_time;
790 gst_object_ref (clock);
791 GST_OBJECT_UNLOCK (rtpsession);
793 /* get current clock time and convert to running time */
794 clock_time = gst_clock_get_time (clock);
795 rt = clock_time - base_time;
797 if (rtpsession->priv->use_pipeline_clock) {
802 /* get current NTP time */
803 g_get_current_time (¤t);
804 ntpns = GST_TIMEVAL_TO_TIME (current);
807 /* add constant to convert from 1970 based time to 1900 based time */
808 ntpns += (2208988800LL * GST_SECOND);
810 gst_object_unref (clock);
812 GST_OBJECT_UNLOCK (rtpsession);
823 rtcp_thread (GstRtpSession * rtpsession)
826 GstClockTime current_time;
827 GstClockTime next_timeout;
829 GstClockTime running_time;
833 GST_DEBUG_OBJECT (rtpsession, "entering RTCP thread");
835 GST_RTP_SESSION_LOCK (rtpsession);
837 while (rtpsession->priv->wait_send) {
838 GST_LOG_OBJECT (rtpsession, "waiting for RTP thread");
839 GST_RTP_SESSION_WAIT (rtpsession);
840 GST_LOG_OBJECT (rtpsession, "signaled...");
843 sysclock = rtpsession->priv->sysclock;
844 current_time = gst_clock_get_time (sysclock);
846 session = rtpsession->priv->session;
848 GST_DEBUG_OBJECT (rtpsession, "starting at %" GST_TIME_FORMAT,
849 GST_TIME_ARGS (current_time));
850 session->start_time = current_time;
852 while (!rtpsession->priv->stop_thread) {
855 /* get initial estimate */
856 next_timeout = rtp_session_next_timeout (session, current_time);
858 GST_DEBUG_OBJECT (rtpsession, "next check time %" GST_TIME_FORMAT,
859 GST_TIME_ARGS (next_timeout));
861 /* leave if no more timeouts, the session ended */
862 if (next_timeout == GST_CLOCK_TIME_NONE)
865 id = rtpsession->priv->id =
866 gst_clock_new_single_shot_id (sysclock, next_timeout);
867 GST_RTP_SESSION_UNLOCK (rtpsession);
869 res = gst_clock_id_wait (id, NULL);
871 GST_RTP_SESSION_LOCK (rtpsession);
872 gst_clock_id_unref (id);
873 rtpsession->priv->id = NULL;
875 if (rtpsession->priv->stop_thread)
878 /* update current time */
879 current_time = gst_clock_get_time (sysclock);
881 /* get current NTP time */
882 get_current_times (rtpsession, &running_time, &ntpnstime);
884 /* we get unlocked because we need to perform reconsideration, don't perform
885 * the timeout but get a new reporting estimate. */
886 GST_DEBUG_OBJECT (rtpsession, "unlocked %d, current %" GST_TIME_FORMAT,
887 res, GST_TIME_ARGS (current_time));
889 /* perform actions, we ignore result. Release lock because it might push. */
890 GST_RTP_SESSION_UNLOCK (rtpsession);
891 rtp_session_on_timeout (session, current_time, ntpnstime, running_time);
892 GST_RTP_SESSION_LOCK (rtpsession);
894 /* mark the thread as stopped now */
895 rtpsession->priv->thread_stopped = TRUE;
896 GST_RTP_SESSION_UNLOCK (rtpsession);
898 GST_DEBUG_OBJECT (rtpsession, "leaving RTCP thread");
902 start_rtcp_thread (GstRtpSession * rtpsession)
904 GError *error = NULL;
907 GST_DEBUG_OBJECT (rtpsession, "starting RTCP thread");
909 GST_RTP_SESSION_LOCK (rtpsession);
910 rtpsession->priv->stop_thread = FALSE;
911 if (rtpsession->priv->thread_stopped) {
912 /* if the thread stopped, and we still have a handle to the thread, join it
913 * now. We can safely join with the lock held, the thread will not take it
915 if (rtpsession->priv->thread)
916 g_thread_join (rtpsession->priv->thread);
917 /* only create a new thread if the old one was stopped. Otherwise we can
918 * just reuse the currently running one. */
919 rtpsession->priv->thread = g_thread_try_new ("rtpsession-rtcp-thread",
920 (GThreadFunc) rtcp_thread, rtpsession, &error);
921 rtpsession->priv->thread_stopped = FALSE;
923 GST_RTP_SESSION_UNLOCK (rtpsession);
927 GST_DEBUG_OBJECT (rtpsession, "failed to start thread, %s", error->message);
928 g_error_free (error);
936 stop_rtcp_thread (GstRtpSession * rtpsession)
938 GST_DEBUG_OBJECT (rtpsession, "stopping RTCP thread");
940 GST_RTP_SESSION_LOCK (rtpsession);
941 rtpsession->priv->stop_thread = TRUE;
942 rtpsession->priv->wait_send = FALSE;
943 GST_RTP_SESSION_SIGNAL (rtpsession);
944 if (rtpsession->priv->id)
945 gst_clock_id_unschedule (rtpsession->priv->id);
946 GST_RTP_SESSION_UNLOCK (rtpsession);
950 join_rtcp_thread (GstRtpSession * rtpsession)
952 GST_RTP_SESSION_LOCK (rtpsession);
953 /* don't try to join when we have no thread */
954 if (rtpsession->priv->thread != NULL) {
955 GST_DEBUG_OBJECT (rtpsession, "joining RTCP thread");
956 GST_RTP_SESSION_UNLOCK (rtpsession);
958 g_thread_join (rtpsession->priv->thread);
960 GST_RTP_SESSION_LOCK (rtpsession);
961 /* after the join, take the lock and clear the thread structure. The caller
962 * is supposed to not concurrently call start and join. */
963 rtpsession->priv->thread = NULL;
965 GST_RTP_SESSION_UNLOCK (rtpsession);
968 static GstStateChangeReturn
969 gst_rtp_session_change_state (GstElement * element, GstStateChange transition)
971 GstStateChangeReturn res;
972 GstRtpSession *rtpsession;
974 rtpsession = GST_RTP_SESSION (element);
976 switch (transition) {
977 case GST_STATE_CHANGE_NULL_TO_READY:
979 case GST_STATE_CHANGE_READY_TO_PAUSED:
980 GST_RTP_SESSION_LOCK (rtpsession);
981 if (rtpsession->send_rtp_src)
982 rtpsession->priv->wait_send = TRUE;
983 GST_RTP_SESSION_UNLOCK (rtpsession);
985 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
987 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
988 case GST_STATE_CHANGE_PAUSED_TO_READY:
989 /* no need to join yet, we might want to continue later. Also, the
990 * dataflow could block downstream so that a join could just block
992 stop_rtcp_thread (rtpsession);
998 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1000 switch (transition) {
1001 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1002 if (!start_rtcp_thread (rtpsession))
1005 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1007 case GST_STATE_CHANGE_PAUSED_TO_READY:
1008 /* downstream is now releasing the dataflow and we can join. */
1009 join_rtcp_thread (rtpsession);
1011 case GST_STATE_CHANGE_READY_TO_NULL:
1021 return GST_STATE_CHANGE_FAILURE;
1026 return_true (gpointer key, gpointer value, gpointer user_data)
1032 gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession)
1034 g_hash_table_foreach_remove (rtpsession->priv->ptmap, return_true, NULL);
1037 /* called when the session manager has an RTP packet or a list of packets
1038 * ready for further processing */
1039 static GstFlowReturn
1040 gst_rtp_session_process_rtp (RTPSession * sess, RTPSource * src,
1041 GstBuffer * buffer, gpointer user_data)
1043 GstFlowReturn result;
1044 GstRtpSession *rtpsession;
1047 rtpsession = GST_RTP_SESSION (user_data);
1049 GST_RTP_SESSION_LOCK (rtpsession);
1050 if ((rtp_src = rtpsession->recv_rtp_src))
1051 gst_object_ref (rtp_src);
1052 GST_RTP_SESSION_UNLOCK (rtpsession);
1055 GST_LOG_OBJECT (rtpsession, "pushing received RTP packet");
1056 result = gst_pad_push (rtp_src, buffer);
1057 gst_object_unref (rtp_src);
1059 GST_DEBUG_OBJECT (rtpsession, "dropping received RTP packet");
1060 gst_buffer_unref (buffer);
1061 result = GST_FLOW_OK;
1066 /* called when the session manager has an RTP packet ready for further
1068 static GstFlowReturn
1069 gst_rtp_session_send_rtp (RTPSession * sess, RTPSource * src,
1070 gpointer data, gpointer user_data)
1072 GstFlowReturn result;
1073 GstRtpSession *rtpsession;
1076 rtpsession = GST_RTP_SESSION (user_data);
1078 GST_RTP_SESSION_LOCK (rtpsession);
1079 if ((rtp_src = rtpsession->send_rtp_src))
1080 gst_object_ref (rtp_src);
1081 if (rtpsession->priv->wait_send) {
1082 GST_LOG_OBJECT (rtpsession, "signal RTCP thread");
1083 rtpsession->priv->wait_send = FALSE;
1084 GST_RTP_SESSION_SIGNAL (rtpsession);
1086 GST_RTP_SESSION_UNLOCK (rtpsession);
1089 if (GST_IS_BUFFER (data)) {
1090 GST_LOG_OBJECT (rtpsession, "sending RTP packet");
1091 result = gst_pad_push (rtp_src, GST_BUFFER_CAST (data));
1093 GST_LOG_OBJECT (rtpsession, "sending RTP list");
1094 result = gst_pad_push_list (rtp_src, GST_BUFFER_LIST_CAST (data));
1096 gst_object_unref (rtp_src);
1098 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1099 result = GST_FLOW_OK;
1105 do_rtcp_events (GstRtpSession * rtpsession, GstPad * srcpad)
1111 gboolean have_group_id;
1115 g_strdup_printf ("%08x%08x%08x%08x", g_random_int (), g_random_int (),
1116 g_random_int (), g_random_int ());
1118 GST_RTP_SESSION_LOCK (rtpsession);
1119 if (rtpsession->recv_rtp_sink) {
1121 gst_pad_get_sticky_event (rtpsession->recv_rtp_sink,
1122 GST_EVENT_STREAM_START, 0);
1124 if (gst_event_parse_group_id (event, &group_id))
1125 have_group_id = TRUE;
1127 have_group_id = FALSE;
1128 gst_event_unref (event);
1130 have_group_id = TRUE;
1131 group_id = gst_util_group_id_next ();
1134 have_group_id = TRUE;
1135 group_id = gst_util_group_id_next ();
1137 GST_RTP_SESSION_UNLOCK (rtpsession);
1139 event = gst_event_new_stream_start (stream_id);
1141 gst_event_set_group_id (event, group_id);
1142 gst_pad_push_event (srcpad, event);
1145 caps = gst_caps_new_empty_simple ("application/x-rtcp");
1146 gst_pad_set_caps (srcpad, caps);
1147 gst_caps_unref (caps);
1149 gst_segment_init (&seg, GST_FORMAT_TIME);
1150 event = gst_event_new_segment (&seg);
1151 gst_pad_push_event (srcpad, event);
1154 /* called when the session manager has an RTCP packet ready for further
1155 * sending. The eos flag is set when an EOS event should be sent downstream as
1157 static GstFlowReturn
1158 gst_rtp_session_send_rtcp (RTPSession * sess, RTPSource * src,
1159 GstBuffer * buffer, gboolean eos, gpointer user_data)
1161 GstFlowReturn result;
1162 GstRtpSession *rtpsession;
1165 rtpsession = GST_RTP_SESSION (user_data);
1167 GST_RTP_SESSION_LOCK (rtpsession);
1168 if (rtpsession->priv->stop_thread)
1171 if ((rtcp_src = rtpsession->send_rtcp_src)) {
1172 gst_object_ref (rtcp_src);
1173 GST_RTP_SESSION_UNLOCK (rtpsession);
1175 /* set rtcp caps on output pad */
1176 if (!gst_pad_has_current_caps (rtcp_src))
1177 do_rtcp_events (rtpsession, rtcp_src);
1179 GST_LOG_OBJECT (rtpsession, "sending RTCP");
1180 result = gst_pad_push (rtcp_src, buffer);
1182 /* we have to send EOS after this packet */
1184 GST_LOG_OBJECT (rtpsession, "sending EOS");
1185 gst_pad_push_event (rtcp_src, gst_event_new_eos ());
1187 gst_object_unref (rtcp_src);
1189 GST_RTP_SESSION_UNLOCK (rtpsession);
1191 GST_DEBUG_OBJECT (rtpsession, "not sending RTCP, no output pad");
1192 gst_buffer_unref (buffer);
1193 result = GST_FLOW_OK;
1200 GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1201 gst_buffer_unref (buffer);
1202 GST_RTP_SESSION_UNLOCK (rtpsession);
1207 /* called when the session manager has an SR RTCP packet ready for handling
1208 * inter stream synchronisation */
1209 static GstFlowReturn
1210 gst_rtp_session_sync_rtcp (RTPSession * sess,
1211 GstBuffer * buffer, gpointer user_data)
1213 GstFlowReturn result;
1214 GstRtpSession *rtpsession;
1217 rtpsession = GST_RTP_SESSION (user_data);
1219 GST_RTP_SESSION_LOCK (rtpsession);
1220 if (rtpsession->priv->stop_thread)
1223 if ((sync_src = rtpsession->sync_src)) {
1224 gst_object_ref (sync_src);
1225 GST_RTP_SESSION_UNLOCK (rtpsession);
1227 GST_LOG_OBJECT (rtpsession, "sending Sync RTCP");
1228 result = gst_pad_push (sync_src, buffer);
1229 gst_object_unref (sync_src);
1231 GST_RTP_SESSION_UNLOCK (rtpsession);
1233 GST_DEBUG_OBJECT (rtpsession, "not sending Sync RTCP, no output pad");
1234 gst_buffer_unref (buffer);
1235 result = GST_FLOW_OK;
1242 GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1243 gst_buffer_unref (buffer);
1244 GST_RTP_SESSION_UNLOCK (rtpsession);
1250 gst_rtp_session_cache_caps (GstRtpSession * rtpsession, GstCaps * caps)
1252 GstRtpSessionPrivate *priv;
1253 const GstStructure *s;
1256 priv = rtpsession->priv;
1258 GST_DEBUG_OBJECT (rtpsession, "parsing caps");
1260 s = gst_caps_get_structure (caps, 0);
1261 if (!gst_structure_get_int (s, "payload", &payload))
1264 if (g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (payload)))
1267 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (payload),
1268 gst_caps_ref (caps));
1272 gst_rtp_session_get_caps_for_pt (GstRtpSession * rtpsession, guint payload)
1274 GstCaps *caps = NULL;
1275 GValue args[2] = { {0}, {0} };
1278 GST_RTP_SESSION_LOCK (rtpsession);
1279 caps = g_hash_table_lookup (rtpsession->priv->ptmap,
1280 GINT_TO_POINTER (payload));
1282 gst_caps_ref (caps);
1286 /* not found in the cache, try to get it with a signal */
1287 g_value_init (&args[0], GST_TYPE_ELEMENT);
1288 g_value_set_object (&args[0], rtpsession);
1289 g_value_init (&args[1], G_TYPE_UINT);
1290 g_value_set_uint (&args[1], payload);
1292 g_value_init (&ret, GST_TYPE_CAPS);
1293 g_value_set_boxed (&ret, NULL);
1295 GST_RTP_SESSION_UNLOCK (rtpsession);
1297 g_signal_emitv (args, gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP], 0,
1300 GST_RTP_SESSION_LOCK (rtpsession);
1302 g_value_unset (&args[0]);
1303 g_value_unset (&args[1]);
1304 caps = (GstCaps *) g_value_dup_boxed (&ret);
1305 g_value_unset (&ret);
1309 gst_rtp_session_cache_caps (rtpsession, caps);
1312 GST_RTP_SESSION_UNLOCK (rtpsession);
1318 GST_DEBUG_OBJECT (rtpsession, "could not get caps");
1323 /* called when the session manager needs the clock rate */
1325 gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
1329 GstRtpSession *rtpsession;
1331 const GstStructure *s;
1333 rtpsession = GST_RTP_SESSION_CAST (user_data);
1335 caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1340 s = gst_caps_get_structure (caps, 0);
1341 if (!gst_structure_get_int (s, "clock-rate", &result))
1344 gst_caps_unref (caps);
1346 GST_DEBUG_OBJECT (rtpsession, "parsed clock-rate %d", result);
1355 gst_caps_unref (caps);
1356 GST_DEBUG_OBJECT (rtpsession, "No clock-rate in caps!");
1361 /* called when the session manager asks us to reconsider the timeout */
1363 gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data)
1365 GstRtpSession *rtpsession;
1367 rtpsession = GST_RTP_SESSION_CAST (user_data);
1369 GST_RTP_SESSION_LOCK (rtpsession);
1370 GST_DEBUG_OBJECT (rtpsession, "unlock timer for reconsideration");
1371 if (rtpsession->priv->id)
1372 gst_clock_id_unschedule (rtpsession->priv->id);
1373 GST_RTP_SESSION_UNLOCK (rtpsession);
1377 gst_rtp_session_event_recv_rtp_sink (GstPad * pad, GstObject * parent,
1380 GstRtpSession *rtpsession;
1381 gboolean ret = FALSE;
1383 rtpsession = GST_RTP_SESSION (parent);
1385 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1386 GST_EVENT_TYPE_NAME (event));
1388 switch (GST_EVENT_TYPE (event)) {
1389 case GST_EVENT_CAPS:
1394 gst_event_parse_caps (event, &caps);
1395 gst_rtp_session_sink_setcaps (pad, rtpsession, caps);
1396 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1399 case GST_EVENT_FLUSH_STOP:
1400 gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
1401 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1403 case GST_EVENT_SEGMENT:
1405 GstSegment *segment, in_segment;
1407 segment = &rtpsession->recv_rtp_seg;
1409 /* the newsegment event is needed to convert the RTP timestamp to
1410 * running_time, which is needed to generate a mapping from RTP to NTP
1411 * timestamps in SR reports */
1412 gst_event_copy_segment (event, &in_segment);
1413 GST_DEBUG_OBJECT (rtpsession, "received segment %" GST_SEGMENT_FORMAT,
1416 /* accept upstream */
1417 gst_segment_copy_into (&in_segment, segment);
1419 /* push event forward */
1420 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1424 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1433 gst_rtp_session_request_remote_key_unit (GstRtpSession * rtpsession,
1434 guint32 ssrc, guint payload, gboolean all_headers, gint count)
1438 caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1441 const GstStructure *s = gst_caps_get_structure (caps, 0);
1445 pli = gst_structure_has_field (s, "rtcp-fb-nack-pli");
1446 fir = gst_structure_has_field (s, "rtcp-fb-ccm-fir") && all_headers;
1448 /* Google Talk uses FIR for repair, so send it even if we just want a
1451 gst_structure_has_field (s, "rtcp-fb-x-gstreamer-fir-as-repair"))
1454 gst_caps_unref (caps);
1457 return rtp_session_request_key_unit (rtpsession->priv->session, ssrc,
1458 gst_clock_get_time (rtpsession->priv->sysclock), fir, count);
1465 gst_rtp_session_event_recv_rtp_src (GstPad * pad, GstObject * parent,
1468 GstRtpSession *rtpsession;
1469 gboolean forward = TRUE;
1470 gboolean ret = TRUE;
1471 const GstStructure *s;
1475 rtpsession = GST_RTP_SESSION (parent);
1477 switch (GST_EVENT_TYPE (event)) {
1478 case GST_EVENT_CUSTOM_UPSTREAM:
1479 s = gst_event_get_structure (event);
1480 if (gst_structure_has_name (s, "GstForceKeyUnit") &&
1481 gst_structure_get_uint (s, "ssrc", &ssrc) &&
1482 gst_structure_get_uint (s, "payload", &pt)) {
1483 gboolean all_headers = FALSE;
1486 gst_structure_get_boolean (s, "all-headers", &all_headers);
1487 if (gst_structure_get_int (s, "count", &count) && count < 0)
1488 count += G_MAXINT; /* Make sure count is positive if present */
1489 if (gst_rtp_session_request_remote_key_unit (rtpsession, ssrc, pt,
1490 all_headers, count))
1499 GstPad *recv_rtp_sink;
1501 GST_RTP_SESSION_LOCK (rtpsession);
1502 if ((recv_rtp_sink = rtpsession->recv_rtp_sink))
1503 gst_object_ref (recv_rtp_sink);
1504 GST_RTP_SESSION_UNLOCK (rtpsession);
1506 if (recv_rtp_sink) {
1507 ret = gst_pad_push_event (recv_rtp_sink, event);
1508 gst_object_unref (recv_rtp_sink);
1510 gst_event_unref (event);
1512 gst_event_unref (event);
1519 static GstIterator *
1520 gst_rtp_session_iterate_internal_links (GstPad * pad, GstObject * parent)
1522 GstRtpSession *rtpsession;
1523 GstPad *otherpad = NULL;
1524 GstIterator *it = NULL;
1526 rtpsession = GST_RTP_SESSION (parent);
1528 GST_RTP_SESSION_LOCK (rtpsession);
1529 if (pad == rtpsession->recv_rtp_src) {
1530 otherpad = gst_object_ref (rtpsession->recv_rtp_sink);
1531 } else if (pad == rtpsession->recv_rtp_sink) {
1532 otherpad = gst_object_ref (rtpsession->recv_rtp_src);
1533 } else if (pad == rtpsession->send_rtp_src) {
1534 otherpad = gst_object_ref (rtpsession->send_rtp_sink);
1535 } else if (pad == rtpsession->send_rtp_sink) {
1536 otherpad = gst_object_ref (rtpsession->send_rtp_src);
1538 GST_RTP_SESSION_UNLOCK (rtpsession);
1541 GValue val = { 0, };
1543 g_value_init (&val, GST_TYPE_PAD);
1544 g_value_set_object (&val, otherpad);
1545 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
1546 g_value_unset (&val);
1547 gst_object_unref (otherpad);
1554 gst_rtp_session_sink_setcaps (GstPad * pad, GstRtpSession * rtpsession,
1557 GST_RTP_SESSION_LOCK (rtpsession);
1558 gst_rtp_session_cache_caps (rtpsession, caps);
1559 GST_RTP_SESSION_UNLOCK (rtpsession);
1564 /* receive a packet from a sender, send it to the RTP session manager and
1565 * forward the packet on the rtp_src pad
1567 static GstFlowReturn
1568 gst_rtp_session_chain_recv_rtp (GstPad * pad, GstObject * parent,
1571 GstRtpSession *rtpsession;
1572 GstRtpSessionPrivate *priv;
1574 GstClockTime current_time, running_time;
1575 GstClockTime timestamp;
1577 rtpsession = GST_RTP_SESSION (parent);
1578 priv = rtpsession->priv;
1580 GST_LOG_OBJECT (rtpsession, "received RTP packet");
1582 /* get NTP time when this packet was captured, this depends on the timestamp. */
1583 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1584 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1585 /* convert to running time using the segment values */
1587 gst_segment_to_running_time (&rtpsession->recv_rtp_seg, GST_FORMAT_TIME,
1590 get_current_times (rtpsession, &running_time, NULL);
1592 current_time = gst_clock_get_time (priv->sysclock);
1594 ret = rtp_session_process_rtp (priv->session, buffer, current_time,
1596 if (ret != GST_FLOW_OK)
1606 GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1607 gst_flow_get_name (ret));
1613 gst_rtp_session_event_recv_rtcp_sink (GstPad * pad, GstObject * parent,
1616 GstRtpSession *rtpsession;
1617 gboolean ret = FALSE;
1619 rtpsession = GST_RTP_SESSION (parent);
1621 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1622 GST_EVENT_TYPE_NAME (event));
1624 switch (GST_EVENT_TYPE (event)) {
1626 ret = gst_pad_push_event (rtpsession->sync_src, event);
1633 /* Receive an RTCP packet from a sender, send it to the RTP session manager and
1634 * forward the SR packets to the sync_src pad.
1636 static GstFlowReturn
1637 gst_rtp_session_chain_recv_rtcp (GstPad * pad, GstObject * parent,
1640 GstRtpSession *rtpsession;
1641 GstRtpSessionPrivate *priv;
1642 GstClockTime current_time;
1645 rtpsession = GST_RTP_SESSION (parent);
1646 priv = rtpsession->priv;
1648 GST_LOG_OBJECT (rtpsession, "received RTCP packet");
1650 current_time = gst_clock_get_time (priv->sysclock);
1651 get_current_times (rtpsession, NULL, &ntpnstime);
1653 rtp_session_process_rtcp (priv->session, buffer, current_time, ntpnstime);
1655 return GST_FLOW_OK; /* always return OK */
1659 gst_rtp_session_query_send_rtcp_src (GstPad * pad, GstObject * parent,
1662 GstRtpSession *rtpsession;
1663 gboolean ret = FALSE;
1665 rtpsession = GST_RTP_SESSION (parent);
1667 GST_DEBUG_OBJECT (rtpsession, "received QUERY %s",
1668 GST_QUERY_TYPE_NAME (query));
1670 switch (GST_QUERY_TYPE (query)) {
1671 case GST_QUERY_LATENCY:
1673 /* use the defaults for the latency query. */
1674 gst_query_set_latency (query, FALSE, 0, -1);
1677 /* other queries simply fail for now */
1685 gst_rtp_session_event_send_rtcp_src (GstPad * pad, GstObject * parent,
1688 GstRtpSession *rtpsession;
1689 gboolean ret = TRUE;
1691 rtpsession = GST_RTP_SESSION (parent);
1692 GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
1693 GST_EVENT_TYPE_NAME (event));
1695 switch (GST_EVENT_TYPE (event)) {
1696 case GST_EVENT_SEEK:
1697 case GST_EVENT_LATENCY:
1698 gst_event_unref (event);
1702 /* other events simply fail for now */
1703 gst_event_unref (event);
1713 gst_rtp_session_event_send_rtp_sink (GstPad * pad, GstObject * parent,
1716 GstRtpSession *rtpsession;
1717 gboolean ret = FALSE;
1719 rtpsession = GST_RTP_SESSION (parent);
1721 GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
1722 GST_EVENT_TYPE_NAME (event));
1724 switch (GST_EVENT_TYPE (event)) {
1725 case GST_EVENT_CAPS:
1730 gst_event_parse_caps (event, &caps);
1731 gst_rtp_session_setcaps_send_rtp (pad, rtpsession, caps);
1732 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1735 case GST_EVENT_FLUSH_STOP:
1736 gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
1737 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1739 case GST_EVENT_SEGMENT:{
1740 GstSegment *segment, in_segment;
1742 segment = &rtpsession->send_rtp_seg;
1744 /* the newsegment event is needed to convert the RTP timestamp to
1745 * running_time, which is needed to generate a mapping from RTP to NTP
1746 * timestamps in SR reports */
1747 gst_event_copy_segment (event, &in_segment);
1748 GST_DEBUG_OBJECT (rtpsession, "received segment %" GST_SEGMENT_FORMAT,
1751 /* accept upstream */
1752 gst_segment_copy_into (&in_segment, segment);
1754 /* push event forward */
1755 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1758 case GST_EVENT_EOS:{
1759 GstClockTime current_time;
1761 /* push downstream FIXME, we are not supposed to leave the session just
1762 * because we stop sending. */
1763 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1764 current_time = gst_clock_get_time (rtpsession->priv->sysclock);
1766 GST_DEBUG_OBJECT (rtpsession, "scheduling BYE message");
1767 rtp_session_mark_all_bye (rtpsession->priv->session, "End Of Stream");
1768 rtp_session_schedule_bye (rtpsession->priv->session, current_time);
1772 GstPad *send_rtp_src;
1774 GST_RTP_SESSION_LOCK (rtpsession);
1775 if ((send_rtp_src = rtpsession->send_rtp_src))
1776 gst_object_ref (send_rtp_src);
1777 GST_RTP_SESSION_UNLOCK (rtpsession);
1780 ret = gst_pad_push_event (send_rtp_src, event);
1781 gst_object_unref (send_rtp_src);
1783 gst_event_unref (event);
1793 gst_rtp_session_event_send_rtp_src (GstPad * pad, GstObject * parent,
1796 GstRtpSession *rtpsession;
1797 gboolean ret = FALSE;
1799 rtpsession = GST_RTP_SESSION (parent);
1801 GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
1802 GST_EVENT_TYPE_NAME (event));
1804 switch (GST_EVENT_TYPE (event)) {
1805 case GST_EVENT_LATENCY:
1806 /* save the latency, we need this to know when an RTP packet will be
1807 * rendered by the sink */
1808 gst_event_parse_latency (event, &rtpsession->priv->send_latency);
1810 ret = gst_pad_event_default (pad, parent, event);
1813 ret = gst_pad_event_default (pad, parent, event);
1820 gst_rtp_session_getcaps_send_rtp (GstPad * pad, GstRtpSession * rtpsession,
1823 GstRtpSessionPrivate *priv;
1825 GstStructure *s1, *s2;
1828 priv = rtpsession->priv;
1830 ssrc = rtp_session_suggest_ssrc (priv->session);
1832 /* we can basically accept anything but we prefer to receive packets with our
1833 * internal SSRC so that we don't have to patch it. Create a structure with
1834 * the SSRC and another one without. */
1835 s1 = gst_structure_new ("application/x-rtp", "ssrc", G_TYPE_UINT, ssrc, NULL);
1836 s2 = gst_structure_new_empty ("application/x-rtp");
1838 result = gst_caps_new_full (s1, s2, NULL);
1841 GstCaps *caps = result;
1843 result = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
1844 gst_caps_unref (caps);
1847 GST_DEBUG_OBJECT (rtpsession, "getting caps %" GST_PTR_FORMAT, result);
1853 gst_rtp_session_query_send_rtp (GstPad * pad, GstObject * parent,
1856 gboolean res = FALSE;
1857 GstRtpSession *rtpsession;
1859 rtpsession = GST_RTP_SESSION (parent);
1861 switch (GST_QUERY_TYPE (query)) {
1862 case GST_QUERY_CAPS:
1864 GstCaps *filter, *caps;
1866 gst_query_parse_caps (query, &filter);
1867 caps = gst_rtp_session_getcaps_send_rtp (pad, rtpsession, filter);
1868 gst_query_set_caps_result (query, caps);
1869 gst_caps_unref (caps);
1874 res = gst_pad_query_default (pad, parent, query);
1882 gst_rtp_session_setcaps_send_rtp (GstPad * pad, GstRtpSession * rtpsession,
1885 GstRtpSessionPrivate *priv;
1886 GstStructure *s = gst_caps_get_structure (caps, 0);
1889 priv = rtpsession->priv;
1891 if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
1892 GST_DEBUG_OBJECT (rtpsession, "setting internal SSRC to %08x", ssrc);
1893 rtp_session_set_internal_ssrc (priv->session, ssrc);
1895 rtp_session_update_send_caps (priv->session, caps);
1900 /* Recieve an RTP packet or a list of packets to be send to the receivers,
1901 * send to RTP session manager and forward to send_rtp_src.
1903 static GstFlowReturn
1904 gst_rtp_session_chain_send_rtp_common (GstRtpSession * rtpsession,
1905 gpointer data, gboolean is_list)
1907 GstRtpSessionPrivate *priv;
1909 GstClockTime timestamp, running_time;
1910 GstClockTime current_time;
1912 priv = rtpsession->priv;
1914 GST_LOG_OBJECT (rtpsession, "received RTP %s", is_list ? "list" : "packet");
1916 /* get NTP time when this packet was captured, this depends on the timestamp. */
1918 GstBuffer *buffer = NULL;
1920 /* All groups in an list have the same timestamp.
1921 * So, just take it from the first group. */
1922 buffer = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0);
1924 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1928 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (data));
1931 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1932 /* convert to running time using the segment start value. */
1934 gst_segment_to_running_time (&rtpsession->send_rtp_seg, GST_FORMAT_TIME,
1936 running_time += priv->send_latency;
1942 current_time = gst_clock_get_time (priv->sysclock);
1943 ret = rtp_session_send_rtp (priv->session, data, is_list, current_time,
1945 if (ret != GST_FLOW_OK)
1955 GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1956 gst_flow_get_name (ret));
1961 static GstFlowReturn
1962 gst_rtp_session_chain_send_rtp (GstPad * pad, GstObject * parent,
1965 GstRtpSession *rtpsession = GST_RTP_SESSION (parent);
1967 return gst_rtp_session_chain_send_rtp_common (rtpsession, buffer, FALSE);
1970 static GstFlowReturn
1971 gst_rtp_session_chain_send_rtp_list (GstPad * pad, GstObject * parent,
1972 GstBufferList * list)
1974 GstRtpSession *rtpsession = GST_RTP_SESSION (parent);
1976 return gst_rtp_session_chain_send_rtp_common (rtpsession, list, TRUE);
1979 /* Create sinkpad to receive RTP packets from senders. This will also create a
1980 * srcpad for the RTP packets.
1983 create_recv_rtp_sink (GstRtpSession * rtpsession)
1985 GST_DEBUG_OBJECT (rtpsession, "creating RTP sink pad");
1987 rtpsession->recv_rtp_sink =
1988 gst_pad_new_from_static_template (&rtpsession_recv_rtp_sink_template,
1990 gst_pad_set_chain_function (rtpsession->recv_rtp_sink,
1991 gst_rtp_session_chain_recv_rtp);
1992 gst_pad_set_event_function (rtpsession->recv_rtp_sink,
1993 gst_rtp_session_event_recv_rtp_sink);
1994 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_sink,
1995 gst_rtp_session_iterate_internal_links);
1996 gst_pad_set_active (rtpsession->recv_rtp_sink, TRUE);
1997 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1998 rtpsession->recv_rtp_sink);
2000 GST_DEBUG_OBJECT (rtpsession, "creating RTP src pad");
2001 rtpsession->recv_rtp_src =
2002 gst_pad_new_from_static_template (&rtpsession_recv_rtp_src_template,
2004 gst_pad_set_event_function (rtpsession->recv_rtp_src,
2005 gst_rtp_session_event_recv_rtp_src);
2006 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_src,
2007 gst_rtp_session_iterate_internal_links);
2008 gst_pad_use_fixed_caps (rtpsession->recv_rtp_src);
2009 gst_pad_set_active (rtpsession->recv_rtp_src, TRUE);
2010 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->recv_rtp_src);
2012 return rtpsession->recv_rtp_sink;
2015 /* Remove sinkpad to receive RTP packets from senders. This will also remove
2016 * the srcpad for the RTP packets.
2019 remove_recv_rtp_sink (GstRtpSession * rtpsession)
2021 GST_DEBUG_OBJECT (rtpsession, "removing RTP sink pad");
2023 /* deactivate from source to sink */
2024 gst_pad_set_active (rtpsession->recv_rtp_src, FALSE);
2025 gst_pad_set_active (rtpsession->recv_rtp_sink, FALSE);
2028 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2029 rtpsession->recv_rtp_sink);
2030 rtpsession->recv_rtp_sink = NULL;
2032 GST_DEBUG_OBJECT (rtpsession, "removing RTP src pad");
2033 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2034 rtpsession->recv_rtp_src);
2035 rtpsession->recv_rtp_src = NULL;
2038 /* Create a sinkpad to receive RTCP messages from senders, this will also create a
2039 * sync_src pad for the SR packets.
2042 create_recv_rtcp_sink (GstRtpSession * rtpsession)
2044 GST_DEBUG_OBJECT (rtpsession, "creating RTCP sink pad");
2046 rtpsession->recv_rtcp_sink =
2047 gst_pad_new_from_static_template (&rtpsession_recv_rtcp_sink_template,
2049 gst_pad_set_chain_function (rtpsession->recv_rtcp_sink,
2050 gst_rtp_session_chain_recv_rtcp);
2051 gst_pad_set_event_function (rtpsession->recv_rtcp_sink,
2052 gst_rtp_session_event_recv_rtcp_sink);
2053 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtcp_sink,
2054 gst_rtp_session_iterate_internal_links);
2055 gst_pad_set_active (rtpsession->recv_rtcp_sink, TRUE);
2056 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2057 rtpsession->recv_rtcp_sink);
2059 GST_DEBUG_OBJECT (rtpsession, "creating sync src pad");
2060 rtpsession->sync_src =
2061 gst_pad_new_from_static_template (&rtpsession_sync_src_template,
2063 gst_pad_set_iterate_internal_links_function (rtpsession->sync_src,
2064 gst_rtp_session_iterate_internal_links);
2065 gst_pad_use_fixed_caps (rtpsession->sync_src);
2066 gst_pad_set_active (rtpsession->sync_src, TRUE);
2067 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
2069 return rtpsession->recv_rtcp_sink;
2073 remove_recv_rtcp_sink (GstRtpSession * rtpsession)
2075 GST_DEBUG_OBJECT (rtpsession, "removing RTCP sink pad");
2077 gst_pad_set_active (rtpsession->sync_src, FALSE);
2078 gst_pad_set_active (rtpsession->recv_rtcp_sink, FALSE);
2080 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2081 rtpsession->recv_rtcp_sink);
2082 rtpsession->recv_rtcp_sink = NULL;
2084 GST_DEBUG_OBJECT (rtpsession, "removing sync src pad");
2085 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
2086 rtpsession->sync_src = NULL;
2089 /* Create a sinkpad to receive RTP packets for receivers. This will also create a
2093 create_send_rtp_sink (GstRtpSession * rtpsession)
2095 GST_DEBUG_OBJECT (rtpsession, "creating pad");
2097 rtpsession->send_rtp_sink =
2098 gst_pad_new_from_static_template (&rtpsession_send_rtp_sink_template,
2100 gst_pad_set_chain_function (rtpsession->send_rtp_sink,
2101 gst_rtp_session_chain_send_rtp);
2102 gst_pad_set_chain_list_function (rtpsession->send_rtp_sink,
2103 gst_rtp_session_chain_send_rtp_list);
2104 gst_pad_set_query_function (rtpsession->send_rtp_sink,
2105 gst_rtp_session_query_send_rtp);
2106 gst_pad_set_event_function (rtpsession->send_rtp_sink,
2107 gst_rtp_session_event_send_rtp_sink);
2108 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_sink,
2109 gst_rtp_session_iterate_internal_links);
2110 gst_pad_set_active (rtpsession->send_rtp_sink, TRUE);
2111 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2112 rtpsession->send_rtp_sink);
2114 rtpsession->send_rtp_src =
2115 gst_pad_new_from_static_template (&rtpsession_send_rtp_src_template,
2117 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_src,
2118 gst_rtp_session_iterate_internal_links);
2119 gst_pad_set_event_function (rtpsession->send_rtp_src,
2120 gst_rtp_session_event_send_rtp_src);
2121 gst_pad_set_active (rtpsession->send_rtp_src, TRUE);
2122 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->send_rtp_src);
2124 return rtpsession->send_rtp_sink;
2128 remove_send_rtp_sink (GstRtpSession * rtpsession)
2130 GST_DEBUG_OBJECT (rtpsession, "removing pad");
2132 gst_pad_set_active (rtpsession->send_rtp_src, FALSE);
2133 gst_pad_set_active (rtpsession->send_rtp_sink, FALSE);
2135 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2136 rtpsession->send_rtp_sink);
2137 rtpsession->send_rtp_sink = NULL;
2139 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2140 rtpsession->send_rtp_src);
2141 rtpsession->send_rtp_src = NULL;
2144 /* Create a srcpad with the RTCP packets to send out.
2145 * This pad will be driven by the RTP session manager when it wants to send out
2149 create_send_rtcp_src (GstRtpSession * rtpsession)
2151 GST_DEBUG_OBJECT (rtpsession, "creating pad");
2153 rtpsession->send_rtcp_src =
2154 gst_pad_new_from_static_template (&rtpsession_send_rtcp_src_template,
2156 gst_pad_use_fixed_caps (rtpsession->send_rtcp_src);
2157 gst_pad_set_active (rtpsession->send_rtcp_src, TRUE);
2158 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtcp_src,
2159 gst_rtp_session_iterate_internal_links);
2160 gst_pad_set_query_function (rtpsession->send_rtcp_src,
2161 gst_rtp_session_query_send_rtcp_src);
2162 gst_pad_set_event_function (rtpsession->send_rtcp_src,
2163 gst_rtp_session_event_send_rtcp_src);
2164 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2165 rtpsession->send_rtcp_src);
2167 return rtpsession->send_rtcp_src;
2171 remove_send_rtcp_src (GstRtpSession * rtpsession)
2173 GST_DEBUG_OBJECT (rtpsession, "removing pad");
2175 gst_pad_set_active (rtpsession->send_rtcp_src, FALSE);
2177 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2178 rtpsession->send_rtcp_src);
2179 rtpsession->send_rtcp_src = NULL;
2183 gst_rtp_session_request_new_pad (GstElement * element,
2184 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
2186 GstRtpSession *rtpsession;
2187 GstElementClass *klass;
2190 g_return_val_if_fail (templ != NULL, NULL);
2191 g_return_val_if_fail (GST_IS_RTP_SESSION (element), NULL);
2193 rtpsession = GST_RTP_SESSION (element);
2194 klass = GST_ELEMENT_GET_CLASS (element);
2196 GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
2198 GST_RTP_SESSION_LOCK (rtpsession);
2200 /* figure out the template */
2201 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink")) {
2202 if (rtpsession->recv_rtp_sink != NULL)
2205 result = create_recv_rtp_sink (rtpsession);
2206 } else if (templ == gst_element_class_get_pad_template (klass,
2207 "recv_rtcp_sink")) {
2208 if (rtpsession->recv_rtcp_sink != NULL)
2211 result = create_recv_rtcp_sink (rtpsession);
2212 } else if (templ == gst_element_class_get_pad_template (klass,
2214 if (rtpsession->send_rtp_sink != NULL)
2217 result = create_send_rtp_sink (rtpsession);
2218 } else if (templ == gst_element_class_get_pad_template (klass,
2220 if (rtpsession->send_rtcp_src != NULL)
2223 result = create_send_rtcp_src (rtpsession);
2225 goto wrong_template;
2227 GST_RTP_SESSION_UNLOCK (rtpsession);
2234 GST_RTP_SESSION_UNLOCK (rtpsession);
2235 g_warning ("gstrtpsession: this is not our template");
2240 GST_RTP_SESSION_UNLOCK (rtpsession);
2241 g_warning ("gstrtpsession: pad already requested");
2247 gst_rtp_session_release_pad (GstElement * element, GstPad * pad)
2249 GstRtpSession *rtpsession;
2251 g_return_if_fail (GST_IS_RTP_SESSION (element));
2252 g_return_if_fail (GST_IS_PAD (pad));
2254 rtpsession = GST_RTP_SESSION (element);
2256 GST_DEBUG_OBJECT (element, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2258 GST_RTP_SESSION_LOCK (rtpsession);
2260 if (rtpsession->recv_rtp_sink == pad) {
2261 remove_recv_rtp_sink (rtpsession);
2262 } else if (rtpsession->recv_rtcp_sink == pad) {
2263 remove_recv_rtcp_sink (rtpsession);
2264 } else if (rtpsession->send_rtp_sink == pad) {
2265 remove_send_rtp_sink (rtpsession);
2266 } else if (rtpsession->send_rtcp_src == pad) {
2267 remove_send_rtcp_src (rtpsession);
2271 GST_RTP_SESSION_UNLOCK (rtpsession);
2278 GST_RTP_SESSION_UNLOCK (rtpsession);
2279 g_warning ("gstrtpsession: asked to release an unknown pad");
2285 gst_rtp_session_request_key_unit (RTPSession * sess,
2286 gboolean all_headers, gpointer user_data)
2288 GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2290 GstPad *send_rtp_sink;
2292 GST_RTP_SESSION_LOCK (rtpsession);
2293 if ((send_rtp_sink = rtpsession->send_rtp_sink))
2294 gst_object_ref (send_rtp_sink);
2295 GST_RTP_SESSION_UNLOCK (rtpsession);
2297 if (send_rtp_sink) {
2298 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
2299 gst_structure_new ("GstForceKeyUnit",
2300 "all-headers", G_TYPE_BOOLEAN, all_headers, NULL));
2301 gst_pad_push_event (send_rtp_sink, event);
2302 gst_object_unref (send_rtp_sink);
2307 gst_rtp_session_request_time (RTPSession * session, gpointer user_data)
2309 GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2311 return gst_clock_get_time (rtpsession->priv->sysclock);