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 guint32 rtp_session_create_new_ssrc (RTPSession * sess);
126 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
127 gboolean * created, RTPArrivalStats * arrival, gboolean rtp);
128 static RTPSource *obtain_internal_source (RTPSession * sess,
129 guint32 ssrc, gboolean * created);
130 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
131 GstClockTime current_time);
132 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
133 gboolean deterministic, gboolean first);
136 accumulate_trues (GSignalInvocationHint * ihint, GValue * return_accu,
137 const GValue * handler_return, gpointer data)
139 if (g_value_get_boolean (handler_return))
140 g_value_set_boolean (return_accu, TRUE);
146 rtp_session_class_init (RTPSessionClass * klass)
148 GObjectClass *gobject_class;
150 gobject_class = (GObjectClass *) klass;
152 gobject_class->finalize = rtp_session_finalize;
153 gobject_class->set_property = rtp_session_set_property;
154 gobject_class->get_property = rtp_session_get_property;
157 * RTPSession::get-source-by-ssrc:
158 * @session: the object which received the signal
159 * @ssrc: the SSRC of the RTPSource
161 * Request the #RTPSource object with SSRC @ssrc in @session.
163 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
164 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
165 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
166 get_source_by_ssrc), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
167 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
170 * RTPSession::on-new-ssrc:
171 * @session: the object which received the signal
172 * @src: the new RTPSource
174 * Notify of a new SSRC that entered @session.
176 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
177 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
178 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
179 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
182 * RTPSession::on-ssrc-collision:
183 * @session: the object which received the signal
184 * @src: the #RTPSource that caused a collision
186 * Notify when we have an SSRC collision
188 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
189 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
190 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
191 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
194 * RTPSession::on-ssrc-validated:
195 * @session: the object which received the signal
196 * @src: the new validated RTPSource
198 * Notify of a new SSRC that became validated.
200 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
201 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
202 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
203 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
206 * RTPSession::on-ssrc-active:
207 * @session: the object which received the signal
208 * @src: the active RTPSource
210 * Notify of a SSRC that is active, i.e., sending RTCP.
212 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
213 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
214 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
215 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
218 * RTPSession::on-ssrc-sdes:
219 * @session: the object which received the signal
220 * @src: the RTPSource
222 * Notify that a new SDES was received for SSRC.
224 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
225 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
226 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
227 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
230 * RTPSession::on-bye-ssrc:
231 * @session: the object which received the signal
232 * @src: the RTPSource that went away
234 * Notify of an SSRC that became inactive because of a BYE packet.
236 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
237 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
238 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
239 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
242 * RTPSession::on-bye-timeout:
243 * @session: the object which received the signal
244 * @src: the RTPSource that timed out
246 * Notify of an SSRC that has timed out because of BYE
248 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
249 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
250 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
251 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
254 * RTPSession::on-timeout:
255 * @session: the object which received the signal
256 * @src: the RTPSource that timed out
258 * Notify of an SSRC that has timed out
260 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
261 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
262 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
263 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
266 * RTPSession::on-sender-timeout:
267 * @session: the object which received the signal
268 * @src: the RTPSource that timed out
270 * Notify of an SSRC that was a sender but timed out and became a receiver.
272 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
273 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
274 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
275 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
279 * RTPSession::on-sending-rtcp
280 * @session: the object which received the signal
281 * @buffer: the #GstBuffer containing the RTCP packet about to be sent
282 * @early: %TRUE if the packet is early, %FALSE if it is regular
284 * This signal is emitted before sending an RTCP packet, it can be used
285 * to add extra RTCP Packets.
287 * Returns: %TRUE if the RTCP buffer should NOT be suppressed, %FALSE
288 * if suppressing it is acceptable
290 rtp_session_signals[SIGNAL_ON_SENDING_RTCP] =
291 g_signal_new ("on-sending-rtcp", G_TYPE_FROM_CLASS (klass),
292 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_rtcp),
293 accumulate_trues, NULL, gst_rtp_bin_marshal_BOOLEAN__BOXED_BOOLEAN,
294 G_TYPE_BOOLEAN, 2, GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE,
298 * RTPSession::on-feedback-rtcp:
299 * @session: the object which received the signal
300 * @type: Type of RTCP packet, will be %GST_RTCP_TYPE_RTPFB or
301 * %GST_RTCP_TYPE_RTPFB
302 * @fbtype: The type of RTCP FB packet, probably part of #GstRTCPFBType
303 * @sender_ssrc: The SSRC of the sender
304 * @media_ssrc: The SSRC of the media this refers to
305 * @fci: a #GstBuffer with the FCI data from the FB packet or %NULL if
308 * Notify that a RTCP feedback packet has been received
310 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP] =
311 g_signal_new ("on-feedback-rtcp", G_TYPE_FROM_CLASS (klass),
312 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_feedback_rtcp),
313 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT_UINT_UINT_BOXED,
314 G_TYPE_NONE, 5, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT,
318 * RTPSession::send-rtcp:
319 * @session: the object which received the signal
320 * @max_delay: The maximum delay after which the feedback will not be useful
323 * Requests that the #RTPSession initiate a new RTCP packet as soon as
324 * possible within the requested delay.
327 rtp_session_signals[SIGNAL_SEND_RTCP] =
328 g_signal_new ("send-rtcp", G_TYPE_FROM_CLASS (klass),
329 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
330 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
331 gst_rtp_bin_marshal_VOID__UINT64, G_TYPE_NONE, 1, G_TYPE_UINT64);
333 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
334 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
335 "The internal SSRC used for the session",
336 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
338 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
339 g_param_spec_object ("internal-source", "Internal Source",
340 "The internal source element of the session",
341 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
343 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
344 g_param_spec_double ("bandwidth", "Bandwidth",
345 "The bandwidth of the session (0 for auto-discover)",
346 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
347 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
349 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
350 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
351 "The fraction of the bandwidth used for RTCP (or as a real fraction of the RTP bandwidth if < 1)",
352 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
353 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
355 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
356 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
357 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
358 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
359 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
361 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
362 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
363 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
364 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
365 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
367 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
368 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
369 "The maximum size of the RTCP packets",
370 16, G_MAXINT16, DEFAULT_RTCP_MTU,
371 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
373 g_object_class_install_property (gobject_class, PROP_SDES,
374 g_param_spec_boxed ("sdes", "SDES",
375 "The SDES items of this session",
376 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
378 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
379 g_param_spec_uint ("num-sources", "Num Sources",
380 "The number of sources in the session", 0, G_MAXUINT,
381 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
383 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
384 g_param_spec_uint ("num-active-sources", "Num Active Sources",
385 "The number of active sources in the session", 0, G_MAXUINT,
386 DEFAULT_NUM_ACTIVE_SOURCES,
387 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
391 * Get a GValue Array of all sources in the session.
394 * <title>Getting the #RTPSources of a session
401 * g_object_get (sess, "sources", &arr, NULL);
403 * for (i = 0; i < arr->n_values; i++) {
406 * val = g_value_array_get_nth (arr, i);
407 * source = g_value_get_object (val);
409 * g_value_array_free (arr);
414 g_object_class_install_property (gobject_class, PROP_SOURCES,
415 g_param_spec_boxed ("sources", "Sources",
416 "An array of all known sources in the session",
417 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
419 g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
420 g_param_spec_boolean ("favor-new", "Favor new sources",
421 "Resolve SSRC conflict in favor of new sources", FALSE,
422 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
424 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
425 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
426 "Minimum interval between Regular RTCP packet (in ns)",
427 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
428 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
430 g_object_class_install_property (gobject_class,
431 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
432 g_param_spec_uint64 ("rtcp-feedback-retention-window",
433 "RTCP Feedback retention window",
434 "Duration during which RTCP Feedback packets are retained (in ns)",
435 0, G_MAXUINT64, DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW,
436 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
438 g_object_class_install_property (gobject_class,
439 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
440 g_param_spec_uint ("rtcp-immediate-feedback-threshold",
441 "RTCP Immediate Feedback threshold",
442 "The maximum number of members of a RTP session for which immediate"
444 0, G_MAXUINT, DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
445 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
447 g_object_class_install_property (gobject_class, PROP_PROBATION,
448 g_param_spec_uint ("probation", "Number of probations",
449 "Consecutive packet sequence numbers to accept the source",
450 0, G_MAXUINT, DEFAULT_PROBATION,
451 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
453 klass->get_source_by_ssrc =
454 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
455 klass->on_sending_rtcp = GST_DEBUG_FUNCPTR (rtp_session_on_sending_rtcp);
456 klass->send_rtcp = GST_DEBUG_FUNCPTR (rtp_session_send_rtcp);
458 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
462 rtp_session_init (RTPSession * sess)
469 g_mutex_init (&sess->lock);
470 sess->key = g_random_int ();
474 for (i = 0; i < 32; i++) {
476 g_hash_table_new_full (NULL, NULL, NULL,
477 (GDestroyNotify) g_object_unref);
480 rtp_stats_init_defaults (&sess->stats);
481 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
482 rtp_stats_set_min_interval (&sess->stats,
483 (gdouble) DEFAULT_RTCP_MIN_INTERVAL / GST_SECOND);
485 sess->recalc_bandwidth = TRUE;
486 sess->bandwidth = DEFAULT_BANDWIDTH;
487 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
488 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
489 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
491 /* default UDP header length */
492 sess->header_len = 28;
493 sess->mtu = DEFAULT_RTCP_MTU;
495 sess->probation = DEFAULT_PROBATION;
497 /* some default SDES entries */
498 sess->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
500 /* we do not want to leak details like the username or hostname here */
501 str = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
502 gst_structure_set (sess->sdes, "cname", G_TYPE_STRING, str, NULL);
506 /* we do not want to leak the user's real name here */
507 str = g_strdup_printf ("Anon%u", g_random_int ());
508 gst_structure_set (sdes, "name", G_TYPE_STRING, str, NULL);
512 gst_structure_set (sess->sdes, "tool", G_TYPE_STRING, "GStreamer", NULL);
514 /* create an active SSRC for this session manager */
515 ssrc = rtp_session_create_new_ssrc (sess);
516 sess->source = obtain_internal_source (sess, ssrc, &created);
518 sess->first_rtcp = TRUE;
519 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
521 sess->allow_early = TRUE;
522 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
523 sess->rtcp_feedback_retention_window = DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW;
524 sess->rtcp_immediate_feedback_threshold =
525 DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD;
527 sess->last_keyframe_request = GST_CLOCK_TIME_NONE;
531 rtp_session_finalize (GObject * object)
536 sess = RTP_SESSION_CAST (object);
538 gst_structure_free (sess->sdes);
540 for (i = 0; i < 32; i++)
541 g_hash_table_destroy (sess->ssrcs[i]);
543 g_object_unref (sess->source);
544 g_mutex_clear (&sess->lock);
546 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
550 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
552 GValue value = { 0 };
554 g_value_init (&value, RTP_TYPE_SOURCE);
555 g_value_take_object (&value, source);
556 /* copies the value */
557 g_value_array_append (arr, &value);
561 rtp_session_create_sources (RTPSession * sess)
566 RTP_SESSION_LOCK (sess);
567 /* get number of elements in the table */
568 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
569 /* create the result value array */
570 res = g_value_array_new (size);
572 /* and copy all values into the array */
573 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
574 RTP_SESSION_UNLOCK (sess);
580 rtp_session_set_property (GObject * object, guint prop_id,
581 const GValue * value, GParamSpec * pspec)
585 sess = RTP_SESSION (object);
588 case PROP_INTERNAL_SSRC:
589 rtp_session_set_internal_ssrc (sess, g_value_get_uint (value));
592 RTP_SESSION_LOCK (sess);
593 sess->bandwidth = g_value_get_double (value);
594 sess->recalc_bandwidth = TRUE;
595 RTP_SESSION_UNLOCK (sess);
597 case PROP_RTCP_FRACTION:
598 RTP_SESSION_LOCK (sess);
599 sess->rtcp_bandwidth = g_value_get_double (value);
600 sess->recalc_bandwidth = TRUE;
601 RTP_SESSION_UNLOCK (sess);
603 case PROP_RTCP_RR_BANDWIDTH:
604 RTP_SESSION_LOCK (sess);
605 sess->rtcp_rr_bandwidth = g_value_get_int (value);
606 sess->recalc_bandwidth = TRUE;
607 RTP_SESSION_UNLOCK (sess);
609 case PROP_RTCP_RS_BANDWIDTH:
610 RTP_SESSION_LOCK (sess);
611 sess->rtcp_rs_bandwidth = g_value_get_int (value);
612 sess->recalc_bandwidth = TRUE;
613 RTP_SESSION_UNLOCK (sess);
616 sess->mtu = g_value_get_uint (value);
619 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
622 sess->favor_new = g_value_get_boolean (value);
624 case PROP_RTCP_MIN_INTERVAL:
625 rtp_stats_set_min_interval (&sess->stats,
626 (gdouble) g_value_get_uint64 (value) / GST_SECOND);
627 /* trigger reconsideration */
628 RTP_SESSION_LOCK (sess);
629 sess->next_rtcp_check_time = 0;
630 RTP_SESSION_UNLOCK (sess);
631 if (sess->callbacks.reconsider)
632 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
634 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
635 sess->rtcp_immediate_feedback_threshold = g_value_get_uint (value);
638 sess->probation = g_value_get_uint (value);
639 g_object_set_property (G_OBJECT (sess->source), "probation", value);
642 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
648 rtp_session_get_property (GObject * object, guint prop_id,
649 GValue * value, GParamSpec * pspec)
653 sess = RTP_SESSION (object);
656 case PROP_INTERNAL_SSRC:
657 g_value_set_uint (value, rtp_session_get_internal_ssrc (sess));
659 case PROP_INTERNAL_SOURCE:
660 g_value_take_object (value, rtp_session_get_internal_source (sess));
663 g_value_set_double (value, sess->bandwidth);
665 case PROP_RTCP_FRACTION:
666 g_value_set_double (value, sess->rtcp_bandwidth);
668 case PROP_RTCP_RR_BANDWIDTH:
669 g_value_set_int (value, sess->rtcp_rr_bandwidth);
671 case PROP_RTCP_RS_BANDWIDTH:
672 g_value_set_int (value, sess->rtcp_rs_bandwidth);
675 g_value_set_uint (value, sess->mtu);
678 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
680 case PROP_NUM_SOURCES:
681 g_value_set_uint (value, rtp_session_get_num_sources (sess));
683 case PROP_NUM_ACTIVE_SOURCES:
684 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
687 g_value_take_boxed (value, rtp_session_create_sources (sess));
690 g_value_set_boolean (value, sess->favor_new);
692 case PROP_RTCP_MIN_INTERVAL:
693 g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
695 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
696 g_value_set_uint (value, sess->rtcp_immediate_feedback_threshold);
699 g_value_set_uint (value, sess->probation);
700 g_object_get_property (G_OBJECT (sess->source), "probation", value);
703 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
709 on_new_ssrc (RTPSession * sess, RTPSource * source)
711 g_object_ref (source);
712 RTP_SESSION_UNLOCK (sess);
713 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
714 RTP_SESSION_LOCK (sess);
715 g_object_unref (source);
719 on_ssrc_collision (RTPSession * sess, RTPSource * source)
721 g_object_ref (source);
722 RTP_SESSION_UNLOCK (sess);
723 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
725 RTP_SESSION_LOCK (sess);
726 g_object_unref (source);
730 on_ssrc_validated (RTPSession * sess, RTPSource * source)
732 g_object_ref (source);
733 RTP_SESSION_UNLOCK (sess);
734 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
736 RTP_SESSION_LOCK (sess);
737 g_object_unref (source);
741 on_ssrc_active (RTPSession * sess, RTPSource * source)
743 g_object_ref (source);
744 RTP_SESSION_UNLOCK (sess);
745 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
746 RTP_SESSION_LOCK (sess);
747 g_object_unref (source);
751 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
753 g_object_ref (source);
754 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
755 RTP_SESSION_UNLOCK (sess);
756 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
757 RTP_SESSION_LOCK (sess);
758 g_object_unref (source);
762 on_bye_ssrc (RTPSession * sess, RTPSource * source)
764 g_object_ref (source);
765 RTP_SESSION_UNLOCK (sess);
766 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
767 RTP_SESSION_LOCK (sess);
768 g_object_unref (source);
772 on_bye_timeout (RTPSession * sess, RTPSource * source)
774 g_object_ref (source);
775 RTP_SESSION_UNLOCK (sess);
776 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
777 RTP_SESSION_LOCK (sess);
778 g_object_unref (source);
782 on_timeout (RTPSession * sess, RTPSource * source)
784 g_object_ref (source);
785 RTP_SESSION_UNLOCK (sess);
786 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
787 RTP_SESSION_LOCK (sess);
788 g_object_unref (source);
792 on_sender_timeout (RTPSession * sess, RTPSource * source)
794 g_object_ref (source);
795 RTP_SESSION_UNLOCK (sess);
796 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
798 RTP_SESSION_LOCK (sess);
799 g_object_unref (source);
805 * Create a new session object.
807 * Returns: a new #RTPSession. g_object_unref() after usage.
810 rtp_session_new (void)
814 sess = g_object_new (RTP_TYPE_SESSION, NULL);
820 * rtp_session_set_callbacks:
821 * @sess: an #RTPSession
822 * @callbacks: callbacks to configure
823 * @user_data: user data passed in the callbacks
825 * Configure a set of callbacks to be notified of actions.
828 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
831 g_return_if_fail (RTP_IS_SESSION (sess));
833 if (callbacks->process_rtp) {
834 sess->callbacks.process_rtp = callbacks->process_rtp;
835 sess->process_rtp_user_data = user_data;
837 if (callbacks->send_rtp) {
838 sess->callbacks.send_rtp = callbacks->send_rtp;
839 sess->send_rtp_user_data = user_data;
841 if (callbacks->send_rtcp) {
842 sess->callbacks.send_rtcp = callbacks->send_rtcp;
843 sess->send_rtcp_user_data = user_data;
845 if (callbacks->sync_rtcp) {
846 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
847 sess->sync_rtcp_user_data = user_data;
849 if (callbacks->clock_rate) {
850 sess->callbacks.clock_rate = callbacks->clock_rate;
851 sess->clock_rate_user_data = user_data;
853 if (callbacks->reconsider) {
854 sess->callbacks.reconsider = callbacks->reconsider;
855 sess->reconsider_user_data = user_data;
857 if (callbacks->request_key_unit) {
858 sess->callbacks.request_key_unit = callbacks->request_key_unit;
859 sess->request_key_unit_user_data = user_data;
861 if (callbacks->request_time) {
862 sess->callbacks.request_time = callbacks->request_time;
863 sess->request_time_user_data = user_data;
868 * rtp_session_set_process_rtp_callback:
869 * @sess: an #RTPSession
870 * @callback: callback to set
871 * @user_data: user data passed in the callback
873 * Configure only the process_rtp callback to be notified of the process_rtp action.
876 rtp_session_set_process_rtp_callback (RTPSession * sess,
877 RTPSessionProcessRTP callback, gpointer user_data)
879 g_return_if_fail (RTP_IS_SESSION (sess));
881 sess->callbacks.process_rtp = callback;
882 sess->process_rtp_user_data = user_data;
886 * rtp_session_set_send_rtp_callback:
887 * @sess: an #RTPSession
888 * @callback: callback to set
889 * @user_data: user data passed in the callback
891 * Configure only the send_rtp callback to be notified of the send_rtp action.
894 rtp_session_set_send_rtp_callback (RTPSession * sess,
895 RTPSessionSendRTP callback, gpointer user_data)
897 g_return_if_fail (RTP_IS_SESSION (sess));
899 sess->callbacks.send_rtp = callback;
900 sess->send_rtp_user_data = user_data;
904 * rtp_session_set_send_rtcp_callback:
905 * @sess: an #RTPSession
906 * @callback: callback to set
907 * @user_data: user data passed in the callback
909 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
912 rtp_session_set_send_rtcp_callback (RTPSession * sess,
913 RTPSessionSendRTCP callback, gpointer user_data)
915 g_return_if_fail (RTP_IS_SESSION (sess));
917 sess->callbacks.send_rtcp = callback;
918 sess->send_rtcp_user_data = user_data;
922 * rtp_session_set_sync_rtcp_callback:
923 * @sess: an #RTPSession
924 * @callback: callback to set
925 * @user_data: user data passed in the callback
927 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
930 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
931 RTPSessionSyncRTCP callback, gpointer user_data)
933 g_return_if_fail (RTP_IS_SESSION (sess));
935 sess->callbacks.sync_rtcp = callback;
936 sess->sync_rtcp_user_data = user_data;
940 * rtp_session_set_clock_rate_callback:
941 * @sess: an #RTPSession
942 * @callback: callback to set
943 * @user_data: user data passed in the callback
945 * Configure only the clock_rate callback to be notified of the clock_rate action.
948 rtp_session_set_clock_rate_callback (RTPSession * sess,
949 RTPSessionClockRate callback, gpointer user_data)
951 g_return_if_fail (RTP_IS_SESSION (sess));
953 sess->callbacks.clock_rate = callback;
954 sess->clock_rate_user_data = user_data;
958 * rtp_session_set_reconsider_callback:
959 * @sess: an #RTPSession
960 * @callback: callback to set
961 * @user_data: user data passed in the callback
963 * Configure only the reconsider callback to be notified of the reconsider action.
966 rtp_session_set_reconsider_callback (RTPSession * sess,
967 RTPSessionReconsider callback, gpointer user_data)
969 g_return_if_fail (RTP_IS_SESSION (sess));
971 sess->callbacks.reconsider = callback;
972 sess->reconsider_user_data = user_data;
976 * rtp_session_set_request_time_callback:
977 * @sess: an #RTPSession
978 * @callback: callback to set
979 * @user_data: user data passed in the callback
981 * Configure only the request_time callback
984 rtp_session_set_request_time_callback (RTPSession * sess,
985 RTPSessionRequestTime callback, gpointer user_data)
987 g_return_if_fail (RTP_IS_SESSION (sess));
989 sess->callbacks.request_time = callback;
990 sess->request_time_user_data = user_data;
994 * rtp_session_set_bandwidth:
995 * @sess: an #RTPSession
996 * @bandwidth: the bandwidth allocated
998 * Set the session bandwidth in bytes per second.
1001 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
1003 g_return_if_fail (RTP_IS_SESSION (sess));
1005 RTP_SESSION_LOCK (sess);
1006 sess->stats.bandwidth = bandwidth;
1007 RTP_SESSION_UNLOCK (sess);
1011 * rtp_session_get_bandwidth:
1012 * @sess: an #RTPSession
1014 * Get the session bandwidth.
1016 * Returns: the session bandwidth.
1019 rtp_session_get_bandwidth (RTPSession * sess)
1023 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1025 RTP_SESSION_LOCK (sess);
1026 result = sess->stats.bandwidth;
1027 RTP_SESSION_UNLOCK (sess);
1033 * rtp_session_set_rtcp_fraction:
1034 * @sess: an #RTPSession
1035 * @bandwidth: the RTCP bandwidth
1037 * Set the bandwidth in bytes per second that should be used for RTCP
1041 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
1043 g_return_if_fail (RTP_IS_SESSION (sess));
1045 RTP_SESSION_LOCK (sess);
1046 sess->stats.rtcp_bandwidth = bandwidth;
1047 RTP_SESSION_UNLOCK (sess);
1051 * rtp_session_get_rtcp_fraction:
1052 * @sess: an #RTPSession
1054 * Get the session bandwidth used for RTCP.
1056 * Returns: The bandwidth used for RTCP messages.
1059 rtp_session_get_rtcp_fraction (RTPSession * sess)
1063 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
1065 RTP_SESSION_LOCK (sess);
1066 result = sess->stats.rtcp_bandwidth;
1067 RTP_SESSION_UNLOCK (sess);
1073 * rtp_session_get_sdes_struct:
1074 * @sess: an #RTSPSession
1076 * Get the SDES data as a #GstStructure
1078 * Returns: a GstStructure with SDES items for @sess. This function returns a
1079 * copy of the SDES structure, use gst_structure_free() after usage.
1082 rtp_session_get_sdes_struct (RTPSession * sess)
1084 GstStructure *result = NULL;
1086 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1088 RTP_SESSION_LOCK (sess);
1090 result = gst_structure_copy (sess->sdes);
1091 RTP_SESSION_UNLOCK (sess);
1097 * rtp_session_set_sdes_struct:
1098 * @sess: an #RTSPSession
1099 * @sdes: a #GstStructure
1101 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1104 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
1106 g_return_if_fail (sdes);
1107 g_return_if_fail (RTP_IS_SESSION (sess));
1109 RTP_SESSION_LOCK (sess);
1111 gst_structure_free (sess->sdes);
1112 sess->sdes = gst_structure_copy (sdes);
1113 RTP_SESSION_UNLOCK (sess);
1116 static GstFlowReturn
1117 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1119 GstFlowReturn result = GST_FLOW_OK;
1121 if (source->internal) {
1122 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1124 RTP_SESSION_UNLOCK (session);
1126 if (session->callbacks.send_rtp)
1128 session->callbacks.send_rtp (session, source, data,
1129 session->send_rtp_user_data);
1131 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1134 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1135 RTP_SESSION_UNLOCK (session);
1137 if (session->callbacks.process_rtp)
1139 session->callbacks.process_rtp (session, source,
1140 GST_BUFFER_CAST (data), session->process_rtp_user_data);
1142 gst_buffer_unref (GST_BUFFER_CAST (data));
1144 RTP_SESSION_LOCK (session);
1150 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1154 RTP_SESSION_UNLOCK (session);
1156 if (session->callbacks.clock_rate)
1158 session->callbacks.clock_rate (session, pt,
1159 session->clock_rate_user_data);
1163 RTP_SESSION_LOCK (session);
1165 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1170 static RTPSourceCallbacks callbacks = {
1171 (RTPSourcePushRTP) source_push_rtp,
1172 (RTPSourceClockRate) source_clock_rate,
1176 check_collision (RTPSession * sess, RTPSource * source,
1177 RTPArrivalStats * arrival, gboolean rtp)
1179 /* If we have no arrival address, we can't do collision checking */
1180 if (!arrival->address)
1183 if (!source->internal) {
1184 GSocketAddress *from;
1186 /* This is not our local source, but lets check if two remote
1189 from = source->rtp_from;
1191 from = source->rtcp_from;
1195 if (__g_socket_address_equal (from, arrival->address)) {
1196 /* Address is the same */
1199 GST_LOG ("we have a third-party collision or loop ssrc:%x",
1200 rtp_source_get_ssrc (source));
1201 if (sess->favor_new) {
1202 if (rtp_source_find_conflicting_address (source,
1203 arrival->address, arrival->current_time)) {
1206 buf1 = __g_socket_address_to_string (arrival->address);
1207 GST_LOG ("Known conflict on %x for %s, dropping packet",
1208 rtp_source_get_ssrc (source), buf1);
1215 /* Current address is not a known conflict, lets assume this is
1216 * a new source. Save old address in possible conflict list
1218 rtp_source_add_conflicting_address (source, from,
1219 arrival->current_time);
1221 buf1 = __g_socket_address_to_string (from);
1222 buf2 = __g_socket_address_to_string (arrival->address);
1224 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1225 " saving old as known conflict",
1226 rtp_source_get_ssrc (source), buf1, buf2);
1229 rtp_source_set_rtp_from (source, arrival->address);
1231 rtp_source_set_rtcp_from (source, arrival->address);
1239 /* Don't need to save old addresses, we ignore new sources */
1244 /* We don't already have a from address for RTP, just set it */
1246 rtp_source_set_rtp_from (source, arrival->address);
1248 rtp_source_set_rtcp_from (source, arrival->address);
1252 /* FIXME: Log 3rd party collision somehow
1253 * Maybe should be done in upper layer, only the SDES can tell us
1254 * if its a collision or a loop
1257 /* This is sending with our ssrc, is it an address we already know */
1259 if (rtp_source_find_conflicting_address (source, arrival->address,
1260 arrival->current_time)) {
1261 /* Its a known conflict, its probably a loop, not a collision
1262 * lets just drop the incoming packet
1264 GST_DEBUG ("Our packets are being looped back to us, dropping");
1266 /* Its a new collision, lets change our SSRC */
1268 rtp_source_add_conflicting_address (source, arrival->address,
1269 arrival->current_time);
1271 GST_DEBUG ("Collision for SSRC %x", rtp_source_get_ssrc (source));
1272 on_ssrc_collision (sess, source);
1274 sess->change_ssrc = TRUE;
1276 rtp_source_mark_bye (source, "SSRC Collision");
1277 rtp_session_schedule_bye_locked (sess, arrival->current_time);
1285 find_source (RTPSession * sess, guint32 ssrc)
1287 return g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1288 GINT_TO_POINTER (ssrc));
1292 add_source (RTPSession * sess, RTPSource * src)
1294 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1295 GINT_TO_POINTER (src->ssrc), src);
1296 /* we have one more source now */
1297 sess->total_sources++;
1298 if (RTP_SOURCE_IS_ACTIVE (src))
1299 sess->stats.active_sources++;
1301 sess->stats.internal_sources++;
1304 /* must be called with the session lock, the returned source needs to be
1305 * unreffed after usage. */
1307 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1308 RTPArrivalStats * arrival, gboolean rtp)
1312 source = find_source (sess, ssrc);
1313 if (source == NULL) {
1314 /* make new Source in probation and insert */
1315 source = rtp_source_new (ssrc);
1317 GST_DEBUG ("creating new source %08x %p", ssrc, source);
1319 /* for RTP packets we need to set the source in probation. Receiving RTCP
1320 * packets of an SSRC, on the other hand, is a strong indication that we
1321 * are dealing with a valid source. */
1323 g_object_set (source, "probation", sess->probation, NULL);
1325 g_object_set (source, "probation", 0, NULL);
1327 /* store from address, if any */
1328 if (arrival->address) {
1330 rtp_source_set_rtp_from (source, arrival->address);
1332 rtp_source_set_rtcp_from (source, arrival->address);
1335 /* configure a callback on the source */
1336 rtp_source_set_callbacks (source, &callbacks, sess);
1338 add_source (sess, source);
1342 /* check for collision, this updates the address when not previously set */
1343 if (check_collision (sess, source, arrival, rtp)) {
1346 /* Receiving RTCP packets of an SSRC is a strong indication that we
1347 * are dealing with a valid source. */
1349 g_object_set (source, "probation", 0, NULL);
1351 /* update last activity */
1352 source->last_activity = arrival->current_time;
1354 source->last_rtp_activity = arrival->current_time;
1355 g_object_ref (source);
1360 /* must be called with the session lock, the returned source needs to be
1361 * unreffed after usage. */
1363 obtain_internal_source (RTPSession * sess, guint32 ssrc, gboolean * created)
1367 source = find_source (sess, ssrc);
1368 if (source == NULL) {
1369 /* make new internal Source and insert */
1370 source = rtp_source_new (ssrc);
1372 GST_DEBUG ("creating new internal source %08x %p", ssrc, source);
1374 source->validated = TRUE;
1375 source->internal = TRUE;
1376 rtp_source_set_sdes_struct (source, gst_structure_copy (sess->sdes));
1377 rtp_source_set_callbacks (source, &callbacks, sess);
1379 add_source (sess, source);
1384 g_object_ref (source);
1390 * rtp_session_get_internal_source:
1391 * @sess: a #RTPSession
1393 * Get the internal #RTPSource of @sess.
1395 * Returns: The internal #RTPSource. g_object_unref() after usage.
1398 rtp_session_get_internal_source (RTPSession * sess)
1402 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1404 result = g_object_ref (sess->source);
1410 * rtp_session_set_internal_ssrc:
1411 * @sess: a #RTPSession
1414 * Set the SSRC of @sess to @ssrc.
1417 rtp_session_set_internal_ssrc (RTPSession * sess, guint32 ssrc)
1419 RTP_SESSION_LOCK (sess);
1420 if (ssrc != sess->source->ssrc) {
1421 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
1422 GINT_TO_POINTER (sess->source->ssrc));
1424 GST_DEBUG ("setting internal SSRC to %08x", ssrc);
1425 /* After this call, any receiver of the old SSRC either in RTP or RTCP
1426 * packets will timeout on the old SSRC, we could potentially schedule a
1427 * BYE RTCP for the old SSRC... */
1428 sess->source->ssrc = ssrc;
1429 rtp_source_reset (sess->source);
1431 /* rehash with the new SSRC */
1432 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1433 GINT_TO_POINTER (sess->source->ssrc), sess->source);
1435 RTP_SESSION_UNLOCK (sess);
1437 g_object_notify (G_OBJECT (sess), "internal-ssrc");
1441 * rtp_session_get_internal_ssrc:
1442 * @sess: a #RTPSession
1444 * Get the internal SSRC of @sess.
1446 * Returns: The SSRC of the session.
1449 rtp_session_get_internal_ssrc (RTPSession * sess)
1453 RTP_SESSION_LOCK (sess);
1454 ssrc = sess->source->ssrc;
1455 RTP_SESSION_UNLOCK (sess);
1461 * rtp_session_add_source:
1462 * @sess: a #RTPSession
1463 * @src: #RTPSource to add
1465 * Add @src to @session.
1467 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1468 * existed in the session.
1471 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1473 gboolean result = FALSE;
1476 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1477 g_return_val_if_fail (src != NULL, FALSE);
1479 RTP_SESSION_LOCK (sess);
1480 find = find_source (sess, src->ssrc);
1482 add_source (sess, src);
1485 RTP_SESSION_UNLOCK (sess);
1491 * rtp_session_get_num_sources:
1492 * @sess: an #RTPSession
1494 * Get the number of sources in @sess.
1496 * Returns: The number of sources in @sess.
1499 rtp_session_get_num_sources (RTPSession * sess)
1503 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1505 RTP_SESSION_LOCK (sess);
1506 result = sess->total_sources;
1507 RTP_SESSION_UNLOCK (sess);
1513 * rtp_session_get_num_active_sources:
1514 * @sess: an #RTPSession
1516 * Get the number of active sources in @sess. A source is considered active when
1517 * it has been validated and has not yet received a BYE RTCP message.
1519 * Returns: The number of active sources in @sess.
1522 rtp_session_get_num_active_sources (RTPSession * sess)
1526 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1528 RTP_SESSION_LOCK (sess);
1529 result = sess->stats.active_sources;
1530 RTP_SESSION_UNLOCK (sess);
1536 * rtp_session_get_source_by_ssrc:
1537 * @sess: an #RTPSession
1540 * Find the source with @ssrc in @sess.
1542 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1543 * g_object_unref() after usage.
1546 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1550 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1552 RTP_SESSION_LOCK (sess);
1553 result = find_source (sess, ssrc);
1555 g_object_ref (result);
1556 RTP_SESSION_UNLOCK (sess);
1561 /* should be called with the SESSION lock */
1563 rtp_session_create_new_ssrc (RTPSession * sess)
1568 ssrc = g_random_int ();
1570 /* see if it exists in the session, we're done if it doesn't */
1571 if (find_source (sess, ssrc) == NULL)
1579 * rtp_session_create_source:
1580 * @sess: an #RTPSession
1582 * Create an #RTPSource for use in @sess. This function will create a source
1583 * with an ssrc that is currently not used by any participants in the session.
1585 * Returns: an #RTPSource.
1588 rtp_session_create_source (RTPSession * sess)
1593 RTP_SESSION_LOCK (sess);
1594 ssrc = rtp_session_create_new_ssrc (sess);
1595 source = rtp_source_new (ssrc);
1596 rtp_source_set_callbacks (source, &callbacks, sess);
1597 /* we need an additional ref for the source in the hashtable */
1598 g_object_ref (source);
1599 add_source (sess, source);
1600 RTP_SESSION_UNLOCK (sess);
1605 /* update the RTPArrivalStats structure with the current time and other bits
1606 * about the current buffer we are handling.
1607 * This function is typically called when a validated packet is received.
1608 * This function should be called with the SESSION_LOCK
1611 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
1612 gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
1613 GstClockTime running_time, guint64 ntpnstime)
1615 GstNetAddressMeta *meta;
1616 GstRTPBuffer rtpb = { NULL };
1618 /* get time of arrival */
1619 arrival->current_time = current_time;
1620 arrival->running_time = running_time;
1621 arrival->ntpnstime = ntpnstime;
1623 /* get packet size including header overhead */
1624 arrival->bytes = gst_buffer_get_size (buffer) + sess->header_len;
1627 gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtpb);
1628 arrival->payload_len = gst_rtp_buffer_get_payload_len (&rtpb);
1629 gst_rtp_buffer_unmap (&rtpb);
1631 arrival->payload_len = 0;
1634 /* for netbuffer we can store the IP address to check for collisions */
1635 meta = gst_buffer_get_net_address_meta (buffer);
1636 if (arrival->address)
1637 g_object_unref (arrival->address);
1639 arrival->address = G_SOCKET_ADDRESS (g_object_ref (meta->addr));
1641 arrival->address = NULL;
1646 clean_arrival_stats (RTPArrivalStats * arrival)
1648 if (arrival->address)
1649 g_object_unref (arrival->address);
1653 * rtp_session_process_rtp:
1654 * @sess: and #RTPSession
1655 * @buffer: an RTP buffer
1656 * @current_time: the current system time
1657 * @running_time: the running_time of @buffer
1659 * Process an RTP buffer in the session manager. This function takes ownership
1662 * Returns: a #GstFlowReturn.
1665 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1666 GstClockTime current_time, GstClockTime running_time)
1668 GstFlowReturn result;
1672 gboolean prevsender, prevactive;
1673 RTPArrivalStats arrival = { NULL, };
1677 GstRTPBuffer rtp = { NULL };
1679 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1680 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1682 if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp))
1683 goto invalid_packet;
1685 /* get SSRC to look up in session database */
1686 ssrc = gst_rtp_buffer_get_ssrc (&rtp);
1687 /* copy available csrc for later */
1688 count = gst_rtp_buffer_get_csrc_count (&rtp);
1689 /* make sure to not overflow our array. An RTP buffer can maximally contain
1691 count = MIN (count, 16);
1693 for (i = 0; i < count; i++)
1694 csrcs[i] = gst_rtp_buffer_get_csrc (&rtp, i);
1696 gst_rtp_buffer_unmap (&rtp);
1698 RTP_SESSION_LOCK (sess);
1699 /* ignore more RTP packets when we left the session */
1700 if (sess->source->marked_bye)
1703 /* update arrival stats */
1704 update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1707 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1711 prevsender = RTP_SOURCE_IS_SENDER (source);
1712 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1713 oldrate = source->bitrate;
1715 /* let source process the packet */
1716 result = rtp_source_process_rtp (source, buffer, &arrival);
1718 /* source became active */
1719 if (prevactive != RTP_SOURCE_IS_ACTIVE (source)) {
1720 sess->stats.active_sources++;
1721 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1722 sess->stats.active_sources);
1723 on_ssrc_validated (sess, source);
1725 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1726 sess->stats.sender_sources++;
1727 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1728 sess->stats.sender_sources);
1730 if (oldrate != source->bitrate)
1731 sess->recalc_bandwidth = TRUE;
1734 on_new_ssrc (sess, source);
1736 if (source->validated) {
1739 /* for validated sources, we add the CSRCs as well */
1740 for (i = 0; i < count; i++) {
1742 RTPSource *csrc_src;
1747 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1752 GST_DEBUG ("created new CSRC: %08x", csrc);
1753 rtp_source_set_as_csrc (csrc_src);
1754 if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1755 sess->stats.active_sources++;
1756 on_new_ssrc (sess, csrc_src);
1758 g_object_unref (csrc_src);
1761 g_object_unref (source);
1763 RTP_SESSION_UNLOCK (sess);
1765 clean_arrival_stats (&arrival);
1772 gst_buffer_unref (buffer);
1773 GST_DEBUG ("invalid RTP packet received");
1778 RTP_SESSION_UNLOCK (sess);
1779 gst_buffer_unref (buffer);
1780 GST_DEBUG ("ignoring RTP packet because we are leaving");
1785 RTP_SESSION_UNLOCK (sess);
1786 gst_buffer_unref (buffer);
1787 clean_arrival_stats (&arrival);
1788 GST_DEBUG ("ignoring packet because its collisioning");
1794 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1795 GstRTCPPacket * packet, RTPArrivalStats * arrival)
1799 count = gst_rtcp_packet_get_rb_count (packet);
1800 for (i = 0; i < count; i++) {
1801 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1802 guint8 fractionlost;
1805 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1806 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1808 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1810 if (ssrc == sess->source->ssrc) {
1811 /* only deal with report blocks for our session, we update the stats of
1812 * the sender of the RTCP message. We could also compare our stats against
1813 * the other sender to see if we are better or worse. */
1814 rtp_source_process_rb (source, arrival->ntpnstime, fractionlost,
1815 packetslost, exthighestseq, jitter, lsr, dlsr);
1818 on_ssrc_active (sess, source);
1821 /* A Sender report contains statistics about how the sender is doing. This
1822 * includes timing informataion such as the relation between RTP and NTP
1823 * timestamps and the number of packets/bytes it sent to us.
1825 * In this report is also included a set of report blocks related to how this
1826 * sender is receiving data (in case we (or somebody else) is also sending stuff
1827 * to it). This info includes the packet loss, jitter and seqnum. It also
1828 * contains information to calculate the round trip time (LSR/DLSR).
1831 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1832 RTPArrivalStats * arrival, gboolean * do_sync)
1834 guint32 senderssrc, rtptime, packet_count, octet_count;
1837 gboolean created, prevsender;
1839 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1840 &packet_count, &octet_count);
1842 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1843 senderssrc, GST_TIME_ARGS (arrival->current_time));
1845 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1849 /* don't try to do lip-sync for sources that sent a BYE */
1850 if (RTP_SOURCE_IS_MARKED_BYE (source))
1855 prevsender = RTP_SOURCE_IS_SENDER (source);
1857 /* first update the source */
1858 rtp_source_process_sr (source, arrival->current_time, ntptime, rtptime,
1859 packet_count, octet_count);
1861 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1862 sess->stats.sender_sources++;
1863 GST_DEBUG ("source: %08x became sender, %d sender sources", senderssrc,
1864 sess->stats.sender_sources);
1868 on_new_ssrc (sess, source);
1870 rtp_session_process_rb (sess, source, packet, arrival);
1871 g_object_unref (source);
1874 /* A receiver report contains statistics about how a receiver is doing. It
1875 * includes stuff like packet loss, jitter and the seqnum it received last. It
1876 * also contains info to calculate the round trip time.
1878 * We are only interested in how the sender of this report is doing wrt to us.
1881 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1882 RTPArrivalStats * arrival)
1888 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1890 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1892 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1897 on_new_ssrc (sess, source);
1899 rtp_session_process_rb (sess, source, packet, arrival);
1900 g_object_unref (source);
1903 /* Get SDES items and store them in the SSRC */
1905 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1906 RTPArrivalStats * arrival)
1909 gboolean more_items, more_entries;
1911 items = gst_rtcp_packet_sdes_get_item_count (packet);
1912 GST_DEBUG ("got SDES packet with %d items", items);
1914 more_items = gst_rtcp_packet_sdes_first_item (packet);
1916 while (more_items) {
1918 gboolean changed, created, validated;
1922 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1924 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1928 /* find src, no probation when dealing with RTCP */
1929 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1933 sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
1935 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1937 while (more_entries) {
1938 GstRTCPSDESType type;
1944 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1946 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1949 if (type == GST_RTCP_SDES_PRIV) {
1950 name = g_strndup ((const gchar *) &data[1], data[0]);
1952 data += data[0] + 1;
1954 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
1957 value = g_strndup ((const gchar *) data, len);
1959 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
1964 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1968 /* takes ownership of sdes */
1969 changed = rtp_source_set_sdes_struct (source, sdes);
1971 validated = !RTP_SOURCE_IS_ACTIVE (source);
1972 source->validated = TRUE;
1975 on_new_ssrc (sess, source);
1977 /* source became active */
1979 sess->stats.active_sources++;
1980 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1981 sess->stats.active_sources);
1982 on_ssrc_validated (sess, source);
1986 on_ssrc_sdes (sess, source);
1988 g_object_unref (source);
1990 more_items = gst_rtcp_packet_sdes_next_item (packet);
1995 /* BYE is sent when a client leaves the session
1998 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
1999 RTPArrivalStats * arrival)
2003 gboolean reconsider = FALSE;
2005 reason = gst_rtcp_packet_bye_get_reason (packet);
2006 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
2008 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
2009 for (i = 0; i < count; i++) {
2012 gboolean created, prevactive, prevsender;
2013 guint pmembers, members;
2015 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
2016 GST_DEBUG ("SSRC: %08x", ssrc);
2018 /* find src and mark bye, no probation when dealing with RTCP */
2019 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
2023 if (source->internal) {
2024 /* our own source, something weird with this packet */
2025 g_object_unref (source);
2029 /* store time for when we need to time out this source */
2030 source->bye_time = arrival->current_time;
2032 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2033 prevsender = RTP_SOURCE_IS_SENDER (source);
2035 /* mark the source BYE */
2036 rtp_source_mark_bye (source, reason);
2038 pmembers = sess->stats.active_sources;
2040 if (prevactive && !RTP_SOURCE_IS_ACTIVE (source)) {
2041 sess->stats.active_sources--;
2042 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
2043 sess->stats.active_sources);
2045 if (prevsender && !RTP_SOURCE_IS_SENDER (source)) {
2046 sess->stats.sender_sources--;
2047 if (source->internal)
2048 sess->stats.internal_sender_sources--;
2049 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
2050 sess->stats.sender_sources);
2052 members = sess->stats.active_sources;
2054 if (!sess->scheduled_bye && members < pmembers) {
2055 /* some members went away since the previous timeout estimate.
2056 * Perform reverse reconsideration but only when we are not scheduling a
2058 if (sess->next_rtcp_check_time != GST_CLOCK_TIME_NONE &&
2059 arrival->current_time < sess->next_rtcp_check_time) {
2060 GstClockTime time_remaining;
2062 time_remaining = sess->next_rtcp_check_time - arrival->current_time;
2063 sess->next_rtcp_check_time =
2064 gst_util_uint64_scale (time_remaining, members, pmembers);
2066 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
2067 GST_TIME_ARGS (sess->next_rtcp_check_time));
2069 sess->next_rtcp_check_time += arrival->current_time;
2071 /* mark pending reconsider. We only want to signal the reconsideration
2072 * once after we handled all the source in the bye packet */
2078 on_new_ssrc (sess, source);
2080 on_bye_ssrc (sess, source);
2082 g_object_unref (source);
2085 RTP_SESSION_UNLOCK (sess);
2086 /* notify app of reconsideration */
2087 if (sess->callbacks.reconsider)
2088 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2089 RTP_SESSION_LOCK (sess);
2095 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
2096 RTPArrivalStats * arrival)
2098 GST_DEBUG ("received APP");
2102 rtp_session_request_local_key_unit (RTPSession * sess, RTPSource * src,
2103 gboolean fir, GstClockTime current_time)
2105 guint32 round_trip = 0;
2107 rtp_source_get_last_rb (src, NULL, NULL, NULL, NULL, NULL, NULL, &round_trip);
2109 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE && round_trip) {
2110 GstClockTime round_trip_in_ns = gst_util_uint64_scale (round_trip,
2113 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE &&
2114 current_time - sess->last_keyframe_request < 2 * round_trip_in_ns) {
2115 GST_DEBUG ("Ignoring %s request because one was send without one "
2116 "RTT (%" GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")",
2117 fir ? "FIR" : "PLI",
2118 GST_TIME_ARGS (current_time - sess->last_keyframe_request),
2119 GST_TIME_ARGS (round_trip_in_ns));;
2124 sess->last_keyframe_request = current_time;
2126 GST_LOG ("received %s request from %X %p(%p)", fir ? "FIR" : "PLI",
2127 rtp_source_get_ssrc (src), sess->callbacks.process_rtp,
2128 sess->callbacks.request_key_unit);
2130 RTP_SESSION_UNLOCK (sess);
2131 sess->callbacks.request_key_unit (sess, fir,
2132 sess->request_key_unit_user_data);
2133 RTP_SESSION_LOCK (sess);
2139 rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
2140 guint32 media_ssrc, GstClockTime current_time)
2144 if (!sess->callbacks.request_key_unit)
2147 src = find_source (sess, sender_ssrc);
2151 rtp_session_request_local_key_unit (sess, src, FALSE, current_time);
2155 rtp_session_process_fir (RTPSession * sess, guint32 sender_ssrc,
2156 guint8 * fci_data, guint fci_length, GstClockTime current_time)
2161 gboolean our_request = FALSE;
2163 if (!sess->callbacks.request_key_unit)
2169 src = find_source (sess, sender_ssrc);
2171 /* Hack because Google fails to set the sender_ssrc correctly */
2172 if (!src && sender_ssrc == 1) {
2173 GHashTableIter iter;
2175 if (sess->stats.sender_sources >
2176 RTP_SOURCE_IS_SENDER (sess->source) ? 2 : 1)
2179 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
2181 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
2182 if (src != sess->source && rtp_source_is_sender (src))
2191 for (position = 0; position < fci_length; position += 8) {
2192 guint8 *data = fci_data + position;
2195 ssrc = GST_READ_UINT32_BE (data);
2197 own = find_source (sess, ssrc);
2198 if (own->internal) {
2206 rtp_session_request_local_key_unit (sess, src, TRUE, current_time);
2210 rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
2211 RTPArrivalStats * arrival, GstClockTime current_time)
2213 GstRTCPType type = gst_rtcp_packet_get_type (packet);
2214 GstRTCPFBType fbtype = gst_rtcp_packet_fb_get_type (packet);
2215 guint32 sender_ssrc = gst_rtcp_packet_fb_get_sender_ssrc (packet);
2216 guint32 media_ssrc = gst_rtcp_packet_fb_get_media_ssrc (packet);
2217 guint8 *fci_data = gst_rtcp_packet_fb_get_fci (packet);
2218 guint fci_length = 4 * gst_rtcp_packet_fb_get_fci_length (packet);
2221 GST_DEBUG ("received feedback %d:%d from %08X about %08X with FCI of "
2222 "length %d", type, fbtype, sender_ssrc, media_ssrc, fci_length);
2224 if (g_signal_has_handler_pending (sess,
2225 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0, TRUE)) {
2226 GstBuffer *fci_buffer = NULL;
2228 if (fci_length > 0) {
2229 fci_buffer = gst_buffer_copy_region (packet->rtcp->buffer,
2230 GST_BUFFER_COPY_MEMORY, fci_data - packet->rtcp->map.data,
2232 GST_BUFFER_TIMESTAMP (fci_buffer) = arrival->running_time;
2235 RTP_SESSION_UNLOCK (sess);
2236 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0,
2237 type, fbtype, sender_ssrc, media_ssrc, fci_buffer);
2238 RTP_SESSION_LOCK (sess);
2241 gst_buffer_unref (fci_buffer);
2244 src = find_source (sess, media_ssrc);
2248 if (sess->rtcp_feedback_retention_window) {
2249 rtp_source_retain_rtcp_packet (src, packet, arrival->running_time);
2252 if (src->internal ||
2253 /* PSFB FIR puts the media ssrc inside the FCI */
2254 (type == GST_RTCP_TYPE_PSFB && fbtype == GST_RTCP_PSFB_TYPE_FIR)) {
2256 case GST_RTCP_TYPE_PSFB:
2258 case GST_RTCP_PSFB_TYPE_PLI:
2259 rtp_session_process_pli (sess, sender_ssrc, media_ssrc,
2262 case GST_RTCP_PSFB_TYPE_FIR:
2263 rtp_session_process_fir (sess, sender_ssrc, fci_data, fci_length,
2270 case GST_RTCP_TYPE_RTPFB:
2278 * rtp_session_process_rtcp:
2279 * @sess: and #RTPSession
2280 * @buffer: an RTCP buffer
2281 * @current_time: the current system time
2282 * @ntpnstime: the current NTP time in nanoseconds
2284 * Process an RTCP buffer in the session manager. This function takes ownership
2287 * Returns: a #GstFlowReturn.
2290 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
2291 GstClockTime current_time, guint64 ntpnstime)
2293 GstRTCPPacket packet;
2294 gboolean more, is_bye = FALSE, do_sync = FALSE;
2295 RTPArrivalStats arrival = { NULL, };
2296 GstFlowReturn result = GST_FLOW_OK;
2297 GstRTCPBuffer rtcp = { NULL, };
2299 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2300 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2302 if (!gst_rtcp_buffer_validate (buffer))
2303 goto invalid_packet;
2305 GST_DEBUG ("received RTCP packet");
2307 RTP_SESSION_LOCK (sess);
2308 /* update arrival stats */
2309 update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1,
2312 if (sess->source->sent_bye)
2315 /* start processing the compound packet */
2316 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
2317 more = gst_rtcp_buffer_get_first_packet (&rtcp, &packet);
2321 type = gst_rtcp_packet_get_type (&packet);
2323 /* when we are leaving the session, we should ignore all non-BYE messages */
2324 if (sess->scheduled_bye && type != GST_RTCP_TYPE_BYE) {
2325 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
2330 case GST_RTCP_TYPE_SR:
2331 rtp_session_process_sr (sess, &packet, &arrival, &do_sync);
2333 case GST_RTCP_TYPE_RR:
2334 rtp_session_process_rr (sess, &packet, &arrival);
2336 case GST_RTCP_TYPE_SDES:
2337 rtp_session_process_sdes (sess, &packet, &arrival);
2339 case GST_RTCP_TYPE_BYE:
2341 /* don't try to attempt lip-sync anymore for streams with a BYE */
2343 rtp_session_process_bye (sess, &packet, &arrival);
2345 case GST_RTCP_TYPE_APP:
2346 rtp_session_process_app (sess, &packet, &arrival);
2348 case GST_RTCP_TYPE_RTPFB:
2349 case GST_RTCP_TYPE_PSFB:
2350 rtp_session_process_feedback (sess, &packet, &arrival, current_time);
2353 GST_WARNING ("got unknown RTCP packet");
2357 more = gst_rtcp_packet_move_to_next (&packet);
2360 gst_rtcp_buffer_unmap (&rtcp);
2362 /* if we are scheduling a BYE, we only want to count bye packets, else we
2363 * count everything */
2364 if (sess->scheduled_bye) {
2366 sess->stats.bye_members++;
2367 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2370 /* keep track of average packet size */
2371 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2373 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
2374 sess->stats.avg_rtcp_packet_size, arrival.bytes);
2375 RTP_SESSION_UNLOCK (sess);
2377 clean_arrival_stats (&arrival);
2379 /* notify caller of sr packets in the callback */
2380 if (do_sync && sess->callbacks.sync_rtcp) {
2381 /* make writable, we might want to change the buffer */
2382 buffer = gst_buffer_make_writable (buffer);
2384 result = sess->callbacks.sync_rtcp (sess, buffer,
2385 sess->sync_rtcp_user_data);
2387 gst_buffer_unref (buffer);
2394 GST_DEBUG ("invalid RTCP packet received");
2395 gst_buffer_unref (buffer);
2400 RTP_SESSION_UNLOCK (sess);
2401 gst_buffer_unref (buffer);
2402 clean_arrival_stats (&arrival);
2403 GST_DEBUG ("ignoring RTCP packet because we left");
2409 * rtp_session_update_send_caps:
2410 * @sess: an #RTPSession
2413 * Update the caps of the sender in the rtp session.
2416 rtp_session_update_send_caps (RTPSession * sess, GstCaps * caps)
2418 g_return_if_fail (RTP_IS_SESSION (sess));
2419 g_return_if_fail (GST_IS_CAPS (caps));
2421 GST_LOG ("received caps %" GST_PTR_FORMAT, caps);
2423 RTP_SESSION_LOCK (sess);
2424 rtp_source_update_caps (sess->source, caps);
2425 RTP_SESSION_UNLOCK (sess);
2429 * rtp_session_send_rtp:
2430 * @sess: an #RTPSession
2431 * @data: pointer to either an RTP buffer or a list of RTP buffers
2432 * @is_list: TRUE when @data is a buffer list
2433 * @current_time: the current system time
2434 * @running_time: the running time of @data
2436 * Send the RTP buffer in the session manager. This function takes ownership of
2439 * Returns: a #GstFlowReturn.
2442 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
2443 GstClockTime current_time, GstClockTime running_time)
2445 GstFlowReturn result;
2447 gboolean prevsender;
2450 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2451 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
2453 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
2455 RTP_SESSION_LOCK (sess);
2456 source = sess->source;
2458 /* update last activity */
2459 source->last_rtp_activity = current_time;
2461 prevsender = RTP_SOURCE_IS_SENDER (source);
2462 oldrate = source->bitrate;
2464 /* we use our own source to send */
2465 result = rtp_source_send_rtp (source, data, is_list, running_time);
2467 if (RTP_SOURCE_IS_SENDER (source) && !prevsender) {
2468 sess->stats.sender_sources++;
2469 sess->stats.internal_sender_sources++;
2471 if (oldrate != source->bitrate)
2472 sess->recalc_bandwidth = TRUE;
2473 RTP_SESSION_UNLOCK (sess);
2479 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
2481 *bandwidth += source->bitrate;
2484 /* must be called with session lock */
2486 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2489 GstClockTime result;
2491 /* recalculate bandwidth when it changed */
2492 if (sess->recalc_bandwidth) {
2495 if (sess->bandwidth > 0)
2496 bandwidth = sess->bandwidth;
2498 /* If it is <= 0, then try to estimate the actual bandwidth */
2501 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2502 (GHFunc) add_bitrates, &bandwidth);
2505 if (bandwidth < 8000)
2506 bandwidth = RTP_STATS_BANDWIDTH;
2508 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
2509 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
2511 sess->recalc_bandwidth = FALSE;
2514 if (sess->scheduled_bye) {
2515 result = rtp_stats_calculate_bye_interval (&sess->stats);
2517 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2518 sess->stats.internal_sender_sources > 0, first);
2521 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2522 GST_TIME_ARGS (result), first);
2524 if (!deterministic && result != GST_CLOCK_TIME_NONE)
2525 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2527 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2533 source_mark_bye (const gchar * key, RTPSource * source, const gchar * reason)
2535 if (source->internal)
2536 rtp_source_mark_bye (source, reason);
2540 * rtp_session_mark_all_bye:
2541 * @sess: an #RTPSession
2544 * Mark all internal sources of the session as BYE with @reason.
2547 rtp_session_mark_all_bye (RTPSession * sess, const gchar * reason)
2549 g_return_if_fail (RTP_IS_SESSION (sess));
2551 RTP_SESSION_LOCK (sess);
2552 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2553 (GHFunc) source_mark_bye, (gpointer) reason);
2554 RTP_SESSION_UNLOCK (sess);
2557 /* Stop the current @sess and schedule a BYE message for the other members.
2558 * One must have the session lock to call this function
2560 static GstFlowReturn
2561 rtp_session_schedule_bye_locked (RTPSession * sess, GstClockTime current_time)
2563 GstFlowReturn result = GST_FLOW_OK;
2564 GstClockTime interval;
2566 /* nothing to do it we already scheduled bye */
2567 if (sess->scheduled_bye)
2570 /* we schedule BYE now */
2571 sess->scheduled_bye = TRUE;
2572 /* at least one member wants to send a BYE */
2573 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
2574 sess->stats.bye_members = 1;
2575 sess->first_rtcp = TRUE;
2576 sess->allow_early = TRUE;
2578 /* reschedule transmission */
2579 sess->last_rtcp_send_time = current_time;
2580 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2582 if (interval != GST_CLOCK_TIME_NONE)
2583 sess->next_rtcp_check_time = current_time + interval;
2585 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
2587 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2588 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2590 RTP_SESSION_UNLOCK (sess);
2591 /* notify app of reconsideration */
2592 if (sess->callbacks.reconsider)
2593 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2594 RTP_SESSION_LOCK (sess);
2601 * rtp_session_schedule_bye:
2602 * @sess: an #RTPSession
2603 * @current_time: the current system time
2605 * Schedule a BYE message for all sources marked as BYE in @sess.
2607 * Returns: a #GstFlowReturn.
2610 rtp_session_schedule_bye (RTPSession * sess, GstClockTime current_time)
2612 GstFlowReturn result = GST_FLOW_OK;
2614 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2616 RTP_SESSION_LOCK (sess);
2617 result = rtp_session_schedule_bye_locked (sess, current_time);
2618 RTP_SESSION_UNLOCK (sess);
2624 * rtp_session_next_timeout:
2625 * @sess: an #RTPSession
2626 * @current_time: the current system time
2628 * Get the next time we should perform session maintenance tasks.
2630 * Returns: a time when rtp_session_on_timeout() should be called with the
2631 * current system time.
2634 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2636 GstClockTime result, interval = 0;
2638 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
2640 RTP_SESSION_LOCK (sess);
2642 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
2643 result = sess->next_early_rtcp_time;
2647 result = sess->next_rtcp_check_time;
2649 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2650 ", next time: %" GST_TIME_FORMAT,
2651 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2653 if (result == GST_CLOCK_TIME_NONE || result < current_time) {
2654 GST_DEBUG ("take current time as base");
2655 /* our previous check time expired, start counting from the current time
2657 result = current_time;
2660 if (sess->scheduled_bye) {
2661 if (sess->source->sent_bye) {
2662 GST_DEBUG ("we sent BYE already");
2663 interval = GST_CLOCK_TIME_NONE;
2664 } else if (sess->stats.active_sources >= 50) {
2665 GST_DEBUG ("reconsider BYE, more than 50 sources");
2666 /* reconsider BYE if members >= 50 */
2667 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2670 if (sess->first_rtcp) {
2671 GST_DEBUG ("first RTCP packet");
2672 /* we are called for the first time */
2673 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2674 } else if (sess->next_rtcp_check_time < current_time) {
2675 GST_DEBUG ("old check time expired, getting new timeout");
2676 /* get a new timeout when we need to */
2677 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
2681 if (interval != GST_CLOCK_TIME_NONE)
2684 result = GST_CLOCK_TIME_NONE;
2686 sess->next_rtcp_check_time = result;
2690 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2691 ", next time: %" GST_TIME_FORMAT,
2692 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2693 RTP_SESSION_UNLOCK (sess);
2700 GstRTCPBuffer rtcpbuf;
2704 GstClockTime current_time;
2706 GstClockTime running_time;
2707 GstClockTime interval;
2708 GstRTCPPacket packet;
2712 gboolean may_suppress;
2717 session_start_rtcp (RTPSession * sess, ReportData * data)
2719 GstRTCPPacket *packet = &data->packet;
2720 RTPSource *own = data->source;
2721 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2723 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2724 data->is_bye = FALSE;
2725 data->has_sdes = FALSE;
2727 gst_rtcp_buffer_map (data->rtcp, GST_MAP_READWRITE, rtcp);
2729 if (RTP_SOURCE_IS_SENDER (own)) {
2732 guint32 packet_count, octet_count;
2734 /* we are a sender, create SR */
2735 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2736 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SR, packet);
2738 /* get latest stats */
2739 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
2740 &ntptime, &rtptime, &packet_count, &octet_count);
2742 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2743 packet_count, octet_count);
2745 /* fill in sender report info */
2746 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2747 ntptime, rtptime, packet_count, octet_count);
2749 /* we are only receiver, create RR */
2750 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2751 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RR, packet);
2752 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2756 /* construct a Sender or Receiver Report */
2758 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2760 GstRTCPPacket *packet = &data->packet;
2762 if (gst_rtcp_packet_get_rb_count (packet) < GST_RTCP_MAX_RB_COUNT) {
2763 /* only report about other sender sources */
2764 if (source != data->source && RTP_SOURCE_IS_SENDER (source)) {
2765 guint8 fractionlost;
2767 guint32 exthighestseq, jitter;
2771 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2772 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2774 /* store last generated RR packet */
2775 source->last_rr.is_valid = TRUE;
2776 source->last_rr.fractionlost = fractionlost;
2777 source->last_rr.packetslost = packetslost;
2778 source->last_rr.exthighestseq = exthighestseq;
2779 source->last_rr.jitter = jitter;
2780 source->last_rr.lsr = lsr;
2781 source->last_rr.dlsr = dlsr;
2783 /* packet is not yet filled, add report block for this source. */
2784 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2785 exthighestseq, jitter, lsr, dlsr);
2790 /* perform cleanup of sources that timed out */
2792 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2794 gboolean remove = FALSE;
2795 gboolean byetimeout = FALSE;
2796 gboolean sendertimeout = FALSE;
2797 gboolean is_sender, is_active;
2798 RTPSession *sess = data->sess;
2799 GstClockTime interval, binterval;
2802 /* check for outdated collisions */
2803 if (source->internal) {
2804 GST_DEBUG ("Timing out collisions");
2805 rtp_source_timeout (source, data->current_time,
2806 /* "a relatively long time" -- RFC 3550 section 8.2 */
2807 RTP_STATS_MIN_INTERVAL * GST_SECOND * 10,
2808 data->running_time - sess->rtcp_feedback_retention_window);
2811 /* nothing else to do when without RTCP */
2812 if (data->interval == GST_CLOCK_TIME_NONE)
2815 is_sender = RTP_SOURCE_IS_SENDER (source);
2816 is_active = RTP_SOURCE_IS_ACTIVE (source);
2818 /* our own rtcp interval may have been forced low by secondary configuration,
2819 * while sender side may still operate with higher interval,
2820 * so do not just take our interval to decide on timing out sender,
2821 * but take (if data->interval <= 5 * GST_SECOND):
2822 * interval = CLAMP (sender_interval, data->interval, 5 * GST_SECOND)
2823 * where sender_interval is difference between last 2 received RTCP reports
2825 if (data->interval >= 5 * GST_SECOND || source->internal) {
2826 binterval = data->interval;
2828 GST_LOG ("prev_rtcp %" GST_TIME_FORMAT ", last_rtcp %" GST_TIME_FORMAT,
2829 GST_TIME_ARGS (source->stats.prev_rtcptime),
2830 GST_TIME_ARGS (source->stats.last_rtcptime));
2831 /* if not received enough yet, fallback to larger default */
2832 if (source->stats.last_rtcptime > source->stats.prev_rtcptime)
2833 binterval = source->stats.last_rtcptime - source->stats.prev_rtcptime;
2835 binterval = 5 * GST_SECOND;
2836 binterval = CLAMP (binterval, data->interval, 5 * GST_SECOND);
2838 GST_LOG ("timeout base interval %" GST_TIME_FORMAT,
2839 GST_TIME_ARGS (binterval));
2841 /* check for our own source, we don't want to delete our own source. */
2842 if (!source->internal) {
2843 if (source->marked_bye) {
2844 /* if we received a BYE from the source, remove the source after some
2846 if (data->current_time > source->bye_time &&
2847 data->current_time - source->bye_time > sess->stats.bye_timeout) {
2848 GST_DEBUG ("removing BYE source %08x", source->ssrc);
2853 /* sources that were inactive for more than 5 times the deterministic reporting
2854 * interval get timed out. the min timeout is 5 seconds. */
2855 /* mind old time that might pre-date last time going to PLAYING */
2856 btime = MAX (source->last_activity, sess->start_time);
2857 if (data->current_time > btime) {
2858 interval = MAX (binterval * 5, 5 * GST_SECOND);
2859 if (data->current_time - btime > interval) {
2860 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2861 source->ssrc, GST_TIME_ARGS (btime));
2867 /* senders that did not send for a long time become a receiver, this also
2868 * holds for our own sources. */
2870 /* mind old time that might pre-date last time going to PLAYING */
2871 btime = MAX (source->last_rtp_activity, sess->start_time);
2872 if (data->current_time > btime) {
2873 interval = MAX (binterval * 2, 5 * GST_SECOND);
2874 if (data->current_time - btime > interval) {
2875 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
2876 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
2877 source->is_sender = FALSE;
2878 sess->stats.sender_sources--;
2879 if (source->internal)
2880 sess->stats.internal_sender_sources--;
2881 sendertimeout = TRUE;
2887 sess->total_sources--;
2889 sess->stats.sender_sources--;
2890 if (source->internal)
2891 sess->stats.internal_sender_sources--;
2894 sess->stats.active_sources--;
2896 if (source->internal)
2897 sess->stats.internal_sources--;
2900 on_bye_timeout (sess, source);
2902 on_timeout (sess, source);
2905 on_sender_timeout (sess, source);
2908 source->closing = remove;
2912 session_sdes (RTPSession * sess, ReportData * data)
2914 GstRTCPPacket *packet = &data->packet;
2915 const GstStructure *sdes;
2917 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2919 /* add SDES packet */
2920 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SDES, packet);
2922 gst_rtcp_packet_sdes_add_item (packet, data->source->ssrc);
2924 sdes = rtp_source_get_sdes_struct (data->source);
2926 /* add all fields in the structure, the order is not important. */
2927 n_fields = gst_structure_n_fields (sdes);
2928 for (i = 0; i < n_fields; ++i) {
2931 GstRTCPSDESType type;
2933 field = gst_structure_nth_field_name (sdes, i);
2936 value = gst_structure_get_string (sdes, field);
2939 type = gst_rtcp_sdes_name_to_type (field);
2941 /* Early packets are minimal and only include the CNAME */
2942 if (data->is_early && type != GST_RTCP_SDES_CNAME)
2945 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
2946 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
2947 (const guint8 *) value);
2948 } else if (type == GST_RTCP_SDES_PRIV) {
2954 /* don't accept entries that are too big */
2955 prefix_len = strlen (field);
2956 if (prefix_len > 255)
2958 value_len = strlen (value);
2959 if (value_len > 255)
2961 data_len = 1 + prefix_len + value_len;
2965 data[0] = prefix_len;
2966 memcpy (&data[1], field, prefix_len);
2967 memcpy (&data[1 + prefix_len], value, value_len);
2969 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
2973 data->has_sdes = TRUE;
2976 /* schedule a BYE packet */
2978 make_source_bye (RTPSession * sess, RTPSource * source, ReportData * data)
2980 GstRTCPPacket *packet = &data->packet;
2981 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2984 session_sdes (sess, data);
2985 /* add a BYE packet */
2986 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_BYE, packet);
2987 gst_rtcp_packet_bye_add_ssrc (packet, source->ssrc);
2988 if (source->bye_reason)
2989 gst_rtcp_packet_bye_set_reason (packet, source->bye_reason);
2991 /* we have a BYE packet now */
2992 data->is_bye = TRUE;
2993 source->sent_bye = TRUE;
2997 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
2999 GstClockTime new_send_time, elapsed;
3001 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3002 data->is_early = TRUE;
3004 data->is_early = FALSE;
3006 if (data->is_early && sess->next_early_rtcp_time < current_time)
3009 /* no need to check yet */
3010 if (sess->next_rtcp_check_time == GST_CLOCK_TIME_NONE ||
3011 sess->next_rtcp_check_time > current_time) {
3012 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
3013 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
3014 GST_TIME_ARGS (current_time));
3018 /* get elapsed time since we last reported */
3019 elapsed = current_time - sess->last_rtcp_send_time;
3021 new_send_time = data->interval;
3022 /* perform forward reconsideration */
3023 if (new_send_time != GST_CLOCK_TIME_NONE) {
3024 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, new_send_time);
3026 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
3027 GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time),
3028 GST_TIME_ARGS (elapsed));
3030 new_send_time += sess->last_rtcp_send_time;
3033 /* check if reconsideration */
3034 if (new_send_time == GST_CLOCK_TIME_NONE || current_time < new_send_time) {
3035 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
3036 GST_TIME_ARGS (new_send_time));
3037 /* store new check time */
3038 sess->next_rtcp_check_time = new_send_time;
3044 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
3046 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
3047 GST_TIME_ARGS (new_send_time));
3049 sess->next_rtcp_check_time = new_send_time;
3050 if (new_send_time != GST_CLOCK_TIME_NONE) {
3051 sess->next_rtcp_check_time += current_time;
3053 /* Apply the rules from RFC 4585 section 3.5.3 */
3054 if (sess->stats.min_interval != 0 && !sess->first_rtcp) {
3055 GstClockTimeDiff T_rr_current_interval =
3056 g_random_double_range (0.5, 1.5) * sess->stats.min_interval;
3058 /* This will caused the RTCP to be suppressed if no FB packets are added */
3059 if (sess->last_rtcp_send_time + T_rr_current_interval >
3060 sess->next_rtcp_check_time) {
3061 GST_DEBUG ("RTCP packet could be suppressed min: %" GST_TIME_FORMAT
3062 " last: %" GST_TIME_FORMAT
3063 " + T_rr_current_interval: %" GST_TIME_FORMAT
3064 " > sess->next_rtcp_check_time: %" GST_TIME_FORMAT,
3065 GST_TIME_ARGS (sess->stats.min_interval),
3066 GST_TIME_ARGS (sess->last_rtcp_send_time),
3067 GST_TIME_ARGS (T_rr_current_interval),
3068 GST_TIME_ARGS (sess->next_rtcp_check_time));
3069 data->may_suppress = TRUE;
3078 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
3080 g_hash_table_insert (hash_table, key, g_object_ref (source));
3084 remove_closing_sources (const gchar * key, RTPSource * source, gpointer * data)
3086 return source->closing;
3090 generate_rtcp (RTPSource * source, ReportData * data)
3092 RTPSession *sess = data->sess;
3094 /* only generate RTCP for active internal sources */
3095 if (!source->internal || source->sent_bye)
3098 data->source = source;
3101 session_start_rtcp (sess, data);
3103 if (source->marked_bye) {
3105 make_source_bye (sess, source, data);
3106 } else if (!data->is_early) {
3107 /* loop over all known sources and add report blocks. If we are ealy, we
3108 * just make a minimal RTCP packet and skip this step */
3109 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3110 (GHFunc) session_report_blocks, data);
3112 if (!data->has_sdes)
3113 session_sdes (sess, data);
3115 gst_rtcp_buffer_unmap (&data->rtcpbuf);
3117 if (sess->change_ssrc) {
3118 GST_DEBUG ("need to change our SSRC (%08x)", source->ssrc);
3119 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
3120 GINT_TO_POINTER (source->ssrc));
3122 source->ssrc = rtp_session_create_new_ssrc (sess);
3123 rtp_source_reset (source);
3125 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
3126 GINT_TO_POINTER (source->ssrc), source);
3128 sess->change_ssrc = FALSE;
3129 data->notify = TRUE;
3130 GST_DEBUG ("changed our SSRC to %08x", source->ssrc);
3135 * rtp_session_on_timeout:
3136 * @sess: an #RTPSession
3137 * @current_time: the current system time
3138 * @ntpnstime: the current NTP time in nanoseconds
3139 * @running_time: the current running_time of the pipeline
3141 * Perform maintenance actions after the timeout obtained with
3142 * rtp_session_next_timeout() expired.
3144 * This function will perform timeouts of receivers and senders, send a BYE
3145 * packet or generate RTCP packets with current session stats.
3147 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
3148 * times, for each packet that should be processed.
3150 * Returns: a #GstFlowReturn.
3153 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
3154 guint64 ntpnstime, GstClockTime running_time)
3156 GstFlowReturn result = GST_FLOW_OK;
3157 ReportData data = { GST_RTCP_BUFFER_INIT };
3159 GHashTable *table_copy;
3161 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3163 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT
3164 ", running-time %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
3165 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (running_time));
3168 data.current_time = current_time;
3169 data.ntpnstime = ntpnstime;
3170 data.running_time = running_time;
3171 data.may_suppress = FALSE;
3172 data.notify = FALSE;
3176 RTP_SESSION_LOCK (sess);
3177 /* get a new interval, we need this for various cleanups etc */
3178 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
3180 /* Make a local copy of the hashtable. We need to do this because the
3181 * cleanup stage below releases the session lock. */
3182 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
3183 (GDestroyNotify) g_object_unref);
3184 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3185 (GHFunc) clone_ssrcs_hashtable, table_copy);
3187 /* Clean up the session, mark the source for removing, this might release the
3189 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
3190 g_hash_table_destroy (table_copy);
3192 /* Now remove the marked sources */
3193 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
3194 (GHRFunc) remove_closing_sources, NULL);
3196 /* see if we need to generate SR or RR packets */
3197 if (!is_rtcp_time (sess, current_time, &data))
3200 generate_rtcp (own, &data);
3202 /* we keep track of the last report time in order to timeout inactive
3203 * receivers or senders */
3204 if (!data.is_early && !data.may_suppress)
3205 sess->last_rtcp_send_time = data.current_time;
3206 sess->first_rtcp = FALSE;
3207 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
3210 RTP_SESSION_UNLOCK (sess);
3213 g_object_notify (G_OBJECT (sess), "internal-ssrc");
3215 /* push out the RTCP packet */
3217 gboolean do_not_suppress;
3218 GstBuffer *buffer = data.rtcp;
3220 /* Give the user a change to add its own packet */
3221 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
3222 buffer, data.is_early, &do_not_suppress);
3224 if (sess->callbacks.send_rtcp && (do_not_suppress || !data.may_suppress)) {
3227 packet_size = gst_buffer_get_size (buffer) + sess->header_len;
3229 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
3230 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
3231 sess->stats.avg_rtcp_packet_size, packet_size);
3233 sess->callbacks.send_rtcp (sess, own, buffer, data.is_bye,
3234 sess->send_rtcp_user_data);
3236 GST_DEBUG ("freeing packet callback: %p"
3237 " do_not_suppress: %d may_suppress: %d",
3238 sess->callbacks.send_rtcp, do_not_suppress, data.may_suppress);
3239 gst_buffer_unref (buffer);
3247 * rtp_session_request_early_rtcp:
3248 * @sess: an #RTPSession
3249 * @current_time: the current system time
3250 * @max_delay: maximum delay
3252 * Request transmission of early RTCP
3255 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
3256 GstClockTimeDiff max_delay)
3258 GstClockTime T_dither_max;
3260 /* Implements the algorithm described in RFC 4585 section 3.5.2 */
3262 RTP_SESSION_LOCK (sess);
3264 /* Check if already requested */
3265 /* RFC 4585 section 3.5.2 step 2 */
3266 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3269 if (!GST_CLOCK_TIME_IS_VALID (sess->next_rtcp_check_time))
3272 /* Ignore the request a scheduled packet will be in time anyway */
3273 if (current_time + max_delay > sess->next_rtcp_check_time)
3276 /* RFC 4585 section 3.5.2 step 2b */
3277 /* If the total sources is <=2, then there is only us and one peer */
3278 if (sess->total_sources <= 2) {
3281 /* Divide by 2 because l = 0.5 */
3282 T_dither_max = sess->next_rtcp_check_time - sess->last_rtcp_send_time;
3286 /* RFC 4585 section 3.5.2 step 3 */
3287 if (current_time + T_dither_max > sess->next_rtcp_check_time)
3290 /* RFC 4585 section 3.5.2 step 4
3291 * Don't send if allow_early is FALSE, but not if we are in
3292 * immediate mode, meaning we are part of a group of at most the
3293 * application-specific threshold.
3295 if (sess->total_sources > sess->rtcp_immediate_feedback_threshold &&
3296 sess->allow_early == FALSE)
3300 /* Schedule an early transmission later */
3301 sess->next_early_rtcp_time = g_random_double () * T_dither_max +
3304 /* If no dithering, schedule it for NOW */
3305 sess->next_early_rtcp_time = current_time;
3308 RTP_SESSION_UNLOCK (sess);
3310 /* notify app of need to send packet early
3311 * and therefore of timeout change */
3312 if (sess->callbacks.reconsider)
3313 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
3319 RTP_SESSION_UNLOCK (sess);
3323 rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc, GstClockTime now,
3324 gboolean fir, gint count)
3326 RTPSource *src = find_source (sess, ssrc);
3332 src->send_pli = FALSE;
3333 src->send_fir = TRUE;
3335 if (count == -1 || count != src->last_fir_count)
3336 src->current_send_fir_seqnum++;
3337 src->last_fir_count = count;
3338 } else if (!src->send_fir) {
3339 src->send_pli = TRUE;
3342 rtp_session_request_early_rtcp (sess, now, 200 * GST_MSECOND);
3348 has_pli_compare_func (gconstpointer a, gconstpointer ignored)
3350 GstRTCPPacket packet;
3351 GstRTCPBuffer rtcp = { NULL, };
3352 gboolean ret = FALSE;
3354 gst_rtcp_buffer_map ((GstBuffer *) a, GST_MAP_READ, &rtcp);
3356 if (gst_rtcp_buffer_get_first_packet (&rtcp, &packet)) {
3357 if (gst_rtcp_packet_get_type (&packet) == GST_RTCP_TYPE_PSFB &&
3358 gst_rtcp_packet_fb_get_type (&packet) == GST_RTCP_PSFB_TYPE_PLI)
3362 gst_rtcp_buffer_unmap (&rtcp);
3368 rtp_session_on_sending_rtcp (RTPSession * sess, GstBuffer * buffer,
3371 gboolean ret = FALSE;
3372 GHashTableIter iter;
3373 gpointer key, value;
3374 gboolean started_fir = FALSE;
3375 GstRTCPPacket fir_rtcppacket;
3376 GstRTCPPacket packet;
3377 GstRTCPBuffer rtcp = { NULL, };
3380 gst_rtcp_buffer_map (buffer, GST_MAP_READWRITE, &rtcp);
3382 gst_rtcp_buffer_get_first_packet (&rtcp, &packet);
3383 switch (gst_rtcp_packet_get_type (&packet)) {
3384 case GST_RTCP_TYPE_SR:
3385 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc,
3386 NULL, NULL, NULL, NULL);
3388 case GST_RTCP_TYPE_RR:
3389 ssrc = gst_rtcp_packet_rr_get_ssrc (&packet);
3395 RTP_SESSION_LOCK (sess);
3396 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
3397 while (g_hash_table_iter_next (&iter, &key, &value)) {
3398 guint media_ssrc = GPOINTER_TO_UINT (key);
3399 RTPSource *media_src = value;
3402 if (media_src->send_fir) {
3404 if (!gst_rtcp_buffer_add_packet (&rtcp, GST_RTCP_TYPE_PSFB,
3407 gst_rtcp_packet_fb_set_type (&fir_rtcppacket, GST_RTCP_PSFB_TYPE_FIR);
3408 gst_rtcp_packet_fb_set_sender_ssrc (&fir_rtcppacket, ssrc);
3409 gst_rtcp_packet_fb_set_media_ssrc (&fir_rtcppacket, 0);
3411 if (!gst_rtcp_packet_fb_set_fci_length (&fir_rtcppacket, 2)) {
3412 gst_rtcp_packet_remove (&fir_rtcppacket);
3418 if (!gst_rtcp_packet_fb_set_fci_length (&fir_rtcppacket,
3419 !gst_rtcp_packet_fb_get_fci_length (&fir_rtcppacket) + 2))
3423 fci_data = gst_rtcp_packet_fb_get_fci (&fir_rtcppacket) -
3424 ((gst_rtcp_packet_fb_get_fci_length (&fir_rtcppacket) - 2) * 4);
3426 GST_WRITE_UINT32_BE (fci_data, media_ssrc);
3428 fci_data[0] = media_src->current_send_fir_seqnum;
3429 fci_data[1] = fci_data[2] = fci_data[3] = 0;
3430 media_src->send_fir = FALSE;
3434 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
3435 while (g_hash_table_iter_next (&iter, &key, &value)) {
3436 guint media_ssrc = GPOINTER_TO_UINT (key);
3437 RTPSource *media_src = value;
3438 GstRTCPPacket pli_rtcppacket;
3440 if (media_src->send_pli && !rtp_source_has_retained (media_src,
3441 has_pli_compare_func, NULL)) {
3442 if (!gst_rtcp_buffer_add_packet (&rtcp, GST_RTCP_TYPE_PSFB,
3444 /* Break because the packet is full, will put next request in a
3447 gst_rtcp_packet_fb_set_type (&pli_rtcppacket, GST_RTCP_PSFB_TYPE_PLI);
3448 gst_rtcp_packet_fb_set_sender_ssrc (&pli_rtcppacket, ssrc);
3449 gst_rtcp_packet_fb_set_media_ssrc (&pli_rtcppacket, media_ssrc);
3452 media_src->send_pli = FALSE;
3454 RTP_SESSION_UNLOCK (sess);
3457 gst_rtcp_buffer_unmap (&rtcp);
3463 rtp_session_send_rtcp (RTPSession * sess, GstClockTimeDiff max_delay)
3467 if (!sess->callbacks.send_rtcp)
3470 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
3472 rtp_session_request_early_rtcp (sess, now, max_delay);