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.
20 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
21 * with newer GLib versions (>= 2.31.0) */
22 #define GLIB_DISABLE_DEPRECATION_WARNINGS
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/rtp/gstrtcpbuffer.h>
29 #include <gst/glib-compat-private.h>
31 #include "gstrtpbin-marshal.h"
32 #include "rtpsession.h"
34 GST_DEBUG_CATEGORY_STATIC (rtp_session_debug);
35 #define GST_CAT_DEFAULT rtp_session_debug
37 /* signals and args */
40 SIGNAL_GET_SOURCE_BY_SSRC,
42 SIGNAL_ON_SSRC_COLLISION,
43 SIGNAL_ON_SSRC_VALIDATED,
44 SIGNAL_ON_SSRC_ACTIVE,
47 SIGNAL_ON_BYE_TIMEOUT,
49 SIGNAL_ON_SENDER_TIMEOUT,
50 SIGNAL_ON_SENDING_RTCP,
51 SIGNAL_ON_FEEDBACK_RTCP,
56 #define DEFAULT_INTERNAL_SOURCE NULL
57 #define DEFAULT_BANDWIDTH RTP_STATS_BANDWIDTH
58 #define DEFAULT_RTCP_FRACTION (RTP_STATS_RTCP_FRACTION * RTP_STATS_BANDWIDTH)
59 #define DEFAULT_RTCP_RR_BANDWIDTH -1
60 #define DEFAULT_RTCP_RS_BANDWIDTH -1
61 #define DEFAULT_RTCP_MTU 1400
62 #define DEFAULT_SDES NULL
63 #define DEFAULT_NUM_SOURCES 0
64 #define DEFAULT_NUM_ACTIVE_SOURCES 0
65 #define DEFAULT_SOURCES NULL
66 #define DEFAULT_RTCP_MIN_INTERVAL (RTP_STATS_MIN_INTERVAL * GST_SECOND)
67 #define DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW (2 * GST_SECOND)
68 #define DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD (3)
69 #define DEFAULT_PROBATION RTP_DEFAULT_PROBATION
78 PROP_RTCP_RR_BANDWIDTH,
79 PROP_RTCP_RS_BANDWIDTH,
83 PROP_NUM_ACTIVE_SOURCES,
86 PROP_RTCP_MIN_INTERVAL,
87 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
88 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
93 /* update average packet size */
94 #define INIT_AVG(avg, val) \
96 #define UPDATE_AVG(avg, val) \
100 (avg) = ((val) + (15 * (avg))) >> 4;
103 /* The number RTCP intervals after which to timeout entries in the
106 #define RTCP_INTERVAL_COLLISION_TIMEOUT 10
108 /* GObject vmethods */
109 static void rtp_session_finalize (GObject * object);
110 static void rtp_session_set_property (GObject * object, guint prop_id,
111 const GValue * value, GParamSpec * pspec);
112 static void rtp_session_get_property (GObject * object, guint prop_id,
113 GValue * value, GParamSpec * pspec);
115 static gboolean rtp_session_on_sending_rtcp (RTPSession * sess,
116 GstBuffer * buffer, gboolean early);
117 static void rtp_session_send_rtcp (RTPSession * sess,
118 GstClockTimeDiff max_delay);
121 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
123 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
125 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
126 gboolean * created, RTPArrivalStats * arrival, gboolean rtp);
127 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
128 const gchar * reason, GstClockTime current_time);
129 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
130 gboolean deterministic, gboolean first);
133 accumulate_trues (GSignalInvocationHint * ihint, GValue * return_accu,
134 const GValue * handler_return, gpointer data)
136 if (g_value_get_boolean (handler_return))
137 g_value_set_boolean (return_accu, TRUE);
143 rtp_session_class_init (RTPSessionClass * klass)
145 GObjectClass *gobject_class;
147 gobject_class = (GObjectClass *) klass;
149 gobject_class->finalize = rtp_session_finalize;
150 gobject_class->set_property = rtp_session_set_property;
151 gobject_class->get_property = rtp_session_get_property;
154 * RTPSession::get-source-by-ssrc:
155 * @session: the object which received the signal
156 * @ssrc: the SSRC of the RTPSource
158 * Request the #RTPSource object with SSRC @ssrc in @session.
160 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
161 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
162 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
163 get_source_by_ssrc), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
164 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
167 * RTPSession::on-new-ssrc:
168 * @session: the object which received the signal
169 * @src: the new RTPSource
171 * Notify of a new SSRC that entered @session.
173 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
174 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
175 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
176 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
179 * RTPSession::on-ssrc-collision:
180 * @session: the object which received the signal
181 * @src: the #RTPSource that caused a collision
183 * Notify when we have an SSRC collision
185 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
186 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
187 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
188 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
191 * RTPSession::on-ssrc-validated:
192 * @session: the object which received the signal
193 * @src: the new validated RTPSource
195 * Notify of a new SSRC that became validated.
197 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
198 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
199 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
200 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
203 * RTPSession::on-ssrc-active:
204 * @session: the object which received the signal
205 * @src: the active RTPSource
207 * Notify of a SSRC that is active, i.e., sending RTCP.
209 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
210 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
211 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
212 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
215 * RTPSession::on-ssrc-sdes:
216 * @session: the object which received the signal
217 * @src: the RTPSource
219 * Notify that a new SDES was received for SSRC.
221 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
222 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
223 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
224 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
227 * RTPSession::on-bye-ssrc:
228 * @session: the object which received the signal
229 * @src: the RTPSource that went away
231 * Notify of an SSRC that became inactive because of a BYE packet.
233 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
234 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
235 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
236 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
239 * RTPSession::on-bye-timeout:
240 * @session: the object which received the signal
241 * @src: the RTPSource that timed out
243 * Notify of an SSRC that has timed out because of BYE
245 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
246 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
247 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
248 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
251 * RTPSession::on-timeout:
252 * @session: the object which received the signal
253 * @src: the RTPSource that timed out
255 * Notify of an SSRC that has timed out
257 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
258 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
259 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
260 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
263 * RTPSession::on-sender-timeout:
264 * @session: the object which received the signal
265 * @src: the RTPSource that timed out
267 * Notify of an SSRC that was a sender but timed out and became a receiver.
269 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
270 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
271 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
272 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
276 * RTPSession::on-sending-rtcp
277 * @session: the object which received the signal
278 * @buffer: the #GstBuffer containing the RTCP packet about to be sent
279 * @early: %TRUE if the packet is early, %FALSE if it is regular
281 * This signal is emitted before sending an RTCP packet, it can be used
282 * to add extra RTCP Packets.
284 * Returns: %TRUE if the RTCP buffer should NOT be suppressed, %FALSE
285 * if suppressing it is acceptable
287 rtp_session_signals[SIGNAL_ON_SENDING_RTCP] =
288 g_signal_new ("on-sending-rtcp", G_TYPE_FROM_CLASS (klass),
289 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_rtcp),
290 accumulate_trues, NULL, gst_rtp_bin_marshal_BOOLEAN__BOXED_BOOLEAN,
291 G_TYPE_BOOLEAN, 2, GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE,
295 * RTPSession::on-feedback-rtcp:
296 * @session: the object which received the signal
297 * @type: Type of RTCP packet, will be %GST_RTCP_TYPE_RTPFB or
298 * %GST_RTCP_TYPE_RTPFB
299 * @fbtype: The type of RTCP FB packet, probably part of #GstRTCPFBType
300 * @sender_ssrc: The SSRC of the sender
301 * @media_ssrc: The SSRC of the media this refers to
302 * @fci: a #GstBuffer with the FCI data from the FB packet or %NULL if
305 * Notify that a RTCP feedback packet has been received
307 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP] =
308 g_signal_new ("on-feedback-rtcp", G_TYPE_FROM_CLASS (klass),
309 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_feedback_rtcp),
310 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT_UINT_UINT_BOXED,
311 G_TYPE_NONE, 5, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT,
315 * RTPSession::send-rtcp:
316 * @session: the object which received the signal
317 * @max_delay: The maximum delay after which the feedback will not be useful
320 * Requests that the #RTPSession initiate a new RTCP packet as soon as
321 * possible within the requested delay.
324 rtp_session_signals[SIGNAL_SEND_RTCP] =
325 g_signal_new ("send-rtcp", G_TYPE_FROM_CLASS (klass),
326 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
327 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
328 gst_rtp_bin_marshal_VOID__UINT64, G_TYPE_NONE, 1, G_TYPE_UINT64);
330 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
331 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
332 "The internal SSRC used for the session",
333 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
335 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
336 g_param_spec_object ("internal-source", "Internal Source",
337 "The internal source element of the session",
338 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
340 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
341 g_param_spec_double ("bandwidth", "Bandwidth",
342 "The bandwidth of the session (0 for auto-discover)",
343 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
344 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
346 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
347 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
348 "The fraction of the bandwidth used for RTCP (or as a real fraction of the RTP bandwidth if < 1)",
349 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
350 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
352 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
353 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
354 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
355 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
356 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
358 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
359 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
360 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
361 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
362 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
364 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
365 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
366 "The maximum size of the RTCP packets",
367 16, G_MAXINT16, DEFAULT_RTCP_MTU,
368 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
370 g_object_class_install_property (gobject_class, PROP_SDES,
371 g_param_spec_boxed ("sdes", "SDES",
372 "The SDES items of this session",
373 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
375 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
376 g_param_spec_uint ("num-sources", "Num Sources",
377 "The number of sources in the session", 0, G_MAXUINT,
378 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
380 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
381 g_param_spec_uint ("num-active-sources", "Num Active Sources",
382 "The number of active sources in the session", 0, G_MAXUINT,
383 DEFAULT_NUM_ACTIVE_SOURCES,
384 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
388 * Get a GValue Array of all sources in the session.
391 * <title>Getting the #RTPSources of a session
398 * g_object_get (sess, "sources", &arr, NULL);
400 * for (i = 0; i < arr->n_values; i++) {
403 * val = g_value_array_get_nth (arr, i);
404 * source = g_value_get_object (val);
406 * g_value_array_free (arr);
411 g_object_class_install_property (gobject_class, PROP_SOURCES,
412 g_param_spec_boxed ("sources", "Sources",
413 "An array of all known sources in the session",
414 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
416 g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
417 g_param_spec_boolean ("favor-new", "Favor new sources",
418 "Resolve SSRC conflict in favor of new sources", FALSE,
419 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
421 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
422 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
423 "Minimum interval between Regular RTCP packet (in ns)",
424 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
425 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
427 g_object_class_install_property (gobject_class,
428 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
429 g_param_spec_uint64 ("rtcp-feedback-retention-window",
430 "RTCP Feedback retention window",
431 "Duration during which RTCP Feedback packets are retained (in ns)",
432 0, G_MAXUINT64, DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW,
433 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
435 g_object_class_install_property (gobject_class,
436 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
437 g_param_spec_uint ("rtcp-immediate-feedback-threshold",
438 "RTCP Immediate Feedback threshold",
439 "The maximum number of members of a RTP session for which immediate"
441 0, G_MAXUINT, DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
442 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
444 g_object_class_install_property (gobject_class, PROP_PROBATION,
445 g_param_spec_uint ("probation", "Number of probations",
446 "Consecutive packet sequence numbers to accept the source",
447 0, G_MAXUINT, DEFAULT_PROBATION,
448 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
450 klass->get_source_by_ssrc =
451 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
452 klass->on_sending_rtcp = GST_DEBUG_FUNCPTR (rtp_session_on_sending_rtcp);
453 klass->send_rtcp = GST_DEBUG_FUNCPTR (rtp_session_send_rtcp);
455 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
459 rtp_session_init (RTPSession * sess)
465 g_mutex_init (&sess->lock);
466 sess->key = g_random_int ();
470 for (i = 0; i < 32; i++) {
472 g_hash_table_new_full (NULL, NULL, NULL,
473 (GDestroyNotify) g_object_unref);
476 rtp_stats_init_defaults (&sess->stats);
478 sess->recalc_bandwidth = TRUE;
479 sess->bandwidth = DEFAULT_BANDWIDTH;
480 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
481 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
482 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
484 /* create an active SSRC for this session manager */
485 sess->source = rtp_session_create_source (sess);
486 sess->source->validated = TRUE;
487 sess->source->internal = TRUE;
488 sess->stats.active_sources++;
489 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
490 sess->source->stats.prev_rtcptime = 0;
491 sess->source->stats.last_rtcptime = 1;
493 rtp_stats_set_min_interval (&sess->stats,
494 (gdouble) DEFAULT_RTCP_MIN_INTERVAL / GST_SECOND);
496 /* default UDP header length */
497 sess->header_len = 28;
498 sess->mtu = DEFAULT_RTCP_MTU;
500 sess->probation = DEFAULT_PROBATION;
502 /* some default SDES entries */
503 sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
505 /* we do not want to leak details like the username or hostname here */
506 str = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
507 gst_structure_set (sdes, "cname", G_TYPE_STRING, str, NULL);
511 /* we do not want to leak the user's real name here */
512 str = g_strdup_printf ("Anon%u", g_random_int ());
513 gst_structure_set (sdes, "name", G_TYPE_STRING, str, NULL);
517 gst_structure_set (sdes, "tool", G_TYPE_STRING, "GStreamer", NULL);
519 /* and configure in the source */
520 rtp_source_set_sdes_struct (sess->source, sdes);
522 sess->first_rtcp = TRUE;
523 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
525 sess->allow_early = TRUE;
526 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
527 sess->rtcp_feedback_retention_window = DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW;
528 sess->rtcp_immediate_feedback_threshold =
529 DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD;
531 sess->last_keyframe_request = GST_CLOCK_TIME_NONE;
533 GST_DEBUG ("%p: session using SSRC: %08x", sess, sess->source->ssrc);
537 rtp_session_finalize (GObject * object)
542 sess = RTP_SESSION_CAST (object);
544 g_mutex_clear (&sess->lock);
546 for (i = 0; i < 32; i++)
547 g_hash_table_destroy (sess->ssrcs[i]);
549 g_object_unref (sess->source);
551 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
555 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
557 GValue value = { 0 };
559 g_value_init (&value, RTP_TYPE_SOURCE);
560 g_value_take_object (&value, source);
561 /* copies the value */
562 g_value_array_append (arr, &value);
566 rtp_session_create_sources (RTPSession * sess)
571 RTP_SESSION_LOCK (sess);
572 /* get number of elements in the table */
573 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
574 /* create the result value array */
575 res = g_value_array_new (size);
577 /* and copy all values into the array */
578 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
579 RTP_SESSION_UNLOCK (sess);
585 rtp_session_set_property (GObject * object, guint prop_id,
586 const GValue * value, GParamSpec * pspec)
590 sess = RTP_SESSION (object);
593 case PROP_INTERNAL_SSRC:
594 rtp_session_set_internal_ssrc (sess, g_value_get_uint (value));
597 RTP_SESSION_LOCK (sess);
598 sess->bandwidth = g_value_get_double (value);
599 sess->recalc_bandwidth = TRUE;
600 RTP_SESSION_UNLOCK (sess);
602 case PROP_RTCP_FRACTION:
603 RTP_SESSION_LOCK (sess);
604 sess->rtcp_bandwidth = g_value_get_double (value);
605 sess->recalc_bandwidth = TRUE;
606 RTP_SESSION_UNLOCK (sess);
608 case PROP_RTCP_RR_BANDWIDTH:
609 RTP_SESSION_LOCK (sess);
610 sess->rtcp_rr_bandwidth = g_value_get_int (value);
611 sess->recalc_bandwidth = TRUE;
612 RTP_SESSION_UNLOCK (sess);
614 case PROP_RTCP_RS_BANDWIDTH:
615 RTP_SESSION_LOCK (sess);
616 sess->rtcp_rs_bandwidth = g_value_get_int (value);
617 sess->recalc_bandwidth = TRUE;
618 RTP_SESSION_UNLOCK (sess);
621 sess->mtu = g_value_get_uint (value);
624 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
627 sess->favor_new = g_value_get_boolean (value);
629 case PROP_RTCP_MIN_INTERVAL:
630 rtp_stats_set_min_interval (&sess->stats,
631 (gdouble) g_value_get_uint64 (value) / GST_SECOND);
632 /* trigger reconsideration */
633 RTP_SESSION_LOCK (sess);
634 sess->next_rtcp_check_time = 0;
635 RTP_SESSION_UNLOCK (sess);
636 if (sess->callbacks.reconsider)
637 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
639 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
640 sess->rtcp_immediate_feedback_threshold = g_value_get_uint (value);
643 sess->probation = g_value_get_uint (value);
644 g_object_set_property (G_OBJECT (sess->source), "probation", value);
647 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
653 rtp_session_get_property (GObject * object, guint prop_id,
654 GValue * value, GParamSpec * pspec)
658 sess = RTP_SESSION (object);
661 case PROP_INTERNAL_SSRC:
662 g_value_set_uint (value, rtp_session_get_internal_ssrc (sess));
664 case PROP_INTERNAL_SOURCE:
665 g_value_take_object (value, rtp_session_get_internal_source (sess));
668 g_value_set_double (value, sess->bandwidth);
670 case PROP_RTCP_FRACTION:
671 g_value_set_double (value, sess->rtcp_bandwidth);
673 case PROP_RTCP_RR_BANDWIDTH:
674 g_value_set_int (value, sess->rtcp_rr_bandwidth);
676 case PROP_RTCP_RS_BANDWIDTH:
677 g_value_set_int (value, sess->rtcp_rs_bandwidth);
680 g_value_set_uint (value, sess->mtu);
683 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
685 case PROP_NUM_SOURCES:
686 g_value_set_uint (value, rtp_session_get_num_sources (sess));
688 case PROP_NUM_ACTIVE_SOURCES:
689 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
692 g_value_take_boxed (value, rtp_session_create_sources (sess));
695 g_value_set_boolean (value, sess->favor_new);
697 case PROP_RTCP_MIN_INTERVAL:
698 g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
700 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
701 g_value_set_uint (value, sess->rtcp_immediate_feedback_threshold);
704 g_value_set_uint (value, sess->probation);
705 g_object_get_property (G_OBJECT (sess->source), "probation", value);
708 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
714 on_new_ssrc (RTPSession * sess, RTPSource * source)
716 g_object_ref (source);
717 RTP_SESSION_UNLOCK (sess);
718 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
719 RTP_SESSION_LOCK (sess);
720 g_object_unref (source);
724 on_ssrc_collision (RTPSession * sess, RTPSource * source)
726 g_object_ref (source);
727 RTP_SESSION_UNLOCK (sess);
728 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
730 RTP_SESSION_LOCK (sess);
731 g_object_unref (source);
735 on_ssrc_validated (RTPSession * sess, RTPSource * source)
737 g_object_ref (source);
738 RTP_SESSION_UNLOCK (sess);
739 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
741 RTP_SESSION_LOCK (sess);
742 g_object_unref (source);
746 on_ssrc_active (RTPSession * sess, RTPSource * source)
748 g_object_ref (source);
749 RTP_SESSION_UNLOCK (sess);
750 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
751 RTP_SESSION_LOCK (sess);
752 g_object_unref (source);
756 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
758 g_object_ref (source);
759 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
760 RTP_SESSION_UNLOCK (sess);
761 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
762 RTP_SESSION_LOCK (sess);
763 g_object_unref (source);
767 on_bye_ssrc (RTPSession * sess, RTPSource * source)
769 g_object_ref (source);
770 RTP_SESSION_UNLOCK (sess);
771 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
772 RTP_SESSION_LOCK (sess);
773 g_object_unref (source);
777 on_bye_timeout (RTPSession * sess, RTPSource * source)
779 g_object_ref (source);
780 RTP_SESSION_UNLOCK (sess);
781 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
782 RTP_SESSION_LOCK (sess);
783 g_object_unref (source);
787 on_timeout (RTPSession * sess, RTPSource * source)
789 g_object_ref (source);
790 RTP_SESSION_UNLOCK (sess);
791 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
792 RTP_SESSION_LOCK (sess);
793 g_object_unref (source);
797 on_sender_timeout (RTPSession * sess, RTPSource * source)
799 g_object_ref (source);
800 RTP_SESSION_UNLOCK (sess);
801 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
803 RTP_SESSION_LOCK (sess);
804 g_object_unref (source);
810 * Create a new session object.
812 * Returns: a new #RTPSession. g_object_unref() after usage.
815 rtp_session_new (void)
819 sess = g_object_new (RTP_TYPE_SESSION, NULL);
825 * rtp_session_set_callbacks:
826 * @sess: an #RTPSession
827 * @callbacks: callbacks to configure
828 * @user_data: user data passed in the callbacks
830 * Configure a set of callbacks to be notified of actions.
833 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
836 g_return_if_fail (RTP_IS_SESSION (sess));
838 if (callbacks->process_rtp) {
839 sess->callbacks.process_rtp = callbacks->process_rtp;
840 sess->process_rtp_user_data = user_data;
842 if (callbacks->send_rtp) {
843 sess->callbacks.send_rtp = callbacks->send_rtp;
844 sess->send_rtp_user_data = user_data;
846 if (callbacks->send_rtcp) {
847 sess->callbacks.send_rtcp = callbacks->send_rtcp;
848 sess->send_rtcp_user_data = user_data;
850 if (callbacks->sync_rtcp) {
851 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
852 sess->sync_rtcp_user_data = user_data;
854 if (callbacks->clock_rate) {
855 sess->callbacks.clock_rate = callbacks->clock_rate;
856 sess->clock_rate_user_data = user_data;
858 if (callbacks->reconsider) {
859 sess->callbacks.reconsider = callbacks->reconsider;
860 sess->reconsider_user_data = user_data;
862 if (callbacks->request_key_unit) {
863 sess->callbacks.request_key_unit = callbacks->request_key_unit;
864 sess->request_key_unit_user_data = user_data;
866 if (callbacks->request_time) {
867 sess->callbacks.request_time = callbacks->request_time;
868 sess->request_time_user_data = user_data;
873 * rtp_session_set_process_rtp_callback:
874 * @sess: an #RTPSession
875 * @callback: callback to set
876 * @user_data: user data passed in the callback
878 * Configure only the process_rtp callback to be notified of the process_rtp action.
881 rtp_session_set_process_rtp_callback (RTPSession * sess,
882 RTPSessionProcessRTP callback, gpointer user_data)
884 g_return_if_fail (RTP_IS_SESSION (sess));
886 sess->callbacks.process_rtp = callback;
887 sess->process_rtp_user_data = user_data;
891 * rtp_session_set_send_rtp_callback:
892 * @sess: an #RTPSession
893 * @callback: callback to set
894 * @user_data: user data passed in the callback
896 * Configure only the send_rtp callback to be notified of the send_rtp action.
899 rtp_session_set_send_rtp_callback (RTPSession * sess,
900 RTPSessionSendRTP callback, gpointer user_data)
902 g_return_if_fail (RTP_IS_SESSION (sess));
904 sess->callbacks.send_rtp = callback;
905 sess->send_rtp_user_data = user_data;
909 * rtp_session_set_send_rtcp_callback:
910 * @sess: an #RTPSession
911 * @callback: callback to set
912 * @user_data: user data passed in the callback
914 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
917 rtp_session_set_send_rtcp_callback (RTPSession * sess,
918 RTPSessionSendRTCP callback, gpointer user_data)
920 g_return_if_fail (RTP_IS_SESSION (sess));
922 sess->callbacks.send_rtcp = callback;
923 sess->send_rtcp_user_data = user_data;
927 * rtp_session_set_sync_rtcp_callback:
928 * @sess: an #RTPSession
929 * @callback: callback to set
930 * @user_data: user data passed in the callback
932 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
935 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
936 RTPSessionSyncRTCP callback, gpointer user_data)
938 g_return_if_fail (RTP_IS_SESSION (sess));
940 sess->callbacks.sync_rtcp = callback;
941 sess->sync_rtcp_user_data = user_data;
945 * rtp_session_set_clock_rate_callback:
946 * @sess: an #RTPSession
947 * @callback: callback to set
948 * @user_data: user data passed in the callback
950 * Configure only the clock_rate callback to be notified of the clock_rate action.
953 rtp_session_set_clock_rate_callback (RTPSession * sess,
954 RTPSessionClockRate callback, gpointer user_data)
956 g_return_if_fail (RTP_IS_SESSION (sess));
958 sess->callbacks.clock_rate = callback;
959 sess->clock_rate_user_data = user_data;
963 * rtp_session_set_reconsider_callback:
964 * @sess: an #RTPSession
965 * @callback: callback to set
966 * @user_data: user data passed in the callback
968 * Configure only the reconsider callback to be notified of the reconsider action.
971 rtp_session_set_reconsider_callback (RTPSession * sess,
972 RTPSessionReconsider callback, gpointer user_data)
974 g_return_if_fail (RTP_IS_SESSION (sess));
976 sess->callbacks.reconsider = callback;
977 sess->reconsider_user_data = user_data;
981 * rtp_session_set_request_time_callback:
982 * @sess: an #RTPSession
983 * @callback: callback to set
984 * @user_data: user data passed in the callback
986 * Configure only the request_time callback
989 rtp_session_set_request_time_callback (RTPSession * sess,
990 RTPSessionRequestTime callback, gpointer user_data)
992 g_return_if_fail (RTP_IS_SESSION (sess));
994 sess->callbacks.request_time = callback;
995 sess->request_time_user_data = user_data;
999 * rtp_session_set_bandwidth:
1000 * @sess: an #RTPSession
1001 * @bandwidth: the bandwidth allocated
1003 * Set the session bandwidth in bytes per second.
1006 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
1008 g_return_if_fail (RTP_IS_SESSION (sess));
1010 RTP_SESSION_LOCK (sess);
1011 sess->stats.bandwidth = bandwidth;
1012 RTP_SESSION_UNLOCK (sess);
1016 * rtp_session_get_bandwidth:
1017 * @sess: an #RTPSession
1019 * Get the session bandwidth.
1021 * Returns: the session bandwidth.
1024 rtp_session_get_bandwidth (RTPSession * sess)
1028 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1030 RTP_SESSION_LOCK (sess);
1031 result = sess->stats.bandwidth;
1032 RTP_SESSION_UNLOCK (sess);
1038 * rtp_session_set_rtcp_fraction:
1039 * @sess: an #RTPSession
1040 * @bandwidth: the RTCP bandwidth
1042 * Set the bandwidth in bytes per second that should be used for RTCP
1046 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
1048 g_return_if_fail (RTP_IS_SESSION (sess));
1050 RTP_SESSION_LOCK (sess);
1051 sess->stats.rtcp_bandwidth = bandwidth;
1052 RTP_SESSION_UNLOCK (sess);
1056 * rtp_session_get_rtcp_fraction:
1057 * @sess: an #RTPSession
1059 * Get the session bandwidth used for RTCP.
1061 * Returns: The bandwidth used for RTCP messages.
1064 rtp_session_get_rtcp_fraction (RTPSession * sess)
1068 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
1070 RTP_SESSION_LOCK (sess);
1071 result = sess->stats.rtcp_bandwidth;
1072 RTP_SESSION_UNLOCK (sess);
1078 * rtp_session_get_sdes_struct:
1079 * @sess: an #RTSPSession
1081 * Get the SDES data as a #GstStructure
1083 * Returns: a GstStructure with SDES items for @sess. This function returns a
1084 * copy of the SDES structure, use gst_structure_free() after usage.
1087 rtp_session_get_sdes_struct (RTPSession * sess)
1089 const GstStructure *sdes;
1090 GstStructure *result = NULL;
1092 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1094 RTP_SESSION_LOCK (sess);
1095 sdes = rtp_source_get_sdes_struct (sess->source);
1097 result = gst_structure_copy (sdes);
1098 RTP_SESSION_UNLOCK (sess);
1104 * rtp_session_set_sdes_struct:
1105 * @sess: an #RTSPSession
1106 * @sdes: a #GstStructure
1108 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1111 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
1113 g_return_if_fail (sdes);
1114 g_return_if_fail (RTP_IS_SESSION (sess));
1116 RTP_SESSION_LOCK (sess);
1117 rtp_source_set_sdes_struct (sess->source, gst_structure_copy (sdes));
1118 RTP_SESSION_UNLOCK (sess);
1121 static GstFlowReturn
1122 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1124 GstFlowReturn result = GST_FLOW_OK;
1126 if (source == session->source) {
1127 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1129 RTP_SESSION_UNLOCK (session);
1131 if (session->callbacks.send_rtp)
1133 session->callbacks.send_rtp (session, source, data,
1134 session->send_rtp_user_data);
1136 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1139 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1140 RTP_SESSION_UNLOCK (session);
1142 if (session->callbacks.process_rtp)
1144 session->callbacks.process_rtp (session, source,
1145 GST_BUFFER_CAST (data), session->process_rtp_user_data);
1147 gst_buffer_unref (GST_BUFFER_CAST (data));
1149 RTP_SESSION_LOCK (session);
1155 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1159 RTP_SESSION_UNLOCK (session);
1161 if (session->callbacks.clock_rate)
1163 session->callbacks.clock_rate (session, pt,
1164 session->clock_rate_user_data);
1168 RTP_SESSION_LOCK (session);
1170 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1175 static RTPSourceCallbacks callbacks = {
1176 (RTPSourcePushRTP) source_push_rtp,
1177 (RTPSourceClockRate) source_clock_rate,
1181 check_collision (RTPSession * sess, RTPSource * source,
1182 RTPArrivalStats * arrival, gboolean rtp)
1184 /* If we have no arrival address, we can't do collision checking */
1185 if (!arrival->address)
1188 if (sess->source != source) {
1189 GSocketAddress *from;
1191 /* This is not our local source, but lets check if two remote
1194 from = source->rtp_from;
1196 from = source->rtcp_from;
1200 if (__g_socket_address_equal (from, arrival->address)) {
1201 /* Address is the same */
1204 GST_LOG ("we have a third-party collision or loop ssrc:%x",
1205 rtp_source_get_ssrc (source));
1206 if (sess->favor_new) {
1207 if (rtp_source_find_conflicting_address (source,
1208 arrival->address, arrival->current_time)) {
1211 buf1 = __g_socket_address_to_string (arrival->address);
1212 GST_LOG ("Known conflict on %x for %s, dropping packet",
1213 rtp_source_get_ssrc (source), buf1);
1220 /* Current address is not a known conflict, lets assume this is
1221 * a new source. Save old address in possible conflict list
1223 rtp_source_add_conflicting_address (source, from,
1224 arrival->current_time);
1226 buf1 = __g_socket_address_to_string (from);
1227 buf2 = __g_socket_address_to_string (arrival->address);
1229 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1230 " saving old as known conflict",
1231 rtp_source_get_ssrc (source), buf1, buf2);
1234 rtp_source_set_rtp_from (source, arrival->address);
1236 rtp_source_set_rtcp_from (source, arrival->address);
1244 /* Don't need to save old addresses, we ignore new sources */
1249 /* We don't already have a from address for RTP, just set it */
1251 rtp_source_set_rtp_from (source, arrival->address);
1253 rtp_source_set_rtcp_from (source, arrival->address);
1257 /* FIXME: Log 3rd party collision somehow
1258 * Maybe should be done in upper layer, only the SDES can tell us
1259 * if its a collision or a loop
1262 /* This is sending with our ssrc, is it an address we already know */
1264 if (rtp_source_find_conflicting_address (source, arrival->address,
1265 arrival->current_time)) {
1266 /* Its a known conflict, its probably a loop, not a collision
1267 * lets just drop the incoming packet
1269 GST_DEBUG ("Our packets are being looped back to us, dropping");
1271 /* Its a new collision, lets change our SSRC */
1273 rtp_source_add_conflicting_address (source, arrival->address,
1274 arrival->current_time);
1276 GST_DEBUG ("Collision for SSRC %x", rtp_source_get_ssrc (source));
1277 on_ssrc_collision (sess, source);
1279 sess->change_ssrc = TRUE;
1281 rtp_session_schedule_bye_locked (sess, "SSRC Collision",
1282 arrival->current_time);
1290 find_source (RTPSession * sess, guint32 ssrc)
1292 return g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1293 GINT_TO_POINTER (ssrc));
1297 add_source (RTPSession * sess, RTPSource * src)
1299 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1300 GINT_TO_POINTER (src->ssrc), src);
1301 /* we have one more source now */
1302 sess->total_sources++;
1305 /* must be called with the session lock, the returned source needs to be
1306 * unreffed after usage. */
1308 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1309 RTPArrivalStats * arrival, gboolean rtp)
1313 source = find_source (sess, ssrc);
1314 if (source == NULL) {
1315 /* make new Source in probation and insert */
1316 source = rtp_source_new (ssrc);
1318 GST_DEBUG ("creating new source %08x %p", ssrc, source);
1320 /* for RTP packets we need to set the source in probation. Receiving RTCP
1321 * packets of an SSRC, on the other hand, is a strong indication that we
1322 * are dealing with a valid source. */
1324 g_object_set (source, "probation", sess->probation, NULL);
1326 g_object_set (source, "probation", 0, NULL);
1328 /* store from address, if any */
1329 if (arrival->address) {
1331 rtp_source_set_rtp_from (source, arrival->address);
1333 rtp_source_set_rtcp_from (source, arrival->address);
1336 /* configure a callback on the source */
1337 rtp_source_set_callbacks (source, &callbacks, sess);
1339 add_source (sess, source);
1343 /* check for collision, this updates the address when not previously set */
1344 if (check_collision (sess, source, arrival, rtp)) {
1347 /* Receiving RTCP packets of an SSRC is a strong indication that we
1348 * are dealing with a valid source. */
1350 g_object_set (source, "probation", 0, NULL);
1352 /* update last activity */
1353 source->last_activity = arrival->current_time;
1355 source->last_rtp_activity = arrival->current_time;
1356 g_object_ref (source);
1362 * rtp_session_get_internal_source:
1363 * @sess: a #RTPSession
1365 * Get the internal #RTPSource of @sess.
1367 * Returns: The internal #RTPSource. g_object_unref() after usage.
1370 rtp_session_get_internal_source (RTPSession * sess)
1374 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1376 result = g_object_ref (sess->source);
1382 * rtp_session_set_internal_ssrc:
1383 * @sess: a #RTPSession
1386 * Set the SSRC of @sess to @ssrc.
1389 rtp_session_set_internal_ssrc (RTPSession * sess, guint32 ssrc)
1391 RTP_SESSION_LOCK (sess);
1392 if (ssrc != sess->source->ssrc) {
1393 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
1394 GINT_TO_POINTER (sess->source->ssrc));
1396 GST_DEBUG ("setting internal SSRC to %08x", ssrc);
1397 /* After this call, any receiver of the old SSRC either in RTP or RTCP
1398 * packets will timeout on the old SSRC, we could potentially schedule a
1399 * BYE RTCP for the old SSRC... */
1400 sess->source->ssrc = ssrc;
1401 rtp_source_reset (sess->source);
1403 /* rehash with the new SSRC */
1404 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1405 GINT_TO_POINTER (sess->source->ssrc), sess->source);
1407 RTP_SESSION_UNLOCK (sess);
1409 g_object_notify (G_OBJECT (sess), "internal-ssrc");
1413 * rtp_session_get_internal_ssrc:
1414 * @sess: a #RTPSession
1416 * Get the internal SSRC of @sess.
1418 * Returns: The SSRC of the session.
1421 rtp_session_get_internal_ssrc (RTPSession * sess)
1425 RTP_SESSION_LOCK (sess);
1426 ssrc = sess->source->ssrc;
1427 RTP_SESSION_UNLOCK (sess);
1433 * rtp_session_add_source:
1434 * @sess: a #RTPSession
1435 * @src: #RTPSource to add
1437 * Add @src to @session.
1439 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1440 * existed in the session.
1443 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1445 gboolean result = FALSE;
1448 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1449 g_return_val_if_fail (src != NULL, FALSE);
1451 RTP_SESSION_LOCK (sess);
1452 find = find_source (sess, src->ssrc);
1454 add_source (sess, src);
1457 RTP_SESSION_UNLOCK (sess);
1463 * rtp_session_get_num_sources:
1464 * @sess: an #RTPSession
1466 * Get the number of sources in @sess.
1468 * Returns: The number of sources in @sess.
1471 rtp_session_get_num_sources (RTPSession * sess)
1475 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1477 RTP_SESSION_LOCK (sess);
1478 result = sess->total_sources;
1479 RTP_SESSION_UNLOCK (sess);
1485 * rtp_session_get_num_active_sources:
1486 * @sess: an #RTPSession
1488 * Get the number of active sources in @sess. A source is considered active when
1489 * it has been validated and has not yet received a BYE RTCP message.
1491 * Returns: The number of active sources in @sess.
1494 rtp_session_get_num_active_sources (RTPSession * sess)
1498 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1500 RTP_SESSION_LOCK (sess);
1501 result = sess->stats.active_sources;
1502 RTP_SESSION_UNLOCK (sess);
1508 * rtp_session_get_source_by_ssrc:
1509 * @sess: an #RTPSession
1512 * Find the source with @ssrc in @sess.
1514 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1515 * g_object_unref() after usage.
1518 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1522 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1524 RTP_SESSION_LOCK (sess);
1525 result = find_source (sess, ssrc);
1527 g_object_ref (result);
1528 RTP_SESSION_UNLOCK (sess);
1533 /* should be called with the SESSION lock */
1535 rtp_session_create_new_ssrc (RTPSession * sess)
1540 ssrc = g_random_int ();
1542 /* see if it exists in the session, we're done if it doesn't */
1543 if (find_source (sess, ssrc) == NULL)
1551 * rtp_session_create_source:
1552 * @sess: an #RTPSession
1554 * Create an #RTPSource for use in @sess. This function will create a source
1555 * with an ssrc that is currently not used by any participants in the session.
1557 * Returns: an #RTPSource.
1560 rtp_session_create_source (RTPSession * sess)
1565 RTP_SESSION_LOCK (sess);
1566 ssrc = rtp_session_create_new_ssrc (sess);
1567 source = rtp_source_new (ssrc);
1568 rtp_source_set_callbacks (source, &callbacks, sess);
1569 /* we need an additional ref for the source in the hashtable */
1570 g_object_ref (source);
1571 add_source (sess, source);
1572 RTP_SESSION_UNLOCK (sess);
1577 /* update the RTPArrivalStats structure with the current time and other bits
1578 * about the current buffer we are handling.
1579 * This function is typically called when a validated packet is received.
1580 * This function should be called with the SESSION_LOCK
1583 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
1584 gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
1585 GstClockTime running_time, guint64 ntpnstime)
1587 GstNetAddressMeta *meta;
1588 GstRTPBuffer rtpb = { NULL };
1590 /* get time of arrival */
1591 arrival->current_time = current_time;
1592 arrival->running_time = running_time;
1593 arrival->ntpnstime = ntpnstime;
1595 /* get packet size including header overhead */
1596 arrival->bytes = gst_buffer_get_size (buffer) + sess->header_len;
1599 gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtpb);
1600 arrival->payload_len = gst_rtp_buffer_get_payload_len (&rtpb);
1601 gst_rtp_buffer_unmap (&rtpb);
1603 arrival->payload_len = 0;
1606 /* for netbuffer we can store the IP address to check for collisions */
1607 meta = gst_buffer_get_net_address_meta (buffer);
1608 if (arrival->address)
1609 g_object_unref (arrival->address);
1611 arrival->address = G_SOCKET_ADDRESS (g_object_ref (meta->addr));
1613 arrival->address = NULL;
1618 clean_arrival_stats (RTPArrivalStats * arrival)
1620 if (arrival->address)
1621 g_object_unref (arrival->address);
1625 * rtp_session_process_rtp:
1626 * @sess: and #RTPSession
1627 * @buffer: an RTP buffer
1628 * @current_time: the current system time
1629 * @running_time: the running_time of @buffer
1631 * Process an RTP buffer in the session manager. This function takes ownership
1634 * Returns: a #GstFlowReturn.
1637 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1638 GstClockTime current_time, GstClockTime running_time)
1640 GstFlowReturn result;
1644 gboolean prevsender, prevactive;
1645 RTPArrivalStats arrival = { NULL, };
1649 GstRTPBuffer rtp = { NULL };
1651 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1652 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1654 if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp))
1655 goto invalid_packet;
1657 RTP_SESSION_LOCK (sess);
1658 /* ignore more RTP packets when we left the session */
1659 if (sess->source->marked_bye)
1662 /* update arrival stats */
1663 update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1666 /* get SSRC and look up in session database */
1667 ssrc = gst_rtp_buffer_get_ssrc (&rtp);
1668 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1672 /* copy available csrc for later */
1673 count = gst_rtp_buffer_get_csrc_count (&rtp);
1674 /* make sure to not overflow our array. An RTP buffer can maximally contain
1676 count = MIN (count, 16);
1678 for (i = 0; i < count; i++)
1679 csrcs[i] = gst_rtp_buffer_get_csrc (&rtp, i);
1681 gst_rtp_buffer_unmap (&rtp);
1683 prevsender = RTP_SOURCE_IS_SENDER (source);
1684 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1685 oldrate = source->bitrate;
1687 /* let source process the packet */
1688 result = rtp_source_process_rtp (source, buffer, &arrival);
1690 /* source became active */
1691 if (prevactive != RTP_SOURCE_IS_ACTIVE (source)) {
1692 sess->stats.active_sources++;
1693 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1694 sess->stats.active_sources);
1695 on_ssrc_validated (sess, source);
1697 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1698 sess->stats.sender_sources++;
1699 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1700 sess->stats.sender_sources);
1702 if (oldrate != source->bitrate)
1703 sess->recalc_bandwidth = TRUE;
1706 on_new_ssrc (sess, source);
1708 if (source->validated) {
1711 /* for validated sources, we add the CSRCs as well */
1712 for (i = 0; i < count; i++) {
1714 RTPSource *csrc_src;
1719 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1724 GST_DEBUG ("created new CSRC: %08x", csrc);
1725 rtp_source_set_as_csrc (csrc_src);
1726 if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1727 sess->stats.active_sources++;
1728 on_new_ssrc (sess, csrc_src);
1730 g_object_unref (csrc_src);
1733 g_object_unref (source);
1735 RTP_SESSION_UNLOCK (sess);
1737 clean_arrival_stats (&arrival);
1744 gst_buffer_unref (buffer);
1745 GST_DEBUG ("invalid RTP packet received");
1750 RTP_SESSION_UNLOCK (sess);
1751 gst_rtp_buffer_unmap (&rtp);
1752 gst_buffer_unref (buffer);
1753 GST_DEBUG ("ignoring RTP packet because we are leaving");
1758 RTP_SESSION_UNLOCK (sess);
1759 gst_rtp_buffer_unmap (&rtp);
1760 gst_buffer_unref (buffer);
1761 clean_arrival_stats (&arrival);
1762 GST_DEBUG ("ignoring packet because its collisioning");
1768 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1769 GstRTCPPacket * packet, RTPArrivalStats * arrival)
1773 count = gst_rtcp_packet_get_rb_count (packet);
1774 for (i = 0; i < count; i++) {
1775 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1776 guint8 fractionlost;
1779 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1780 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1782 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1784 if (ssrc == sess->source->ssrc) {
1785 /* only deal with report blocks for our session, we update the stats of
1786 * the sender of the RTCP message. We could also compare our stats against
1787 * the other sender to see if we are better or worse. */
1788 rtp_source_process_rb (source, arrival->ntpnstime, fractionlost,
1789 packetslost, exthighestseq, jitter, lsr, dlsr);
1792 on_ssrc_active (sess, source);
1795 /* A Sender report contains statistics about how the sender is doing. This
1796 * includes timing informataion such as the relation between RTP and NTP
1797 * timestamps and the number of packets/bytes it sent to us.
1799 * In this report is also included a set of report blocks related to how this
1800 * sender is receiving data (in case we (or somebody else) is also sending stuff
1801 * to it). This info includes the packet loss, jitter and seqnum. It also
1802 * contains information to calculate the round trip time (LSR/DLSR).
1805 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1806 RTPArrivalStats * arrival, gboolean * do_sync)
1808 guint32 senderssrc, rtptime, packet_count, octet_count;
1811 gboolean created, prevsender;
1813 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1814 &packet_count, &octet_count);
1816 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1817 senderssrc, GST_TIME_ARGS (arrival->current_time));
1819 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1823 /* don't try to do lip-sync for sources that sent a BYE */
1824 if (RTP_SOURCE_IS_MARKED_BYE (source))
1829 prevsender = RTP_SOURCE_IS_SENDER (source);
1831 /* first update the source */
1832 rtp_source_process_sr (source, arrival->current_time, ntptime, rtptime,
1833 packet_count, octet_count);
1835 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1836 sess->stats.sender_sources++;
1837 GST_DEBUG ("source: %08x became sender, %d sender sources", senderssrc,
1838 sess->stats.sender_sources);
1842 on_new_ssrc (sess, source);
1844 rtp_session_process_rb (sess, source, packet, arrival);
1845 g_object_unref (source);
1848 /* A receiver report contains statistics about how a receiver is doing. It
1849 * includes stuff like packet loss, jitter and the seqnum it received last. It
1850 * also contains info to calculate the round trip time.
1852 * We are only interested in how the sender of this report is doing wrt to us.
1855 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1856 RTPArrivalStats * arrival)
1862 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1864 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1866 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1871 on_new_ssrc (sess, source);
1873 rtp_session_process_rb (sess, source, packet, arrival);
1874 g_object_unref (source);
1877 /* Get SDES items and store them in the SSRC */
1879 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1880 RTPArrivalStats * arrival)
1883 gboolean more_items, more_entries;
1885 items = gst_rtcp_packet_sdes_get_item_count (packet);
1886 GST_DEBUG ("got SDES packet with %d items", items);
1888 more_items = gst_rtcp_packet_sdes_first_item (packet);
1890 while (more_items) {
1892 gboolean changed, created, validated;
1896 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1898 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1902 /* find src, no probation when dealing with RTCP */
1903 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1907 sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
1909 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1911 while (more_entries) {
1912 GstRTCPSDESType type;
1918 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1920 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1923 if (type == GST_RTCP_SDES_PRIV) {
1924 name = g_strndup ((const gchar *) &data[1], data[0]);
1926 data += data[0] + 1;
1928 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
1931 value = g_strndup ((const gchar *) data, len);
1933 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
1938 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1942 /* takes ownership of sdes */
1943 changed = rtp_source_set_sdes_struct (source, sdes);
1945 validated = !RTP_SOURCE_IS_ACTIVE (source);
1946 source->validated = TRUE;
1949 on_new_ssrc (sess, source);
1951 /* source became active */
1953 sess->stats.active_sources++;
1954 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1955 sess->stats.active_sources);
1956 on_ssrc_validated (sess, source);
1960 on_ssrc_sdes (sess, source);
1962 g_object_unref (source);
1964 more_items = gst_rtcp_packet_sdes_next_item (packet);
1969 /* BYE is sent when a client leaves the session
1972 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
1973 RTPArrivalStats * arrival)
1977 gboolean reconsider = FALSE;
1979 reason = gst_rtcp_packet_bye_get_reason (packet);
1980 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
1982 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
1983 for (i = 0; i < count; i++) {
1986 gboolean created, prevactive, prevsender;
1987 guint pmembers, members;
1989 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
1990 GST_DEBUG ("SSRC: %08x", ssrc);
1992 if (ssrc == sess->source->ssrc)
1995 /* find src and mark bye, no probation when dealing with RTCP */
1996 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
2000 /* store time for when we need to time out this source */
2001 source->bye_time = arrival->current_time;
2003 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2004 prevsender = RTP_SOURCE_IS_SENDER (source);
2006 /* mark the source BYE */
2007 rtp_source_mark_bye (source, reason);
2009 pmembers = sess->stats.active_sources;
2011 if (prevactive && !RTP_SOURCE_IS_ACTIVE (source)) {
2012 sess->stats.active_sources--;
2013 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
2014 sess->stats.active_sources);
2016 if (prevsender && !RTP_SOURCE_IS_SENDER (source)) {
2017 sess->stats.sender_sources--;
2018 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
2019 sess->stats.sender_sources);
2021 members = sess->stats.active_sources;
2023 if (!sess->source->marked_bye && members < pmembers) {
2024 /* some members went away since the previous timeout estimate.
2025 * Perform reverse reconsideration but only when we are not scheduling a
2027 if (sess->next_rtcp_check_time != GST_CLOCK_TIME_NONE &&
2028 arrival->current_time < sess->next_rtcp_check_time) {
2029 GstClockTime time_remaining;
2031 time_remaining = sess->next_rtcp_check_time - arrival->current_time;
2032 sess->next_rtcp_check_time =
2033 gst_util_uint64_scale (time_remaining, members, pmembers);
2035 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
2036 GST_TIME_ARGS (sess->next_rtcp_check_time));
2038 sess->next_rtcp_check_time += arrival->current_time;
2040 /* mark pending reconsider. We only want to signal the reconsideration
2041 * once after we handled all the source in the bye packet */
2047 on_new_ssrc (sess, source);
2049 on_bye_ssrc (sess, source);
2051 g_object_unref (source);
2054 RTP_SESSION_UNLOCK (sess);
2055 /* notify app of reconsideration */
2056 if (sess->callbacks.reconsider)
2057 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2058 RTP_SESSION_LOCK (sess);
2064 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
2065 RTPArrivalStats * arrival)
2067 GST_DEBUG ("received APP");
2071 rtp_session_request_local_key_unit (RTPSession * sess, RTPSource * src,
2072 gboolean fir, GstClockTime current_time)
2074 guint32 round_trip = 0;
2076 rtp_source_get_last_rb (src, NULL, NULL, NULL, NULL, NULL, NULL, &round_trip);
2078 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE && round_trip) {
2079 GstClockTime round_trip_in_ns = gst_util_uint64_scale (round_trip,
2082 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE &&
2083 current_time - sess->last_keyframe_request < 2 * round_trip_in_ns) {
2084 GST_DEBUG ("Ignoring %s request because one was send without one "
2085 "RTT (%" GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")",
2086 fir ? "FIR" : "PLI",
2087 GST_TIME_ARGS (current_time - sess->last_keyframe_request),
2088 GST_TIME_ARGS (round_trip_in_ns));;
2093 sess->last_keyframe_request = current_time;
2095 GST_LOG ("received %s request from %X %p(%p)", fir ? "FIR" : "PLI",
2096 rtp_source_get_ssrc (src), sess->callbacks.process_rtp,
2097 sess->callbacks.request_key_unit);
2099 RTP_SESSION_UNLOCK (sess);
2100 sess->callbacks.request_key_unit (sess, fir,
2101 sess->request_key_unit_user_data);
2102 RTP_SESSION_LOCK (sess);
2108 rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
2109 guint32 media_ssrc, GstClockTime current_time)
2113 if (!sess->callbacks.request_key_unit)
2116 src = find_source (sess, sender_ssrc);
2120 rtp_session_request_local_key_unit (sess, src, FALSE, current_time);
2124 rtp_session_process_fir (RTPSession * sess, guint32 sender_ssrc,
2125 guint8 * fci_data, guint fci_length, GstClockTime current_time)
2130 gboolean our_request = FALSE;
2132 if (!sess->callbacks.request_key_unit)
2138 src = find_source (sess, sender_ssrc);
2140 /* Hack because Google fails to set the sender_ssrc correctly */
2141 if (!src && sender_ssrc == 1) {
2142 GHashTableIter iter;
2144 if (sess->stats.sender_sources >
2145 RTP_SOURCE_IS_SENDER (sess->source) ? 2 : 1)
2148 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
2150 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
2151 if (src != sess->source && rtp_source_is_sender (src))
2160 for (position = 0; position < fci_length; position += 8) {
2161 guint8 *data = fci_data + position;
2163 ssrc = GST_READ_UINT32_BE (data);
2165 if (ssrc == rtp_source_get_ssrc (sess->source)) {
2173 rtp_session_request_local_key_unit (sess, src, TRUE, current_time);
2177 rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
2178 RTPArrivalStats * arrival, GstClockTime current_time)
2180 GstRTCPType type = gst_rtcp_packet_get_type (packet);
2181 GstRTCPFBType fbtype = gst_rtcp_packet_fb_get_type (packet);
2182 guint32 sender_ssrc = gst_rtcp_packet_fb_get_sender_ssrc (packet);
2183 guint32 media_ssrc = gst_rtcp_packet_fb_get_media_ssrc (packet);
2184 guint8 *fci_data = gst_rtcp_packet_fb_get_fci (packet);
2185 guint fci_length = 4 * gst_rtcp_packet_fb_get_fci_length (packet);
2187 GST_DEBUG ("received feedback %d:%d from %08X about %08X with FCI of "
2188 "length %d", type, fbtype, sender_ssrc, media_ssrc, fci_length);
2190 if (g_signal_has_handler_pending (sess,
2191 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0, TRUE)) {
2192 GstBuffer *fci_buffer = NULL;
2194 if (fci_length > 0) {
2195 fci_buffer = gst_buffer_copy_region (packet->rtcp->buffer,
2196 GST_BUFFER_COPY_MEMORY, fci_data - packet->rtcp->map.data,
2198 GST_BUFFER_TIMESTAMP (fci_buffer) = arrival->running_time;
2201 RTP_SESSION_UNLOCK (sess);
2202 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0,
2203 type, fbtype, sender_ssrc, media_ssrc, fci_buffer);
2204 RTP_SESSION_LOCK (sess);
2207 gst_buffer_unref (fci_buffer);
2210 if (sess->rtcp_feedback_retention_window) {
2211 RTPSource *src = find_source (sess, media_ssrc);
2214 rtp_source_retain_rtcp_packet (src, packet, arrival->running_time);
2217 if (rtp_source_get_ssrc (sess->source) == media_ssrc ||
2218 /* PSFB FIR puts the media ssrc inside the FCI */
2219 (type == GST_RTCP_TYPE_PSFB && fbtype == GST_RTCP_PSFB_TYPE_FIR)) {
2221 case GST_RTCP_TYPE_PSFB:
2223 case GST_RTCP_PSFB_TYPE_PLI:
2224 rtp_session_process_pli (sess, sender_ssrc, media_ssrc,
2227 case GST_RTCP_PSFB_TYPE_FIR:
2228 rtp_session_process_fir (sess, sender_ssrc, fci_data, fci_length,
2235 case GST_RTCP_TYPE_RTPFB:
2243 * rtp_session_process_rtcp:
2244 * @sess: and #RTPSession
2245 * @buffer: an RTCP buffer
2246 * @current_time: the current system time
2247 * @ntpnstime: the current NTP time in nanoseconds
2249 * Process an RTCP buffer in the session manager. This function takes ownership
2252 * Returns: a #GstFlowReturn.
2255 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
2256 GstClockTime current_time, guint64 ntpnstime)
2258 GstRTCPPacket packet;
2259 gboolean more, is_bye = FALSE, do_sync = FALSE;
2260 RTPArrivalStats arrival = { NULL, };
2261 GstFlowReturn result = GST_FLOW_OK;
2262 GstRTCPBuffer rtcp = { NULL, };
2264 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2265 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2267 if (!gst_rtcp_buffer_validate (buffer))
2268 goto invalid_packet;
2270 GST_DEBUG ("received RTCP packet");
2272 RTP_SESSION_LOCK (sess);
2273 /* update arrival stats */
2274 update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1,
2277 if (sess->source->sent_bye)
2280 /* start processing the compound packet */
2281 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
2282 more = gst_rtcp_buffer_get_first_packet (&rtcp, &packet);
2286 type = gst_rtcp_packet_get_type (&packet);
2288 /* when we are leaving the session, we should ignore all non-BYE messages */
2289 if (sess->source->marked_bye && type != GST_RTCP_TYPE_BYE) {
2290 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
2295 case GST_RTCP_TYPE_SR:
2296 rtp_session_process_sr (sess, &packet, &arrival, &do_sync);
2298 case GST_RTCP_TYPE_RR:
2299 rtp_session_process_rr (sess, &packet, &arrival);
2301 case GST_RTCP_TYPE_SDES:
2302 rtp_session_process_sdes (sess, &packet, &arrival);
2304 case GST_RTCP_TYPE_BYE:
2306 /* don't try to attempt lip-sync anymore for streams with a BYE */
2308 rtp_session_process_bye (sess, &packet, &arrival);
2310 case GST_RTCP_TYPE_APP:
2311 rtp_session_process_app (sess, &packet, &arrival);
2313 case GST_RTCP_TYPE_RTPFB:
2314 case GST_RTCP_TYPE_PSFB:
2315 rtp_session_process_feedback (sess, &packet, &arrival, current_time);
2318 GST_WARNING ("got unknown RTCP packet");
2322 more = gst_rtcp_packet_move_to_next (&packet);
2325 gst_rtcp_buffer_unmap (&rtcp);
2327 /* if we are scheduling a BYE, we only want to count bye packets, else we
2328 * count everything */
2329 if (sess->source->marked_bye) {
2331 sess->stats.bye_members++;
2332 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2335 /* keep track of average packet size */
2336 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2338 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
2339 sess->stats.avg_rtcp_packet_size, arrival.bytes);
2340 RTP_SESSION_UNLOCK (sess);
2342 clean_arrival_stats (&arrival);
2344 /* notify caller of sr packets in the callback */
2345 if (do_sync && sess->callbacks.sync_rtcp) {
2346 /* make writable, we might want to change the buffer */
2347 buffer = gst_buffer_make_writable (buffer);
2349 result = sess->callbacks.sync_rtcp (sess, buffer,
2350 sess->sync_rtcp_user_data);
2352 gst_buffer_unref (buffer);
2359 GST_DEBUG ("invalid RTCP packet received");
2360 gst_buffer_unref (buffer);
2365 RTP_SESSION_UNLOCK (sess);
2366 gst_buffer_unref (buffer);
2367 clean_arrival_stats (&arrival);
2368 GST_DEBUG ("ignoring RTCP packet because we left");
2374 * rtp_session_update_send_caps:
2375 * @sess: an #RTPSession
2378 * Update the caps of the sender in the rtp session.
2381 rtp_session_update_send_caps (RTPSession * sess, GstCaps * caps)
2383 g_return_if_fail (RTP_IS_SESSION (sess));
2384 g_return_if_fail (GST_IS_CAPS (caps));
2386 GST_LOG ("received caps %" GST_PTR_FORMAT, caps);
2388 RTP_SESSION_LOCK (sess);
2389 rtp_source_update_caps (sess->source, caps);
2390 RTP_SESSION_UNLOCK (sess);
2394 * rtp_session_send_rtp:
2395 * @sess: an #RTPSession
2396 * @data: pointer to either an RTP buffer or a list of RTP buffers
2397 * @is_list: TRUE when @data is a buffer list
2398 * @current_time: the current system time
2399 * @running_time: the running time of @data
2401 * Send the RTP buffer in the session manager. This function takes ownership of
2404 * Returns: a #GstFlowReturn.
2407 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
2408 GstClockTime current_time, GstClockTime running_time)
2410 GstFlowReturn result;
2412 gboolean prevsender;
2415 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2416 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
2418 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
2420 RTP_SESSION_LOCK (sess);
2421 source = sess->source;
2423 /* update last activity */
2424 source->last_rtp_activity = current_time;
2426 prevsender = RTP_SOURCE_IS_SENDER (source);
2427 oldrate = source->bitrate;
2429 /* we use our own source to send */
2430 result = rtp_source_send_rtp (source, data, is_list, running_time);
2432 if (RTP_SOURCE_IS_SENDER (source) && !prevsender)
2433 sess->stats.sender_sources++;
2434 if (oldrate != source->bitrate)
2435 sess->recalc_bandwidth = TRUE;
2436 RTP_SESSION_UNLOCK (sess);
2442 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
2444 *bandwidth += source->bitrate;
2447 /* must be called with session lock */
2449 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2452 GstClockTime result;
2454 /* recalculate bandwidth when it changed */
2455 if (sess->recalc_bandwidth) {
2458 if (sess->bandwidth > 0)
2459 bandwidth = sess->bandwidth;
2461 /* If it is <= 0, then try to estimate the actual bandwidth */
2462 bandwidth = sess->source->bitrate;
2464 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2465 (GHFunc) add_bitrates, &bandwidth);
2468 if (bandwidth < 8000)
2469 bandwidth = RTP_STATS_BANDWIDTH;
2471 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
2472 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
2474 sess->recalc_bandwidth = FALSE;
2477 if (sess->source->marked_bye) {
2478 result = rtp_stats_calculate_bye_interval (&sess->stats);
2480 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2481 RTP_SOURCE_IS_SENDER (sess->source), first);
2484 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2485 GST_TIME_ARGS (result), first);
2487 if (!deterministic && result != GST_CLOCK_TIME_NONE)
2488 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2490 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2495 /* Stop the current @sess and schedule a BYE message for the other members.
2496 * One must have the session lock to call this function
2498 static GstFlowReturn
2499 rtp_session_schedule_bye_locked (RTPSession * sess, const gchar * reason,
2500 GstClockTime current_time)
2502 GstFlowReturn result = GST_FLOW_OK;
2504 GstClockTime interval;
2506 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2508 source = sess->source;
2510 /* ignore more BYEs */
2511 if (source->marked_bye)
2514 /* we have BYE now */
2515 rtp_source_mark_bye (source, reason);
2516 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
2517 sess->stats.bye_members = 1;
2518 sess->first_rtcp = TRUE;
2519 sess->allow_early = TRUE;
2521 /* reschedule transmission */
2522 sess->last_rtcp_send_time = current_time;
2523 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2525 if (interval != GST_CLOCK_TIME_NONE)
2526 sess->next_rtcp_check_time = current_time + interval;
2528 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
2530 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2531 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2533 RTP_SESSION_UNLOCK (sess);
2534 /* notify app of reconsideration */
2535 if (sess->callbacks.reconsider)
2536 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2537 RTP_SESSION_LOCK (sess);
2544 * rtp_session_schedule_bye:
2545 * @sess: an #RTPSession
2546 * @reason: a reason or NULL
2547 * @current_time: the current system time
2549 * Stop the current @sess and schedule a BYE message for the other members.
2551 * Returns: a #GstFlowReturn.
2554 rtp_session_schedule_bye (RTPSession * sess, const gchar * reason,
2555 GstClockTime current_time)
2557 GstFlowReturn result = GST_FLOW_OK;
2559 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2561 RTP_SESSION_LOCK (sess);
2562 result = rtp_session_schedule_bye_locked (sess, reason, current_time);
2563 RTP_SESSION_UNLOCK (sess);
2569 * rtp_session_next_timeout:
2570 * @sess: an #RTPSession
2571 * @current_time: the current system time
2573 * Get the next time we should perform session maintenance tasks.
2575 * Returns: a time when rtp_session_on_timeout() should be called with the
2576 * current system time.
2579 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2581 GstClockTime result, interval = 0;
2583 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
2585 RTP_SESSION_LOCK (sess);
2587 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
2588 result = sess->next_early_rtcp_time;
2592 result = sess->next_rtcp_check_time;
2594 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2595 ", next time: %" GST_TIME_FORMAT,
2596 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2598 if (result == GST_CLOCK_TIME_NONE || result < current_time) {
2599 GST_DEBUG ("take current time as base");
2600 /* our previous check time expired, start counting from the current time
2602 result = current_time;
2605 if (sess->source->marked_bye) {
2606 if (sess->source->sent_bye) {
2607 GST_DEBUG ("we sent BYE already");
2608 interval = GST_CLOCK_TIME_NONE;
2609 } else if (sess->stats.active_sources >= 50) {
2610 GST_DEBUG ("reconsider BYE, more than 50 sources");
2611 /* reconsider BYE if members >= 50 */
2612 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2615 if (sess->first_rtcp) {
2616 GST_DEBUG ("first RTCP packet");
2617 /* we are called for the first time */
2618 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2619 } else if (sess->next_rtcp_check_time < current_time) {
2620 GST_DEBUG ("old check time expired, getting new timeout");
2621 /* get a new timeout when we need to */
2622 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
2626 if (interval != GST_CLOCK_TIME_NONE)
2629 result = GST_CLOCK_TIME_NONE;
2631 sess->next_rtcp_check_time = result;
2635 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2636 ", next time: %" GST_TIME_FORMAT,
2637 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2638 RTP_SESSION_UNLOCK (sess);
2645 GstRTCPBuffer rtcpbuf;
2648 GstClockTime current_time;
2650 GstClockTime running_time;
2651 GstClockTime interval;
2652 GstRTCPPacket packet;
2656 gboolean may_suppress;
2660 session_start_rtcp (RTPSession * sess, ReportData * data)
2662 GstRTCPPacket *packet = &data->packet;
2663 RTPSource *own = sess->source;
2664 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2666 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2668 gst_rtcp_buffer_map (data->rtcp, GST_MAP_READWRITE, rtcp);
2670 if (RTP_SOURCE_IS_SENDER (own)) {
2673 guint32 packet_count, octet_count;
2675 /* we are a sender, create SR */
2676 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2677 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SR, packet);
2679 /* get latest stats */
2680 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
2681 &ntptime, &rtptime, &packet_count, &octet_count);
2683 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2684 packet_count, octet_count);
2686 /* fill in sender report info */
2687 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2688 ntptime, rtptime, packet_count, octet_count);
2690 /* we are only receiver, create RR */
2691 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2692 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RR, packet);
2693 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2697 /* construct a Sender or Receiver Report */
2699 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2701 RTPSession *sess = data->sess;
2702 GstRTCPPacket *packet = &data->packet;
2704 /* create a new buffer if needed */
2705 if (data->rtcp == NULL) {
2706 session_start_rtcp (sess, data);
2707 } else if (data->is_early) {
2708 /* Put a single RR or SR in minimal compound packets */
2711 if (gst_rtcp_packet_get_rb_count (packet) < GST_RTCP_MAX_RB_COUNT) {
2712 /* only report about other sender sources */
2713 if (source != sess->source && RTP_SOURCE_IS_SENDER (source)) {
2714 guint8 fractionlost;
2716 guint32 exthighestseq, jitter;
2720 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2721 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2723 /* store last generated RR packet */
2724 source->last_rr.is_valid = TRUE;
2725 source->last_rr.fractionlost = fractionlost;
2726 source->last_rr.packetslost = packetslost;
2727 source->last_rr.exthighestseq = exthighestseq;
2728 source->last_rr.jitter = jitter;
2729 source->last_rr.lsr = lsr;
2730 source->last_rr.dlsr = dlsr;
2732 /* packet is not yet filled, add report block for this source. */
2733 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2734 exthighestseq, jitter, lsr, dlsr);
2739 /* perform cleanup of sources that timed out */
2741 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2743 gboolean remove = FALSE;
2744 gboolean byetimeout = FALSE;
2745 gboolean sendertimeout = FALSE;
2746 gboolean is_sender, is_active;
2747 RTPSession *sess = data->sess;
2748 GstClockTime interval, binterval;
2751 is_sender = RTP_SOURCE_IS_SENDER (source);
2752 is_active = RTP_SOURCE_IS_ACTIVE (source);
2754 /* nothing to do when without RTCP */
2755 if (data->interval == GST_CLOCK_TIME_NONE)
2758 /* our own rtcp interval may have been forced low by secondary configuration,
2759 * while sender side may still operate with higher interval,
2760 * so do not just take our interval to decide on timing out sender,
2761 * but take (if data->interval <= 5 * GST_SECOND):
2762 * interval = CLAMP (sender_interval, data->interval, 5 * GST_SECOND)
2763 * where sender_interval is difference between last 2 received RTCP reports
2765 if (data->interval >= 5 * GST_SECOND || (source == sess->source)) {
2766 binterval = data->interval;
2768 GST_LOG ("prev_rtcp %" GST_TIME_FORMAT ", last_rtcp %" GST_TIME_FORMAT,
2769 GST_TIME_ARGS (source->stats.prev_rtcptime),
2770 GST_TIME_ARGS (source->stats.last_rtcptime));
2771 /* if not received enough yet, fallback to larger default */
2772 if (source->stats.last_rtcptime > source->stats.prev_rtcptime)
2773 binterval = source->stats.last_rtcptime - source->stats.prev_rtcptime;
2775 binterval = 5 * GST_SECOND;
2776 binterval = CLAMP (binterval, data->interval, 5 * GST_SECOND);
2778 GST_LOG ("timeout base interval %" GST_TIME_FORMAT,
2779 GST_TIME_ARGS (binterval));
2781 /* check for our own source, we don't want to delete our own source. */
2782 if (!(source == sess->source)) {
2783 if (source->marked_bye) {
2784 /* if we received a BYE from the source, remove the source after some
2786 if (data->current_time > source->bye_time &&
2787 data->current_time - source->bye_time > sess->stats.bye_timeout) {
2788 GST_DEBUG ("removing BYE source %08x", source->ssrc);
2793 /* sources that were inactive for more than 5 times the deterministic reporting
2794 * interval get timed out. the min timeout is 5 seconds. */
2795 /* mind old time that might pre-date last time going to PLAYING */
2796 btime = MAX (source->last_activity, sess->start_time);
2797 if (data->current_time > btime) {
2798 interval = MAX (binterval * 5, 5 * GST_SECOND);
2799 if (data->current_time - btime > interval) {
2800 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2801 source->ssrc, GST_TIME_ARGS (btime));
2807 /* senders that did not send for a long time become a receiver, this also
2808 * holds for our own source. */
2810 /* mind old time that might pre-date last time going to PLAYING */
2811 btime = MAX (source->last_rtp_activity, sess->start_time);
2812 if (data->current_time > btime) {
2813 interval = MAX (binterval * 2, 5 * GST_SECOND);
2814 if (data->current_time - btime > interval) {
2815 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
2816 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
2817 source->is_sender = FALSE;
2818 sess->stats.sender_sources--;
2819 sendertimeout = TRUE;
2825 sess->total_sources--;
2827 sess->stats.sender_sources--;
2829 sess->stats.active_sources--;
2832 on_bye_timeout (sess, source);
2834 on_timeout (sess, source);
2837 on_sender_timeout (sess, source);
2840 source->closing = remove;
2844 session_sdes (RTPSession * sess, ReportData * data)
2846 GstRTCPPacket *packet = &data->packet;
2847 const GstStructure *sdes;
2849 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2851 /* add SDES packet */
2852 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SDES, packet);
2854 gst_rtcp_packet_sdes_add_item (packet, sess->source->ssrc);
2856 sdes = rtp_source_get_sdes_struct (sess->source);
2858 /* add all fields in the structure, the order is not important. */
2859 n_fields = gst_structure_n_fields (sdes);
2860 for (i = 0; i < n_fields; ++i) {
2863 GstRTCPSDESType type;
2865 field = gst_structure_nth_field_name (sdes, i);
2868 value = gst_structure_get_string (sdes, field);
2871 type = gst_rtcp_sdes_name_to_type (field);
2873 /* Early packets are minimal and only include the CNAME */
2874 if (data->is_early && type != GST_RTCP_SDES_CNAME)
2877 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
2878 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
2879 (const guint8 *) value);
2880 } else if (type == GST_RTCP_SDES_PRIV) {
2886 /* don't accept entries that are too big */
2887 prefix_len = strlen (field);
2888 if (prefix_len > 255)
2890 value_len = strlen (value);
2891 if (value_len > 255)
2893 data_len = 1 + prefix_len + value_len;
2897 data[0] = prefix_len;
2898 memcpy (&data[1], field, prefix_len);
2899 memcpy (&data[1 + prefix_len], value, value_len);
2901 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
2905 data->has_sdes = TRUE;
2908 /* schedule a BYE packet */
2910 session_bye (RTPSession * sess, ReportData * data)
2912 GstRTCPPacket *packet = &data->packet;
2913 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2914 RTPSource *source = sess->source;
2917 session_start_rtcp (sess, data);
2920 session_sdes (sess, data);
2922 /* add a BYE packet */
2923 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_BYE, packet);
2924 gst_rtcp_packet_bye_add_ssrc (packet, source->ssrc);
2925 if (source->bye_reason)
2926 gst_rtcp_packet_bye_set_reason (packet, source->bye_reason);
2928 /* we have a BYE packet now */
2929 data->is_bye = TRUE;
2933 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
2935 GstClockTime new_send_time, elapsed;
2937 if (data->is_early && sess->next_early_rtcp_time < current_time)
2940 /* no need to check yet */
2941 if (sess->next_rtcp_check_time == GST_CLOCK_TIME_NONE ||
2942 sess->next_rtcp_check_time > current_time) {
2943 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
2944 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
2945 GST_TIME_ARGS (current_time));
2949 /* get elapsed time since we last reported */
2950 elapsed = current_time - sess->last_rtcp_send_time;
2952 new_send_time = data->interval;
2953 /* perform forward reconsideration */
2954 if (new_send_time != GST_CLOCK_TIME_NONE) {
2955 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, new_send_time);
2957 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
2958 GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time),
2959 GST_TIME_ARGS (elapsed));
2961 new_send_time += sess->last_rtcp_send_time;
2964 /* check if reconsideration */
2965 if (new_send_time == GST_CLOCK_TIME_NONE || current_time < new_send_time) {
2966 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
2967 GST_TIME_ARGS (new_send_time));
2968 /* store new check time */
2969 sess->next_rtcp_check_time = new_send_time;
2975 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
2977 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
2978 GST_TIME_ARGS (new_send_time));
2980 sess->next_rtcp_check_time = new_send_time;
2981 if (new_send_time != GST_CLOCK_TIME_NONE) {
2982 sess->next_rtcp_check_time += current_time;
2984 /* Apply the rules from RFC 4585 section 3.5.3 */
2985 if (sess->stats.min_interval != 0 && !sess->first_rtcp) {
2986 GstClockTimeDiff T_rr_current_interval =
2987 g_random_double_range (0.5, 1.5) * sess->stats.min_interval;
2989 /* This will caused the RTCP to be suppressed if no FB packets are added */
2990 if (sess->last_rtcp_send_time + T_rr_current_interval >
2991 sess->next_rtcp_check_time) {
2992 GST_DEBUG ("RTCP packet could be suppressed min: %" GST_TIME_FORMAT
2993 " last: %" GST_TIME_FORMAT
2994 " + T_rr_current_interval: %" GST_TIME_FORMAT
2995 " > sess->next_rtcp_check_time: %" GST_TIME_FORMAT,
2996 GST_TIME_ARGS (sess->stats.min_interval),
2997 GST_TIME_ARGS (sess->last_rtcp_send_time),
2998 GST_TIME_ARGS (T_rr_current_interval),
2999 GST_TIME_ARGS (sess->next_rtcp_check_time));
3000 data->may_suppress = TRUE;
3009 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
3011 g_hash_table_insert (hash_table, key, g_object_ref (source));
3015 remove_closing_sources (const gchar * key, RTPSource * source, gpointer * data)
3017 return source->closing;
3021 * rtp_session_on_timeout:
3022 * @sess: an #RTPSession
3023 * @current_time: the current system time
3024 * @ntpnstime: the current NTP time in nanoseconds
3025 * @running_time: the current running_time of the pipeline
3027 * Perform maintenance actions after the timeout obtained with
3028 * rtp_session_next_timeout() expired.
3030 * This function will perform timeouts of receivers and senders, send a BYE
3031 * packet or generate RTCP packets with current session stats.
3033 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
3034 * times, for each packet that should be processed.
3036 * Returns: a #GstFlowReturn.
3039 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
3040 guint64 ntpnstime, GstClockTime running_time)
3042 GstFlowReturn result = GST_FLOW_OK;
3043 ReportData data = { GST_RTCP_BUFFER_INIT };
3045 GHashTable *table_copy;
3046 gboolean notify = FALSE;
3048 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3050 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT
3051 ", running-time %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
3052 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (running_time));
3056 data.current_time = current_time;
3057 data.ntpnstime = ntpnstime;
3058 data.is_bye = FALSE;
3059 data.has_sdes = FALSE;
3060 data.may_suppress = FALSE;
3061 data.running_time = running_time;
3065 RTP_SESSION_LOCK (sess);
3066 /* get a new interval, we need this for various cleanups etc */
3067 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
3069 /* Make a local copy of the hashtable. We need to do this because the
3070 * cleanup stage below releases the session lock. */
3071 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
3072 (GDestroyNotify) g_object_unref);
3073 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3074 (GHFunc) clone_ssrcs_hashtable, table_copy);
3076 /* Clean up the session, mark the source for removing, this might release the
3078 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
3079 g_hash_table_destroy (table_copy);
3081 /* Now remove the marked sources */
3082 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
3083 (GHRFunc) remove_closing_sources, NULL);
3085 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3086 data.is_early = TRUE;
3088 data.is_early = FALSE;
3090 /* see if we need to generate SR or RR packets */
3091 if (is_rtcp_time (sess, current_time, &data)) {
3092 if (own->marked_bye) {
3093 /* generate BYE instead */
3094 GST_DEBUG ("generating BYE message");
3095 session_bye (sess, &data);
3096 own->sent_bye = TRUE;
3098 /* loop over all known sources and do something */
3099 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3100 (GHFunc) session_report_blocks, &data);
3105 /* we keep track of the last report time in order to timeout inactive
3106 * receivers or senders */
3107 if (!data.is_early && !data.may_suppress)
3108 sess->last_rtcp_send_time = data.current_time;
3109 sess->first_rtcp = FALSE;
3110 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
3112 /* add SDES for this source when not already added */
3114 session_sdes (sess, &data);
3117 /* check for outdated collisions */
3118 GST_DEBUG ("Timing out collisions");
3119 rtp_source_timeout (sess->source, current_time,
3120 /* "a relatively long time" -- RFC 3550 section 8.2 */
3121 RTP_STATS_MIN_INTERVAL * GST_SECOND * 10,
3122 running_time - sess->rtcp_feedback_retention_window);
3124 if (sess->change_ssrc) {
3125 GST_DEBUG ("need to change our SSRC (%08x)", own->ssrc);
3126 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
3127 GINT_TO_POINTER (own->ssrc));
3129 own->ssrc = rtp_session_create_new_ssrc (sess);
3130 rtp_source_reset (own);
3132 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
3133 GINT_TO_POINTER (own->ssrc), own);
3135 sess->change_ssrc = FALSE;
3137 GST_DEBUG ("changed our SSRC to %08x", own->ssrc);
3140 sess->allow_early = TRUE;
3142 RTP_SESSION_UNLOCK (sess);
3145 g_object_notify (G_OBJECT (sess), "internal-ssrc");
3147 /* push out the RTCP packet */
3149 gboolean do_not_suppress;
3151 gst_rtcp_buffer_unmap (&data.rtcpbuf);
3153 /* Give the user a change to add its own packet */
3154 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
3155 data.rtcp, data.is_early, &do_not_suppress);
3157 if (sess->callbacks.send_rtcp && (do_not_suppress || !data.may_suppress)) {
3160 packet_size = gst_buffer_get_size (data.rtcp) + sess->header_len;
3162 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
3163 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
3164 sess->stats.avg_rtcp_packet_size, packet_size);
3166 sess->callbacks.send_rtcp (sess, own, data.rtcp, own->sent_bye,
3167 sess->send_rtcp_user_data);
3169 GST_DEBUG ("freeing packet callback: %p"
3170 " do_not_suppress: %d may_suppress: %d",
3171 sess->callbacks.send_rtcp, do_not_suppress, data.may_suppress);
3172 gst_buffer_unref (data.rtcp);
3180 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
3181 GstClockTimeDiff max_delay)
3183 GstClockTime T_dither_max;
3185 /* Implements the algorithm described in RFC 4585 section 3.5.2 */
3187 RTP_SESSION_LOCK (sess);
3189 /* Check if already requested */
3190 /* RFC 4585 section 3.5.2 step 2 */
3191 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3194 if (!GST_CLOCK_TIME_IS_VALID (sess->next_rtcp_check_time))
3197 /* Ignore the request a scheduled packet will be in time anyway */
3198 if (current_time + max_delay > sess->next_rtcp_check_time)
3201 /* RFC 4585 section 3.5.2 step 2b */
3202 /* If the total sources is <=2, then there is only us and one peer */
3203 if (sess->total_sources <= 2) {
3206 /* Divide by 2 because l = 0.5 */
3207 T_dither_max = sess->next_rtcp_check_time - sess->last_rtcp_send_time;
3211 /* RFC 4585 section 3.5.2 step 3 */
3212 if (current_time + T_dither_max > sess->next_rtcp_check_time)
3215 /* RFC 4585 section 3.5.2 step 4
3216 * Don't send if allow_early is FALSE, but not if we are in
3217 * immediate mode, meaning we are part of a group of at most the
3218 * application-specific threshold.
3220 if (sess->total_sources > sess->rtcp_immediate_feedback_threshold &&
3221 sess->allow_early == FALSE)
3225 /* Schedule an early transmission later */
3226 sess->next_early_rtcp_time = g_random_double () * T_dither_max +
3229 /* If no dithering, schedule it for NOW */
3230 sess->next_early_rtcp_time = current_time;
3233 RTP_SESSION_UNLOCK (sess);
3235 /* notify app of need to send packet early
3236 * and therefore of timeout change */
3237 if (sess->callbacks.reconsider)
3238 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
3244 RTP_SESSION_UNLOCK (sess);
3248 rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc, GstClockTime now,
3249 gboolean fir, gint count)
3251 RTPSource *src = find_source (sess, ssrc);
3257 src->send_pli = FALSE;
3258 src->send_fir = TRUE;
3260 if (count == -1 || count != src->last_fir_count)
3261 src->current_send_fir_seqnum++;
3262 src->last_fir_count = count;
3263 } else if (!src->send_fir) {
3264 src->send_pli = TRUE;
3267 rtp_session_request_early_rtcp (sess, now, 200 * GST_MSECOND);
3273 has_pli_compare_func (gconstpointer a, gconstpointer ignored)
3275 GstRTCPPacket packet;
3276 GstRTCPBuffer rtcp = { NULL, };
3277 gboolean ret = FALSE;
3279 gst_rtcp_buffer_map ((GstBuffer *) a, GST_MAP_READ, &rtcp);
3281 if (gst_rtcp_buffer_get_first_packet (&rtcp, &packet)) {
3282 if (gst_rtcp_packet_get_type (&packet) == GST_RTCP_TYPE_PSFB &&
3283 gst_rtcp_packet_fb_get_type (&packet) == GST_RTCP_PSFB_TYPE_PLI)
3287 gst_rtcp_buffer_unmap (&rtcp);
3293 rtp_session_on_sending_rtcp (RTPSession * sess, GstBuffer * buffer,
3296 gboolean ret = FALSE;
3297 GHashTableIter iter;
3298 gpointer key, value;
3299 gboolean started_fir = FALSE;
3300 GstRTCPPacket fir_rtcppacket;
3301 GstRTCPBuffer rtcp = { NULL, };
3303 RTP_SESSION_LOCK (sess);
3305 gst_rtcp_buffer_map (buffer, GST_MAP_READWRITE, &rtcp);
3307 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
3308 while (g_hash_table_iter_next (&iter, &key, &value)) {
3309 guint media_ssrc = GPOINTER_TO_UINT (key);
3310 RTPSource *media_src = value;
3313 if (media_src->send_fir) {
3315 if (!gst_rtcp_buffer_add_packet (&rtcp, GST_RTCP_TYPE_PSFB,
3318 gst_rtcp_packet_fb_set_type (&fir_rtcppacket, GST_RTCP_PSFB_TYPE_FIR);
3319 gst_rtcp_packet_fb_set_sender_ssrc (&fir_rtcppacket,
3320 rtp_source_get_ssrc (sess->source));
3321 gst_rtcp_packet_fb_set_media_ssrc (&fir_rtcppacket, 0);
3323 if (!gst_rtcp_packet_fb_set_fci_length (&fir_rtcppacket, 2)) {
3324 gst_rtcp_packet_remove (&fir_rtcppacket);
3330 if (!gst_rtcp_packet_fb_set_fci_length (&fir_rtcppacket,
3331 !gst_rtcp_packet_fb_get_fci_length (&fir_rtcppacket) + 2))
3335 fci_data = gst_rtcp_packet_fb_get_fci (&fir_rtcppacket) -
3336 ((gst_rtcp_packet_fb_get_fci_length (&fir_rtcppacket) - 2) * 4);
3338 GST_WRITE_UINT32_BE (fci_data, media_ssrc);
3340 fci_data[0] = media_src->current_send_fir_seqnum;
3341 fci_data[1] = fci_data[2] = fci_data[3] = 0;
3342 media_src->send_fir = FALSE;
3346 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
3347 while (g_hash_table_iter_next (&iter, &key, &value)) {
3348 guint media_ssrc = GPOINTER_TO_UINT (key);
3349 RTPSource *media_src = value;
3350 GstRTCPPacket pli_rtcppacket;
3352 if (media_src->send_pli && !rtp_source_has_retained (media_src,
3353 has_pli_compare_func, NULL)) {
3354 if (!gst_rtcp_buffer_add_packet (&rtcp, GST_RTCP_TYPE_PSFB,
3356 /* Break because the packet is full, will put next request in a
3359 gst_rtcp_packet_fb_set_type (&pli_rtcppacket, GST_RTCP_PSFB_TYPE_PLI);
3360 gst_rtcp_packet_fb_set_sender_ssrc (&pli_rtcppacket,
3361 rtp_source_get_ssrc (sess->source));
3362 gst_rtcp_packet_fb_set_media_ssrc (&pli_rtcppacket, media_ssrc);
3365 media_src->send_pli = FALSE;
3367 gst_rtcp_buffer_unmap (&rtcp);
3369 RTP_SESSION_UNLOCK (sess);
3375 rtp_session_send_rtcp (RTPSession * sess, GstClockTimeDiff max_delay)
3379 if (!sess->callbacks.send_rtcp)
3382 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
3384 rtp_session_request_early_rtcp (sess, now, max_delay);