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 struct _GstRtpSessionPrivate
236 /* thread for sending out RTCP */
238 gboolean stop_thread;
240 gboolean thread_stopped;
245 GstClockTime send_latency;
247 gboolean use_pipeline_clock;
250 /* callbacks to handle actions from the session manager */
251 static GstFlowReturn gst_rtp_session_process_rtp (RTPSession * sess,
252 RTPSource * src, GstBuffer * buffer, gpointer user_data);
253 static GstFlowReturn gst_rtp_session_send_rtp (RTPSession * sess,
254 RTPSource * src, gpointer data, gpointer user_data);
255 static GstFlowReturn gst_rtp_session_send_rtcp (RTPSession * sess,
256 RTPSource * src, GstBuffer * buffer, gboolean eos, gpointer user_data);
257 static GstFlowReturn gst_rtp_session_sync_rtcp (RTPSession * sess,
258 RTPSource * src, GstBuffer * buffer, gpointer user_data);
259 static gint gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
261 static void gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data);
262 static void gst_rtp_session_request_key_unit (RTPSession * sess,
263 gboolean all_headers, gpointer user_data);
264 static GstClockTime gst_rtp_session_request_time (RTPSession * session,
267 static RTPSessionCallbacks callbacks = {
268 gst_rtp_session_process_rtp,
269 gst_rtp_session_send_rtp,
270 gst_rtp_session_sync_rtcp,
271 gst_rtp_session_send_rtcp,
272 gst_rtp_session_clock_rate,
273 gst_rtp_session_reconsider,
274 gst_rtp_session_request_key_unit,
275 gst_rtp_session_request_time
278 /* GObject vmethods */
279 static void gst_rtp_session_finalize (GObject * object);
280 static void gst_rtp_session_set_property (GObject * object, guint prop_id,
281 const GValue * value, GParamSpec * pspec);
282 static void gst_rtp_session_get_property (GObject * object, guint prop_id,
283 GValue * value, GParamSpec * pspec);
285 /* GstElement vmethods */
286 static GstStateChangeReturn gst_rtp_session_change_state (GstElement * element,
287 GstStateChange transition);
288 static GstPad *gst_rtp_session_request_new_pad (GstElement * element,
289 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
290 static void gst_rtp_session_release_pad (GstElement * element, GstPad * pad);
292 static gboolean gst_rtp_session_sink_setcaps (GstPad * pad,
293 GstRtpSession * rtpsession, GstCaps * caps);
294 static gboolean gst_rtp_session_setcaps_send_rtp (GstPad * pad,
295 GstRtpSession * rtpsession, GstCaps * caps);
297 static void gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession);
299 static guint gst_rtp_session_signals[LAST_SIGNAL] = { 0 };
302 on_new_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
304 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0,
309 on_ssrc_collision (RTPSession * session, RTPSource * src, GstRtpSession * sess)
311 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
316 on_ssrc_validated (RTPSession * session, RTPSource * src, GstRtpSession * sess)
318 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
323 on_ssrc_active (RTPSession * session, RTPSource * src, GstRtpSession * sess)
325 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0,
330 on_ssrc_sdes (RTPSession * session, RTPSource * src, GstRtpSession * sess)
335 /* convert the new SDES info into a message */
336 RTP_SESSION_LOCK (session);
337 g_object_get (src, "sdes", &s, NULL);
338 RTP_SESSION_UNLOCK (session);
340 m = gst_message_new_custom (GST_MESSAGE_ELEMENT, GST_OBJECT (sess), s);
341 gst_element_post_message (GST_ELEMENT_CAST (sess), m);
343 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0,
348 on_bye_ssrc (RTPSession * session, RTPSource * src, GstRtpSession * sess)
350 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0,
355 on_bye_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
357 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0,
362 on_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
364 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_TIMEOUT], 0,
369 on_sender_timeout (RTPSession * session, RTPSource * src, GstRtpSession * sess)
371 g_signal_emit (sess, gst_rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
375 #define gst_rtp_session_parent_class parent_class
376 G_DEFINE_TYPE (GstRtpSession, gst_rtp_session, GST_TYPE_ELEMENT);
379 gst_rtp_session_class_init (GstRtpSessionClass * klass)
381 GObjectClass *gobject_class;
382 GstElementClass *gstelement_class;
384 gobject_class = (GObjectClass *) klass;
385 gstelement_class = (GstElementClass *) klass;
387 g_type_class_add_private (klass, sizeof (GstRtpSessionPrivate));
389 gobject_class->finalize = gst_rtp_session_finalize;
390 gobject_class->set_property = gst_rtp_session_set_property;
391 gobject_class->get_property = gst_rtp_session_get_property;
394 * GstRtpSession::request-pt-map:
395 * @sess: the object which received the signal
398 * Request the payload type as #GstCaps for @pt.
400 gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP] =
401 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
402 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, request_pt_map),
403 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT, GST_TYPE_CAPS, 1,
406 * GstRtpSession::clear-pt-map:
407 * @sess: the object which received the signal
409 * Clear the cached pt-maps requested with #GstRtpSession::request-pt-map.
411 gst_rtp_session_signals[SIGNAL_CLEAR_PT_MAP] =
412 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
413 G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRtpSessionClass, clear_pt_map),
414 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
417 * GstRtpSession::on-new-ssrc:
418 * @sess: the object which received the signal
421 * Notify of a new SSRC that entered @session.
423 gst_rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
424 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
425 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_new_ssrc),
426 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
428 * GstRtpSession::on-ssrc_collision:
429 * @sess: the object which received the signal
432 * Notify when we have an SSRC collision
434 gst_rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
435 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
436 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
437 on_ssrc_collision), NULL, NULL, g_cclosure_marshal_VOID__UINT,
438 G_TYPE_NONE, 1, G_TYPE_UINT);
440 * GstRtpSession::on-ssrc_validated:
441 * @sess: the object which received the signal
444 * Notify of a new SSRC that became validated.
446 gst_rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
447 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
448 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
449 on_ssrc_validated), NULL, NULL, g_cclosure_marshal_VOID__UINT,
450 G_TYPE_NONE, 1, G_TYPE_UINT);
452 * GstRtpSession::on-ssrc_active:
453 * @sess: the object which received the signal
456 * Notify of a SSRC that is active, i.e., sending RTCP.
458 gst_rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
459 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
460 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
461 on_ssrc_active), NULL, NULL, g_cclosure_marshal_VOID__UINT,
462 G_TYPE_NONE, 1, G_TYPE_UINT);
464 * GstRtpSession::on-ssrc-sdes:
465 * @session: the object which received the signal
468 * Notify that a new SDES was received for SSRC.
470 gst_rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
471 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
472 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_ssrc_sdes),
473 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
476 * GstRtpSession::on-bye-ssrc:
477 * @sess: the object which received the signal
480 * Notify of an SSRC that became inactive because of a BYE packet.
482 gst_rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
483 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
484 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_ssrc),
485 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
487 * GstRtpSession::on-bye-timeout:
488 * @sess: the object which received the signal
491 * Notify of an SSRC that has timed out because of BYE
493 gst_rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
494 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
495 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_bye_timeout),
496 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
498 * GstRtpSession::on-timeout:
499 * @sess: the object which received the signal
502 * Notify of an SSRC that has timed out
504 gst_rtp_session_signals[SIGNAL_ON_TIMEOUT] =
505 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
506 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass, on_timeout),
507 NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
509 * GstRtpSession::on-sender-timeout:
510 * @sess: the object which received the signal
513 * Notify of a sender SSRC that has timed out and became a receiver
515 gst_rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
516 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
517 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpSessionClass,
518 on_sender_timeout), NULL, NULL, g_cclosure_marshal_VOID__UINT,
519 G_TYPE_NONE, 1, G_TYPE_UINT);
521 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
522 g_param_spec_double ("bandwidth", "Bandwidth",
523 "The bandwidth of the session in bytes per second (0 for auto-discover)",
524 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
525 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
527 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
528 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
529 "The RTCP bandwidth of the session in bytes per second "
530 "(or as a real fraction of the RTP bandwidth if < 1.0)",
531 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
532 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
534 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
535 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
536 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
537 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
538 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
540 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
541 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
542 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
543 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
544 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
546 g_object_class_install_property (gobject_class, PROP_SDES,
547 g_param_spec_boxed ("sdes", "SDES",
548 "The SDES items of this session",
549 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
551 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
552 g_param_spec_uint ("num-sources", "Num Sources",
553 "The number of sources in the session", 0, G_MAXUINT,
554 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
556 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
557 g_param_spec_uint ("num-active-sources", "Num Active Sources",
558 "The number of active sources in the session", 0, G_MAXUINT,
559 DEFAULT_NUM_ACTIVE_SOURCES,
560 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
562 g_object_class_install_property (gobject_class, PROP_INTERNAL_SESSION,
563 g_param_spec_object ("internal-session", "Internal Session",
564 "The internal RTPSession object", RTP_TYPE_SESSION,
565 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
567 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
568 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
569 "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
570 DEFAULT_USE_PIPELINE_CLOCK,
571 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
573 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
574 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
575 "Minimum interval between Regular RTCP packet (in ns)",
576 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
577 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
579 g_object_class_install_property (gobject_class, PROP_PROBATION,
580 g_param_spec_uint ("probation", "Number of probations",
581 "Consecutive packet sequence numbers to accept the source",
582 0, G_MAXUINT, DEFAULT_PROBATION,
583 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
585 gstelement_class->change_state =
586 GST_DEBUG_FUNCPTR (gst_rtp_session_change_state);
587 gstelement_class->request_new_pad =
588 GST_DEBUG_FUNCPTR (gst_rtp_session_request_new_pad);
589 gstelement_class->release_pad =
590 GST_DEBUG_FUNCPTR (gst_rtp_session_release_pad);
592 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_session_clear_pt_map);
595 gst_element_class_add_pad_template (gstelement_class,
596 gst_static_pad_template_get (&rtpsession_recv_rtp_sink_template));
597 gst_element_class_add_pad_template (gstelement_class,
598 gst_static_pad_template_get (&rtpsession_recv_rtcp_sink_template));
599 gst_element_class_add_pad_template (gstelement_class,
600 gst_static_pad_template_get (&rtpsession_send_rtp_sink_template));
603 gst_element_class_add_pad_template (gstelement_class,
604 gst_static_pad_template_get (&rtpsession_recv_rtp_src_template));
605 gst_element_class_add_pad_template (gstelement_class,
606 gst_static_pad_template_get (&rtpsession_sync_src_template));
607 gst_element_class_add_pad_template (gstelement_class,
608 gst_static_pad_template_get (&rtpsession_send_rtp_src_template));
609 gst_element_class_add_pad_template (gstelement_class,
610 gst_static_pad_template_get (&rtpsession_send_rtcp_src_template));
612 gst_element_class_set_static_metadata (gstelement_class, "RTP Session",
613 "Filter/Network/RTP",
614 "Implement an RTP session", "Wim Taymans <wim.taymans@gmail.com>");
616 GST_DEBUG_CATEGORY_INIT (gst_rtp_session_debug,
617 "rtpsession", 0, "RTP Session");
621 gst_rtp_session_init (GstRtpSession * rtpsession)
623 rtpsession->priv = GST_RTP_SESSION_GET_PRIVATE (rtpsession);
624 g_mutex_init (&rtpsession->priv->lock);
625 rtpsession->priv->sysclock = gst_system_clock_obtain ();
626 rtpsession->priv->session = rtp_session_new ();
627 rtpsession->priv->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
629 /* configure callbacks */
630 rtp_session_set_callbacks (rtpsession->priv->session, &callbacks, rtpsession);
631 /* configure signals */
632 g_signal_connect (rtpsession->priv->session, "on-new-ssrc",
633 (GCallback) on_new_ssrc, rtpsession);
634 g_signal_connect (rtpsession->priv->session, "on-ssrc-collision",
635 (GCallback) on_ssrc_collision, rtpsession);
636 g_signal_connect (rtpsession->priv->session, "on-ssrc-validated",
637 (GCallback) on_ssrc_validated, rtpsession);
638 g_signal_connect (rtpsession->priv->session, "on-ssrc-active",
639 (GCallback) on_ssrc_active, rtpsession);
640 g_signal_connect (rtpsession->priv->session, "on-ssrc-sdes",
641 (GCallback) on_ssrc_sdes, rtpsession);
642 g_signal_connect (rtpsession->priv->session, "on-bye-ssrc",
643 (GCallback) on_bye_ssrc, rtpsession);
644 g_signal_connect (rtpsession->priv->session, "on-bye-timeout",
645 (GCallback) on_bye_timeout, rtpsession);
646 g_signal_connect (rtpsession->priv->session, "on-timeout",
647 (GCallback) on_timeout, rtpsession);
648 g_signal_connect (rtpsession->priv->session, "on-sender-timeout",
649 (GCallback) on_sender_timeout, rtpsession);
650 rtpsession->priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
651 (GDestroyNotify) gst_caps_unref);
653 gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
654 gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
656 rtpsession->priv->thread_stopped = TRUE;
660 gst_rtp_session_finalize (GObject * object)
662 GstRtpSession *rtpsession;
664 rtpsession = GST_RTP_SESSION (object);
666 g_hash_table_destroy (rtpsession->priv->ptmap);
667 g_mutex_clear (&rtpsession->priv->lock);
668 g_object_unref (rtpsession->priv->sysclock);
669 g_object_unref (rtpsession->priv->session);
671 G_OBJECT_CLASS (parent_class)->finalize (object);
675 gst_rtp_session_set_property (GObject * object, guint prop_id,
676 const GValue * value, GParamSpec * pspec)
678 GstRtpSession *rtpsession;
679 GstRtpSessionPrivate *priv;
681 rtpsession = GST_RTP_SESSION (object);
682 priv = rtpsession->priv;
686 g_object_set_property (G_OBJECT (priv->session), "bandwidth", value);
688 case PROP_RTCP_FRACTION:
689 g_object_set_property (G_OBJECT (priv->session), "rtcp-fraction", value);
691 case PROP_RTCP_RR_BANDWIDTH:
692 g_object_set_property (G_OBJECT (priv->session), "rtcp-rr-bandwidth",
695 case PROP_RTCP_RS_BANDWIDTH:
696 g_object_set_property (G_OBJECT (priv->session), "rtcp-rs-bandwidth",
700 rtp_session_set_sdes_struct (priv->session, g_value_get_boxed (value));
702 case PROP_USE_PIPELINE_CLOCK:
703 priv->use_pipeline_clock = g_value_get_boolean (value);
705 case PROP_RTCP_MIN_INTERVAL:
706 g_object_set_property (G_OBJECT (priv->session), "rtcp-min-interval",
710 g_object_set_property (G_OBJECT (priv->session), "probation", value);
713 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
719 gst_rtp_session_get_property (GObject * object, guint prop_id,
720 GValue * value, GParamSpec * pspec)
722 GstRtpSession *rtpsession;
723 GstRtpSessionPrivate *priv;
725 rtpsession = GST_RTP_SESSION (object);
726 priv = rtpsession->priv;
730 g_object_get_property (G_OBJECT (priv->session), "bandwidth", value);
732 case PROP_RTCP_FRACTION:
733 g_object_get_property (G_OBJECT (priv->session), "rtcp-fraction", value);
735 case PROP_RTCP_RR_BANDWIDTH:
736 g_object_get_property (G_OBJECT (priv->session), "rtcp-rr-bandwidth",
739 case PROP_RTCP_RS_BANDWIDTH:
740 g_object_get_property (G_OBJECT (priv->session), "rtcp-rs-bandwidth",
744 g_value_take_boxed (value, rtp_session_get_sdes_struct (priv->session));
746 case PROP_NUM_SOURCES:
747 g_value_set_uint (value, rtp_session_get_num_sources (priv->session));
749 case PROP_NUM_ACTIVE_SOURCES:
750 g_value_set_uint (value,
751 rtp_session_get_num_active_sources (priv->session));
753 case PROP_INTERNAL_SESSION:
754 g_value_set_object (value, priv->session);
756 case PROP_USE_PIPELINE_CLOCK:
757 g_value_set_boolean (value, priv->use_pipeline_clock);
759 case PROP_RTCP_MIN_INTERVAL:
760 g_object_get_property (G_OBJECT (priv->session), "rtcp-min-interval",
764 g_object_get_property (G_OBJECT (priv->session), "probation", value);
767 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
773 get_current_times (GstRtpSession * rtpsession, GstClockTime * running_time,
778 GstClockTime base_time, rt, clock_time;
780 GST_OBJECT_LOCK (rtpsession);
781 if ((clock = GST_ELEMENT_CLOCK (rtpsession))) {
782 base_time = GST_ELEMENT_CAST (rtpsession)->base_time;
783 gst_object_ref (clock);
784 GST_OBJECT_UNLOCK (rtpsession);
786 /* get current clock time and convert to running time */
787 clock_time = gst_clock_get_time (clock);
788 rt = clock_time - base_time;
790 if (rtpsession->priv->use_pipeline_clock) {
795 /* get current NTP time */
796 g_get_current_time (¤t);
797 ntpns = GST_TIMEVAL_TO_TIME (current);
800 /* add constant to convert from 1970 based time to 1900 based time */
801 ntpns += (2208988800LL * GST_SECOND);
803 gst_object_unref (clock);
805 GST_OBJECT_UNLOCK (rtpsession);
816 rtcp_thread (GstRtpSession * rtpsession)
819 GstClockTime current_time;
820 GstClockTime next_timeout;
822 GstClockTime running_time;
826 GST_DEBUG_OBJECT (rtpsession, "entering RTCP thread");
828 GST_RTP_SESSION_LOCK (rtpsession);
830 sysclock = rtpsession->priv->sysclock;
831 current_time = gst_clock_get_time (sysclock);
833 session = rtpsession->priv->session;
835 GST_DEBUG_OBJECT (rtpsession, "starting at %" GST_TIME_FORMAT,
836 GST_TIME_ARGS (current_time));
837 session->start_time = current_time;
839 while (!rtpsession->priv->stop_thread) {
842 /* get initial estimate */
843 next_timeout = rtp_session_next_timeout (session, current_time);
845 GST_DEBUG_OBJECT (rtpsession, "next check time %" GST_TIME_FORMAT,
846 GST_TIME_ARGS (next_timeout));
848 /* leave if no more timeouts, the session ended */
849 if (next_timeout == GST_CLOCK_TIME_NONE)
852 id = rtpsession->priv->id =
853 gst_clock_new_single_shot_id (sysclock, next_timeout);
854 GST_RTP_SESSION_UNLOCK (rtpsession);
856 res = gst_clock_id_wait (id, NULL);
858 GST_RTP_SESSION_LOCK (rtpsession);
859 gst_clock_id_unref (id);
860 rtpsession->priv->id = NULL;
862 if (rtpsession->priv->stop_thread)
865 /* update current time */
866 current_time = gst_clock_get_time (sysclock);
868 /* get current NTP time */
869 get_current_times (rtpsession, &running_time, &ntpnstime);
871 /* we get unlocked because we need to perform reconsideration, don't perform
872 * the timeout but get a new reporting estimate. */
873 GST_DEBUG_OBJECT (rtpsession, "unlocked %d, current %" GST_TIME_FORMAT,
874 res, GST_TIME_ARGS (current_time));
876 /* perform actions, we ignore result. Release lock because it might push. */
877 GST_RTP_SESSION_UNLOCK (rtpsession);
878 rtp_session_on_timeout (session, current_time, ntpnstime, running_time);
879 GST_RTP_SESSION_LOCK (rtpsession);
881 /* mark the thread as stopped now */
882 rtpsession->priv->thread_stopped = TRUE;
883 GST_RTP_SESSION_UNLOCK (rtpsession);
885 GST_DEBUG_OBJECT (rtpsession, "leaving RTCP thread");
889 start_rtcp_thread (GstRtpSession * rtpsession)
891 GError *error = NULL;
894 GST_DEBUG_OBJECT (rtpsession, "starting RTCP thread");
896 GST_RTP_SESSION_LOCK (rtpsession);
897 rtpsession->priv->stop_thread = FALSE;
898 if (rtpsession->priv->thread_stopped) {
899 /* if the thread stopped, and we still have a handle to the thread, join it
900 * now. We can safely join with the lock held, the thread will not take it
902 if (rtpsession->priv->thread)
903 g_thread_join (rtpsession->priv->thread);
904 /* only create a new thread if the old one was stopped. Otherwise we can
905 * just reuse the currently running one. */
906 rtpsession->priv->thread = g_thread_try_new ("rtpsession-rtcp-thread",
907 (GThreadFunc) rtcp_thread, rtpsession, &error);
908 rtpsession->priv->thread_stopped = FALSE;
910 GST_RTP_SESSION_UNLOCK (rtpsession);
914 GST_DEBUG_OBJECT (rtpsession, "failed to start thread, %s", error->message);
915 g_error_free (error);
923 stop_rtcp_thread (GstRtpSession * rtpsession)
925 GST_DEBUG_OBJECT (rtpsession, "stopping RTCP thread");
927 GST_RTP_SESSION_LOCK (rtpsession);
928 rtpsession->priv->stop_thread = TRUE;
929 if (rtpsession->priv->id)
930 gst_clock_id_unschedule (rtpsession->priv->id);
931 GST_RTP_SESSION_UNLOCK (rtpsession);
935 join_rtcp_thread (GstRtpSession * rtpsession)
937 GST_RTP_SESSION_LOCK (rtpsession);
938 /* don't try to join when we have no thread */
939 if (rtpsession->priv->thread != NULL) {
940 GST_DEBUG_OBJECT (rtpsession, "joining RTCP thread");
941 GST_RTP_SESSION_UNLOCK (rtpsession);
943 g_thread_join (rtpsession->priv->thread);
945 GST_RTP_SESSION_LOCK (rtpsession);
946 /* after the join, take the lock and clear the thread structure. The caller
947 * is supposed to not concurrently call start and join. */
948 rtpsession->priv->thread = NULL;
950 GST_RTP_SESSION_UNLOCK (rtpsession);
953 static GstStateChangeReturn
954 gst_rtp_session_change_state (GstElement * element, GstStateChange transition)
956 GstStateChangeReturn res;
957 GstRtpSession *rtpsession;
959 rtpsession = GST_RTP_SESSION (element);
961 switch (transition) {
962 case GST_STATE_CHANGE_NULL_TO_READY:
964 case GST_STATE_CHANGE_READY_TO_PAUSED:
966 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
968 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
969 case GST_STATE_CHANGE_PAUSED_TO_READY:
970 /* no need to join yet, we might want to continue later. Also, the
971 * dataflow could block downstream so that a join could just block
973 stop_rtcp_thread (rtpsession);
979 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
981 switch (transition) {
982 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
983 if (!start_rtcp_thread (rtpsession))
986 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
988 case GST_STATE_CHANGE_PAUSED_TO_READY:
989 /* downstream is now releasing the dataflow and we can join. */
990 join_rtcp_thread (rtpsession);
992 case GST_STATE_CHANGE_READY_TO_NULL:
1002 return GST_STATE_CHANGE_FAILURE;
1007 return_true (gpointer key, gpointer value, gpointer user_data)
1013 gst_rtp_session_clear_pt_map (GstRtpSession * rtpsession)
1015 g_hash_table_foreach_remove (rtpsession->priv->ptmap, return_true, NULL);
1018 /* called when the session manager has an RTP packet or a list of packets
1019 * ready for further processing */
1020 static GstFlowReturn
1021 gst_rtp_session_process_rtp (RTPSession * sess, RTPSource * src,
1022 GstBuffer * buffer, gpointer user_data)
1024 GstFlowReturn result;
1025 GstRtpSession *rtpsession;
1028 rtpsession = GST_RTP_SESSION (user_data);
1030 GST_RTP_SESSION_LOCK (rtpsession);
1031 if ((rtp_src = rtpsession->recv_rtp_src))
1032 gst_object_ref (rtp_src);
1033 GST_RTP_SESSION_UNLOCK (rtpsession);
1036 GST_LOG_OBJECT (rtpsession, "pushing received RTP packet");
1037 result = gst_pad_push (rtp_src, buffer);
1038 gst_object_unref (rtp_src);
1040 GST_DEBUG_OBJECT (rtpsession, "dropping received RTP packet");
1041 gst_buffer_unref (buffer);
1042 result = GST_FLOW_OK;
1047 /* called when the session manager has an RTP packet ready for further
1049 static GstFlowReturn
1050 gst_rtp_session_send_rtp (RTPSession * sess, RTPSource * src,
1051 gpointer data, gpointer user_data)
1053 GstFlowReturn result;
1054 GstRtpSession *rtpsession;
1057 rtpsession = GST_RTP_SESSION (user_data);
1059 GST_RTP_SESSION_LOCK (rtpsession);
1060 if ((rtp_src = rtpsession->send_rtp_src))
1061 gst_object_ref (rtp_src);
1062 GST_RTP_SESSION_UNLOCK (rtpsession);
1065 if (GST_IS_BUFFER (data)) {
1066 GST_LOG_OBJECT (rtpsession, "sending RTP packet");
1067 result = gst_pad_push (rtp_src, GST_BUFFER_CAST (data));
1069 GST_LOG_OBJECT (rtpsession, "sending RTP list");
1070 result = gst_pad_push_list (rtp_src, GST_BUFFER_LIST_CAST (data));
1072 gst_object_unref (rtp_src);
1074 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1075 result = GST_FLOW_OK;
1080 /* called when the session manager has an RTCP packet ready for further
1081 * sending. The eos flag is set when an EOS event should be sent downstream as
1083 static GstFlowReturn
1084 gst_rtp_session_send_rtcp (RTPSession * sess, RTPSource * src,
1085 GstBuffer * buffer, gboolean eos, gpointer user_data)
1087 GstFlowReturn result;
1088 GstRtpSession *rtpsession;
1091 rtpsession = GST_RTP_SESSION (user_data);
1093 GST_RTP_SESSION_LOCK (rtpsession);
1094 if (rtpsession->priv->stop_thread)
1097 if ((rtcp_src = rtpsession->send_rtcp_src)) {
1100 gst_object_ref (rtcp_src);
1101 GST_RTP_SESSION_UNLOCK (rtpsession);
1103 /* set rtcp caps on output pad */
1104 if (!(caps = gst_pad_get_current_caps (rtcp_src))) {
1105 caps = gst_caps_new_empty_simple ("application/x-rtcp");
1106 gst_pad_set_caps (rtcp_src, caps);
1108 gst_caps_unref (caps);
1110 GST_LOG_OBJECT (rtpsession, "sending RTCP");
1111 result = gst_pad_push (rtcp_src, buffer);
1113 /* we have to send EOS after this packet */
1115 GST_LOG_OBJECT (rtpsession, "sending EOS");
1116 gst_pad_push_event (rtcp_src, gst_event_new_eos ());
1118 gst_object_unref (rtcp_src);
1120 GST_RTP_SESSION_UNLOCK (rtpsession);
1122 GST_DEBUG_OBJECT (rtpsession, "not sending RTCP, no output pad");
1123 gst_buffer_unref (buffer);
1124 result = GST_FLOW_OK;
1131 GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1132 gst_buffer_unref (buffer);
1133 GST_RTP_SESSION_UNLOCK (rtpsession);
1138 /* called when the session manager has an SR RTCP packet ready for handling
1139 * inter stream synchronisation */
1140 static GstFlowReturn
1141 gst_rtp_session_sync_rtcp (RTPSession * sess, RTPSource * src,
1142 GstBuffer * buffer, gpointer user_data)
1144 GstFlowReturn result;
1145 GstRtpSession *rtpsession;
1148 rtpsession = GST_RTP_SESSION (user_data);
1150 GST_RTP_SESSION_LOCK (rtpsession);
1151 if (rtpsession->priv->stop_thread)
1154 if ((sync_src = rtpsession->sync_src)) {
1157 gst_object_ref (sync_src);
1158 GST_RTP_SESSION_UNLOCK (rtpsession);
1160 /* set rtcp caps on output pad */
1161 if (!(caps = gst_pad_get_current_caps (sync_src))) {
1162 caps = gst_caps_new_empty_simple ("application/x-rtcp");
1163 gst_pad_set_caps (sync_src, caps);
1165 gst_caps_unref (caps);
1167 GST_LOG_OBJECT (rtpsession, "sending Sync RTCP");
1168 result = gst_pad_push (sync_src, buffer);
1169 gst_object_unref (sync_src);
1171 GST_RTP_SESSION_UNLOCK (rtpsession);
1173 GST_DEBUG_OBJECT (rtpsession, "not sending Sync RTCP, no output pad");
1174 gst_buffer_unref (buffer);
1175 result = GST_FLOW_OK;
1182 GST_DEBUG_OBJECT (rtpsession, "we are stopping");
1183 gst_buffer_unref (buffer);
1184 GST_RTP_SESSION_UNLOCK (rtpsession);
1190 gst_rtp_session_cache_caps (GstRtpSession * rtpsession, GstCaps * caps)
1192 GstRtpSessionPrivate *priv;
1193 const GstStructure *s;
1196 priv = rtpsession->priv;
1198 GST_DEBUG_OBJECT (rtpsession, "parsing caps");
1200 s = gst_caps_get_structure (caps, 0);
1201 if (!gst_structure_get_int (s, "payload", &payload))
1204 if (g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (payload)))
1207 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (payload),
1208 gst_caps_ref (caps));
1212 gst_rtp_session_get_caps_for_pt (GstRtpSession * rtpsession, guint payload)
1214 GstCaps *caps = NULL;
1215 GValue args[2] = { {0}, {0} };
1218 GST_RTP_SESSION_LOCK (rtpsession);
1219 caps = g_hash_table_lookup (rtpsession->priv->ptmap,
1220 GINT_TO_POINTER (payload));
1222 gst_caps_ref (caps);
1226 /* not found in the cache, try to get it with a signal */
1227 g_value_init (&args[0], GST_TYPE_ELEMENT);
1228 g_value_set_object (&args[0], rtpsession);
1229 g_value_init (&args[1], G_TYPE_UINT);
1230 g_value_set_uint (&args[1], payload);
1232 g_value_init (&ret, GST_TYPE_CAPS);
1233 g_value_set_boxed (&ret, NULL);
1235 GST_RTP_SESSION_UNLOCK (rtpsession);
1237 g_signal_emitv (args, gst_rtp_session_signals[SIGNAL_REQUEST_PT_MAP], 0,
1240 GST_RTP_SESSION_LOCK (rtpsession);
1242 g_value_unset (&args[0]);
1243 g_value_unset (&args[1]);
1244 caps = (GstCaps *) g_value_dup_boxed (&ret);
1245 g_value_unset (&ret);
1249 gst_rtp_session_cache_caps (rtpsession, caps);
1252 GST_RTP_SESSION_UNLOCK (rtpsession);
1258 GST_DEBUG_OBJECT (rtpsession, "could not get caps");
1263 /* called when the session manager needs the clock rate */
1265 gst_rtp_session_clock_rate (RTPSession * sess, guint8 payload,
1269 GstRtpSession *rtpsession;
1271 const GstStructure *s;
1273 rtpsession = GST_RTP_SESSION_CAST (user_data);
1275 caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1280 s = gst_caps_get_structure (caps, 0);
1281 if (!gst_structure_get_int (s, "clock-rate", &result))
1284 gst_caps_unref (caps);
1286 GST_DEBUG_OBJECT (rtpsession, "parsed clock-rate %d", result);
1295 gst_caps_unref (caps);
1296 GST_DEBUG_OBJECT (rtpsession, "No clock-rate in caps!");
1301 /* called when the session manager asks us to reconsider the timeout */
1303 gst_rtp_session_reconsider (RTPSession * sess, gpointer user_data)
1305 GstRtpSession *rtpsession;
1307 rtpsession = GST_RTP_SESSION_CAST (user_data);
1309 GST_RTP_SESSION_LOCK (rtpsession);
1310 GST_DEBUG_OBJECT (rtpsession, "unlock timer for reconsideration");
1311 if (rtpsession->priv->id)
1312 gst_clock_id_unschedule (rtpsession->priv->id);
1313 GST_RTP_SESSION_UNLOCK (rtpsession);
1317 gst_rtp_session_event_recv_rtp_sink (GstPad * pad, GstObject * parent,
1320 GstRtpSession *rtpsession;
1321 gboolean ret = FALSE;
1323 rtpsession = GST_RTP_SESSION (parent);
1325 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1326 GST_EVENT_TYPE_NAME (event));
1328 switch (GST_EVENT_TYPE (event)) {
1329 case GST_EVENT_CAPS:
1334 gst_event_parse_caps (event, &caps);
1335 gst_rtp_session_sink_setcaps (pad, rtpsession, caps);
1336 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1339 case GST_EVENT_FLUSH_STOP:
1340 gst_segment_init (&rtpsession->recv_rtp_seg, GST_FORMAT_UNDEFINED);
1341 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1343 case GST_EVENT_SEGMENT:
1345 GstSegment *segment, in_segment;
1347 segment = &rtpsession->recv_rtp_seg;
1349 /* the newsegment event is needed to convert the RTP timestamp to
1350 * running_time, which is needed to generate a mapping from RTP to NTP
1351 * timestamps in SR reports */
1352 gst_event_copy_segment (event, &in_segment);
1353 GST_DEBUG_OBJECT (rtpsession, "received segment %" GST_SEGMENT_FORMAT,
1356 /* accept upstream */
1357 gst_segment_copy_into (&in_segment, segment);
1359 /* push event forward */
1360 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1364 ret = gst_pad_push_event (rtpsession->recv_rtp_src, event);
1373 gst_rtp_session_request_remote_key_unit (GstRtpSession * rtpsession,
1374 guint32 ssrc, guint payload, gboolean all_headers, gint count)
1378 caps = gst_rtp_session_get_caps_for_pt (rtpsession, payload);
1381 const GstStructure *s = gst_caps_get_structure (caps, 0);
1385 pli = gst_structure_has_field (s, "rtcp-fb-nack-pli");
1386 fir = gst_structure_has_field (s, "rtcp-fb-ccm-fir") && all_headers;
1388 /* Google Talk uses FIR for repair, so send it even if we just want a
1391 gst_structure_has_field (s, "rtcp-fb-x-gstreamer-fir-as-repair"))
1394 gst_caps_unref (caps);
1397 return rtp_session_request_key_unit (rtpsession->priv->session, ssrc,
1398 gst_clock_get_time (rtpsession->priv->sysclock), fir, count);
1405 gst_rtp_session_event_recv_rtp_src (GstPad * pad, GstObject * parent,
1408 GstRtpSession *rtpsession;
1409 gboolean forward = TRUE;
1410 gboolean ret = TRUE;
1411 const GstStructure *s;
1415 rtpsession = GST_RTP_SESSION (parent);
1417 switch (GST_EVENT_TYPE (event)) {
1418 case GST_EVENT_CUSTOM_UPSTREAM:
1419 s = gst_event_get_structure (event);
1420 if (gst_structure_has_name (s, "GstForceKeyUnit") &&
1421 gst_structure_get_uint (s, "ssrc", &ssrc) &&
1422 gst_structure_get_uint (s, "payload", &pt)) {
1423 gboolean all_headers = FALSE;
1426 gst_structure_get_boolean (s, "all-headers", &all_headers);
1427 if (gst_structure_get_int (s, "count", &count) && count < 0)
1428 count += G_MAXINT; /* Make sure count is positive if present */
1429 if (gst_rtp_session_request_remote_key_unit (rtpsession, ssrc, pt,
1430 all_headers, count))
1439 GstPad *recv_rtp_sink = NULL;
1440 GST_RTP_SESSION_LOCK (rtpsession);
1441 if (rtpsession->recv_rtp_sink)
1442 recv_rtp_sink = gst_object_ref (rtpsession->recv_rtp_sink);
1443 GST_RTP_SESSION_UNLOCK (rtpsession);
1445 if (recv_rtp_sink) {
1446 ret = gst_pad_push_event (recv_rtp_sink, event);
1447 gst_object_unref (recv_rtp_sink);
1449 gst_event_unref (event);
1451 gst_event_unref (event);
1458 static GstIterator *
1459 gst_rtp_session_iterate_internal_links (GstPad * pad, GstObject * parent)
1461 GstRtpSession *rtpsession;
1462 GstPad *otherpad = NULL;
1463 GstIterator *it = NULL;
1465 rtpsession = GST_RTP_SESSION (parent);
1467 GST_RTP_SESSION_LOCK (rtpsession);
1468 if (pad == rtpsession->recv_rtp_src) {
1469 otherpad = gst_object_ref (rtpsession->recv_rtp_sink);
1470 } else if (pad == rtpsession->recv_rtp_sink) {
1471 otherpad = gst_object_ref (rtpsession->recv_rtp_src);
1472 } else if (pad == rtpsession->send_rtp_src) {
1473 otherpad = gst_object_ref (rtpsession->send_rtp_sink);
1474 } else if (pad == rtpsession->send_rtp_sink) {
1475 otherpad = gst_object_ref (rtpsession->send_rtp_src);
1477 GST_RTP_SESSION_UNLOCK (rtpsession);
1480 GValue val = { 0, };
1482 g_value_init (&val, GST_TYPE_PAD);
1483 g_value_set_object (&val, otherpad);
1484 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
1485 g_value_unset (&val);
1486 gst_object_unref (otherpad);
1493 gst_rtp_session_sink_setcaps (GstPad * pad, GstRtpSession * rtpsession,
1496 GST_RTP_SESSION_LOCK (rtpsession);
1497 gst_rtp_session_cache_caps (rtpsession, caps);
1498 GST_RTP_SESSION_UNLOCK (rtpsession);
1503 /* receive a packet from a sender, send it to the RTP session manager and
1504 * forward the packet on the rtp_src pad
1506 static GstFlowReturn
1507 gst_rtp_session_chain_recv_rtp (GstPad * pad, GstObject * parent,
1510 GstRtpSession *rtpsession;
1511 GstRtpSessionPrivate *priv;
1513 GstClockTime current_time, running_time;
1514 GstClockTime timestamp;
1516 rtpsession = GST_RTP_SESSION (parent);
1517 priv = rtpsession->priv;
1519 GST_LOG_OBJECT (rtpsession, "received RTP packet");
1521 /* get NTP time when this packet was captured, this depends on the timestamp. */
1522 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1523 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1524 /* convert to running time using the segment values */
1526 gst_segment_to_running_time (&rtpsession->recv_rtp_seg, GST_FORMAT_TIME,
1529 get_current_times (rtpsession, &running_time, NULL);
1531 current_time = gst_clock_get_time (priv->sysclock);
1533 ret = rtp_session_process_rtp (priv->session, buffer, current_time,
1535 if (ret != GST_FLOW_OK)
1545 GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1546 gst_flow_get_name (ret));
1552 gst_rtp_session_event_recv_rtcp_sink (GstPad * pad, GstObject * parent,
1555 GstRtpSession *rtpsession;
1556 gboolean ret = FALSE;
1558 rtpsession = GST_RTP_SESSION (parent);
1560 GST_DEBUG_OBJECT (rtpsession, "received event %s",
1561 GST_EVENT_TYPE_NAME (event));
1563 switch (GST_EVENT_TYPE (event)) {
1565 ret = gst_pad_push_event (rtpsession->sync_src, event);
1572 /* Receive an RTCP packet from a sender, send it to the RTP session manager and
1573 * forward the SR packets to the sync_src pad.
1575 static GstFlowReturn
1576 gst_rtp_session_chain_recv_rtcp (GstPad * pad, GstObject * parent,
1579 GstRtpSession *rtpsession;
1580 GstRtpSessionPrivate *priv;
1581 GstClockTime current_time;
1584 rtpsession = GST_RTP_SESSION (parent);
1585 priv = rtpsession->priv;
1587 GST_LOG_OBJECT (rtpsession, "received RTCP packet");
1589 current_time = gst_clock_get_time (priv->sysclock);
1590 get_current_times (rtpsession, NULL, &ntpnstime);
1592 rtp_session_process_rtcp (priv->session, buffer, current_time, ntpnstime);
1594 return GST_FLOW_OK; /* always return OK */
1598 gst_rtp_session_query_send_rtcp_src (GstPad * pad, GstObject * parent,
1601 GstRtpSession *rtpsession;
1602 gboolean ret = FALSE;
1604 rtpsession = GST_RTP_SESSION (parent);
1606 GST_DEBUG_OBJECT (rtpsession, "received QUERY %s",
1607 GST_QUERY_TYPE_NAME (query));
1609 switch (GST_QUERY_TYPE (query)) {
1610 case GST_QUERY_LATENCY:
1612 /* use the defaults for the latency query. */
1613 gst_query_set_latency (query, FALSE, 0, -1);
1616 /* other queries simply fail for now */
1624 gst_rtp_session_event_send_rtcp_src (GstPad * pad, GstObject * parent,
1627 GstRtpSession *rtpsession;
1628 gboolean ret = TRUE;
1630 rtpsession = GST_RTP_SESSION (parent);
1631 GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
1632 GST_EVENT_TYPE_NAME (event));
1634 switch (GST_EVENT_TYPE (event)) {
1635 case GST_EVENT_SEEK:
1636 case GST_EVENT_LATENCY:
1637 gst_event_unref (event);
1641 /* other events simply fail for now */
1642 gst_event_unref (event);
1652 gst_rtp_session_event_send_rtp_sink (GstPad * pad, GstObject * parent,
1655 GstRtpSession *rtpsession;
1656 gboolean ret = FALSE;
1658 rtpsession = GST_RTP_SESSION (parent);
1660 GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
1661 GST_EVENT_TYPE_NAME (event));
1663 switch (GST_EVENT_TYPE (event)) {
1664 case GST_EVENT_CAPS:
1669 gst_event_parse_caps (event, &caps);
1670 gst_rtp_session_setcaps_send_rtp (pad, rtpsession, caps);
1671 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1674 case GST_EVENT_FLUSH_STOP:
1675 gst_segment_init (&rtpsession->send_rtp_seg, GST_FORMAT_UNDEFINED);
1676 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1678 case GST_EVENT_SEGMENT:{
1679 GstSegment *segment, in_segment;
1681 segment = &rtpsession->send_rtp_seg;
1683 /* the newsegment event is needed to convert the RTP timestamp to
1684 * running_time, which is needed to generate a mapping from RTP to NTP
1685 * timestamps in SR reports */
1686 gst_event_copy_segment (event, &in_segment);
1687 GST_DEBUG_OBJECT (rtpsession, "received segment %" GST_SEGMENT_FORMAT,
1690 /* accept upstream */
1691 gst_segment_copy_into (&in_segment, segment);
1693 /* push event forward */
1694 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1697 case GST_EVENT_EOS:{
1698 GstClockTime current_time;
1700 /* push downstream FIXME, we are not supposed to leave the session just
1701 * because we stop sending. */
1702 ret = gst_pad_push_event (rtpsession->send_rtp_src, event);
1703 current_time = gst_clock_get_time (rtpsession->priv->sysclock);
1704 GST_DEBUG_OBJECT (rtpsession, "scheduling BYE message");
1705 rtp_session_schedule_bye (rtpsession->priv->session, "End of stream",
1710 GstPad *send_rtp_src = NULL;
1711 GST_RTP_SESSION_LOCK (rtpsession);
1712 if (rtpsession->send_rtp_src)
1713 send_rtp_src = gst_object_ref (rtpsession->send_rtp_src);
1714 GST_RTP_SESSION_UNLOCK (rtpsession);
1717 ret = gst_pad_push_event (send_rtp_src, event);
1718 gst_object_unref (send_rtp_src);
1720 gst_event_unref (event);
1730 gst_rtp_session_event_send_rtp_src (GstPad * pad, GstObject * parent,
1733 GstRtpSession *rtpsession;
1734 gboolean ret = FALSE;
1736 rtpsession = GST_RTP_SESSION (parent);
1738 GST_DEBUG_OBJECT (rtpsession, "received EVENT %s",
1739 GST_EVENT_TYPE_NAME (event));
1741 switch (GST_EVENT_TYPE (event)) {
1742 case GST_EVENT_LATENCY:
1743 /* save the latency, we need this to know when an RTP packet will be
1744 * rendered by the sink */
1745 gst_event_parse_latency (event, &rtpsession->priv->send_latency);
1747 ret = gst_pad_event_default (pad, parent, event);
1750 ret = gst_pad_event_default (pad, parent, event);
1757 gst_rtp_session_getcaps_send_rtp (GstPad * pad, GstRtpSession * rtpsession,
1760 GstRtpSessionPrivate *priv;
1762 GstStructure *s1, *s2;
1765 priv = rtpsession->priv;
1767 ssrc = rtp_session_get_internal_ssrc (priv->session);
1769 /* we can basically accept anything but we prefer to receive packets with our
1770 * internal SSRC so that we don't have to patch it. Create a structure with
1771 * the SSRC and another one without. */
1772 s1 = gst_structure_new ("application/x-rtp", "ssrc", G_TYPE_UINT, ssrc, NULL);
1773 s2 = gst_structure_new_empty ("application/x-rtp");
1775 result = gst_caps_new_full (s1, s2, NULL);
1778 GstCaps *caps = result;
1780 result = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
1781 gst_caps_unref (caps);
1784 GST_DEBUG_OBJECT (rtpsession, "getting caps %" GST_PTR_FORMAT, result);
1790 gst_rtp_session_query_send_rtp (GstPad * pad, GstObject * parent,
1793 gboolean res = FALSE;
1794 GstRtpSession *rtpsession;
1796 rtpsession = GST_RTP_SESSION (parent);
1798 switch (GST_QUERY_TYPE (query)) {
1799 case GST_QUERY_CAPS:
1801 GstCaps *filter, *caps;
1803 gst_query_parse_caps (query, &filter);
1804 caps = gst_rtp_session_getcaps_send_rtp (pad, rtpsession, filter);
1805 gst_query_set_caps_result (query, caps);
1806 gst_caps_unref (caps);
1811 res = gst_pad_query_default (pad, parent, query);
1819 gst_rtp_session_setcaps_send_rtp (GstPad * pad, GstRtpSession * rtpsession,
1822 GstRtpSessionPrivate *priv;
1823 GstStructure *s = gst_caps_get_structure (caps, 0);
1826 priv = rtpsession->priv;
1828 if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
1829 GST_DEBUG_OBJECT (rtpsession, "setting internal SSRC to %08x", ssrc);
1830 rtp_session_set_internal_ssrc (priv->session, ssrc);
1832 rtp_session_update_send_caps (priv->session, caps);
1837 /* Recieve an RTP packet or a list of packets to be send to the receivers,
1838 * send to RTP session manager and forward to send_rtp_src.
1840 static GstFlowReturn
1841 gst_rtp_session_chain_send_rtp_common (GstRtpSession * rtpsession,
1842 gpointer data, gboolean is_list)
1844 GstRtpSessionPrivate *priv;
1846 GstClockTime timestamp, running_time;
1847 GstClockTime current_time;
1849 priv = rtpsession->priv;
1851 GST_LOG_OBJECT (rtpsession, "received RTP %s", is_list ? "list" : "packet");
1853 /* get NTP time when this packet was captured, this depends on the timestamp. */
1855 GstBuffer *buffer = NULL;
1857 /* All groups in an list have the same timestamp.
1858 * So, just take it from the first group. */
1859 buffer = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0);
1861 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1865 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (data));
1868 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1869 /* convert to running time using the segment start value. */
1871 gst_segment_to_running_time (&rtpsession->send_rtp_seg, GST_FORMAT_TIME,
1873 running_time += priv->send_latency;
1879 current_time = gst_clock_get_time (priv->sysclock);
1880 ret = rtp_session_send_rtp (priv->session, data, is_list, current_time,
1882 if (ret != GST_FLOW_OK)
1892 GST_DEBUG_OBJECT (rtpsession, "process returned %s",
1893 gst_flow_get_name (ret));
1898 static GstFlowReturn
1899 gst_rtp_session_chain_send_rtp (GstPad * pad, GstObject * parent,
1902 GstRtpSession *rtpsession = GST_RTP_SESSION (parent);
1904 return gst_rtp_session_chain_send_rtp_common (rtpsession, buffer, FALSE);
1907 static GstFlowReturn
1908 gst_rtp_session_chain_send_rtp_list (GstPad * pad, GstObject * parent,
1909 GstBufferList * list)
1911 GstRtpSession *rtpsession = GST_RTP_SESSION (parent);
1913 return gst_rtp_session_chain_send_rtp_common (rtpsession, list, TRUE);
1916 /* Create sinkpad to receive RTP packets from senders. This will also create a
1917 * srcpad for the RTP packets.
1920 create_recv_rtp_sink (GstRtpSession * rtpsession)
1922 GST_DEBUG_OBJECT (rtpsession, "creating RTP sink pad");
1924 rtpsession->recv_rtp_sink =
1925 gst_pad_new_from_static_template (&rtpsession_recv_rtp_sink_template,
1927 gst_pad_set_chain_function (rtpsession->recv_rtp_sink,
1928 gst_rtp_session_chain_recv_rtp);
1929 gst_pad_set_event_function (rtpsession->recv_rtp_sink,
1930 gst_rtp_session_event_recv_rtp_sink);
1931 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_sink,
1932 gst_rtp_session_iterate_internal_links);
1933 gst_pad_set_active (rtpsession->recv_rtp_sink, TRUE);
1934 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1935 rtpsession->recv_rtp_sink);
1937 GST_DEBUG_OBJECT (rtpsession, "creating RTP src pad");
1938 rtpsession->recv_rtp_src =
1939 gst_pad_new_from_static_template (&rtpsession_recv_rtp_src_template,
1941 gst_pad_set_event_function (rtpsession->recv_rtp_src,
1942 gst_rtp_session_event_recv_rtp_src);
1943 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtp_src,
1944 gst_rtp_session_iterate_internal_links);
1945 gst_pad_use_fixed_caps (rtpsession->recv_rtp_src);
1946 gst_pad_set_active (rtpsession->recv_rtp_src, TRUE);
1947 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->recv_rtp_src);
1949 return rtpsession->recv_rtp_sink;
1952 /* Remove sinkpad to receive RTP packets from senders. This will also remove
1953 * the srcpad for the RTP packets.
1956 remove_recv_rtp_sink (GstRtpSession * rtpsession)
1958 GST_DEBUG_OBJECT (rtpsession, "removing RTP sink pad");
1960 /* deactivate from source to sink */
1961 gst_pad_set_active (rtpsession->recv_rtp_src, FALSE);
1962 gst_pad_set_active (rtpsession->recv_rtp_sink, FALSE);
1965 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1966 rtpsession->recv_rtp_sink);
1967 rtpsession->recv_rtp_sink = NULL;
1969 GST_DEBUG_OBJECT (rtpsession, "removing RTP src pad");
1970 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
1971 rtpsession->recv_rtp_src);
1972 rtpsession->recv_rtp_src = NULL;
1975 /* Create a sinkpad to receive RTCP messages from senders, this will also create a
1976 * sync_src pad for the SR packets.
1979 create_recv_rtcp_sink (GstRtpSession * rtpsession)
1981 GST_DEBUG_OBJECT (rtpsession, "creating RTCP sink pad");
1983 rtpsession->recv_rtcp_sink =
1984 gst_pad_new_from_static_template (&rtpsession_recv_rtcp_sink_template,
1986 gst_pad_set_chain_function (rtpsession->recv_rtcp_sink,
1987 gst_rtp_session_chain_recv_rtcp);
1988 gst_pad_set_event_function (rtpsession->recv_rtcp_sink,
1989 gst_rtp_session_event_recv_rtcp_sink);
1990 gst_pad_set_iterate_internal_links_function (rtpsession->recv_rtcp_sink,
1991 gst_rtp_session_iterate_internal_links);
1992 gst_pad_set_active (rtpsession->recv_rtcp_sink, TRUE);
1993 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
1994 rtpsession->recv_rtcp_sink);
1996 GST_DEBUG_OBJECT (rtpsession, "creating sync src pad");
1997 rtpsession->sync_src =
1998 gst_pad_new_from_static_template (&rtpsession_sync_src_template,
2000 gst_pad_set_iterate_internal_links_function (rtpsession->sync_src,
2001 gst_rtp_session_iterate_internal_links);
2002 gst_pad_use_fixed_caps (rtpsession->sync_src);
2003 gst_pad_set_active (rtpsession->sync_src, TRUE);
2004 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
2006 return rtpsession->recv_rtcp_sink;
2010 remove_recv_rtcp_sink (GstRtpSession * rtpsession)
2012 GST_DEBUG_OBJECT (rtpsession, "removing RTCP sink pad");
2014 gst_pad_set_active (rtpsession->sync_src, FALSE);
2015 gst_pad_set_active (rtpsession->recv_rtcp_sink, FALSE);
2017 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2018 rtpsession->recv_rtcp_sink);
2019 rtpsession->recv_rtcp_sink = NULL;
2021 GST_DEBUG_OBJECT (rtpsession, "removing sync src pad");
2022 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->sync_src);
2023 rtpsession->sync_src = NULL;
2026 /* Create a sinkpad to receive RTP packets for receivers. This will also create a
2030 create_send_rtp_sink (GstRtpSession * rtpsession)
2032 GST_DEBUG_OBJECT (rtpsession, "creating pad");
2034 rtpsession->send_rtp_sink =
2035 gst_pad_new_from_static_template (&rtpsession_send_rtp_sink_template,
2037 gst_pad_set_chain_function (rtpsession->send_rtp_sink,
2038 gst_rtp_session_chain_send_rtp);
2039 gst_pad_set_chain_list_function (rtpsession->send_rtp_sink,
2040 gst_rtp_session_chain_send_rtp_list);
2041 gst_pad_set_query_function (rtpsession->send_rtp_sink,
2042 gst_rtp_session_query_send_rtp);
2043 gst_pad_set_event_function (rtpsession->send_rtp_sink,
2044 gst_rtp_session_event_send_rtp_sink);
2045 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_sink,
2046 gst_rtp_session_iterate_internal_links);
2047 gst_pad_set_active (rtpsession->send_rtp_sink, TRUE);
2048 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2049 rtpsession->send_rtp_sink);
2051 rtpsession->send_rtp_src =
2052 gst_pad_new_from_static_template (&rtpsession_send_rtp_src_template,
2054 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtp_src,
2055 gst_rtp_session_iterate_internal_links);
2056 gst_pad_set_event_function (rtpsession->send_rtp_src,
2057 gst_rtp_session_event_send_rtp_src);
2058 gst_pad_set_active (rtpsession->send_rtp_src, TRUE);
2059 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession), rtpsession->send_rtp_src);
2061 return rtpsession->send_rtp_sink;
2065 remove_send_rtp_sink (GstRtpSession * rtpsession)
2067 GST_DEBUG_OBJECT (rtpsession, "removing pad");
2069 gst_pad_set_active (rtpsession->send_rtp_src, FALSE);
2070 gst_pad_set_active (rtpsession->send_rtp_sink, FALSE);
2072 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2073 rtpsession->send_rtp_sink);
2074 rtpsession->send_rtp_sink = NULL;
2076 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2077 rtpsession->send_rtp_src);
2078 rtpsession->send_rtp_src = NULL;
2081 /* Create a srcpad with the RTCP packets to send out.
2082 * This pad will be driven by the RTP session manager when it wants to send out
2086 create_send_rtcp_src (GstRtpSession * rtpsession)
2088 GST_DEBUG_OBJECT (rtpsession, "creating pad");
2090 rtpsession->send_rtcp_src =
2091 gst_pad_new_from_static_template (&rtpsession_send_rtcp_src_template,
2093 gst_pad_use_fixed_caps (rtpsession->send_rtcp_src);
2094 gst_pad_set_active (rtpsession->send_rtcp_src, TRUE);
2095 gst_pad_set_iterate_internal_links_function (rtpsession->send_rtcp_src,
2096 gst_rtp_session_iterate_internal_links);
2097 gst_pad_set_query_function (rtpsession->send_rtcp_src,
2098 gst_rtp_session_query_send_rtcp_src);
2099 gst_pad_set_event_function (rtpsession->send_rtcp_src,
2100 gst_rtp_session_event_send_rtcp_src);
2101 gst_element_add_pad (GST_ELEMENT_CAST (rtpsession),
2102 rtpsession->send_rtcp_src);
2104 return rtpsession->send_rtcp_src;
2108 remove_send_rtcp_src (GstRtpSession * rtpsession)
2110 GST_DEBUG_OBJECT (rtpsession, "removing pad");
2112 gst_pad_set_active (rtpsession->send_rtcp_src, FALSE);
2114 gst_element_remove_pad (GST_ELEMENT_CAST (rtpsession),
2115 rtpsession->send_rtcp_src);
2116 rtpsession->send_rtcp_src = NULL;
2120 gst_rtp_session_request_new_pad (GstElement * element,
2121 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
2123 GstRtpSession *rtpsession;
2124 GstElementClass *klass;
2127 g_return_val_if_fail (templ != NULL, NULL);
2128 g_return_val_if_fail (GST_IS_RTP_SESSION (element), NULL);
2130 rtpsession = GST_RTP_SESSION (element);
2131 klass = GST_ELEMENT_GET_CLASS (element);
2133 GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
2135 GST_RTP_SESSION_LOCK (rtpsession);
2137 /* figure out the template */
2138 if (templ == gst_element_class_get_pad_template (klass, "recv_rtp_sink")) {
2139 if (rtpsession->recv_rtp_sink != NULL)
2142 result = create_recv_rtp_sink (rtpsession);
2143 } else if (templ == gst_element_class_get_pad_template (klass,
2144 "recv_rtcp_sink")) {
2145 if (rtpsession->recv_rtcp_sink != NULL)
2148 result = create_recv_rtcp_sink (rtpsession);
2149 } else if (templ == gst_element_class_get_pad_template (klass,
2151 if (rtpsession->send_rtp_sink != NULL)
2154 result = create_send_rtp_sink (rtpsession);
2155 } else if (templ == gst_element_class_get_pad_template (klass,
2157 if (rtpsession->send_rtcp_src != NULL)
2160 result = create_send_rtcp_src (rtpsession);
2162 goto wrong_template;
2164 GST_RTP_SESSION_UNLOCK (rtpsession);
2171 GST_RTP_SESSION_UNLOCK (rtpsession);
2172 g_warning ("gstrtpsession: this is not our template");
2177 GST_RTP_SESSION_UNLOCK (rtpsession);
2178 g_warning ("gstrtpsession: pad already requested");
2184 gst_rtp_session_release_pad (GstElement * element, GstPad * pad)
2186 GstRtpSession *rtpsession;
2188 g_return_if_fail (GST_IS_RTP_SESSION (element));
2189 g_return_if_fail (GST_IS_PAD (pad));
2191 rtpsession = GST_RTP_SESSION (element);
2193 GST_DEBUG_OBJECT (element, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2195 GST_RTP_SESSION_LOCK (rtpsession);
2197 if (rtpsession->recv_rtp_sink == pad) {
2198 remove_recv_rtp_sink (rtpsession);
2199 } else if (rtpsession->recv_rtcp_sink == pad) {
2200 remove_recv_rtcp_sink (rtpsession);
2201 } else if (rtpsession->send_rtp_sink == pad) {
2202 remove_send_rtp_sink (rtpsession);
2203 } else if (rtpsession->send_rtcp_src == pad) {
2204 remove_send_rtcp_src (rtpsession);
2208 GST_RTP_SESSION_UNLOCK (rtpsession);
2215 GST_RTP_SESSION_UNLOCK (rtpsession);
2216 g_warning ("gstrtpsession: asked to release an unknown pad");
2222 gst_rtp_session_request_key_unit (RTPSession * sess,
2223 gboolean all_headers, gpointer user_data)
2225 GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2227 GstPad *send_rtp_sink;
2229 GST_RTP_SESSION_LOCK (rtpsession);
2230 if (rtpsession->send_rtp_sink)
2231 send_rtp_sink = gst_object_ref (rtpsession->send_rtp_sink);
2232 GST_RTP_SESSION_UNLOCK (rtpsession);
2234 if (send_rtp_sink) {
2235 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
2236 gst_structure_new ("GstForceKeyUnit",
2237 "all-headers", G_TYPE_BOOLEAN, all_headers, NULL));
2238 gst_pad_push_event (send_rtp_sink, event);
2239 gst_object_unref (send_rtp_sink);
2244 gst_rtp_session_request_time (RTPSession * session, gpointer user_data)
2246 GstRtpSession *rtpsession = GST_RTP_SESSION (user_data);
2248 return gst_clock_get_time (rtpsession->priv->sysclock);