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)
464 g_mutex_init (&sess->lock);
465 sess->key = g_random_int ();
469 for (i = 0; i < 32; i++) {
471 g_hash_table_new_full (NULL, NULL, NULL,
472 (GDestroyNotify) g_object_unref);
475 rtp_stats_init_defaults (&sess->stats);
477 sess->recalc_bandwidth = TRUE;
478 sess->bandwidth = DEFAULT_BANDWIDTH;
479 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
480 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
481 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
483 /* create an active SSRC for this session manager */
484 sess->source = rtp_session_create_source (sess);
485 sess->source->validated = TRUE;
486 sess->source->internal = TRUE;
487 sess->stats.active_sources++;
488 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
489 sess->source->stats.prev_rtcptime = 0;
490 sess->source->stats.last_rtcptime = 1;
492 rtp_stats_set_min_interval (&sess->stats,
493 (gdouble) DEFAULT_RTCP_MIN_INTERVAL / GST_SECOND);
495 /* default UDP header length */
496 sess->header_len = 28;
497 sess->mtu = DEFAULT_RTCP_MTU;
499 sess->probation = DEFAULT_PROBATION;
501 /* some default SDES entries */
503 /* we do not want to leak details like the username or hostname here */
504 str = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
505 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_CNAME, str);
509 /* we do not want to leak the user's real name here */
510 str = g_strdup_printf ("Anon%u", g_random_int ());
511 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_NAME, str);
515 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_TOOL, "GStreamer");
517 sess->first_rtcp = TRUE;
518 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
520 sess->allow_early = TRUE;
521 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
522 sess->rtcp_feedback_retention_window = DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW;
523 sess->rtcp_immediate_feedback_threshold =
524 DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD;
526 sess->last_keyframe_request = GST_CLOCK_TIME_NONE;
528 GST_DEBUG ("%p: session using SSRC: %08x", sess, sess->source->ssrc);
532 rtp_session_finalize (GObject * object)
537 sess = RTP_SESSION_CAST (object);
539 g_mutex_clear (&sess->lock);
540 for (i = 0; i < 32; i++)
541 g_hash_table_destroy (sess->ssrcs[i]);
543 g_free (sess->bye_reason);
545 g_object_unref (sess->source);
547 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
551 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
553 GValue value = { 0 };
555 g_value_init (&value, RTP_TYPE_SOURCE);
556 g_value_take_object (&value, source);
557 /* copies the value */
558 g_value_array_append (arr, &value);
562 rtp_session_create_sources (RTPSession * sess)
567 RTP_SESSION_LOCK (sess);
568 /* get number of elements in the table */
569 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
570 /* create the result value array */
571 res = g_value_array_new (size);
573 /* and copy all values into the array */
574 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
575 RTP_SESSION_UNLOCK (sess);
581 rtp_session_set_property (GObject * object, guint prop_id,
582 const GValue * value, GParamSpec * pspec)
586 sess = RTP_SESSION (object);
589 case PROP_INTERNAL_SSRC:
590 rtp_session_set_internal_ssrc (sess, g_value_get_uint (value));
593 RTP_SESSION_LOCK (sess);
594 sess->bandwidth = g_value_get_double (value);
595 sess->recalc_bandwidth = TRUE;
596 RTP_SESSION_UNLOCK (sess);
598 case PROP_RTCP_FRACTION:
599 RTP_SESSION_LOCK (sess);
600 sess->rtcp_bandwidth = g_value_get_double (value);
601 sess->recalc_bandwidth = TRUE;
602 RTP_SESSION_UNLOCK (sess);
604 case PROP_RTCP_RR_BANDWIDTH:
605 RTP_SESSION_LOCK (sess);
606 sess->rtcp_rr_bandwidth = g_value_get_int (value);
607 sess->recalc_bandwidth = TRUE;
608 RTP_SESSION_UNLOCK (sess);
610 case PROP_RTCP_RS_BANDWIDTH:
611 RTP_SESSION_LOCK (sess);
612 sess->rtcp_rs_bandwidth = g_value_get_int (value);
613 sess->recalc_bandwidth = TRUE;
614 RTP_SESSION_UNLOCK (sess);
617 sess->mtu = g_value_get_uint (value);
620 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
623 sess->favor_new = g_value_get_boolean (value);
625 case PROP_RTCP_MIN_INTERVAL:
626 rtp_stats_set_min_interval (&sess->stats,
627 (gdouble) g_value_get_uint64 (value) / GST_SECOND);
628 /* trigger reconsideration */
629 RTP_SESSION_LOCK (sess);
630 sess->next_rtcp_check_time = 0;
631 RTP_SESSION_UNLOCK (sess);
632 if (sess->callbacks.reconsider)
633 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
635 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
636 sess->rtcp_immediate_feedback_threshold = g_value_get_uint (value);
639 sess->probation = g_value_get_uint (value);
640 g_object_set_property (G_OBJECT (sess->source), "probation", value);
643 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
649 rtp_session_get_property (GObject * object, guint prop_id,
650 GValue * value, GParamSpec * pspec)
654 sess = RTP_SESSION (object);
657 case PROP_INTERNAL_SSRC:
658 g_value_set_uint (value, rtp_session_get_internal_ssrc (sess));
660 case PROP_INTERNAL_SOURCE:
661 g_value_take_object (value, rtp_session_get_internal_source (sess));
664 g_value_set_double (value, sess->bandwidth);
666 case PROP_RTCP_FRACTION:
667 g_value_set_double (value, sess->rtcp_bandwidth);
669 case PROP_RTCP_RR_BANDWIDTH:
670 g_value_set_int (value, sess->rtcp_rr_bandwidth);
672 case PROP_RTCP_RS_BANDWIDTH:
673 g_value_set_int (value, sess->rtcp_rs_bandwidth);
676 g_value_set_uint (value, sess->mtu);
679 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
681 case PROP_NUM_SOURCES:
682 g_value_set_uint (value, rtp_session_get_num_sources (sess));
684 case PROP_NUM_ACTIVE_SOURCES:
685 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
688 g_value_take_boxed (value, rtp_session_create_sources (sess));
691 g_value_set_boolean (value, sess->favor_new);
693 case PROP_RTCP_MIN_INTERVAL:
694 g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
696 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
697 g_value_set_uint (value, sess->rtcp_immediate_feedback_threshold);
700 g_value_set_uint (value, sess->probation);
701 g_object_get_property (G_OBJECT (sess->source), "probation", value);
704 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
710 on_new_ssrc (RTPSession * sess, RTPSource * source)
712 g_object_ref (source);
713 RTP_SESSION_UNLOCK (sess);
714 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
715 RTP_SESSION_LOCK (sess);
716 g_object_unref (source);
720 on_ssrc_collision (RTPSession * sess, RTPSource * source)
722 g_object_ref (source);
723 RTP_SESSION_UNLOCK (sess);
724 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
726 RTP_SESSION_LOCK (sess);
727 g_object_unref (source);
731 on_ssrc_validated (RTPSession * sess, RTPSource * source)
733 g_object_ref (source);
734 RTP_SESSION_UNLOCK (sess);
735 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
737 RTP_SESSION_LOCK (sess);
738 g_object_unref (source);
742 on_ssrc_active (RTPSession * sess, RTPSource * source)
744 g_object_ref (source);
745 RTP_SESSION_UNLOCK (sess);
746 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
747 RTP_SESSION_LOCK (sess);
748 g_object_unref (source);
752 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
754 g_object_ref (source);
755 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
756 RTP_SESSION_UNLOCK (sess);
757 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
758 RTP_SESSION_LOCK (sess);
759 g_object_unref (source);
763 on_bye_ssrc (RTPSession * sess, RTPSource * source)
765 g_object_ref (source);
766 RTP_SESSION_UNLOCK (sess);
767 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
768 RTP_SESSION_LOCK (sess);
769 g_object_unref (source);
773 on_bye_timeout (RTPSession * sess, RTPSource * source)
775 g_object_ref (source);
776 RTP_SESSION_UNLOCK (sess);
777 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
778 RTP_SESSION_LOCK (sess);
779 g_object_unref (source);
783 on_timeout (RTPSession * sess, RTPSource * source)
785 g_object_ref (source);
786 RTP_SESSION_UNLOCK (sess);
787 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
788 RTP_SESSION_LOCK (sess);
789 g_object_unref (source);
793 on_sender_timeout (RTPSession * sess, RTPSource * source)
795 g_object_ref (source);
796 RTP_SESSION_UNLOCK (sess);
797 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
799 RTP_SESSION_LOCK (sess);
800 g_object_unref (source);
806 * Create a new session object.
808 * Returns: a new #RTPSession. g_object_unref() after usage.
811 rtp_session_new (void)
815 sess = g_object_new (RTP_TYPE_SESSION, NULL);
821 * rtp_session_set_callbacks:
822 * @sess: an #RTPSession
823 * @callbacks: callbacks to configure
824 * @user_data: user data passed in the callbacks
826 * Configure a set of callbacks to be notified of actions.
829 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
832 g_return_if_fail (RTP_IS_SESSION (sess));
834 if (callbacks->process_rtp) {
835 sess->callbacks.process_rtp = callbacks->process_rtp;
836 sess->process_rtp_user_data = user_data;
838 if (callbacks->send_rtp) {
839 sess->callbacks.send_rtp = callbacks->send_rtp;
840 sess->send_rtp_user_data = user_data;
842 if (callbacks->send_rtcp) {
843 sess->callbacks.send_rtcp = callbacks->send_rtcp;
844 sess->send_rtcp_user_data = user_data;
846 if (callbacks->sync_rtcp) {
847 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
848 sess->sync_rtcp_user_data = user_data;
850 if (callbacks->clock_rate) {
851 sess->callbacks.clock_rate = callbacks->clock_rate;
852 sess->clock_rate_user_data = user_data;
854 if (callbacks->reconsider) {
855 sess->callbacks.reconsider = callbacks->reconsider;
856 sess->reconsider_user_data = user_data;
858 if (callbacks->request_key_unit) {
859 sess->callbacks.request_key_unit = callbacks->request_key_unit;
860 sess->request_key_unit_user_data = user_data;
862 if (callbacks->request_time) {
863 sess->callbacks.request_time = callbacks->request_time;
864 sess->request_time_user_data = user_data;
869 * rtp_session_set_process_rtp_callback:
870 * @sess: an #RTPSession
871 * @callback: callback to set
872 * @user_data: user data passed in the callback
874 * Configure only the process_rtp callback to be notified of the process_rtp action.
877 rtp_session_set_process_rtp_callback (RTPSession * sess,
878 RTPSessionProcessRTP callback, gpointer user_data)
880 g_return_if_fail (RTP_IS_SESSION (sess));
882 sess->callbacks.process_rtp = callback;
883 sess->process_rtp_user_data = user_data;
887 * rtp_session_set_send_rtp_callback:
888 * @sess: an #RTPSession
889 * @callback: callback to set
890 * @user_data: user data passed in the callback
892 * Configure only the send_rtp callback to be notified of the send_rtp action.
895 rtp_session_set_send_rtp_callback (RTPSession * sess,
896 RTPSessionSendRTP callback, gpointer user_data)
898 g_return_if_fail (RTP_IS_SESSION (sess));
900 sess->callbacks.send_rtp = callback;
901 sess->send_rtp_user_data = user_data;
905 * rtp_session_set_send_rtcp_callback:
906 * @sess: an #RTPSession
907 * @callback: callback to set
908 * @user_data: user data passed in the callback
910 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
913 rtp_session_set_send_rtcp_callback (RTPSession * sess,
914 RTPSessionSendRTCP callback, gpointer user_data)
916 g_return_if_fail (RTP_IS_SESSION (sess));
918 sess->callbacks.send_rtcp = callback;
919 sess->send_rtcp_user_data = user_data;
923 * rtp_session_set_sync_rtcp_callback:
924 * @sess: an #RTPSession
925 * @callback: callback to set
926 * @user_data: user data passed in the callback
928 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
931 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
932 RTPSessionSyncRTCP callback, gpointer user_data)
934 g_return_if_fail (RTP_IS_SESSION (sess));
936 sess->callbacks.sync_rtcp = callback;
937 sess->sync_rtcp_user_data = user_data;
941 * rtp_session_set_clock_rate_callback:
942 * @sess: an #RTPSession
943 * @callback: callback to set
944 * @user_data: user data passed in the callback
946 * Configure only the clock_rate callback to be notified of the clock_rate action.
949 rtp_session_set_clock_rate_callback (RTPSession * sess,
950 RTPSessionClockRate callback, gpointer user_data)
952 g_return_if_fail (RTP_IS_SESSION (sess));
954 sess->callbacks.clock_rate = callback;
955 sess->clock_rate_user_data = user_data;
959 * rtp_session_set_reconsider_callback:
960 * @sess: an #RTPSession
961 * @callback: callback to set
962 * @user_data: user data passed in the callback
964 * Configure only the reconsider callback to be notified of the reconsider action.
967 rtp_session_set_reconsider_callback (RTPSession * sess,
968 RTPSessionReconsider callback, gpointer user_data)
970 g_return_if_fail (RTP_IS_SESSION (sess));
972 sess->callbacks.reconsider = callback;
973 sess->reconsider_user_data = user_data;
977 * rtp_session_set_request_time_callback:
978 * @sess: an #RTPSession
979 * @callback: callback to set
980 * @user_data: user data passed in the callback
982 * Configure only the request_time callback
985 rtp_session_set_request_time_callback (RTPSession * sess,
986 RTPSessionRequestTime callback, gpointer user_data)
988 g_return_if_fail (RTP_IS_SESSION (sess));
990 sess->callbacks.request_time = callback;
991 sess->request_time_user_data = user_data;
995 * rtp_session_set_bandwidth:
996 * @sess: an #RTPSession
997 * @bandwidth: the bandwidth allocated
999 * Set the session bandwidth in bytes per second.
1002 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
1004 g_return_if_fail (RTP_IS_SESSION (sess));
1006 RTP_SESSION_LOCK (sess);
1007 sess->stats.bandwidth = bandwidth;
1008 RTP_SESSION_UNLOCK (sess);
1012 * rtp_session_get_bandwidth:
1013 * @sess: an #RTPSession
1015 * Get the session bandwidth.
1017 * Returns: the session bandwidth.
1020 rtp_session_get_bandwidth (RTPSession * sess)
1024 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1026 RTP_SESSION_LOCK (sess);
1027 result = sess->stats.bandwidth;
1028 RTP_SESSION_UNLOCK (sess);
1034 * rtp_session_set_rtcp_fraction:
1035 * @sess: an #RTPSession
1036 * @bandwidth: the RTCP bandwidth
1038 * Set the bandwidth in bytes per second that should be used for RTCP
1042 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
1044 g_return_if_fail (RTP_IS_SESSION (sess));
1046 RTP_SESSION_LOCK (sess);
1047 sess->stats.rtcp_bandwidth = bandwidth;
1048 RTP_SESSION_UNLOCK (sess);
1052 * rtp_session_get_rtcp_fraction:
1053 * @sess: an #RTPSession
1055 * Get the session bandwidth used for RTCP.
1057 * Returns: The bandwidth used for RTCP messages.
1060 rtp_session_get_rtcp_fraction (RTPSession * sess)
1064 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
1066 RTP_SESSION_LOCK (sess);
1067 result = sess->stats.rtcp_bandwidth;
1068 RTP_SESSION_UNLOCK (sess);
1074 * rtp_session_set_sdes_string:
1075 * @sess: an #RTPSession
1076 * @type: the type of the SDES item
1077 * @item: a null-terminated string to set.
1079 * Store an SDES item of @type in @sess.
1081 * Returns: %FALSE if the data was unchanged @type is invalid.
1084 rtp_session_set_sdes_string (RTPSession * sess, GstRTCPSDESType type,
1089 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1091 RTP_SESSION_LOCK (sess);
1092 result = rtp_source_set_sdes_string (sess->source, type, item);
1093 RTP_SESSION_UNLOCK (sess);
1099 * rtp_session_get_sdes_string:
1100 * @sess: an #RTPSession
1101 * @type: the type of the SDES item
1103 * Get the SDES item of @type from @sess.
1105 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
1106 * valid. g_free() after usage.
1109 rtp_session_get_sdes_string (RTPSession * sess, GstRTCPSDESType type)
1113 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1115 RTP_SESSION_LOCK (sess);
1116 result = rtp_source_get_sdes_string (sess->source, type);
1117 RTP_SESSION_UNLOCK (sess);
1123 * rtp_session_get_sdes_struct:
1124 * @sess: an #RTSPSession
1126 * Get the SDES data as a #GstStructure
1128 * Returns: a GstStructure with SDES items for @sess. This function returns a
1129 * copy of the SDES structure, use gst_structure_free() after usage.
1132 rtp_session_get_sdes_struct (RTPSession * sess)
1134 const GstStructure *sdes;
1135 GstStructure *result = NULL;
1137 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1139 RTP_SESSION_LOCK (sess);
1140 sdes = rtp_source_get_sdes_struct (sess->source);
1142 result = gst_structure_copy (sdes);
1143 RTP_SESSION_UNLOCK (sess);
1149 * rtp_session_set_sdes_struct:
1150 * @sess: an #RTSPSession
1151 * @sdes: a #GstStructure
1153 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1156 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
1158 g_return_if_fail (sdes);
1159 g_return_if_fail (RTP_IS_SESSION (sess));
1161 RTP_SESSION_LOCK (sess);
1162 rtp_source_set_sdes_struct (sess->source, gst_structure_copy (sdes));
1163 RTP_SESSION_UNLOCK (sess);
1166 static GstFlowReturn
1167 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1169 GstFlowReturn result = GST_FLOW_OK;
1171 if (source == session->source) {
1172 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1174 RTP_SESSION_UNLOCK (session);
1176 if (session->callbacks.send_rtp)
1178 session->callbacks.send_rtp (session, source, data,
1179 session->send_rtp_user_data);
1181 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1184 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1185 RTP_SESSION_UNLOCK (session);
1187 if (session->callbacks.process_rtp)
1189 session->callbacks.process_rtp (session, source,
1190 GST_BUFFER_CAST (data), session->process_rtp_user_data);
1192 gst_buffer_unref (GST_BUFFER_CAST (data));
1194 RTP_SESSION_LOCK (session);
1200 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1204 RTP_SESSION_UNLOCK (session);
1206 if (session->callbacks.clock_rate)
1208 session->callbacks.clock_rate (session, pt,
1209 session->clock_rate_user_data);
1213 RTP_SESSION_LOCK (session);
1215 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1220 static RTPSourceCallbacks callbacks = {
1221 (RTPSourcePushRTP) source_push_rtp,
1222 (RTPSourceClockRate) source_clock_rate,
1226 check_collision (RTPSession * sess, RTPSource * source,
1227 RTPArrivalStats * arrival, gboolean rtp)
1229 /* If we have no arrival address, we can't do collision checking */
1230 if (!arrival->address)
1233 if (sess->source != source) {
1234 GSocketAddress *from;
1236 /* This is not our local source, but lets check if two remote
1239 from = source->rtp_from;
1241 from = source->rtcp_from;
1245 if (__g_socket_address_equal (from, arrival->address)) {
1246 /* Address is the same */
1249 GST_LOG ("we have a third-party collision or loop ssrc:%x",
1250 rtp_source_get_ssrc (source));
1251 if (sess->favor_new) {
1252 if (rtp_source_find_conflicting_address (source,
1253 arrival->address, arrival->current_time)) {
1256 buf1 = __g_socket_address_to_string (arrival->address);
1257 GST_LOG ("Known conflict on %x for %s, dropping packet",
1258 rtp_source_get_ssrc (source), buf1);
1265 /* Current address is not a known conflict, lets assume this is
1266 * a new source. Save old address in possible conflict list
1268 rtp_source_add_conflicting_address (source, from,
1269 arrival->current_time);
1271 buf1 = __g_socket_address_to_string (from);
1272 buf2 = __g_socket_address_to_string (arrival->address);
1274 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1275 " saving old as known conflict",
1276 rtp_source_get_ssrc (source), buf1, buf2);
1279 rtp_source_set_rtp_from (source, arrival->address);
1281 rtp_source_set_rtcp_from (source, arrival->address);
1289 /* Don't need to save old addresses, we ignore new sources */
1294 /* We don't already have a from address for RTP, just set it */
1296 rtp_source_set_rtp_from (source, arrival->address);
1298 rtp_source_set_rtcp_from (source, arrival->address);
1302 /* FIXME: Log 3rd party collision somehow
1303 * Maybe should be done in upper layer, only the SDES can tell us
1304 * if its a collision or a loop
1307 /* This is sending with our ssrc, is it an address we already know */
1309 if (rtp_source_find_conflicting_address (source, arrival->address,
1310 arrival->current_time)) {
1311 /* Its a known conflict, its probably a loop, not a collision
1312 * lets just drop the incoming packet
1314 GST_DEBUG ("Our packets are being looped back to us, dropping");
1316 /* Its a new collision, lets change our SSRC */
1318 rtp_source_add_conflicting_address (source, arrival->address,
1319 arrival->current_time);
1321 GST_DEBUG ("Collision for SSRC %x", rtp_source_get_ssrc (source));
1322 on_ssrc_collision (sess, source);
1324 sess->change_ssrc = TRUE;
1326 rtp_session_schedule_bye_locked (sess, "SSRC Collision",
1327 arrival->current_time);
1335 find_source (RTPSession * sess, guint32 ssrc)
1337 return g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1338 GINT_TO_POINTER (ssrc));
1342 add_source (RTPSession * sess, RTPSource * src)
1344 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1345 GINT_TO_POINTER (src->ssrc), src);
1346 /* we have one more source now */
1347 sess->total_sources++;
1350 /* must be called with the session lock, the returned source needs to be
1351 * unreffed after usage. */
1353 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1354 RTPArrivalStats * arrival, gboolean rtp)
1358 source = find_source (sess, ssrc);
1359 if (source == NULL) {
1360 /* make new Source in probation and insert */
1361 source = rtp_source_new (ssrc);
1363 GST_DEBUG ("creating new source %08x %p", ssrc, source);
1365 /* for RTP packets we need to set the source in probation. Receiving RTCP
1366 * packets of an SSRC, on the other hand, is a strong indication that we
1367 * are dealing with a valid source. */
1369 g_object_set (source, "probation", sess->probation, NULL);
1371 g_object_set (source, "probation", 0, NULL);
1373 /* store from address, if any */
1374 if (arrival->address) {
1376 rtp_source_set_rtp_from (source, arrival->address);
1378 rtp_source_set_rtcp_from (source, arrival->address);
1381 /* configure a callback on the source */
1382 rtp_source_set_callbacks (source, &callbacks, sess);
1384 add_source (sess, source);
1388 /* check for collision, this updates the address when not previously set */
1389 if (check_collision (sess, source, arrival, rtp)) {
1392 /* Receiving RTCP packets of an SSRC is a strong indication that we
1393 * are dealing with a valid source. */
1395 g_object_set (source, "probation", 0, NULL);
1397 /* update last activity */
1398 source->last_activity = arrival->current_time;
1400 source->last_rtp_activity = arrival->current_time;
1401 g_object_ref (source);
1407 * rtp_session_get_internal_source:
1408 * @sess: a #RTPSession
1410 * Get the internal #RTPSource of @sess.
1412 * Returns: The internal #RTPSource. g_object_unref() after usage.
1415 rtp_session_get_internal_source (RTPSession * sess)
1419 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1421 result = g_object_ref (sess->source);
1427 * rtp_session_set_internal_ssrc:
1428 * @sess: a #RTPSession
1431 * Set the SSRC of @sess to @ssrc.
1434 rtp_session_set_internal_ssrc (RTPSession * sess, guint32 ssrc)
1436 RTP_SESSION_LOCK (sess);
1437 if (ssrc != sess->source->ssrc) {
1438 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
1439 GINT_TO_POINTER (sess->source->ssrc));
1441 GST_DEBUG ("setting internal SSRC to %08x", ssrc);
1442 /* After this call, any receiver of the old SSRC either in RTP or RTCP
1443 * packets will timeout on the old SSRC, we could potentially schedule a
1444 * BYE RTCP for the old SSRC... */
1445 sess->source->ssrc = ssrc;
1446 rtp_source_reset (sess->source);
1448 /* rehash with the new SSRC */
1449 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1450 GINT_TO_POINTER (sess->source->ssrc), sess->source);
1452 RTP_SESSION_UNLOCK (sess);
1454 g_object_notify (G_OBJECT (sess), "internal-ssrc");
1458 * rtp_session_get_internal_ssrc:
1459 * @sess: a #RTPSession
1461 * Get the internal SSRC of @sess.
1463 * Returns: The SSRC of the session.
1466 rtp_session_get_internal_ssrc (RTPSession * sess)
1470 RTP_SESSION_LOCK (sess);
1471 ssrc = sess->source->ssrc;
1472 RTP_SESSION_UNLOCK (sess);
1478 * rtp_session_add_source:
1479 * @sess: a #RTPSession
1480 * @src: #RTPSource to add
1482 * Add @src to @session.
1484 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1485 * existed in the session.
1488 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1490 gboolean result = FALSE;
1493 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1494 g_return_val_if_fail (src != NULL, FALSE);
1496 RTP_SESSION_LOCK (sess);
1497 find = find_source (sess, src->ssrc);
1499 add_source (sess, src);
1502 RTP_SESSION_UNLOCK (sess);
1508 * rtp_session_get_num_sources:
1509 * @sess: an #RTPSession
1511 * Get the number of sources in @sess.
1513 * Returns: The number of sources in @sess.
1516 rtp_session_get_num_sources (RTPSession * sess)
1520 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1522 RTP_SESSION_LOCK (sess);
1523 result = sess->total_sources;
1524 RTP_SESSION_UNLOCK (sess);
1530 * rtp_session_get_num_active_sources:
1531 * @sess: an #RTPSession
1533 * Get the number of active sources in @sess. A source is considered active when
1534 * it has been validated and has not yet received a BYE RTCP message.
1536 * Returns: The number of active sources in @sess.
1539 rtp_session_get_num_active_sources (RTPSession * sess)
1543 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1545 RTP_SESSION_LOCK (sess);
1546 result = sess->stats.active_sources;
1547 RTP_SESSION_UNLOCK (sess);
1553 * rtp_session_get_source_by_ssrc:
1554 * @sess: an #RTPSession
1557 * Find the source with @ssrc in @sess.
1559 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1560 * g_object_unref() after usage.
1563 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1567 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1569 RTP_SESSION_LOCK (sess);
1570 result = find_source (sess, ssrc);
1572 g_object_ref (result);
1573 RTP_SESSION_UNLOCK (sess);
1578 /* should be called with the SESSION lock */
1580 rtp_session_create_new_ssrc (RTPSession * sess)
1585 ssrc = g_random_int ();
1587 /* see if it exists in the session, we're done if it doesn't */
1588 if (find_source (sess, ssrc) == NULL)
1596 * rtp_session_create_source:
1597 * @sess: an #RTPSession
1599 * Create an #RTPSource for use in @sess. This function will create a source
1600 * with an ssrc that is currently not used by any participants in the session.
1602 * Returns: an #RTPSource.
1605 rtp_session_create_source (RTPSession * sess)
1610 RTP_SESSION_LOCK (sess);
1611 ssrc = rtp_session_create_new_ssrc (sess);
1612 source = rtp_source_new (ssrc);
1613 rtp_source_set_callbacks (source, &callbacks, sess);
1614 /* we need an additional ref for the source in the hashtable */
1615 g_object_ref (source);
1616 add_source (sess, source);
1617 RTP_SESSION_UNLOCK (sess);
1622 /* update the RTPArrivalStats structure with the current time and other bits
1623 * about the current buffer we are handling.
1624 * This function is typically called when a validated packet is received.
1625 * This function should be called with the SESSION_LOCK
1628 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
1629 gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
1630 GstClockTime running_time, guint64 ntpnstime)
1632 GstNetAddressMeta *meta;
1633 GstRTPBuffer rtpb = { NULL };
1635 /* get time of arrival */
1636 arrival->current_time = current_time;
1637 arrival->running_time = running_time;
1638 arrival->ntpnstime = ntpnstime;
1640 /* get packet size including header overhead */
1641 arrival->bytes = gst_buffer_get_size (buffer) + sess->header_len;
1644 gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtpb);
1645 arrival->payload_len = gst_rtp_buffer_get_payload_len (&rtpb);
1646 gst_rtp_buffer_unmap (&rtpb);
1648 arrival->payload_len = 0;
1651 /* for netbuffer we can store the IP address to check for collisions */
1652 meta = gst_buffer_get_net_address_meta (buffer);
1653 if (arrival->address)
1654 g_object_unref (arrival->address);
1656 arrival->address = G_SOCKET_ADDRESS (g_object_ref (meta->addr));
1658 arrival->address = NULL;
1663 clean_arrival_stats (RTPArrivalStats * arrival)
1665 if (arrival->address)
1666 g_object_unref (arrival->address);
1670 * rtp_session_process_rtp:
1671 * @sess: and #RTPSession
1672 * @buffer: an RTP buffer
1673 * @current_time: the current system time
1674 * @running_time: the running_time of @buffer
1676 * Process an RTP buffer in the session manager. This function takes ownership
1679 * Returns: a #GstFlowReturn.
1682 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1683 GstClockTime current_time, GstClockTime running_time)
1685 GstFlowReturn result;
1689 gboolean prevsender, prevactive;
1690 RTPArrivalStats arrival = { NULL, };
1694 GstRTPBuffer rtp = { NULL };
1696 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1697 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1699 if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp))
1700 goto invalid_packet;
1702 RTP_SESSION_LOCK (sess);
1703 /* ignore more RTP packets when we left the session */
1704 if (sess->source->received_bye)
1707 /* update arrival stats */
1708 update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1711 /* get SSRC and look up in session database */
1712 ssrc = gst_rtp_buffer_get_ssrc (&rtp);
1713 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1717 /* copy available csrc for later */
1718 count = gst_rtp_buffer_get_csrc_count (&rtp);
1719 /* make sure to not overflow our array. An RTP buffer can maximally contain
1721 count = MIN (count, 16);
1723 for (i = 0; i < count; i++)
1724 csrcs[i] = gst_rtp_buffer_get_csrc (&rtp, i);
1726 gst_rtp_buffer_unmap (&rtp);
1728 prevsender = RTP_SOURCE_IS_SENDER (source);
1729 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1730 oldrate = source->bitrate;
1732 /* let source process the packet */
1733 result = rtp_source_process_rtp (source, buffer, &arrival);
1735 /* source became active */
1736 if (prevactive != RTP_SOURCE_IS_ACTIVE (source)) {
1737 sess->stats.active_sources++;
1738 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1739 sess->stats.active_sources);
1740 on_ssrc_validated (sess, source);
1742 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1743 sess->stats.sender_sources++;
1744 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1745 sess->stats.sender_sources);
1747 if (oldrate != source->bitrate)
1748 sess->recalc_bandwidth = TRUE;
1751 on_new_ssrc (sess, source);
1753 if (source->validated) {
1756 /* for validated sources, we add the CSRCs as well */
1757 for (i = 0; i < count; i++) {
1759 RTPSource *csrc_src;
1764 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1769 GST_DEBUG ("created new CSRC: %08x", csrc);
1770 rtp_source_set_as_csrc (csrc_src);
1771 if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1772 sess->stats.active_sources++;
1773 on_new_ssrc (sess, csrc_src);
1775 g_object_unref (csrc_src);
1778 g_object_unref (source);
1780 RTP_SESSION_UNLOCK (sess);
1782 clean_arrival_stats (&arrival);
1789 gst_buffer_unref (buffer);
1790 GST_DEBUG ("invalid RTP packet received");
1795 RTP_SESSION_UNLOCK (sess);
1796 gst_rtp_buffer_unmap (&rtp);
1797 gst_buffer_unref (buffer);
1798 GST_DEBUG ("ignoring RTP packet because we are leaving");
1803 RTP_SESSION_UNLOCK (sess);
1804 gst_rtp_buffer_unmap (&rtp);
1805 gst_buffer_unref (buffer);
1806 clean_arrival_stats (&arrival);
1807 GST_DEBUG ("ignoring packet because its collisioning");
1813 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1814 GstRTCPPacket * packet, RTPArrivalStats * arrival)
1818 count = gst_rtcp_packet_get_rb_count (packet);
1819 for (i = 0; i < count; i++) {
1820 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1821 guint8 fractionlost;
1824 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1825 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1827 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1829 if (ssrc == sess->source->ssrc) {
1830 /* only deal with report blocks for our session, we update the stats of
1831 * the sender of the RTCP message. We could also compare our stats against
1832 * the other sender to see if we are better or worse. */
1833 rtp_source_process_rb (source, arrival->ntpnstime, fractionlost,
1834 packetslost, exthighestseq, jitter, lsr, dlsr);
1837 on_ssrc_active (sess, source);
1840 /* A Sender report contains statistics about how the sender is doing. This
1841 * includes timing informataion such as the relation between RTP and NTP
1842 * timestamps and the number of packets/bytes it sent to us.
1844 * In this report is also included a set of report blocks related to how this
1845 * sender is receiving data (in case we (or somebody else) is also sending stuff
1846 * to it). This info includes the packet loss, jitter and seqnum. It also
1847 * contains information to calculate the round trip time (LSR/DLSR).
1850 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1851 RTPArrivalStats * arrival, gboolean * do_sync)
1853 guint32 senderssrc, rtptime, packet_count, octet_count;
1856 gboolean created, prevsender;
1858 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1859 &packet_count, &octet_count);
1861 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1862 senderssrc, GST_TIME_ARGS (arrival->current_time));
1864 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1868 /* don't try to do lip-sync for sources that sent a BYE */
1869 if (rtp_source_received_bye (source))
1874 prevsender = RTP_SOURCE_IS_SENDER (source);
1876 /* first update the source */
1877 rtp_source_process_sr (source, arrival->current_time, ntptime, rtptime,
1878 packet_count, octet_count);
1880 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1881 sess->stats.sender_sources++;
1882 GST_DEBUG ("source: %08x became sender, %d sender sources", senderssrc,
1883 sess->stats.sender_sources);
1887 on_new_ssrc (sess, source);
1889 rtp_session_process_rb (sess, source, packet, arrival);
1890 g_object_unref (source);
1893 /* A receiver report contains statistics about how a receiver is doing. It
1894 * includes stuff like packet loss, jitter and the seqnum it received last. It
1895 * also contains info to calculate the round trip time.
1897 * We are only interested in how the sender of this report is doing wrt to us.
1900 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1901 RTPArrivalStats * arrival)
1907 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1909 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1911 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1916 on_new_ssrc (sess, source);
1918 rtp_session_process_rb (sess, source, packet, arrival);
1919 g_object_unref (source);
1922 /* Get SDES items and store them in the SSRC */
1924 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1925 RTPArrivalStats * arrival)
1928 gboolean more_items, more_entries;
1930 items = gst_rtcp_packet_sdes_get_item_count (packet);
1931 GST_DEBUG ("got SDES packet with %d items", items);
1933 more_items = gst_rtcp_packet_sdes_first_item (packet);
1935 while (more_items) {
1937 gboolean changed, created, validated;
1941 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1943 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1947 /* find src, no probation when dealing with RTCP */
1948 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1952 sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
1954 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1956 while (more_entries) {
1957 GstRTCPSDESType type;
1963 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1965 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1968 if (type == GST_RTCP_SDES_PRIV) {
1969 name = g_strndup ((const gchar *) &data[1], data[0]);
1971 data += data[0] + 1;
1973 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
1976 value = g_strndup ((const gchar *) data, len);
1978 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
1983 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1987 /* takes ownership of sdes */
1988 changed = rtp_source_set_sdes_struct (source, sdes);
1990 validated = !RTP_SOURCE_IS_ACTIVE (source);
1991 source->validated = TRUE;
1994 on_new_ssrc (sess, source);
1996 /* source became active */
1998 sess->stats.active_sources++;
1999 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
2000 sess->stats.active_sources);
2001 on_ssrc_validated (sess, source);
2005 on_ssrc_sdes (sess, source);
2007 g_object_unref (source);
2009 more_items = gst_rtcp_packet_sdes_next_item (packet);
2014 /* BYE is sent when a client leaves the session
2017 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
2018 RTPArrivalStats * arrival)
2022 gboolean reconsider = FALSE;
2024 reason = gst_rtcp_packet_bye_get_reason (packet);
2025 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
2027 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
2028 for (i = 0; i < count; i++) {
2031 gboolean created, prevactive, prevsender;
2032 guint pmembers, members;
2034 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
2035 GST_DEBUG ("SSRC: %08x", ssrc);
2037 if (ssrc == sess->source->ssrc)
2040 /* find src and mark bye, no probation when dealing with RTCP */
2041 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
2045 /* store time for when we need to time out this source */
2046 source->bye_time = arrival->current_time;
2048 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2049 prevsender = RTP_SOURCE_IS_SENDER (source);
2051 /* let the source handle the rest */
2052 rtp_source_process_bye (source, reason);
2054 pmembers = sess->stats.active_sources;
2056 if (prevactive && !RTP_SOURCE_IS_ACTIVE (source)) {
2057 sess->stats.active_sources--;
2058 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
2059 sess->stats.active_sources);
2061 if (prevsender && !RTP_SOURCE_IS_SENDER (source)) {
2062 sess->stats.sender_sources--;
2063 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
2064 sess->stats.sender_sources);
2066 members = sess->stats.active_sources;
2068 if (!sess->source->received_bye && members < pmembers) {
2069 /* some members went away since the previous timeout estimate.
2070 * Perform reverse reconsideration but only when we are not scheduling a
2072 if (sess->next_rtcp_check_time != GST_CLOCK_TIME_NONE &&
2073 arrival->current_time < sess->next_rtcp_check_time) {
2074 GstClockTime time_remaining;
2076 time_remaining = sess->next_rtcp_check_time - arrival->current_time;
2077 sess->next_rtcp_check_time =
2078 gst_util_uint64_scale (time_remaining, members, pmembers);
2080 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
2081 GST_TIME_ARGS (sess->next_rtcp_check_time));
2083 sess->next_rtcp_check_time += arrival->current_time;
2085 /* mark pending reconsider. We only want to signal the reconsideration
2086 * once after we handled all the source in the bye packet */
2092 on_new_ssrc (sess, source);
2094 on_bye_ssrc (sess, source);
2096 g_object_unref (source);
2099 RTP_SESSION_UNLOCK (sess);
2100 /* notify app of reconsideration */
2101 if (sess->callbacks.reconsider)
2102 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2103 RTP_SESSION_LOCK (sess);
2109 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
2110 RTPArrivalStats * arrival)
2112 GST_DEBUG ("received APP");
2116 rtp_session_request_local_key_unit (RTPSession * sess, RTPSource * src,
2117 gboolean fir, GstClockTime current_time)
2119 guint32 round_trip = 0;
2121 rtp_source_get_last_rb (src, NULL, NULL, NULL, NULL, NULL, NULL, &round_trip);
2123 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE && round_trip) {
2124 GstClockTime round_trip_in_ns = gst_util_uint64_scale (round_trip,
2127 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE &&
2128 current_time - sess->last_keyframe_request < 2 * round_trip_in_ns) {
2129 GST_DEBUG ("Ignoring %s request because one was send without one "
2130 "RTT (%" GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")",
2131 fir ? "FIR" : "PLI",
2132 GST_TIME_ARGS (current_time - sess->last_keyframe_request),
2133 GST_TIME_ARGS (round_trip_in_ns));;
2138 sess->last_keyframe_request = current_time;
2140 GST_LOG ("received %s request from %X %p(%p)", fir ? "FIR" : "PLI",
2141 rtp_source_get_ssrc (src), sess->callbacks.process_rtp,
2142 sess->callbacks.request_key_unit);
2144 RTP_SESSION_UNLOCK (sess);
2145 sess->callbacks.request_key_unit (sess, fir,
2146 sess->request_key_unit_user_data);
2147 RTP_SESSION_LOCK (sess);
2153 rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
2154 guint32 media_ssrc, GstClockTime current_time)
2158 if (!sess->callbacks.request_key_unit)
2161 src = find_source (sess, sender_ssrc);
2165 rtp_session_request_local_key_unit (sess, src, FALSE, current_time);
2169 rtp_session_process_fir (RTPSession * sess, guint32 sender_ssrc,
2170 guint8 * fci_data, guint fci_length, GstClockTime current_time)
2175 gboolean our_request = FALSE;
2177 if (!sess->callbacks.request_key_unit)
2183 src = find_source (sess, sender_ssrc);
2185 /* Hack because Google fails to set the sender_ssrc correctly */
2186 if (!src && sender_ssrc == 1) {
2187 GHashTableIter iter;
2189 if (sess->stats.sender_sources >
2190 RTP_SOURCE_IS_SENDER (sess->source) ? 2 : 1)
2193 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
2195 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
2196 if (src != sess->source && rtp_source_is_sender (src))
2205 for (position = 0; position < fci_length; position += 8) {
2206 guint8 *data = fci_data + position;
2208 ssrc = GST_READ_UINT32_BE (data);
2210 if (ssrc == rtp_source_get_ssrc (sess->source)) {
2218 rtp_session_request_local_key_unit (sess, src, TRUE, current_time);
2222 rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
2223 RTPArrivalStats * arrival, GstClockTime current_time)
2225 GstRTCPType type = gst_rtcp_packet_get_type (packet);
2226 GstRTCPFBType fbtype = gst_rtcp_packet_fb_get_type (packet);
2227 guint32 sender_ssrc = gst_rtcp_packet_fb_get_sender_ssrc (packet);
2228 guint32 media_ssrc = gst_rtcp_packet_fb_get_media_ssrc (packet);
2229 guint8 *fci_data = gst_rtcp_packet_fb_get_fci (packet);
2230 guint fci_length = 4 * gst_rtcp_packet_fb_get_fci_length (packet);
2232 GST_DEBUG ("received feedback %d:%d from %08X about %08X with FCI of "
2233 "length %d", type, fbtype, sender_ssrc, media_ssrc, fci_length);
2235 if (g_signal_has_handler_pending (sess,
2236 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0, TRUE)) {
2237 GstBuffer *fci_buffer = NULL;
2239 if (fci_length > 0) {
2240 fci_buffer = gst_buffer_copy_region (packet->rtcp->buffer,
2241 GST_BUFFER_COPY_MEMORY, fci_data - packet->rtcp->map.data,
2243 GST_BUFFER_TIMESTAMP (fci_buffer) = arrival->running_time;
2246 RTP_SESSION_UNLOCK (sess);
2247 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0,
2248 type, fbtype, sender_ssrc, media_ssrc, fci_buffer);
2249 RTP_SESSION_LOCK (sess);
2252 gst_buffer_unref (fci_buffer);
2255 if (sess->rtcp_feedback_retention_window) {
2256 RTPSource *src = find_source (sess, media_ssrc);
2259 rtp_source_retain_rtcp_packet (src, packet, arrival->running_time);
2262 if (rtp_source_get_ssrc (sess->source) == media_ssrc ||
2263 /* PSFB FIR puts the media ssrc inside the FCI */
2264 (type == GST_RTCP_TYPE_PSFB && fbtype == GST_RTCP_PSFB_TYPE_FIR)) {
2266 case GST_RTCP_TYPE_PSFB:
2268 case GST_RTCP_PSFB_TYPE_PLI:
2269 rtp_session_process_pli (sess, sender_ssrc, media_ssrc,
2272 case GST_RTCP_PSFB_TYPE_FIR:
2273 rtp_session_process_fir (sess, sender_ssrc, fci_data, fci_length,
2280 case GST_RTCP_TYPE_RTPFB:
2288 * rtp_session_process_rtcp:
2289 * @sess: and #RTPSession
2290 * @buffer: an RTCP buffer
2291 * @current_time: the current system time
2292 * @ntpnstime: the current NTP time in nanoseconds
2294 * Process an RTCP buffer in the session manager. This function takes ownership
2297 * Returns: a #GstFlowReturn.
2300 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
2301 GstClockTime current_time, guint64 ntpnstime)
2303 GstRTCPPacket packet;
2304 gboolean more, is_bye = FALSE, do_sync = FALSE;
2305 RTPArrivalStats arrival = { NULL, };
2306 GstFlowReturn result = GST_FLOW_OK;
2307 GstRTCPBuffer rtcp = { NULL, };
2309 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2310 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2312 if (!gst_rtcp_buffer_validate (buffer))
2313 goto invalid_packet;
2315 GST_DEBUG ("received RTCP packet");
2317 RTP_SESSION_LOCK (sess);
2318 /* update arrival stats */
2319 update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1,
2325 /* start processing the compound packet */
2326 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
2327 more = gst_rtcp_buffer_get_first_packet (&rtcp, &packet);
2331 type = gst_rtcp_packet_get_type (&packet);
2333 /* when we are leaving the session, we should ignore all non-BYE messages */
2334 if (sess->source->received_bye && type != GST_RTCP_TYPE_BYE) {
2335 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
2340 case GST_RTCP_TYPE_SR:
2341 rtp_session_process_sr (sess, &packet, &arrival, &do_sync);
2343 case GST_RTCP_TYPE_RR:
2344 rtp_session_process_rr (sess, &packet, &arrival);
2346 case GST_RTCP_TYPE_SDES:
2347 rtp_session_process_sdes (sess, &packet, &arrival);
2349 case GST_RTCP_TYPE_BYE:
2351 /* don't try to attempt lip-sync anymore for streams with a BYE */
2353 rtp_session_process_bye (sess, &packet, &arrival);
2355 case GST_RTCP_TYPE_APP:
2356 rtp_session_process_app (sess, &packet, &arrival);
2358 case GST_RTCP_TYPE_RTPFB:
2359 case GST_RTCP_TYPE_PSFB:
2360 rtp_session_process_feedback (sess, &packet, &arrival, current_time);
2363 GST_WARNING ("got unknown RTCP packet");
2367 more = gst_rtcp_packet_move_to_next (&packet);
2370 gst_rtcp_buffer_unmap (&rtcp);
2372 /* if we are scheduling a BYE, we only want to count bye packets, else we
2373 * count everything */
2374 if (sess->source->received_bye) {
2376 sess->stats.bye_members++;
2377 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2380 /* keep track of average packet size */
2381 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2383 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
2384 sess->stats.avg_rtcp_packet_size, arrival.bytes);
2385 RTP_SESSION_UNLOCK (sess);
2387 clean_arrival_stats (&arrival);
2389 /* notify caller of sr packets in the callback */
2390 if (do_sync && sess->callbacks.sync_rtcp) {
2391 /* make writable, we might want to change the buffer */
2392 buffer = gst_buffer_make_writable (buffer);
2394 result = sess->callbacks.sync_rtcp (sess, buffer,
2395 sess->sync_rtcp_user_data);
2397 gst_buffer_unref (buffer);
2404 GST_DEBUG ("invalid RTCP packet received");
2405 gst_buffer_unref (buffer);
2410 RTP_SESSION_UNLOCK (sess);
2411 gst_buffer_unref (buffer);
2412 clean_arrival_stats (&arrival);
2413 GST_DEBUG ("ignoring RTCP packet because we left");
2419 * rtp_session_update_send_caps:
2420 * @sess: an #RTPSession
2423 * Update the caps of the sender in the rtp session.
2426 rtp_session_update_send_caps (RTPSession * sess, GstCaps * caps)
2428 g_return_if_fail (RTP_IS_SESSION (sess));
2429 g_return_if_fail (GST_IS_CAPS (caps));
2431 GST_LOG ("received caps %" GST_PTR_FORMAT, caps);
2433 RTP_SESSION_LOCK (sess);
2434 rtp_source_update_caps (sess->source, caps);
2435 RTP_SESSION_UNLOCK (sess);
2439 * rtp_session_send_rtp:
2440 * @sess: an #RTPSession
2441 * @data: pointer to either an RTP buffer or a list of RTP buffers
2442 * @is_list: TRUE when @data is a buffer list
2443 * @current_time: the current system time
2444 * @running_time: the running time of @data
2446 * Send the RTP buffer in the session manager. This function takes ownership of
2449 * Returns: a #GstFlowReturn.
2452 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
2453 GstClockTime current_time, GstClockTime running_time)
2455 GstFlowReturn result;
2457 gboolean prevsender;
2460 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2461 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
2463 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
2465 RTP_SESSION_LOCK (sess);
2466 source = sess->source;
2468 /* update last activity */
2469 source->last_rtp_activity = current_time;
2471 prevsender = RTP_SOURCE_IS_SENDER (source);
2472 oldrate = source->bitrate;
2474 /* we use our own source to send */
2475 result = rtp_source_send_rtp (source, data, is_list, running_time);
2477 if (RTP_SOURCE_IS_SENDER (source) && !prevsender)
2478 sess->stats.sender_sources++;
2479 if (oldrate != source->bitrate)
2480 sess->recalc_bandwidth = TRUE;
2481 RTP_SESSION_UNLOCK (sess);
2487 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
2489 *bandwidth += source->bitrate;
2492 /* must be called with session lock */
2494 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2497 GstClockTime result;
2499 /* recalculate bandwidth when it changed */
2500 if (sess->recalc_bandwidth) {
2503 if (sess->bandwidth > 0)
2504 bandwidth = sess->bandwidth;
2506 /* If it is <= 0, then try to estimate the actual bandwidth */
2507 bandwidth = sess->source->bitrate;
2509 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2510 (GHFunc) add_bitrates, &bandwidth);
2513 if (bandwidth < 8000)
2514 bandwidth = RTP_STATS_BANDWIDTH;
2516 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
2517 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
2519 sess->recalc_bandwidth = FALSE;
2522 if (sess->source->received_bye) {
2523 result = rtp_stats_calculate_bye_interval (&sess->stats);
2525 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2526 RTP_SOURCE_IS_SENDER (sess->source), first);
2529 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2530 GST_TIME_ARGS (result), first);
2532 if (!deterministic && result != GST_CLOCK_TIME_NONE)
2533 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2535 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2540 /* Stop the current @sess and schedule a BYE message for the other members.
2541 * One must have the session lock to call this function
2543 static GstFlowReturn
2544 rtp_session_schedule_bye_locked (RTPSession * sess, const gchar * reason,
2545 GstClockTime current_time)
2547 GstFlowReturn result = GST_FLOW_OK;
2549 GstClockTime interval;
2551 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2553 source = sess->source;
2555 /* ignore more BYEs */
2556 if (source->received_bye)
2559 /* we have BYE now */
2560 source->received_bye = TRUE;
2561 /* at least one member wants to send a BYE */
2562 g_free (sess->bye_reason);
2563 sess->bye_reason = g_strdup (reason);
2564 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
2565 sess->stats.bye_members = 1;
2566 sess->first_rtcp = TRUE;
2567 sess->sent_bye = FALSE;
2568 sess->allow_early = TRUE;
2570 /* reschedule transmission */
2571 sess->last_rtcp_send_time = current_time;
2572 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2574 if (interval != GST_CLOCK_TIME_NONE)
2575 sess->next_rtcp_check_time = current_time + interval;
2577 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
2579 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2580 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2582 RTP_SESSION_UNLOCK (sess);
2583 /* notify app of reconsideration */
2584 if (sess->callbacks.reconsider)
2585 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2586 RTP_SESSION_LOCK (sess);
2593 * rtp_session_schedule_bye:
2594 * @sess: an #RTPSession
2595 * @reason: a reason or NULL
2596 * @current_time: the current system time
2598 * Stop the current @sess and schedule a BYE message for the other members.
2600 * Returns: a #GstFlowReturn.
2603 rtp_session_schedule_bye (RTPSession * sess, const gchar * reason,
2604 GstClockTime current_time)
2606 GstFlowReturn result = GST_FLOW_OK;
2608 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2610 RTP_SESSION_LOCK (sess);
2611 result = rtp_session_schedule_bye_locked (sess, reason, current_time);
2612 RTP_SESSION_UNLOCK (sess);
2618 * rtp_session_next_timeout:
2619 * @sess: an #RTPSession
2620 * @current_time: the current system time
2622 * Get the next time we should perform session maintenance tasks.
2624 * Returns: a time when rtp_session_on_timeout() should be called with the
2625 * current system time.
2628 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2630 GstClockTime result, interval = 0;
2632 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
2634 RTP_SESSION_LOCK (sess);
2636 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
2637 result = sess->next_early_rtcp_time;
2641 result = sess->next_rtcp_check_time;
2643 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2644 ", next time: %" GST_TIME_FORMAT,
2645 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2647 if (result == GST_CLOCK_TIME_NONE || result < current_time) {
2648 GST_DEBUG ("take current time as base");
2649 /* our previous check time expired, start counting from the current time
2651 result = current_time;
2654 if (sess->source->received_bye) {
2655 if (sess->sent_bye) {
2656 GST_DEBUG ("we sent BYE already");
2657 interval = GST_CLOCK_TIME_NONE;
2658 } else if (sess->stats.active_sources >= 50) {
2659 GST_DEBUG ("reconsider BYE, more than 50 sources");
2660 /* reconsider BYE if members >= 50 */
2661 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2664 if (sess->first_rtcp) {
2665 GST_DEBUG ("first RTCP packet");
2666 /* we are called for the first time */
2667 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2668 } else if (sess->next_rtcp_check_time < current_time) {
2669 GST_DEBUG ("old check time expired, getting new timeout");
2670 /* get a new timeout when we need to */
2671 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
2675 if (interval != GST_CLOCK_TIME_NONE)
2678 result = GST_CLOCK_TIME_NONE;
2680 sess->next_rtcp_check_time = result;
2684 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2685 ", next time: %" GST_TIME_FORMAT,
2686 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2687 RTP_SESSION_UNLOCK (sess);
2694 GstRTCPBuffer rtcpbuf;
2697 GstClockTime current_time;
2699 GstClockTime running_time;
2700 GstClockTime interval;
2701 GstRTCPPacket packet;
2705 gboolean may_suppress;
2709 session_start_rtcp (RTPSession * sess, ReportData * data)
2711 GstRTCPPacket *packet = &data->packet;
2712 RTPSource *own = sess->source;
2713 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2715 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2717 gst_rtcp_buffer_map (data->rtcp, GST_MAP_READWRITE, rtcp);
2719 if (RTP_SOURCE_IS_SENDER (own)) {
2722 guint32 packet_count, octet_count;
2724 /* we are a sender, create SR */
2725 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2726 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SR, packet);
2728 /* get latest stats */
2729 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
2730 &ntptime, &rtptime, &packet_count, &octet_count);
2732 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2733 packet_count, octet_count);
2735 /* fill in sender report info */
2736 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2737 ntptime, rtptime, packet_count, octet_count);
2739 /* we are only receiver, create RR */
2740 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2741 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RR, packet);
2742 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2746 /* construct a Sender or Receiver Report */
2748 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2750 RTPSession *sess = data->sess;
2751 GstRTCPPacket *packet = &data->packet;
2753 /* create a new buffer if needed */
2754 if (data->rtcp == NULL) {
2755 session_start_rtcp (sess, data);
2756 } else if (data->is_early) {
2757 /* Put a single RR or SR in minimal compound packets */
2760 if (gst_rtcp_packet_get_rb_count (packet) < GST_RTCP_MAX_RB_COUNT) {
2761 /* only report about other sender sources */
2762 if (source != sess->source && RTP_SOURCE_IS_SENDER (source)) {
2763 guint8 fractionlost;
2765 guint32 exthighestseq, jitter;
2769 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2770 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2772 /* store last generated RR packet */
2773 source->last_rr.is_valid = TRUE;
2774 source->last_rr.fractionlost = fractionlost;
2775 source->last_rr.packetslost = packetslost;
2776 source->last_rr.exthighestseq = exthighestseq;
2777 source->last_rr.jitter = jitter;
2778 source->last_rr.lsr = lsr;
2779 source->last_rr.dlsr = dlsr;
2781 /* packet is not yet filled, add report block for this source. */
2782 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2783 exthighestseq, jitter, lsr, dlsr);
2788 /* perform cleanup of sources that timed out */
2790 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2792 gboolean remove = FALSE;
2793 gboolean byetimeout = FALSE;
2794 gboolean sendertimeout = FALSE;
2795 gboolean is_sender, is_active;
2796 RTPSession *sess = data->sess;
2797 GstClockTime interval, binterval;
2800 is_sender = RTP_SOURCE_IS_SENDER (source);
2801 is_active = RTP_SOURCE_IS_ACTIVE (source);
2803 /* nothing to do when without RTCP */
2804 if (data->interval == GST_CLOCK_TIME_NONE)
2807 /* our own rtcp interval may have been forced low by secondary configuration,
2808 * while sender side may still operate with higher interval,
2809 * so do not just take our interval to decide on timing out sender,
2810 * but take (if data->interval <= 5 * GST_SECOND):
2811 * interval = CLAMP (sender_interval, data->interval, 5 * GST_SECOND)
2812 * where sender_interval is difference between last 2 received RTCP reports
2814 if (data->interval >= 5 * GST_SECOND || (source == sess->source)) {
2815 binterval = data->interval;
2817 GST_LOG ("prev_rtcp %" GST_TIME_FORMAT ", last_rtcp %" GST_TIME_FORMAT,
2818 GST_TIME_ARGS (source->stats.prev_rtcptime),
2819 GST_TIME_ARGS (source->stats.last_rtcptime));
2820 /* if not received enough yet, fallback to larger default */
2821 if (source->stats.last_rtcptime > source->stats.prev_rtcptime)
2822 binterval = source->stats.last_rtcptime - source->stats.prev_rtcptime;
2824 binterval = 5 * GST_SECOND;
2825 binterval = CLAMP (binterval, data->interval, 5 * GST_SECOND);
2827 GST_LOG ("timeout base interval %" GST_TIME_FORMAT,
2828 GST_TIME_ARGS (binterval));
2830 /* check for our own source, we don't want to delete our own source. */
2831 if (!(source == sess->source)) {
2832 if (source->received_bye) {
2833 /* if we received a BYE from the source, remove the source after some
2835 if (data->current_time > source->bye_time &&
2836 data->current_time - source->bye_time > sess->stats.bye_timeout) {
2837 GST_DEBUG ("removing BYE source %08x", source->ssrc);
2842 /* sources that were inactive for more than 5 times the deterministic reporting
2843 * interval get timed out. the min timeout is 5 seconds. */
2844 /* mind old time that might pre-date last time going to PLAYING */
2845 btime = MAX (source->last_activity, sess->start_time);
2846 if (data->current_time > btime) {
2847 interval = MAX (binterval * 5, 5 * GST_SECOND);
2848 if (data->current_time - btime > interval) {
2849 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2850 source->ssrc, GST_TIME_ARGS (btime));
2856 /* senders that did not send for a long time become a receiver, this also
2857 * holds for our own source. */
2859 /* mind old time that might pre-date last time going to PLAYING */
2860 btime = MAX (source->last_rtp_activity, sess->start_time);
2861 if (data->current_time > btime) {
2862 interval = MAX (binterval * 2, 5 * GST_SECOND);
2863 if (data->current_time - btime > interval) {
2864 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
2865 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
2866 source->is_sender = FALSE;
2867 sess->stats.sender_sources--;
2868 sendertimeout = TRUE;
2874 sess->total_sources--;
2876 sess->stats.sender_sources--;
2878 sess->stats.active_sources--;
2881 on_bye_timeout (sess, source);
2883 on_timeout (sess, source);
2886 on_sender_timeout (sess, source);
2889 source->closing = remove;
2893 session_sdes (RTPSession * sess, ReportData * data)
2895 GstRTCPPacket *packet = &data->packet;
2896 const GstStructure *sdes;
2898 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2900 /* add SDES packet */
2901 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SDES, packet);
2903 gst_rtcp_packet_sdes_add_item (packet, sess->source->ssrc);
2905 sdes = rtp_source_get_sdes_struct (sess->source);
2907 /* add all fields in the structure, the order is not important. */
2908 n_fields = gst_structure_n_fields (sdes);
2909 for (i = 0; i < n_fields; ++i) {
2912 GstRTCPSDESType type;
2914 field = gst_structure_nth_field_name (sdes, i);
2917 value = gst_structure_get_string (sdes, field);
2920 type = gst_rtcp_sdes_name_to_type (field);
2922 /* Early packets are minimal and only include the CNAME */
2923 if (data->is_early && type != GST_RTCP_SDES_CNAME)
2926 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
2927 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
2928 (const guint8 *) value);
2929 } else if (type == GST_RTCP_SDES_PRIV) {
2935 /* don't accept entries that are too big */
2936 prefix_len = strlen (field);
2937 if (prefix_len > 255)
2939 value_len = strlen (value);
2940 if (value_len > 255)
2942 data_len = 1 + prefix_len + value_len;
2946 data[0] = prefix_len;
2947 memcpy (&data[1], field, prefix_len);
2948 memcpy (&data[1 + prefix_len], value, value_len);
2950 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
2954 data->has_sdes = TRUE;
2957 /* schedule a BYE packet */
2959 session_bye (RTPSession * sess, ReportData * data)
2961 GstRTCPPacket *packet = &data->packet;
2962 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2965 session_start_rtcp (sess, data);
2968 session_sdes (sess, data);
2970 /* add a BYE packet */
2971 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_BYE, packet);
2972 gst_rtcp_packet_bye_add_ssrc (packet, sess->source->ssrc);
2973 if (sess->bye_reason)
2974 gst_rtcp_packet_bye_set_reason (packet, sess->bye_reason);
2976 /* we have a BYE packet now */
2977 data->is_bye = TRUE;
2981 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
2983 GstClockTime new_send_time, elapsed;
2985 if (data->is_early && sess->next_early_rtcp_time < current_time)
2988 /* no need to check yet */
2989 if (sess->next_rtcp_check_time == GST_CLOCK_TIME_NONE ||
2990 sess->next_rtcp_check_time > current_time) {
2991 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
2992 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
2993 GST_TIME_ARGS (current_time));
2997 /* get elapsed time since we last reported */
2998 elapsed = current_time - sess->last_rtcp_send_time;
3000 new_send_time = data->interval;
3001 /* perform forward reconsideration */
3002 if (new_send_time != GST_CLOCK_TIME_NONE) {
3003 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, new_send_time);
3005 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
3006 GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time),
3007 GST_TIME_ARGS (elapsed));
3009 new_send_time += sess->last_rtcp_send_time;
3012 /* check if reconsideration */
3013 if (new_send_time == GST_CLOCK_TIME_NONE || current_time < new_send_time) {
3014 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
3015 GST_TIME_ARGS (new_send_time));
3016 /* store new check time */
3017 sess->next_rtcp_check_time = new_send_time;
3023 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
3025 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
3026 GST_TIME_ARGS (new_send_time));
3028 sess->next_rtcp_check_time = new_send_time;
3029 if (new_send_time != GST_CLOCK_TIME_NONE) {
3030 sess->next_rtcp_check_time += current_time;
3032 /* Apply the rules from RFC 4585 section 3.5.3 */
3033 if (sess->stats.min_interval != 0 && !sess->first_rtcp) {
3034 GstClockTimeDiff T_rr_current_interval =
3035 g_random_double_range (0.5, 1.5) * sess->stats.min_interval;
3037 /* This will caused the RTCP to be suppressed if no FB packets are added */
3038 if (sess->last_rtcp_send_time + T_rr_current_interval >
3039 sess->next_rtcp_check_time) {
3040 GST_DEBUG ("RTCP packet could be suppressed min: %" GST_TIME_FORMAT
3041 " last: %" GST_TIME_FORMAT
3042 " + T_rr_current_interval: %" GST_TIME_FORMAT
3043 " > sess->next_rtcp_check_time: %" GST_TIME_FORMAT,
3044 GST_TIME_ARGS (sess->stats.min_interval),
3045 GST_TIME_ARGS (sess->last_rtcp_send_time),
3046 GST_TIME_ARGS (T_rr_current_interval),
3047 GST_TIME_ARGS (sess->next_rtcp_check_time));
3048 data->may_suppress = TRUE;
3057 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
3059 g_hash_table_insert (hash_table, key, g_object_ref (source));
3063 remove_closing_sources (const gchar * key, RTPSource * source, gpointer * data)
3065 return source->closing;
3069 * rtp_session_on_timeout:
3070 * @sess: an #RTPSession
3071 * @current_time: the current system time
3072 * @ntpnstime: the current NTP time in nanoseconds
3073 * @running_time: the current running_time of the pipeline
3075 * Perform maintenance actions after the timeout obtained with
3076 * rtp_session_next_timeout() expired.
3078 * This function will perform timeouts of receivers and senders, send a BYE
3079 * packet or generate RTCP packets with current session stats.
3081 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
3082 * times, for each packet that should be processed.
3084 * Returns: a #GstFlowReturn.
3087 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
3088 guint64 ntpnstime, GstClockTime running_time)
3090 GstFlowReturn result = GST_FLOW_OK;
3091 ReportData data = { GST_RTCP_BUFFER_INIT };
3093 GHashTable *table_copy;
3094 gboolean notify = FALSE;
3096 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3098 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT
3099 ", running-time %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
3100 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (running_time));
3104 data.current_time = current_time;
3105 data.ntpnstime = ntpnstime;
3106 data.is_bye = FALSE;
3107 data.has_sdes = FALSE;
3108 data.may_suppress = FALSE;
3109 data.running_time = running_time;
3113 RTP_SESSION_LOCK (sess);
3114 /* get a new interval, we need this for various cleanups etc */
3115 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
3117 /* Make a local copy of the hashtable. We need to do this because the
3118 * cleanup stage below releases the session lock. */
3119 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
3120 (GDestroyNotify) g_object_unref);
3121 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3122 (GHFunc) clone_ssrcs_hashtable, table_copy);
3124 /* Clean up the session, mark the source for removing, this might release the
3126 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
3127 g_hash_table_destroy (table_copy);
3129 /* Now remove the marked sources */
3130 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
3131 (GHRFunc) remove_closing_sources, NULL);
3133 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3134 data.is_early = TRUE;
3136 data.is_early = FALSE;
3138 /* see if we need to generate SR or RR packets */
3139 if (is_rtcp_time (sess, current_time, &data)) {
3140 if (own->received_bye) {
3141 /* generate BYE instead */
3142 GST_DEBUG ("generating BYE message");
3143 session_bye (sess, &data);
3144 sess->sent_bye = TRUE;
3146 /* loop over all known sources and do something */
3147 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3148 (GHFunc) session_report_blocks, &data);
3153 /* we keep track of the last report time in order to timeout inactive
3154 * receivers or senders */
3155 if (!data.is_early && !data.may_suppress)
3156 sess->last_rtcp_send_time = data.current_time;
3157 sess->first_rtcp = FALSE;
3158 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
3160 /* add SDES for this source when not already added */
3162 session_sdes (sess, &data);
3165 /* check for outdated collisions */
3166 GST_DEBUG ("Timing out collisions");
3167 rtp_source_timeout (sess->source, current_time,
3168 /* "a relatively long time" -- RFC 3550 section 8.2 */
3169 RTP_STATS_MIN_INTERVAL * GST_SECOND * 10,
3170 running_time - sess->rtcp_feedback_retention_window);
3172 if (sess->change_ssrc) {
3173 GST_DEBUG ("need to change our SSRC (%08x)", own->ssrc);
3174 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
3175 GINT_TO_POINTER (own->ssrc));
3177 own->ssrc = rtp_session_create_new_ssrc (sess);
3178 rtp_source_reset (own);
3180 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
3181 GINT_TO_POINTER (own->ssrc), own);
3183 g_free (sess->bye_reason);
3184 sess->bye_reason = NULL;
3185 sess->sent_bye = FALSE;
3186 sess->change_ssrc = FALSE;
3188 GST_DEBUG ("changed our SSRC to %08x", own->ssrc);
3191 sess->allow_early = TRUE;
3193 RTP_SESSION_UNLOCK (sess);
3196 g_object_notify (G_OBJECT (sess), "internal-ssrc");
3198 /* push out the RTCP packet */
3200 gboolean do_not_suppress;
3202 gst_rtcp_buffer_unmap (&data.rtcpbuf);
3204 /* Give the user a change to add its own packet */
3205 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
3206 data.rtcp, data.is_early, &do_not_suppress);
3208 if (sess->callbacks.send_rtcp && (do_not_suppress || !data.may_suppress)) {
3211 packet_size = gst_buffer_get_size (data.rtcp) + sess->header_len;
3213 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
3214 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
3215 sess->stats.avg_rtcp_packet_size, packet_size);
3217 sess->callbacks.send_rtcp (sess, own, data.rtcp, sess->sent_bye,
3218 sess->send_rtcp_user_data);
3220 GST_DEBUG ("freeing packet callback: %p"
3221 " do_not_suppress: %d may_suppress: %d",
3222 sess->callbacks.send_rtcp, do_not_suppress, data.may_suppress);
3223 gst_buffer_unref (data.rtcp);
3231 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
3232 GstClockTimeDiff max_delay)
3234 GstClockTime T_dither_max;
3236 /* Implements the algorithm described in RFC 4585 section 3.5.2 */
3238 RTP_SESSION_LOCK (sess);
3240 /* Check if already requested */
3241 /* RFC 4585 section 3.5.2 step 2 */
3242 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3245 if (!GST_CLOCK_TIME_IS_VALID (sess->next_rtcp_check_time))
3248 /* Ignore the request a scheduled packet will be in time anyway */
3249 if (current_time + max_delay > sess->next_rtcp_check_time)
3252 /* RFC 4585 section 3.5.2 step 2b */
3253 /* If the total sources is <=2, then there is only us and one peer */
3254 if (sess->total_sources <= 2) {
3257 /* Divide by 2 because l = 0.5 */
3258 T_dither_max = sess->next_rtcp_check_time - sess->last_rtcp_send_time;
3262 /* RFC 4585 section 3.5.2 step 3 */
3263 if (current_time + T_dither_max > sess->next_rtcp_check_time)
3266 /* RFC 4585 section 3.5.2 step 4
3267 * Don't send if allow_early is FALSE, but not if we are in
3268 * immediate mode, meaning we are part of a group of at most the
3269 * application-specific threshold.
3271 if (sess->total_sources > sess->rtcp_immediate_feedback_threshold &&
3272 sess->allow_early == FALSE)
3276 /* Schedule an early transmission later */
3277 sess->next_early_rtcp_time = g_random_double () * T_dither_max +
3280 /* If no dithering, schedule it for NOW */
3281 sess->next_early_rtcp_time = current_time;
3284 RTP_SESSION_UNLOCK (sess);
3286 /* notify app of need to send packet early
3287 * and therefore of timeout change */
3288 if (sess->callbacks.reconsider)
3289 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
3295 RTP_SESSION_UNLOCK (sess);
3299 rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc, GstClockTime now,
3300 gboolean fir, gint count)
3302 RTPSource *src = find_source (sess, ssrc);
3308 src->send_pli = FALSE;
3309 src->send_fir = TRUE;
3311 if (count == -1 || count != src->last_fir_count)
3312 src->current_send_fir_seqnum++;
3313 src->last_fir_count = count;
3314 } else if (!src->send_fir) {
3315 src->send_pli = TRUE;
3318 rtp_session_request_early_rtcp (sess, now, 200 * GST_MSECOND);
3324 has_pli_compare_func (gconstpointer a, gconstpointer ignored)
3326 GstRTCPPacket packet;
3327 GstRTCPBuffer rtcp = { NULL, };
3328 gboolean ret = FALSE;
3330 gst_rtcp_buffer_map ((GstBuffer *) a, GST_MAP_READ, &rtcp);
3332 if (gst_rtcp_buffer_get_first_packet (&rtcp, &packet)) {
3333 if (gst_rtcp_packet_get_type (&packet) == GST_RTCP_TYPE_PSFB &&
3334 gst_rtcp_packet_fb_get_type (&packet) == GST_RTCP_PSFB_TYPE_PLI)
3338 gst_rtcp_buffer_unmap (&rtcp);
3344 rtp_session_on_sending_rtcp (RTPSession * sess, GstBuffer * buffer,
3347 gboolean ret = FALSE;
3348 GHashTableIter iter;
3349 gpointer key, value;
3350 gboolean started_fir = FALSE;
3351 GstRTCPPacket fir_rtcppacket;
3352 GstRTCPBuffer rtcp = { NULL, };
3354 RTP_SESSION_LOCK (sess);
3356 gst_rtcp_buffer_map (buffer, GST_MAP_READWRITE, &rtcp);
3358 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
3359 while (g_hash_table_iter_next (&iter, &key, &value)) {
3360 guint media_ssrc = GPOINTER_TO_UINT (key);
3361 RTPSource *media_src = value;
3364 if (media_src->send_fir) {
3366 if (!gst_rtcp_buffer_add_packet (&rtcp, GST_RTCP_TYPE_PSFB,
3369 gst_rtcp_packet_fb_set_type (&fir_rtcppacket, GST_RTCP_PSFB_TYPE_FIR);
3370 gst_rtcp_packet_fb_set_sender_ssrc (&fir_rtcppacket,
3371 rtp_source_get_ssrc (sess->source));
3372 gst_rtcp_packet_fb_set_media_ssrc (&fir_rtcppacket, 0);
3374 if (!gst_rtcp_packet_fb_set_fci_length (&fir_rtcppacket, 2)) {
3375 gst_rtcp_packet_remove (&fir_rtcppacket);
3381 if (!gst_rtcp_packet_fb_set_fci_length (&fir_rtcppacket,
3382 !gst_rtcp_packet_fb_get_fci_length (&fir_rtcppacket) + 2))
3386 fci_data = gst_rtcp_packet_fb_get_fci (&fir_rtcppacket) -
3387 ((gst_rtcp_packet_fb_get_fci_length (&fir_rtcppacket) - 2) * 4);
3389 GST_WRITE_UINT32_BE (fci_data, media_ssrc);
3391 fci_data[0] = media_src->current_send_fir_seqnum;
3392 fci_data[1] = fci_data[2] = fci_data[3] = 0;
3393 media_src->send_fir = FALSE;
3397 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
3398 while (g_hash_table_iter_next (&iter, &key, &value)) {
3399 guint media_ssrc = GPOINTER_TO_UINT (key);
3400 RTPSource *media_src = value;
3401 GstRTCPPacket pli_rtcppacket;
3403 if (media_src->send_pli && !rtp_source_has_retained (media_src,
3404 has_pli_compare_func, NULL)) {
3405 if (!gst_rtcp_buffer_add_packet (&rtcp, GST_RTCP_TYPE_PSFB,
3407 /* Break because the packet is full, will put next request in a
3410 gst_rtcp_packet_fb_set_type (&pli_rtcppacket, GST_RTCP_PSFB_TYPE_PLI);
3411 gst_rtcp_packet_fb_set_sender_ssrc (&pli_rtcppacket,
3412 rtp_source_get_ssrc (sess->source));
3413 gst_rtcp_packet_fb_set_media_ssrc (&pli_rtcppacket, media_ssrc);
3416 media_src->send_pli = FALSE;
3418 gst_rtcp_buffer_unmap (&rtcp);
3420 RTP_SESSION_UNLOCK (sess);
3426 rtp_session_send_rtcp (RTPSession * sess, GstClockTimeDiff max_delay)
3430 if (!sess->callbacks.send_rtcp)
3433 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
3435 rtp_session_request_early_rtcp (sess, now, max_delay);