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 RTPSource * src, 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 rtpsession->priv->wait_send = TRUE;
982 GST_RTP_SESSION_UNLOCK (rtpsession);
984 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
986 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
987 case GST_STATE_CHANGE_PAUSED_TO_READY:
988 /* no need to join yet, we might want to continue later. Also, the
989 * dataflow could block downstream so that a join could just block
991 stop_rtcp_thread (rtpsession);
997 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
999 switch (transition) {
1000 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1001 if (!start_rtcp_thread (rtpsession))
1004 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1006 case GST_STATE_CHANGE_PAUSED_TO_READY:
1007 /* downstream is now releasing the dataflow and we can join. */
1008 join_rtcp_thread (rtpsession);
1010 case GST_STATE_CHANGE_READY_TO_NULL:
1020 return GST_STATE_CHANGE_FAILURE;
1025 return_true (gpointer key, gpointer value, gpointer user_data)
1031 gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession)
1033 g_hash_table_foreach_remove (rtpsession->priv->ptmap, return_true, NULL);
1036 /* called when the session manager has an RTP packet or a list of packets
1037 * ready for further processing */
1038 static GstFlowReturn
1039 gst_rtp_session_process_rtp (RTPSession * sess, RTPSource * src,
1040 GstBuffer * buffer, gpointer user_data)
1042 GstFlowReturn result;
1043 GstRtpSession *rtpsession;
1046 rtpsession = GST_RTP_SESSION (user_data);
1048 GST_RTP_SESSION_LOCK (rtpsession);
1049 if ((rtp_src = rtpsession->recv_rtp_src))
1050 gst_object_ref (rtp_src);
1051 GST_RTP_SESSION_UNLOCK (rtpsession);
1054 GST_LOG_OBJECT (rtpsession, "pushing received RTP packet");
1055 result = gst_pad_push (rtp_src, buffer);
1056 gst_object_unref (rtp_src);
1058 GST_DEBUG_OBJECT (rtpsession, "dropping received RTP packet");
1059 gst_buffer_unref (buffer);
1060 result = GST_FLOW_OK;
1065 /* called when the session manager has an RTP packet ready for further
1067 static GstFlowReturn
1068 gst_rtp_session_send_rtp (RTPSession * sess, RTPSource * src,
1069 gpointer data, gpointer user_data)
1071 GstFlowReturn result;
1072 GstRtpSession *rtpsession;
1075 rtpsession = GST_RTP_SESSION (user_data);
1077 GST_RTP_SESSION_LOCK (rtpsession);
1078 if ((rtp_src = rtpsession->send_rtp_src))
1079 gst_object_ref (rtp_src);
1080 if (rtpsession->priv->wait_send) {
1081 GST_LOG_OBJECT (rtpsession, "signal RTCP thread");
1082 rtpsession->priv->wait_send = FALSE;
1083 GST_RTP_SESSION_SIGNAL (rtpsession);
1085 GST_RTP_SESSION_UNLOCK (rtpsession);
1088 if (GST_IS_BUFFER (data)) {
1089 GST_LOG_OBJECT (rtpsession, "sending RTP packet");
1090 result = gst_pad_push (rtp_src, GST_BUFFER_CAST (data));
1092 GST_LOG_OBJECT (rtpsession, "sending RTP list");
1093 result = gst_pad_push_list (rtp_src, GST_BUFFER_LIST_CAST (data));
1095 gst_object_unref (rtp_src);
1097 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1098 result = GST_FLOW_OK;
1103 /* called when the session manager has an RTCP packet ready for further
1104 * sending. The eos flag is set when an EOS event should be sent downstream as
1106 static GstFlowReturn
1107 gst_rtp_session_send_rtcp (RTPSession * sess, RTPSource * src,
1108 GstBuffer * buffer, gboolean eos, gpointer user_data)
1110 GstFlowReturn result;
1111 GstRtpSession *rtpsession;
1114 rtpsession = GST_RTP_SESSION (user_data);
1116 GST_RTP_SESSION_LOCK (rtpsession);
1117 if (rtpsession->priv->stop_thread)
1120 if ((rtcp_src = rtpsession->send_rtcp_src)) {
1123 gst_object_ref (rtcp_src);
1124 GST_RTP_SESSION_UNLOCK (rtpsession);
1126 /* set rtcp caps on output pad */
1127 if (!(caps = gst_pad_get_current_caps (rtcp_src))) {
1128 caps = gst_caps_new_empty_simple ("application/x-rtcp");
1129 gst_pad_set_caps (rtcp_src, caps);
1131 gst_caps_unref (caps);
1133 GST_LOG_OBJECT (rtpsession, "sending RTCP");
1134 result = gst_pad_push (rtcp_src, buffer);
1136 /* we have to send EOS after this packet */
1138 GST_LOG_OBJECT (rtpsession, "sending EOS");
1139 gst_pad_push_event (rtcp_src, gst_event_new_eos ());
1141 gst_object_unref (rtcp_src);
1143 GST_RTP_SESSION_UNLOCK (rtpsession);
1145 GST_DEBUG_OBJECT (rtpsession, "not sending RTCP, no output pad");
1146 gst_buffer_unref (buffer);
1147 result = GST_FLOW_OK;
1154 GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1155 gst_buffer_unref (buffer);
1156 GST_RTP_SESSION_UNLOCK (rtpsession);
1161 /* called when the session manager has an SR RTCP packet ready for handling
1162 * inter stream synchronisation */
1163 static GstFlowReturn
1164 gst_rtp_session_sync_rtcp (RTPSession * sess, RTPSource * src,
1165 GstBuffer * buffer, gpointer user_data)
1167 GstFlowReturn result;
1168 GstRtpSession *rtpsession;
1171 rtpsession = GST_RTP_SESSION (user_data);
1173 GST_RTP_SESSION_LOCK (rtpsession);
1174 if (rtpsession->priv->stop_thread)
1177 if ((sync_src = rtpsession->sync_src)) {
1180 gst_object_ref (sync_src);
1181 GST_RTP_SESSION_UNLOCK (rtpsession);
1183 /* set rtcp caps on output pad */
1184 if (!(caps = gst_pad_get_current_caps (sync_src))) {
1185 caps = gst_caps_new_empty_simple ("application/x-rtcp");
1186 gst_pad_set_caps (sync_src, caps);
1188 gst_caps_unref (caps);
1190 GST_LOG_OBJECT (rtpsession, "sending Sync RTCP");
1191 result = gst_pad_push (sync_src, buffer);
1192 gst_object_unref (sync_src);
1194 GST_RTP_SESSION_UNLOCK (rtpsession);
1196 GST_DEBUG_OBJECT (rtpsession, "not sending Sync RTCP, no output pad");
1197 gst_buffer_unref (buffer);
1198 result = GST_FLOW_OK;
1205 GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1206 gst_buffer_unref (buffer);
1207 GST_RTP_SESSION_UNLOCK (rtpsession);
1213 gst_rtp_session_cache_caps (GstRtpSession * rtpsession, GstCaps * caps)
1215 GstRtpSessionPrivate *priv;
1216 const GstStructure *s;
1219 priv = rtpsession->priv;
1221 GST_DEBUG_OBJECT (rtpsession, "parsing caps");
1223 s = gst_caps_get_structure (caps, 0);
1224 if (!gst_structure_get_int (s, "payload", &payload))
1227 if (g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (payload)))
1230 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (payload),
1231 gst_caps_ref (caps));
1235 gst_rtp_session_get_caps_for_pt (GstRtpSession * rtpsession, guint payload)
1237 GstCaps *caps = NULL;
1238 GValue args[2] = { {0}, {0} };
1241 GST_RTP_SESSION_LOCK (rtpsession);
1242 caps = g_hash_table_lookup (rtpsession->priv->ptmap,
1243 GINT_TO_POINTER (payload));
1245 gst_caps_ref (caps);
1249 /* not found in the cache, try to get it with a signal */
1250 g_value_init (&args[0], GST_TYPE_ELEMENT);
1251 g_value_set_object (&args[0], rtpsession);
1252 g_value_init (&args[1], G_TYPE_UINT);
1253 g_value_set_uint (&args[1], payload);
1255 g_value_init (&ret, GST_TYPE_CAPS);
1256 g_value_set_boxed (&ret, NULL);
1258 GST_RTP_SESSION_UNLOCK (rtpsession);
1260 g_signal_emitv (args, gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP], 0,
1263 GST_RTP_SESSION_LOCK (rtpsession);
1265 g_value_unset (&args[0]);
1266 g_value_unset (&args[1]);
1267 caps = (GstCaps *) g_value_dup_boxed (&ret);
1268 g_value_unset (&ret);
1272 gst_rtp_session_cache_caps (rtpsession, caps);
1275 GST_RTP_SESSION_UNLOCK (rtpsession);
1281 GST_DEBUG_OBJECT (rtpsession, "could not get caps");
1286 /* called when the session manager needs the clock rate */
1288 gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
1292 GstRtpSession *rtpsession;
1294 const GstStructure *s;
1296 rtpsession = GST_RTP_SESSION_CAST (user_data);
1298 caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1303 s = gst_caps_get_structure (caps, 0);
1304 if (!gst_structure_get_int (s, "clock-rate", &result))
1307 gst_caps_unref (caps);
1309 GST_DEBUG_OBJECT (rtpsession, "parsed clock-rate %d", result);
1318 gst_caps_unref (caps);
1319 GST_DEBUG_OBJECT (rtpsession, "No clock-rate in caps!");
1324 /* called when the session manager asks us to reconsider the timeout */
1326 gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data)
1328 GstRtpSession *rtpsession;
1330 rtpsession = GST_RTP_SESSION_CAST (user_data);
1332 GST_RTP_SESSION_LOCK (rtpsession);
1333 GST_DEBUG_OBJECT (rtpsession, "unlock timer for reconsideration");
1334 if (rtpsession->priv->id)
1335 gst_clock_id_unschedule (rtpsession->priv->id);
1336 GST_RTP_SESSION_UNLOCK (rtpsession);
1340 gst_rtp_session_event_recv_rtp_sink (GstPad * pad, GstObject * parent,
1343 GstRtpSession *rtpsession;
1344 gboolean ret = FALSE;
1346 rtpsession = GST_RTP_SESSION (parent);
1348 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1349 GST_EVENT_TYPE_NAME (event));
1351 switch (GST_EVENT_TYPE (event)) {
1352 case GST_EVENT_CAPS:
1357 gst_event_parse_caps (event, &caps);
1358 gst_rtp_session_sink_setcaps (pad, rtpsession, caps);
1359 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1362 case GST_EVENT_FLUSH_STOP:
1363 gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
1364 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1366 case GST_EVENT_SEGMENT:
1368 GstSegment *segment, in_segment;
1370 segment = &rtpsession->recv_rtp_seg;
1372 /* the newsegment event is needed to convert the RTP timestamp to
1373 * running_time, which is needed to generate a mapping from RTP to NTP
1374 * timestamps in SR reports */
1375 gst_event_copy_segment (event, &in_segment);
1376 GST_DEBUG_OBJECT (rtpsession, "received segment %" GST_SEGMENT_FORMAT,
1379 /* accept upstream */
1380 gst_segment_copy_into (&in_segment, segment);
1382 /* push event forward */
1383 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1387 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1396 gst_rtp_session_request_remote_key_unit (GstRtpSession * rtpsession,
1397 guint32 ssrc, guint payload, gboolean all_headers, gint count)
1401 caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1404 const GstStructure *s = gst_caps_get_structure (caps, 0);
1408 pli = gst_structure_has_field (s, "rtcp-fb-nack-pli");
1409 fir = gst_structure_has_field (s, "rtcp-fb-ccm-fir") && all_headers;
1411 /* Google Talk uses FIR for repair, so send it even if we just want a
1414 gst_structure_has_field (s, "rtcp-fb-x-gstreamer-fir-as-repair"))
1417 gst_caps_unref (caps);
1420 return rtp_session_request_key_unit (rtpsession->priv->session, ssrc,
1421 gst_clock_get_time (rtpsession->priv->sysclock), fir, count);
1428 gst_rtp_session_event_recv_rtp_src (GstPad * pad, GstObject * parent,
1431 GstRtpSession *rtpsession;
1432 gboolean forward = TRUE;
1433 gboolean ret = TRUE;
1434 const GstStructure *s;
1438 rtpsession = GST_RTP_SESSION (parent);
1440 switch (GST_EVENT_TYPE (event)) {
1441 case GST_EVENT_CUSTOM_UPSTREAM:
1442 s = gst_event_get_structure (event);
1443 if (gst_structure_has_name (s, "GstForceKeyUnit") &&
1444 gst_structure_get_uint (s, "ssrc", &ssrc) &&
1445 gst_structure_get_uint (s, "payload", &pt)) {
1446 gboolean all_headers = FALSE;
1449 gst_structure_get_boolean (s, "all-headers", &all_headers);
1450 if (gst_structure_get_int (s, "count", &count) && count < 0)
1451 count += G_MAXINT; /* Make sure count is positive if present */
1452 if (gst_rtp_session_request_remote_key_unit (rtpsession, ssrc, pt,
1453 all_headers, count))
1462 GstPad *recv_rtp_sink;
1464 GST_RTP_SESSION_LOCK (rtpsession);
1465 if ((recv_rtp_sink = rtpsession->recv_rtp_sink))
1466 gst_object_ref (recv_rtp_sink);
1467 GST_RTP_SESSION_UNLOCK (rtpsession);
1469 if (recv_rtp_sink) {
1470 ret = gst_pad_push_event (recv_rtp_sink, event);
1471 gst_object_unref (recv_rtp_sink);
1473 gst_event_unref (event);
1475 gst_event_unref (event);
1482 static GstIterator *
1483 gst_rtp_session_iterate_internal_links (GstPad * pad, GstObject * parent)
1485 GstRtpSession *rtpsession;
1486 GstPad *otherpad = NULL;
1487 GstIterator *it = NULL;
1489 rtpsession = GST_RTP_SESSION (parent);
1491 GST_RTP_SESSION_LOCK (rtpsession);
1492 if (pad == rtpsession->recv_rtp_src) {
1493 otherpad = gst_object_ref (rtpsession->recv_rtp_sink);
1494 } else if (pad == rtpsession->recv_rtp_sink) {
1495 otherpad = gst_object_ref (rtpsession->recv_rtp_src);
1496 } else if (pad == rtpsession->send_rtp_src) {
1497 otherpad = gst_object_ref (rtpsession->send_rtp_sink);
1498 } else if (pad == rtpsession->send_rtp_sink) {
1499 otherpad = gst_object_ref (rtpsession->send_rtp_src);
1501 GST_RTP_SESSION_UNLOCK (rtpsession);
1504 GValue val = { 0, };
1506 g_value_init (&val, GST_TYPE_PAD);
1507 g_value_set_object (&val, otherpad);
1508 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
1509 g_value_unset (&val);
1510 gst_object_unref (otherpad);
1517 gst_rtp_session_sink_setcaps (GstPad * pad, GstRtpSession * rtpsession,
1520 GST_RTP_SESSION_LOCK (rtpsession);
1521 gst_rtp_session_cache_caps (rtpsession, caps);
1522 GST_RTP_SESSION_UNLOCK (rtpsession);
1527 /* receive a packet from a sender, send it to the RTP session manager and
1528 * forward the packet on the rtp_src pad
1530 static GstFlowReturn
1531 gst_rtp_session_chain_recv_rtp (GstPad * pad, GstObject * parent,
1534 GstRtpSession *rtpsession;
1535 GstRtpSessionPrivate *priv;
1537 GstClockTime current_time, running_time;
1538 GstClockTime timestamp;
1540 rtpsession = GST_RTP_SESSION (parent);
1541 priv = rtpsession->priv;
1543 GST_LOG_OBJECT (rtpsession, "received RTP packet");
1545 /* get NTP time when this packet was captured, this depends on the timestamp. */
1546 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1547 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1548 /* convert to running time using the segment values */
1550 gst_segment_to_running_time (&rtpsession->recv_rtp_seg, GST_FORMAT_TIME,
1553 get_current_times (rtpsession, &running_time, NULL);
1555 current_time = gst_clock_get_time (priv->sysclock);
1557 ret = rtp_session_process_rtp (priv->session, buffer, current_time,
1559 if (ret != GST_FLOW_OK)
1569 GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1570 gst_flow_get_name (ret));
1576 gst_rtp_session_event_recv_rtcp_sink (GstPad * pad, GstObject * parent,
1579 GstRtpSession *rtpsession;
1580 gboolean ret = FALSE;
1582 rtpsession = GST_RTP_SESSION (parent);
1584 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1585 GST_EVENT_TYPE_NAME (event));
1587 switch (GST_EVENT_TYPE (event)) {
1589 ret = gst_pad_push_event (rtpsession->sync_src, event);
1596 /* Receive an RTCP packet from a sender, send it to the RTP session manager and
1597 * forward the SR packets to the sync_src pad.
1599 static GstFlowReturn
1600 gst_rtp_session_chain_recv_rtcp (GstPad * pad, GstObject * parent,
1603 GstRtpSession *rtpsession;
1604 GstRtpSessionPrivate *priv;
1605 GstClockTime current_time;
1608 rtpsession = GST_RTP_SESSION (parent);
1609 priv = rtpsession->priv;
1611 GST_LOG_OBJECT (rtpsession, "received RTCP packet");
1613 current_time = gst_clock_get_time (priv->sysclock);
1614 get_current_times (rtpsession, NULL, &ntpnstime);
1616 rtp_session_process_rtcp (priv->session, buffer, current_time, ntpnstime);
1618 return GST_FLOW_OK; /* always return OK */
1622 gst_rtp_session_query_send_rtcp_src (GstPad * pad, GstObject * parent,
1625 GstRtpSession *rtpsession;
1626 gboolean ret = FALSE;
1628 rtpsession = GST_RTP_SESSION (parent);
1630 GST_DEBUG_OBJECT (rtpsession, "received QUERY %s",
1631 GST_QUERY_TYPE_NAME (query));
1633 switch (GST_QUERY_TYPE (query)) {
1634 case GST_QUERY_LATENCY:
1636 /* use the defaults for the latency query. */
1637 gst_query_set_latency (query, FALSE, 0, -1);
1640 /* other queries simply fail for now */
1648 gst_rtp_session_event_send_rtcp_src (GstPad * pad, GstObject * parent,
1651 GstRtpSession *rtpsession;
1652 gboolean ret = TRUE;
1654 rtpsession = GST_RTP_SESSION (parent);
1655 GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
1656 GST_EVENT_TYPE_NAME (event));
1658 switch (GST_EVENT_TYPE (event)) {
1659 case GST_EVENT_SEEK:
1660 case GST_EVENT_LATENCY:
1661 gst_event_unref (event);
1665 /* other events simply fail for now */
1666 gst_event_unref (event);
1676 gst_rtp_session_event_send_rtp_sink (GstPad * pad, GstObject * parent,
1679 GstRtpSession *rtpsession;
1680 gboolean ret = FALSE;
1682 rtpsession = GST_RTP_SESSION (parent);
1684 GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
1685 GST_EVENT_TYPE_NAME (event));
1687 switch (GST_EVENT_TYPE (event)) {
1688 case GST_EVENT_CAPS:
1693 gst_event_parse_caps (event, &caps);
1694 gst_rtp_session_setcaps_send_rtp (pad, rtpsession, caps);
1695 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1698 case GST_EVENT_FLUSH_STOP:
1699 gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
1700 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1702 case GST_EVENT_SEGMENT:{
1703 GstSegment *segment, in_segment;
1705 segment = &rtpsession->send_rtp_seg;
1707 /* the newsegment event is needed to convert the RTP timestamp to
1708 * running_time, which is needed to generate a mapping from RTP to NTP
1709 * timestamps in SR reports */
1710 gst_event_copy_segment (event, &in_segment);
1711 GST_DEBUG_OBJECT (rtpsession, "received segment %" GST_SEGMENT_FORMAT,
1714 /* accept upstream */
1715 gst_segment_copy_into (&in_segment, segment);
1717 /* push event forward */
1718 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1721 case GST_EVENT_EOS:{
1722 GstClockTime current_time;
1724 /* push downstream FIXME, we are not supposed to leave the session just
1725 * because we stop sending. */
1726 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1727 current_time = gst_clock_get_time (rtpsession->priv->sysclock);
1728 GST_DEBUG_OBJECT (rtpsession, "scheduling BYE message");
1729 rtp_session_schedule_bye (rtpsession->priv->session, "End of stream",
1734 GstPad *send_rtp_src;
1736 GST_RTP_SESSION_LOCK (rtpsession);
1737 if ((send_rtp_src = rtpsession->send_rtp_src))
1738 gst_object_ref (send_rtp_src);
1739 GST_RTP_SESSION_UNLOCK (rtpsession);
1742 ret = gst_pad_push_event (send_rtp_src, event);
1743 gst_object_unref (send_rtp_src);
1745 gst_event_unref (event);
1755 gst_rtp_session_event_send_rtp_src (GstPad * pad, GstObject * parent,
1758 GstRtpSession *rtpsession;
1759 gboolean ret = FALSE;
1761 rtpsession = GST_RTP_SESSION (parent);
1763 GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
1764 GST_EVENT_TYPE_NAME (event));
1766 switch (GST_EVENT_TYPE (event)) {
1767 case GST_EVENT_LATENCY:
1768 /* save the latency, we need this to know when an RTP packet will be
1769 * rendered by the sink */
1770 gst_event_parse_latency (event, &rtpsession->priv->send_latency);
1772 ret = gst_pad_event_default (pad, parent, event);
1775 ret = gst_pad_event_default (pad, parent, event);
1782 gst_rtp_session_getcaps_send_rtp (GstPad * pad, GstRtpSession * rtpsession,
1785 GstRtpSessionPrivate *priv;
1787 GstStructure *s1, *s2;
1790 priv = rtpsession->priv;
1792 ssrc = rtp_session_get_internal_ssrc (priv->session);
1794 /* we can basically accept anything but we prefer to receive packets with our
1795 * internal SSRC so that we don't have to patch it. Create a structure with
1796 * the SSRC and another one without. */
1797 s1 = gst_structure_new ("application/x-rtp", "ssrc", G_TYPE_UINT, ssrc, NULL);
1798 s2 = gst_structure_new_empty ("application/x-rtp");
1800 result = gst_caps_new_full (s1, s2, NULL);
1803 GstCaps *caps = result;
1805 result = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
1806 gst_caps_unref (caps);
1809 GST_DEBUG_OBJECT (rtpsession, "getting caps %" GST_PTR_FORMAT, result);
1815 gst_rtp_session_query_send_rtp (GstPad * pad, GstObject * parent,
1818 gboolean res = FALSE;
1819 GstRtpSession *rtpsession;
1821 rtpsession = GST_RTP_SESSION (parent);
1823 switch (GST_QUERY_TYPE (query)) {
1824 case GST_QUERY_CAPS:
1826 GstCaps *filter, *caps;
1828 gst_query_parse_caps (query, &filter);
1829 caps = gst_rtp_session_getcaps_send_rtp (pad, rtpsession, filter);
1830 gst_query_set_caps_result (query, caps);
1831 gst_caps_unref (caps);
1836 res = gst_pad_query_default (pad, parent, query);
1844 gst_rtp_session_setcaps_send_rtp (GstPad * pad, GstRtpSession * rtpsession,
1847 GstRtpSessionPrivate *priv;
1848 GstStructure *s = gst_caps_get_structure (caps, 0);
1851 priv = rtpsession->priv;
1853 if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
1854 GST_DEBUG_OBJECT (rtpsession, "setting internal SSRC to %08x", ssrc);
1855 rtp_session_set_internal_ssrc (priv->session, ssrc);
1857 rtp_session_update_send_caps (priv->session, caps);
1862 /* Recieve an RTP packet or a list of packets to be send to the receivers,
1863 * send to RTP session manager and forward to send_rtp_src.
1865 static GstFlowReturn
1866 gst_rtp_session_chain_send_rtp_common (GstRtpSession * rtpsession,
1867 gpointer data, gboolean is_list)
1869 GstRtpSessionPrivate *priv;
1871 GstClockTime timestamp, running_time;
1872 GstClockTime current_time;
1874 priv = rtpsession->priv;
1876 GST_LOG_OBJECT (rtpsession, "received RTP %s", is_list ? "list" : "packet");
1878 /* get NTP time when this packet was captured, this depends on the timestamp. */
1880 GstBuffer *buffer = NULL;
1882 /* All groups in an list have the same timestamp.
1883 * So, just take it from the first group. */
1884 buffer = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0);
1886 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1890 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (data));
1893 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1894 /* convert to running time using the segment start value. */
1896 gst_segment_to_running_time (&rtpsession->send_rtp_seg, GST_FORMAT_TIME,
1898 running_time += priv->send_latency;
1904 current_time = gst_clock_get_time (priv->sysclock);
1905 ret = rtp_session_send_rtp (priv->session, data, is_list, current_time,
1907 if (ret != GST_FLOW_OK)
1917 GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1918 gst_flow_get_name (ret));
1923 static GstFlowReturn
1924 gst_rtp_session_chain_send_rtp (GstPad * pad, GstObject * parent,
1927 GstRtpSession *rtpsession = GST_RTP_SESSION (parent);
1929 return gst_rtp_session_chain_send_rtp_common (rtpsession, buffer, FALSE);
1932 static GstFlowReturn
1933 gst_rtp_session_chain_send_rtp_list (GstPad * pad, GstObject * parent,
1934 GstBufferList * list)
1936 GstRtpSession *rtpsession = GST_RTP_SESSION (parent);
1938 return gst_rtp_session_chain_send_rtp_common (rtpsession, list, TRUE);
1941 /* Create sinkpad to receive RTP packets from senders. This will also create a
1942 * srcpad for the RTP packets.
1945 create_recv_rtp_sink (GstRtpSession * rtpsession)
1947 GST_DEBUG_OBJECT (rtpsession, "creating RTP sink pad");
1949 rtpsession->recv_rtp_sink =
1950 gst_pad_new_from_static_template (&rtpsession_recv_rtp_sink_template,
1952 gst_pad_set_chain_function (rtpsession->recv_rtp_sink,
1953 gst_rtp_session_chain_recv_rtp);
1954 gst_pad_set_event_function (rtpsession->recv_rtp_sink,
1955 gst_rtp_session_event_recv_rtp_sink);
1956 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_sink,
1957 gst_rtp_session_iterate_internal_links);
1958 gst_pad_set_active (rtpsession->recv_rtp_sink, TRUE);
1959 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1960 rtpsession->recv_rtp_sink);
1962 GST_DEBUG_OBJECT (rtpsession, "creating RTP src pad");
1963 rtpsession->recv_rtp_src =
1964 gst_pad_new_from_static_template (&rtpsession_recv_rtp_src_template,
1966 gst_pad_set_event_function (rtpsession->recv_rtp_src,
1967 gst_rtp_session_event_recv_rtp_src);
1968 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_src,
1969 gst_rtp_session_iterate_internal_links);
1970 gst_pad_use_fixed_caps (rtpsession->recv_rtp_src);
1971 gst_pad_set_active (rtpsession->recv_rtp_src, TRUE);
1972 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->recv_rtp_src);
1974 return rtpsession->recv_rtp_sink;
1977 /* Remove sinkpad to receive RTP packets from senders. This will also remove
1978 * the srcpad for the RTP packets.
1981 remove_recv_rtp_sink (GstRtpSession * rtpsession)
1983 GST_DEBUG_OBJECT (rtpsession, "removing RTP sink pad");
1985 /* deactivate from source to sink */
1986 gst_pad_set_active (rtpsession->recv_rtp_src, FALSE);
1987 gst_pad_set_active (rtpsession->recv_rtp_sink, FALSE);
1990 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1991 rtpsession->recv_rtp_sink);
1992 rtpsession->recv_rtp_sink = NULL;
1994 GST_DEBUG_OBJECT (rtpsession, "removing RTP src pad");
1995 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1996 rtpsession->recv_rtp_src);
1997 rtpsession->recv_rtp_src = NULL;
2000 /* Create a sinkpad to receive RTCP messages from senders, this will also create a
2001 * sync_src pad for the SR packets.
2004 create_recv_rtcp_sink (GstRtpSession * rtpsession)
2006 GST_DEBUG_OBJECT (rtpsession, "creating RTCP sink pad");
2008 rtpsession->recv_rtcp_sink =
2009 gst_pad_new_from_static_template (&rtpsession_recv_rtcp_sink_template,
2011 gst_pad_set_chain_function (rtpsession->recv_rtcp_sink,
2012 gst_rtp_session_chain_recv_rtcp);
2013 gst_pad_set_event_function (rtpsession->recv_rtcp_sink,
2014 gst_rtp_session_event_recv_rtcp_sink);
2015 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtcp_sink,
2016 gst_rtp_session_iterate_internal_links);
2017 gst_pad_set_active (rtpsession->recv_rtcp_sink, TRUE);
2018 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2019 rtpsession->recv_rtcp_sink);
2021 GST_DEBUG_OBJECT (rtpsession, "creating sync src pad");
2022 rtpsession->sync_src =
2023 gst_pad_new_from_static_template (&rtpsession_sync_src_template,
2025 gst_pad_set_iterate_internal_links_function (rtpsession->sync_src,
2026 gst_rtp_session_iterate_internal_links);
2027 gst_pad_use_fixed_caps (rtpsession->sync_src);
2028 gst_pad_set_active (rtpsession->sync_src, TRUE);
2029 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
2031 return rtpsession->recv_rtcp_sink;
2035 remove_recv_rtcp_sink (GstRtpSession * rtpsession)
2037 GST_DEBUG_OBJECT (rtpsession, "removing RTCP sink pad");
2039 gst_pad_set_active (rtpsession->sync_src, FALSE);
2040 gst_pad_set_active (rtpsession->recv_rtcp_sink, FALSE);
2042 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2043 rtpsession->recv_rtcp_sink);
2044 rtpsession->recv_rtcp_sink = NULL;
2046 GST_DEBUG_OBJECT (rtpsession, "removing sync src pad");
2047 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
2048 rtpsession->sync_src = NULL;
2051 /* Create a sinkpad to receive RTP packets for receivers. This will also create a
2055 create_send_rtp_sink (GstRtpSession * rtpsession)
2057 GST_DEBUG_OBJECT (rtpsession, "creating pad");
2059 rtpsession->send_rtp_sink =
2060 gst_pad_new_from_static_template (&rtpsession_send_rtp_sink_template,
2062 gst_pad_set_chain_function (rtpsession->send_rtp_sink,
2063 gst_rtp_session_chain_send_rtp);
2064 gst_pad_set_chain_list_function (rtpsession->send_rtp_sink,
2065 gst_rtp_session_chain_send_rtp_list);
2066 gst_pad_set_query_function (rtpsession->send_rtp_sink,
2067 gst_rtp_session_query_send_rtp);
2068 gst_pad_set_event_function (rtpsession->send_rtp_sink,
2069 gst_rtp_session_event_send_rtp_sink);
2070 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_sink,
2071 gst_rtp_session_iterate_internal_links);
2072 gst_pad_set_active (rtpsession->send_rtp_sink, TRUE);
2073 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2074 rtpsession->send_rtp_sink);
2076 rtpsession->send_rtp_src =
2077 gst_pad_new_from_static_template (&rtpsession_send_rtp_src_template,
2079 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_src,
2080 gst_rtp_session_iterate_internal_links);
2081 gst_pad_set_event_function (rtpsession->send_rtp_src,
2082 gst_rtp_session_event_send_rtp_src);
2083 gst_pad_set_active (rtpsession->send_rtp_src, TRUE);
2084 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->send_rtp_src);
2086 return rtpsession->send_rtp_sink;
2090 remove_send_rtp_sink (GstRtpSession * rtpsession)
2092 GST_DEBUG_OBJECT (rtpsession, "removing pad");
2094 gst_pad_set_active (rtpsession->send_rtp_src, FALSE);
2095 gst_pad_set_active (rtpsession->send_rtp_sink, FALSE);
2097 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2098 rtpsession->send_rtp_sink);
2099 rtpsession->send_rtp_sink = NULL;
2101 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2102 rtpsession->send_rtp_src);
2103 rtpsession->send_rtp_src = NULL;
2106 /* Create a srcpad with the RTCP packets to send out.
2107 * This pad will be driven by the RTP session manager when it wants to send out
2111 create_send_rtcp_src (GstRtpSession * rtpsession)
2113 GST_DEBUG_OBJECT (rtpsession, "creating pad");
2115 rtpsession->send_rtcp_src =
2116 gst_pad_new_from_static_template (&rtpsession_send_rtcp_src_template,
2118 gst_pad_use_fixed_caps (rtpsession->send_rtcp_src);
2119 gst_pad_set_active (rtpsession->send_rtcp_src, TRUE);
2120 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtcp_src,
2121 gst_rtp_session_iterate_internal_links);
2122 gst_pad_set_query_function (rtpsession->send_rtcp_src,
2123 gst_rtp_session_query_send_rtcp_src);
2124 gst_pad_set_event_function (rtpsession->send_rtcp_src,
2125 gst_rtp_session_event_send_rtcp_src);
2126 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2127 rtpsession->send_rtcp_src);
2129 return rtpsession->send_rtcp_src;
2133 remove_send_rtcp_src (GstRtpSession * rtpsession)
2135 GST_DEBUG_OBJECT (rtpsession, "removing pad");
2137 gst_pad_set_active (rtpsession->send_rtcp_src, FALSE);
2139 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2140 rtpsession->send_rtcp_src);
2141 rtpsession->send_rtcp_src = NULL;
2145 gst_rtp_session_request_new_pad (GstElement * element,
2146 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
2148 GstRtpSession *rtpsession;
2149 GstElementClass *klass;
2152 g_return_val_if_fail (templ != NULL, NULL);
2153 g_return_val_if_fail (GST_IS_RTP_SESSION (element), NULL);
2155 rtpsession = GST_RTP_SESSION (element);
2156 klass = GST_ELEMENT_GET_CLASS (element);
2158 GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
2160 GST_RTP_SESSION_LOCK (rtpsession);
2162 /* figure out the template */
2163 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink")) {
2164 if (rtpsession->recv_rtp_sink != NULL)
2167 result = create_recv_rtp_sink (rtpsession);
2168 } else if (templ == gst_element_class_get_pad_template (klass,
2169 "recv_rtcp_sink")) {
2170 if (rtpsession->recv_rtcp_sink != NULL)
2173 result = create_recv_rtcp_sink (rtpsession);
2174 } else if (templ == gst_element_class_get_pad_template (klass,
2176 if (rtpsession->send_rtp_sink != NULL)
2179 result = create_send_rtp_sink (rtpsession);
2180 } else if (templ == gst_element_class_get_pad_template (klass,
2182 if (rtpsession->send_rtcp_src != NULL)
2185 result = create_send_rtcp_src (rtpsession);
2187 goto wrong_template;
2189 GST_RTP_SESSION_UNLOCK (rtpsession);
2196 GST_RTP_SESSION_UNLOCK (rtpsession);
2197 g_warning ("gstrtpsession: this is not our template");
2202 GST_RTP_SESSION_UNLOCK (rtpsession);
2203 g_warning ("gstrtpsession: pad already requested");
2209 gst_rtp_session_release_pad (GstElement * element, GstPad * pad)
2211 GstRtpSession *rtpsession;
2213 g_return_if_fail (GST_IS_RTP_SESSION (element));
2214 g_return_if_fail (GST_IS_PAD (pad));
2216 rtpsession = GST_RTP_SESSION (element);
2218 GST_DEBUG_OBJECT (element, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2220 GST_RTP_SESSION_LOCK (rtpsession);
2222 if (rtpsession->recv_rtp_sink == pad) {
2223 remove_recv_rtp_sink (rtpsession);
2224 } else if (rtpsession->recv_rtcp_sink == pad) {
2225 remove_recv_rtcp_sink (rtpsession);
2226 } else if (rtpsession->send_rtp_sink == pad) {
2227 remove_send_rtp_sink (rtpsession);
2228 } else if (rtpsession->send_rtcp_src == pad) {
2229 remove_send_rtcp_src (rtpsession);
2233 GST_RTP_SESSION_UNLOCK (rtpsession);
2240 GST_RTP_SESSION_UNLOCK (rtpsession);
2241 g_warning ("gstrtpsession: asked to release an unknown pad");
2247 gst_rtp_session_request_key_unit (RTPSession * sess,
2248 gboolean all_headers, gpointer user_data)
2250 GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2252 GstPad *send_rtp_sink;
2254 GST_RTP_SESSION_LOCK (rtpsession);
2255 if ((send_rtp_sink = rtpsession->send_rtp_sink))
2256 gst_object_ref (send_rtp_sink);
2257 GST_RTP_SESSION_UNLOCK (rtpsession);
2259 if (send_rtp_sink) {
2260 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
2261 gst_structure_new ("GstForceKeyUnit",
2262 "all-headers", G_TYPE_BOOLEAN, all_headers, NULL));
2263 gst_pad_push_event (send_rtp_sink, event);
2264 gst_object_unref (send_rtp_sink);
2269 gst_rtp_session_request_time (RTPSession * session, gpointer user_data)
2271 GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2273 return gst_clock_get_time (rtpsession->priv->sysclock);