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 void rtp_session_send_rtcp (RTPSession * sess,
116 GstClockTimeDiff max_delay);
119 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
121 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
123 static guint32 rtp_session_create_new_ssrc (RTPSession * sess);
124 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
125 gboolean * created, RTPArrivalStats * arrival, gboolean rtp);
126 static RTPSource *obtain_internal_source (RTPSession * sess,
127 guint32 ssrc, gboolean * created);
128 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
129 GstClockTime current_time);
130 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
131 gboolean deterministic, gboolean first);
134 accumulate_trues (GSignalInvocationHint * ihint, GValue * return_accu,
135 const GValue * handler_return, gpointer data)
137 if (g_value_get_boolean (handler_return))
138 g_value_set_boolean (return_accu, TRUE);
144 rtp_session_class_init (RTPSessionClass * klass)
146 GObjectClass *gobject_class;
148 gobject_class = (GObjectClass *) klass;
150 gobject_class->finalize = rtp_session_finalize;
151 gobject_class->set_property = rtp_session_set_property;
152 gobject_class->get_property = rtp_session_get_property;
155 * RTPSession::get-source-by-ssrc:
156 * @session: the object which received the signal
157 * @ssrc: the SSRC of the RTPSource
159 * Request the #RTPSource object with SSRC @ssrc in @session.
161 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
162 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
163 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
164 get_source_by_ssrc), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
165 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
168 * RTPSession::on-new-ssrc:
169 * @session: the object which received the signal
170 * @src: the new RTPSource
172 * Notify of a new SSRC that entered @session.
174 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
175 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
176 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
177 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
180 * RTPSession::on-ssrc-collision:
181 * @session: the object which received the signal
182 * @src: the #RTPSource that caused a collision
184 * Notify when we have an SSRC collision
186 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
187 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
188 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
189 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
192 * RTPSession::on-ssrc-validated:
193 * @session: the object which received the signal
194 * @src: the new validated RTPSource
196 * Notify of a new SSRC that became validated.
198 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
199 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
200 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
201 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
204 * RTPSession::on-ssrc-active:
205 * @session: the object which received the signal
206 * @src: the active RTPSource
208 * Notify of a SSRC that is active, i.e., sending RTCP.
210 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
211 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
212 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
213 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
216 * RTPSession::on-ssrc-sdes:
217 * @session: the object which received the signal
218 * @src: the RTPSource
220 * Notify that a new SDES was received for SSRC.
222 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
223 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
224 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
225 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
228 * RTPSession::on-bye-ssrc:
229 * @session: the object which received the signal
230 * @src: the RTPSource that went away
232 * Notify of an SSRC that became inactive because of a BYE packet.
234 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
235 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
236 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
237 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
240 * RTPSession::on-bye-timeout:
241 * @session: the object which received the signal
242 * @src: the RTPSource that timed out
244 * Notify of an SSRC that has timed out because of BYE
246 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
247 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
248 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
249 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
252 * RTPSession::on-timeout:
253 * @session: the object which received the signal
254 * @src: the RTPSource that timed out
256 * Notify of an SSRC that has timed out
258 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
259 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
260 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
261 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
264 * RTPSession::on-sender-timeout:
265 * @session: the object which received the signal
266 * @src: the RTPSource that timed out
268 * Notify of an SSRC that was a sender but timed out and became a receiver.
270 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
271 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
272 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
273 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
277 * RTPSession::on-sending-rtcp
278 * @session: the object which received the signal
279 * @buffer: the #GstBuffer containing the RTCP packet about to be sent
280 * @early: %TRUE if the packet is early, %FALSE if it is regular
282 * This signal is emitted before sending an RTCP packet, it can be used
283 * to add extra RTCP Packets.
285 * Returns: %TRUE if the RTCP buffer should NOT be suppressed, %FALSE
286 * if suppressing it is acceptable
288 rtp_session_signals[SIGNAL_ON_SENDING_RTCP] =
289 g_signal_new ("on-sending-rtcp", G_TYPE_FROM_CLASS (klass),
290 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_rtcp),
291 accumulate_trues, NULL, gst_rtp_bin_marshal_BOOLEAN__BOXED_BOOLEAN,
292 G_TYPE_BOOLEAN, 2, GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE,
296 * RTPSession::on-feedback-rtcp:
297 * @session: the object which received the signal
298 * @type: Type of RTCP packet, will be %GST_RTCP_TYPE_RTPFB or
299 * %GST_RTCP_TYPE_RTPFB
300 * @fbtype: The type of RTCP FB packet, probably part of #GstRTCPFBType
301 * @sender_ssrc: The SSRC of the sender
302 * @media_ssrc: The SSRC of the media this refers to
303 * @fci: a #GstBuffer with the FCI data from the FB packet or %NULL if
306 * Notify that a RTCP feedback packet has been received
308 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP] =
309 g_signal_new ("on-feedback-rtcp", G_TYPE_FROM_CLASS (klass),
310 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_feedback_rtcp),
311 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT_UINT_UINT_BOXED,
312 G_TYPE_NONE, 5, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT,
316 * RTPSession::send-rtcp:
317 * @session: the object which received the signal
318 * @max_delay: The maximum delay after which the feedback will not be useful
321 * Requests that the #RTPSession initiate a new RTCP packet as soon as
322 * possible within the requested delay.
324 rtp_session_signals[SIGNAL_SEND_RTCP] =
325 g_signal_new ("send-rtcp", G_TYPE_FROM_CLASS (klass),
326 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
327 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
328 gst_rtp_bin_marshal_VOID__UINT64, G_TYPE_NONE, 1, G_TYPE_UINT64);
330 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
331 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
332 "The internal SSRC used for the session (deprecated)",
333 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
335 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
336 g_param_spec_object ("internal-source", "Internal Source",
337 "The internal source element of the session (deprecated)",
338 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
340 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
341 g_param_spec_double ("bandwidth", "Bandwidth",
342 "The bandwidth of the session (0 for auto-discover)",
343 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
344 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
346 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
347 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
348 "The fraction of the bandwidth used for RTCP (or as a real fraction of the RTP bandwidth if < 1)",
349 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
350 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
352 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
353 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
354 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
355 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
356 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
358 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
359 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
360 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
361 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
362 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
364 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
365 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
366 "The maximum size of the RTCP packets",
367 16, G_MAXINT16, DEFAULT_RTCP_MTU,
368 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
370 g_object_class_install_property (gobject_class, PROP_SDES,
371 g_param_spec_boxed ("sdes", "SDES",
372 "The SDES items of this session",
373 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
375 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
376 g_param_spec_uint ("num-sources", "Num Sources",
377 "The number of sources in the session", 0, G_MAXUINT,
378 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
380 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
381 g_param_spec_uint ("num-active-sources", "Num Active Sources",
382 "The number of active sources in the session", 0, G_MAXUINT,
383 DEFAULT_NUM_ACTIVE_SOURCES,
384 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
388 * Get a GValue Array of all sources in the session.
391 * <title>Getting the #RTPSources of a session
398 * g_object_get (sess, "sources", &arr, NULL);
400 * for (i = 0; i < arr->n_values; i++) {
403 * val = g_value_array_get_nth (arr, i);
404 * source = g_value_get_object (val);
406 * g_value_array_free (arr);
411 g_object_class_install_property (gobject_class, PROP_SOURCES,
412 g_param_spec_boxed ("sources", "Sources",
413 "An array of all known sources in the session",
414 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
416 g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
417 g_param_spec_boolean ("favor-new", "Favor new sources",
418 "Resolve SSRC conflict in favor of new sources", FALSE,
419 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
421 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
422 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
423 "Minimum interval between Regular RTCP packet (in ns)",
424 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
425 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
427 g_object_class_install_property (gobject_class,
428 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
429 g_param_spec_uint64 ("rtcp-feedback-retention-window",
430 "RTCP Feedback retention window",
431 "Duration during which RTCP Feedback packets are retained (in ns)",
432 0, G_MAXUINT64, DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW,
433 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
435 g_object_class_install_property (gobject_class,
436 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
437 g_param_spec_uint ("rtcp-immediate-feedback-threshold",
438 "RTCP Immediate Feedback threshold",
439 "The maximum number of members of a RTP session for which immediate"
441 0, G_MAXUINT, DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
442 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
444 g_object_class_install_property (gobject_class, PROP_PROBATION,
445 g_param_spec_uint ("probation", "Number of probations",
446 "Consecutive packet sequence numbers to accept the source",
447 0, G_MAXUINT, DEFAULT_PROBATION,
448 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
450 klass->get_source_by_ssrc =
451 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
452 klass->send_rtcp = GST_DEBUG_FUNCPTR (rtp_session_send_rtcp);
454 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
458 rtp_session_init (RTPSession * sess)
463 g_mutex_init (&sess->lock);
464 sess->key = g_random_int ();
468 for (i = 0; i < 32; i++) {
470 g_hash_table_new_full (NULL, NULL, NULL,
471 (GDestroyNotify) g_object_unref);
474 rtp_stats_init_defaults (&sess->stats);
475 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
476 rtp_stats_set_min_interval (&sess->stats,
477 (gdouble) DEFAULT_RTCP_MIN_INTERVAL / GST_SECOND);
479 sess->recalc_bandwidth = TRUE;
480 sess->bandwidth = DEFAULT_BANDWIDTH;
481 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
482 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
483 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
485 /* default UDP header length */
486 sess->header_len = 28;
487 sess->mtu = DEFAULT_RTCP_MTU;
489 sess->probation = DEFAULT_PROBATION;
491 /* some default SDES entries */
492 sess->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
494 /* we do not want to leak details like the username or hostname here */
495 str = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
496 gst_structure_set (sess->sdes, "cname", G_TYPE_STRING, str, NULL);
500 /* we do not want to leak the user's real name here */
501 str = g_strdup_printf ("Anon%u", g_random_int ());
502 gst_structure_set (sdes, "name", G_TYPE_STRING, str, NULL);
506 gst_structure_set (sess->sdes, "tool", G_TYPE_STRING, "GStreamer", NULL);
508 /* this is the SSRC we suggest */
509 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
511 sess->first_rtcp = TRUE;
512 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
514 sess->allow_early = TRUE;
515 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
516 sess->rtcp_feedback_retention_window = DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW;
517 sess->rtcp_immediate_feedback_threshold =
518 DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD;
520 sess->last_keyframe_request = GST_CLOCK_TIME_NONE;
524 rtp_session_finalize (GObject * object)
529 sess = RTP_SESSION_CAST (object);
531 gst_structure_free (sess->sdes);
533 for (i = 0; i < 32; i++)
534 g_hash_table_destroy (sess->ssrcs[i]);
536 g_mutex_clear (&sess->lock);
538 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
542 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
544 GValue value = { 0 };
546 g_value_init (&value, RTP_TYPE_SOURCE);
547 g_value_take_object (&value, source);
548 /* copies the value */
549 g_value_array_append (arr, &value);
553 rtp_session_create_sources (RTPSession * sess)
558 RTP_SESSION_LOCK (sess);
559 /* get number of elements in the table */
560 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
561 /* create the result value array */
562 res = g_value_array_new (size);
564 /* and copy all values into the array */
565 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
566 RTP_SESSION_UNLOCK (sess);
572 rtp_session_set_property (GObject * object, guint prop_id,
573 const GValue * value, GParamSpec * pspec)
577 sess = RTP_SESSION (object);
580 case PROP_INTERNAL_SSRC:
583 RTP_SESSION_LOCK (sess);
584 sess->bandwidth = g_value_get_double (value);
585 sess->recalc_bandwidth = TRUE;
586 RTP_SESSION_UNLOCK (sess);
588 case PROP_RTCP_FRACTION:
589 RTP_SESSION_LOCK (sess);
590 sess->rtcp_bandwidth = g_value_get_double (value);
591 sess->recalc_bandwidth = TRUE;
592 RTP_SESSION_UNLOCK (sess);
594 case PROP_RTCP_RR_BANDWIDTH:
595 RTP_SESSION_LOCK (sess);
596 sess->rtcp_rr_bandwidth = g_value_get_int (value);
597 sess->recalc_bandwidth = TRUE;
598 RTP_SESSION_UNLOCK (sess);
600 case PROP_RTCP_RS_BANDWIDTH:
601 RTP_SESSION_LOCK (sess);
602 sess->rtcp_rs_bandwidth = g_value_get_int (value);
603 sess->recalc_bandwidth = TRUE;
604 RTP_SESSION_UNLOCK (sess);
607 sess->mtu = g_value_get_uint (value);
610 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
613 sess->favor_new = g_value_get_boolean (value);
615 case PROP_RTCP_MIN_INTERVAL:
616 rtp_stats_set_min_interval (&sess->stats,
617 (gdouble) g_value_get_uint64 (value) / GST_SECOND);
618 /* trigger reconsideration */
619 RTP_SESSION_LOCK (sess);
620 sess->next_rtcp_check_time = 0;
621 RTP_SESSION_UNLOCK (sess);
622 if (sess->callbacks.reconsider)
623 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
625 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
626 sess->rtcp_immediate_feedback_threshold = g_value_get_uint (value);
629 sess->probation = g_value_get_uint (value);
632 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
638 rtp_session_get_property (GObject * object, guint prop_id,
639 GValue * value, GParamSpec * pspec)
643 sess = RTP_SESSION (object);
646 case PROP_INTERNAL_SSRC:
647 g_value_set_uint (value, rtp_session_suggest_ssrc (sess));
649 case PROP_INTERNAL_SOURCE:
650 /* FIXME, return a random source */
651 g_value_set_object (value, NULL);
654 g_value_set_double (value, sess->bandwidth);
656 case PROP_RTCP_FRACTION:
657 g_value_set_double (value, sess->rtcp_bandwidth);
659 case PROP_RTCP_RR_BANDWIDTH:
660 g_value_set_int (value, sess->rtcp_rr_bandwidth);
662 case PROP_RTCP_RS_BANDWIDTH:
663 g_value_set_int (value, sess->rtcp_rs_bandwidth);
666 g_value_set_uint (value, sess->mtu);
669 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
671 case PROP_NUM_SOURCES:
672 g_value_set_uint (value, rtp_session_get_num_sources (sess));
674 case PROP_NUM_ACTIVE_SOURCES:
675 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
678 g_value_take_boxed (value, rtp_session_create_sources (sess));
681 g_value_set_boolean (value, sess->favor_new);
683 case PROP_RTCP_MIN_INTERVAL:
684 g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
686 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
687 g_value_set_uint (value, sess->rtcp_immediate_feedback_threshold);
690 g_value_set_uint (value, sess->probation);
693 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
699 on_new_ssrc (RTPSession * sess, RTPSource * source)
701 g_object_ref (source);
702 RTP_SESSION_UNLOCK (sess);
703 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
704 RTP_SESSION_LOCK (sess);
705 g_object_unref (source);
709 on_ssrc_collision (RTPSession * sess, RTPSource * source)
711 g_object_ref (source);
712 RTP_SESSION_UNLOCK (sess);
713 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
715 RTP_SESSION_LOCK (sess);
716 g_object_unref (source);
720 on_ssrc_validated (RTPSession * sess, RTPSource * source)
722 g_object_ref (source);
723 RTP_SESSION_UNLOCK (sess);
724 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
726 RTP_SESSION_LOCK (sess);
727 g_object_unref (source);
731 on_ssrc_active (RTPSession * sess, RTPSource * source)
733 g_object_ref (source);
734 RTP_SESSION_UNLOCK (sess);
735 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
736 RTP_SESSION_LOCK (sess);
737 g_object_unref (source);
741 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
743 g_object_ref (source);
744 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
745 RTP_SESSION_UNLOCK (sess);
746 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
747 RTP_SESSION_LOCK (sess);
748 g_object_unref (source);
752 on_bye_ssrc (RTPSession * sess, RTPSource * source)
754 g_object_ref (source);
755 RTP_SESSION_UNLOCK (sess);
756 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
757 RTP_SESSION_LOCK (sess);
758 g_object_unref (source);
762 on_bye_timeout (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_TIMEOUT], 0, source);
767 RTP_SESSION_LOCK (sess);
768 g_object_unref (source);
772 on_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_TIMEOUT], 0, source);
777 RTP_SESSION_LOCK (sess);
778 g_object_unref (source);
782 on_sender_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_SENDER_TIMEOUT], 0,
788 RTP_SESSION_LOCK (sess);
789 g_object_unref (source);
795 * Create a new session object.
797 * Returns: a new #RTPSession. g_object_unref() after usage.
800 rtp_session_new (void)
804 sess = g_object_new (RTP_TYPE_SESSION, NULL);
810 * rtp_session_set_callbacks:
811 * @sess: an #RTPSession
812 * @callbacks: callbacks to configure
813 * @user_data: user data passed in the callbacks
815 * Configure a set of callbacks to be notified of actions.
818 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
821 g_return_if_fail (RTP_IS_SESSION (sess));
823 if (callbacks->process_rtp) {
824 sess->callbacks.process_rtp = callbacks->process_rtp;
825 sess->process_rtp_user_data = user_data;
827 if (callbacks->send_rtp) {
828 sess->callbacks.send_rtp = callbacks->send_rtp;
829 sess->send_rtp_user_data = user_data;
831 if (callbacks->send_rtcp) {
832 sess->callbacks.send_rtcp = callbacks->send_rtcp;
833 sess->send_rtcp_user_data = user_data;
835 if (callbacks->sync_rtcp) {
836 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
837 sess->sync_rtcp_user_data = user_data;
839 if (callbacks->clock_rate) {
840 sess->callbacks.clock_rate = callbacks->clock_rate;
841 sess->clock_rate_user_data = user_data;
843 if (callbacks->reconsider) {
844 sess->callbacks.reconsider = callbacks->reconsider;
845 sess->reconsider_user_data = user_data;
847 if (callbacks->request_key_unit) {
848 sess->callbacks.request_key_unit = callbacks->request_key_unit;
849 sess->request_key_unit_user_data = user_data;
851 if (callbacks->request_time) {
852 sess->callbacks.request_time = callbacks->request_time;
853 sess->request_time_user_data = user_data;
858 * rtp_session_set_process_rtp_callback:
859 * @sess: an #RTPSession
860 * @callback: callback to set
861 * @user_data: user data passed in the callback
863 * Configure only the process_rtp callback to be notified of the process_rtp action.
866 rtp_session_set_process_rtp_callback (RTPSession * sess,
867 RTPSessionProcessRTP callback, gpointer user_data)
869 g_return_if_fail (RTP_IS_SESSION (sess));
871 sess->callbacks.process_rtp = callback;
872 sess->process_rtp_user_data = user_data;
876 * rtp_session_set_send_rtp_callback:
877 * @sess: an #RTPSession
878 * @callback: callback to set
879 * @user_data: user data passed in the callback
881 * Configure only the send_rtp callback to be notified of the send_rtp action.
884 rtp_session_set_send_rtp_callback (RTPSession * sess,
885 RTPSessionSendRTP callback, gpointer user_data)
887 g_return_if_fail (RTP_IS_SESSION (sess));
889 sess->callbacks.send_rtp = callback;
890 sess->send_rtp_user_data = user_data;
894 * rtp_session_set_send_rtcp_callback:
895 * @sess: an #RTPSession
896 * @callback: callback to set
897 * @user_data: user data passed in the callback
899 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
902 rtp_session_set_send_rtcp_callback (RTPSession * sess,
903 RTPSessionSendRTCP callback, gpointer user_data)
905 g_return_if_fail (RTP_IS_SESSION (sess));
907 sess->callbacks.send_rtcp = callback;
908 sess->send_rtcp_user_data = user_data;
912 * rtp_session_set_sync_rtcp_callback:
913 * @sess: an #RTPSession
914 * @callback: callback to set
915 * @user_data: user data passed in the callback
917 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
920 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
921 RTPSessionSyncRTCP callback, gpointer user_data)
923 g_return_if_fail (RTP_IS_SESSION (sess));
925 sess->callbacks.sync_rtcp = callback;
926 sess->sync_rtcp_user_data = user_data;
930 * rtp_session_set_clock_rate_callback:
931 * @sess: an #RTPSession
932 * @callback: callback to set
933 * @user_data: user data passed in the callback
935 * Configure only the clock_rate callback to be notified of the clock_rate action.
938 rtp_session_set_clock_rate_callback (RTPSession * sess,
939 RTPSessionClockRate callback, gpointer user_data)
941 g_return_if_fail (RTP_IS_SESSION (sess));
943 sess->callbacks.clock_rate = callback;
944 sess->clock_rate_user_data = user_data;
948 * rtp_session_set_reconsider_callback:
949 * @sess: an #RTPSession
950 * @callback: callback to set
951 * @user_data: user data passed in the callback
953 * Configure only the reconsider callback to be notified of the reconsider action.
956 rtp_session_set_reconsider_callback (RTPSession * sess,
957 RTPSessionReconsider callback, gpointer user_data)
959 g_return_if_fail (RTP_IS_SESSION (sess));
961 sess->callbacks.reconsider = callback;
962 sess->reconsider_user_data = user_data;
966 * rtp_session_set_request_time_callback:
967 * @sess: an #RTPSession
968 * @callback: callback to set
969 * @user_data: user data passed in the callback
971 * Configure only the request_time callback
974 rtp_session_set_request_time_callback (RTPSession * sess,
975 RTPSessionRequestTime callback, gpointer user_data)
977 g_return_if_fail (RTP_IS_SESSION (sess));
979 sess->callbacks.request_time = callback;
980 sess->request_time_user_data = user_data;
984 * rtp_session_set_bandwidth:
985 * @sess: an #RTPSession
986 * @bandwidth: the bandwidth allocated
988 * Set the session bandwidth in bytes per second.
991 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
993 g_return_if_fail (RTP_IS_SESSION (sess));
995 RTP_SESSION_LOCK (sess);
996 sess->stats.bandwidth = bandwidth;
997 RTP_SESSION_UNLOCK (sess);
1001 * rtp_session_get_bandwidth:
1002 * @sess: an #RTPSession
1004 * Get the session bandwidth.
1006 * Returns: the session bandwidth.
1009 rtp_session_get_bandwidth (RTPSession * sess)
1013 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1015 RTP_SESSION_LOCK (sess);
1016 result = sess->stats.bandwidth;
1017 RTP_SESSION_UNLOCK (sess);
1023 * rtp_session_set_rtcp_fraction:
1024 * @sess: an #RTPSession
1025 * @bandwidth: the RTCP bandwidth
1027 * Set the bandwidth in bytes per second that should be used for RTCP
1031 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
1033 g_return_if_fail (RTP_IS_SESSION (sess));
1035 RTP_SESSION_LOCK (sess);
1036 sess->stats.rtcp_bandwidth = bandwidth;
1037 RTP_SESSION_UNLOCK (sess);
1041 * rtp_session_get_rtcp_fraction:
1042 * @sess: an #RTPSession
1044 * Get the session bandwidth used for RTCP.
1046 * Returns: The bandwidth used for RTCP messages.
1049 rtp_session_get_rtcp_fraction (RTPSession * sess)
1053 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
1055 RTP_SESSION_LOCK (sess);
1056 result = sess->stats.rtcp_bandwidth;
1057 RTP_SESSION_UNLOCK (sess);
1063 * rtp_session_get_sdes_struct:
1064 * @sess: an #RTSPSession
1066 * Get the SDES data as a #GstStructure
1068 * Returns: a GstStructure with SDES items for @sess. This function returns a
1069 * copy of the SDES structure, use gst_structure_free() after usage.
1072 rtp_session_get_sdes_struct (RTPSession * sess)
1074 GstStructure *result = NULL;
1076 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1078 RTP_SESSION_LOCK (sess);
1080 result = gst_structure_copy (sess->sdes);
1081 RTP_SESSION_UNLOCK (sess);
1087 * rtp_session_set_sdes_struct:
1088 * @sess: an #RTSPSession
1089 * @sdes: a #GstStructure
1091 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1094 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
1096 g_return_if_fail (sdes);
1097 g_return_if_fail (RTP_IS_SESSION (sess));
1099 RTP_SESSION_LOCK (sess);
1101 gst_structure_free (sess->sdes);
1102 sess->sdes = gst_structure_copy (sdes);
1103 RTP_SESSION_UNLOCK (sess);
1106 static GstFlowReturn
1107 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1109 GstFlowReturn result = GST_FLOW_OK;
1111 if (source->internal) {
1112 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1114 RTP_SESSION_UNLOCK (session);
1116 if (session->callbacks.send_rtp)
1118 session->callbacks.send_rtp (session, source, data,
1119 session->send_rtp_user_data);
1121 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1124 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1125 RTP_SESSION_UNLOCK (session);
1127 if (session->callbacks.process_rtp)
1129 session->callbacks.process_rtp (session, source,
1130 GST_BUFFER_CAST (data), session->process_rtp_user_data);
1132 gst_buffer_unref (GST_BUFFER_CAST (data));
1134 RTP_SESSION_LOCK (session);
1140 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1144 RTP_SESSION_UNLOCK (session);
1146 if (session->callbacks.clock_rate)
1148 session->callbacks.clock_rate (session, pt,
1149 session->clock_rate_user_data);
1153 RTP_SESSION_LOCK (session);
1155 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1160 static RTPSourceCallbacks callbacks = {
1161 (RTPSourcePushRTP) source_push_rtp,
1162 (RTPSourceClockRate) source_clock_rate,
1166 check_collision (RTPSession * sess, RTPSource * source,
1167 RTPArrivalStats * arrival, gboolean rtp)
1171 /* If we have no arrival address, we can't do collision checking */
1172 if (!arrival->address)
1175 ssrc = rtp_source_get_ssrc (source);
1177 if (!source->internal) {
1178 GSocketAddress *from;
1180 /* This is not our local source, but lets check if two remote
1183 from = source->rtp_from;
1185 from = source->rtcp_from;
1189 if (__g_socket_address_equal (from, arrival->address)) {
1190 /* Address is the same */
1193 GST_LOG ("we have a third-party collision or loop ssrc:%x", ssrc);
1194 if (sess->favor_new) {
1195 if (rtp_source_find_conflicting_address (source,
1196 arrival->address, arrival->current_time)) {
1199 buf1 = __g_socket_address_to_string (arrival->address);
1200 GST_LOG ("Known conflict on %x for %s, dropping packet", ssrc,
1208 /* Current address is not a known conflict, lets assume this is
1209 * a new source. Save old address in possible conflict list
1211 rtp_source_add_conflicting_address (source, from,
1212 arrival->current_time);
1214 buf1 = __g_socket_address_to_string (from);
1215 buf2 = __g_socket_address_to_string (arrival->address);
1217 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1218 " saving old as known conflict", ssrc, buf1, buf2);
1221 rtp_source_set_rtp_from (source, arrival->address);
1223 rtp_source_set_rtcp_from (source, arrival->address);
1231 /* Don't need to save old addresses, we ignore new sources */
1236 /* We don't already have a from address for RTP, just set it */
1238 rtp_source_set_rtp_from (source, arrival->address);
1240 rtp_source_set_rtcp_from (source, arrival->address);
1244 /* FIXME: Log 3rd party collision somehow
1245 * Maybe should be done in upper layer, only the SDES can tell us
1246 * if its a collision or a loop
1249 /* This is sending with our ssrc, is it an address we already know */
1250 if (rtp_source_find_conflicting_address (source, arrival->address,
1251 arrival->current_time)) {
1252 /* Its a known conflict, its probably a loop, not a collision
1253 * lets just drop the incoming packet
1255 GST_DEBUG ("Our packets are being looped back to us, dropping");
1257 /* Its a new collision, lets change our SSRC */
1258 rtp_source_add_conflicting_address (source, arrival->address,
1259 arrival->current_time);
1261 GST_DEBUG ("Collision for SSRC %x", ssrc);
1262 /* mark the source BYE */
1263 rtp_source_mark_bye (source, "SSRC Collision");
1264 /* if we were suggesting this SSRC, change to something else */
1265 if (sess->suggested_ssrc == ssrc)
1266 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
1268 on_ssrc_collision (sess, source);
1270 rtp_session_schedule_bye_locked (sess, arrival->current_time);
1278 find_source (RTPSession * sess, guint32 ssrc)
1280 return g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1281 GINT_TO_POINTER (ssrc));
1285 add_source (RTPSession * sess, RTPSource * src)
1287 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1288 GINT_TO_POINTER (src->ssrc), src);
1289 /* report the new source ASAP */
1290 src->generation = sess->generation;
1291 /* we have one more source now */
1292 sess->total_sources++;
1293 if (RTP_SOURCE_IS_ACTIVE (src))
1294 sess->stats.active_sources++;
1295 if (src->internal) {
1296 sess->stats.internal_sources++;
1297 if (sess->suggested_ssrc != src->ssrc)
1298 sess->suggested_ssrc = src->ssrc;
1302 /* must be called with the session lock, the returned source needs to be
1303 * unreffed after usage. */
1305 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1306 RTPArrivalStats * arrival, gboolean rtp)
1310 source = find_source (sess, ssrc);
1311 if (source == NULL) {
1312 /* make new Source in probation and insert */
1313 source = rtp_source_new (ssrc);
1315 GST_DEBUG ("creating new source %08x %p", ssrc, source);
1317 /* for RTP packets we need to set the source in probation. Receiving RTCP
1318 * packets of an SSRC, on the other hand, is a strong indication that we
1319 * are dealing with a valid source. */
1321 g_object_set (source, "probation", sess->probation, NULL);
1323 g_object_set (source, "probation", 0, NULL);
1325 /* store from address, if any */
1326 if (arrival->address) {
1328 rtp_source_set_rtp_from (source, arrival->address);
1330 rtp_source_set_rtcp_from (source, arrival->address);
1333 /* configure a callback on the source */
1334 rtp_source_set_callbacks (source, &callbacks, sess);
1336 add_source (sess, source);
1340 /* check for collision, this updates the address when not previously set */
1341 if (check_collision (sess, source, arrival, rtp)) {
1344 /* Receiving RTCP packets of an SSRC is a strong indication that we
1345 * are dealing with a valid source. */
1347 g_object_set (source, "probation", 0, NULL);
1349 /* update last activity */
1350 source->last_activity = arrival->current_time;
1352 source->last_rtp_activity = arrival->current_time;
1353 g_object_ref (source);
1358 /* must be called with the session lock, the returned source needs to be
1359 * unreffed after usage. */
1361 obtain_internal_source (RTPSession * sess, guint32 ssrc, gboolean * created)
1365 source = find_source (sess, ssrc);
1366 if (source == NULL) {
1367 /* make new internal Source and insert */
1368 source = rtp_source_new (ssrc);
1370 GST_DEBUG ("creating new internal source %08x %p", ssrc, source);
1372 source->validated = TRUE;
1373 source->internal = TRUE;
1374 rtp_source_set_sdes_struct (source, gst_structure_copy (sess->sdes));
1375 rtp_source_set_callbacks (source, &callbacks, sess);
1377 add_source (sess, source);
1382 g_object_ref (source);
1388 * rtp_session_suggest_ssrc:
1389 * @sess: a #RTPSession
1391 * Suggest an unused SSRC in @sess.
1393 * Returns: a free unused SSRC
1396 rtp_session_suggest_ssrc (RTPSession * sess)
1400 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1402 RTP_SESSION_LOCK (sess);
1403 result = sess->suggested_ssrc;
1404 RTP_SESSION_UNLOCK (sess);
1410 * rtp_session_add_source:
1411 * @sess: a #RTPSession
1412 * @src: #RTPSource to add
1414 * Add @src to @session.
1416 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1417 * existed in the session.
1420 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1422 gboolean result = FALSE;
1425 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1426 g_return_val_if_fail (src != NULL, FALSE);
1428 RTP_SESSION_LOCK (sess);
1429 find = find_source (sess, src->ssrc);
1431 add_source (sess, src);
1434 RTP_SESSION_UNLOCK (sess);
1440 * rtp_session_get_num_sources:
1441 * @sess: an #RTPSession
1443 * Get the number of sources in @sess.
1445 * Returns: The number of sources in @sess.
1448 rtp_session_get_num_sources (RTPSession * sess)
1452 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1454 RTP_SESSION_LOCK (sess);
1455 result = sess->total_sources;
1456 RTP_SESSION_UNLOCK (sess);
1462 * rtp_session_get_num_active_sources:
1463 * @sess: an #RTPSession
1465 * Get the number of active sources in @sess. A source is considered active when
1466 * it has been validated and has not yet received a BYE RTCP message.
1468 * Returns: The number of active sources in @sess.
1471 rtp_session_get_num_active_sources (RTPSession * sess)
1475 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1477 RTP_SESSION_LOCK (sess);
1478 result = sess->stats.active_sources;
1479 RTP_SESSION_UNLOCK (sess);
1485 * rtp_session_get_source_by_ssrc:
1486 * @sess: an #RTPSession
1489 * Find the source with @ssrc in @sess.
1491 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1492 * g_object_unref() after usage.
1495 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1499 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1501 RTP_SESSION_LOCK (sess);
1502 result = find_source (sess, ssrc);
1504 g_object_ref (result);
1505 RTP_SESSION_UNLOCK (sess);
1510 /* should be called with the SESSION lock */
1512 rtp_session_create_new_ssrc (RTPSession * sess)
1517 ssrc = g_random_int ();
1519 /* see if it exists in the session, we're done if it doesn't */
1520 if (find_source (sess, ssrc) == NULL)
1528 * rtp_session_create_source:
1529 * @sess: an #RTPSession
1531 * Create an #RTPSource for use in @sess. This function will create a source
1532 * with an ssrc that is currently not used by any participants in the session.
1534 * Returns: an #RTPSource.
1537 rtp_session_create_source (RTPSession * sess)
1542 RTP_SESSION_LOCK (sess);
1543 ssrc = rtp_session_create_new_ssrc (sess);
1544 source = rtp_source_new (ssrc);
1545 rtp_source_set_callbacks (source, &callbacks, sess);
1546 /* we need an additional ref for the source in the hashtable */
1547 g_object_ref (source);
1548 add_source (sess, source);
1549 RTP_SESSION_UNLOCK (sess);
1554 /* update the RTPArrivalStats structure with the current time and other bits
1555 * about the current buffer we are handling.
1556 * This function is typically called when a validated packet is received.
1557 * This function should be called with the SESSION_LOCK
1560 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
1561 gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
1562 GstClockTime running_time, guint64 ntpnstime)
1564 GstNetAddressMeta *meta;
1565 GstRTPBuffer rtpb = { NULL };
1567 /* get time of arrival */
1568 arrival->current_time = current_time;
1569 arrival->running_time = running_time;
1570 arrival->ntpnstime = ntpnstime;
1572 /* get packet size including header overhead */
1573 arrival->bytes = gst_buffer_get_size (buffer) + sess->header_len;
1576 gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtpb);
1577 arrival->payload_len = gst_rtp_buffer_get_payload_len (&rtpb);
1578 gst_rtp_buffer_unmap (&rtpb);
1580 arrival->payload_len = 0;
1583 /* for netbuffer we can store the IP address to check for collisions */
1584 meta = gst_buffer_get_net_address_meta (buffer);
1585 if (arrival->address)
1586 g_object_unref (arrival->address);
1588 arrival->address = G_SOCKET_ADDRESS (g_object_ref (meta->addr));
1590 arrival->address = NULL;
1595 clean_arrival_stats (RTPArrivalStats * arrival)
1597 if (arrival->address)
1598 g_object_unref (arrival->address);
1602 source_update_active (RTPSession * sess, RTPSource * source,
1603 gboolean prevactive)
1605 gboolean active = RTP_SOURCE_IS_ACTIVE (source);
1606 guint32 ssrc = source->ssrc;
1608 if (prevactive == active)
1612 sess->stats.active_sources++;
1613 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1614 sess->stats.active_sources);
1616 sess->stats.active_sources--;
1617 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
1618 sess->stats.active_sources);
1624 source_update_sender (RTPSession * sess, RTPSource * source,
1625 gboolean prevsender)
1627 gboolean sender = RTP_SOURCE_IS_SENDER (source);
1628 guint32 ssrc = source->ssrc;
1630 if (prevsender == sender)
1634 sess->stats.sender_sources++;
1635 if (source->internal)
1636 sess->stats.internal_sender_sources++;
1637 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1638 sess->stats.sender_sources);
1640 sess->stats.sender_sources--;
1641 if (source->internal)
1642 sess->stats.internal_sender_sources--;
1643 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
1644 sess->stats.sender_sources);
1650 * rtp_session_process_rtp:
1651 * @sess: and #RTPSession
1652 * @buffer: an RTP buffer
1653 * @current_time: the current system time
1654 * @running_time: the running_time of @buffer
1656 * Process an RTP buffer in the session manager. This function takes ownership
1659 * Returns: a #GstFlowReturn.
1662 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1663 GstClockTime current_time, GstClockTime running_time)
1665 GstFlowReturn result;
1669 gboolean prevsender, prevactive;
1670 RTPArrivalStats arrival = { NULL, };
1674 GstRTPBuffer rtp = { NULL };
1676 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1677 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1679 if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp))
1680 goto invalid_packet;
1682 /* get SSRC to look up in session database */
1683 ssrc = gst_rtp_buffer_get_ssrc (&rtp);
1684 /* copy available csrc for later */
1685 count = gst_rtp_buffer_get_csrc_count (&rtp);
1686 /* make sure to not overflow our array. An RTP buffer can maximally contain
1688 count = MIN (count, 16);
1690 for (i = 0; i < count; i++)
1691 csrcs[i] = gst_rtp_buffer_get_csrc (&rtp, i);
1693 gst_rtp_buffer_unmap (&rtp);
1695 RTP_SESSION_LOCK (sess);
1697 /* update arrival stats */
1698 update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1701 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1705 prevsender = RTP_SOURCE_IS_SENDER (source);
1706 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1707 oldrate = source->bitrate;
1709 /* let source process the packet */
1710 result = rtp_source_process_rtp (source, buffer, &arrival);
1712 /* source became active */
1713 if (source_update_active (sess, source, prevactive))
1714 on_ssrc_validated (sess, source);
1716 source_update_sender (sess, source, prevsender);
1718 if (oldrate != source->bitrate)
1719 sess->recalc_bandwidth = TRUE;
1722 on_new_ssrc (sess, source);
1724 if (source->validated) {
1727 /* for validated sources, we add the CSRCs as well */
1728 for (i = 0; i < count; i++) {
1730 RTPSource *csrc_src;
1735 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1740 GST_DEBUG ("created new CSRC: %08x", csrc);
1741 rtp_source_set_as_csrc (csrc_src);
1742 source_update_active (sess, csrc_src, FALSE);
1743 on_new_ssrc (sess, csrc_src);
1745 g_object_unref (csrc_src);
1748 g_object_unref (source);
1750 RTP_SESSION_UNLOCK (sess);
1752 clean_arrival_stats (&arrival);
1759 gst_buffer_unref (buffer);
1760 GST_DEBUG ("invalid RTP packet received");
1765 RTP_SESSION_UNLOCK (sess);
1766 gst_buffer_unref (buffer);
1767 clean_arrival_stats (&arrival);
1768 GST_DEBUG ("ignoring packet because its collisioning");
1774 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1775 GstRTCPPacket * packet, RTPArrivalStats * arrival)
1779 count = gst_rtcp_packet_get_rb_count (packet);
1780 for (i = 0; i < count; i++) {
1781 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1782 guint8 fractionlost;
1786 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1787 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1789 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1791 /* find our own source */
1792 src = find_source (sess, ssrc);
1796 if (src->internal && RTP_SOURCE_IS_ACTIVE (src)) {
1797 /* only deal with report blocks for our session, we update the stats of
1798 * the sender of the RTCP message. We could also compare our stats against
1799 * the other sender to see if we are better or worse. */
1800 /* FIXME, need to keep track who the RB block is from */
1801 rtp_source_process_rb (source, arrival->ntpnstime, fractionlost,
1802 packetslost, exthighestseq, jitter, lsr, dlsr);
1805 on_ssrc_active (sess, source);
1808 /* A Sender report contains statistics about how the sender is doing. This
1809 * includes timing informataion such as the relation between RTP and NTP
1810 * timestamps and the number of packets/bytes it sent to us.
1812 * In this report is also included a set of report blocks related to how this
1813 * sender is receiving data (in case we (or somebody else) is also sending stuff
1814 * to it). This info includes the packet loss, jitter and seqnum. It also
1815 * contains information to calculate the round trip time (LSR/DLSR).
1818 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1819 RTPArrivalStats * arrival, gboolean * do_sync)
1821 guint32 senderssrc, rtptime, packet_count, octet_count;
1824 gboolean created, prevsender;
1826 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1827 &packet_count, &octet_count);
1829 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1830 senderssrc, GST_TIME_ARGS (arrival->current_time));
1832 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1836 /* don't try to do lip-sync for sources that sent a BYE */
1837 if (RTP_SOURCE_IS_MARKED_BYE (source))
1842 prevsender = RTP_SOURCE_IS_SENDER (source);
1844 /* first update the source */
1845 rtp_source_process_sr (source, arrival->current_time, ntptime, rtptime,
1846 packet_count, octet_count);
1848 source_update_sender (sess, source, prevsender);
1851 on_new_ssrc (sess, source);
1853 rtp_session_process_rb (sess, source, packet, arrival);
1854 g_object_unref (source);
1857 /* A receiver report contains statistics about how a receiver is doing. It
1858 * includes stuff like packet loss, jitter and the seqnum it received last. It
1859 * also contains info to calculate the round trip time.
1861 * We are only interested in how the sender of this report is doing wrt to us.
1864 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1865 RTPArrivalStats * arrival)
1871 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1873 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1875 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1880 on_new_ssrc (sess, source);
1882 rtp_session_process_rb (sess, source, packet, arrival);
1883 g_object_unref (source);
1886 /* Get SDES items and store them in the SSRC */
1888 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1889 RTPArrivalStats * arrival)
1892 gboolean more_items, more_entries;
1894 items = gst_rtcp_packet_sdes_get_item_count (packet);
1895 GST_DEBUG ("got SDES packet with %d items", items);
1897 more_items = gst_rtcp_packet_sdes_first_item (packet);
1899 while (more_items) {
1901 gboolean changed, created, prevactive;
1905 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1907 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1911 /* find src, no probation when dealing with RTCP */
1912 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1916 sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
1918 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1920 while (more_entries) {
1921 GstRTCPSDESType type;
1927 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1929 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1932 if (type == GST_RTCP_SDES_PRIV) {
1933 name = g_strndup ((const gchar *) &data[1], data[0]);
1935 data += data[0] + 1;
1937 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
1940 value = g_strndup ((const gchar *) data, len);
1942 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
1947 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1951 /* takes ownership of sdes */
1952 changed = rtp_source_set_sdes_struct (source, sdes);
1954 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1955 source->validated = TRUE;
1958 on_new_ssrc (sess, source);
1960 /* source became active */
1961 if (source_update_active (sess, source, prevactive))
1962 on_ssrc_validated (sess, source);
1965 on_ssrc_sdes (sess, source);
1967 g_object_unref (source);
1969 more_items = gst_rtcp_packet_sdes_next_item (packet);
1974 /* BYE is sent when a client leaves the session
1977 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
1978 RTPArrivalStats * arrival)
1982 gboolean reconsider = FALSE;
1984 reason = gst_rtcp_packet_bye_get_reason (packet);
1985 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
1987 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
1988 for (i = 0; i < count; i++) {
1991 gboolean created, prevactive, prevsender;
1992 guint pmembers, members;
1994 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
1995 GST_DEBUG ("SSRC: %08x", ssrc);
1997 /* find src and mark bye, no probation when dealing with RTCP */
1998 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
2002 if (source->internal) {
2003 /* our own source, something weird with this packet */
2004 g_object_unref (source);
2008 /* store time for when we need to time out this source */
2009 source->bye_time = arrival->current_time;
2011 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2012 prevsender = RTP_SOURCE_IS_SENDER (source);
2014 /* mark the source BYE */
2015 rtp_source_mark_bye (source, reason);
2017 pmembers = sess->stats.active_sources;
2019 source_update_active (sess, source, prevactive);
2020 source_update_sender (sess, source, prevsender);
2022 members = sess->stats.active_sources;
2024 if (!sess->scheduled_bye && members < pmembers) {
2025 /* some members went away since the previous timeout estimate.
2026 * Perform reverse reconsideration but only when we are not scheduling a
2028 if (sess->next_rtcp_check_time != GST_CLOCK_TIME_NONE &&
2029 arrival->current_time < sess->next_rtcp_check_time) {
2030 GstClockTime time_remaining;
2032 time_remaining = sess->next_rtcp_check_time - arrival->current_time;
2033 sess->next_rtcp_check_time =
2034 gst_util_uint64_scale (time_remaining, members, pmembers);
2036 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
2037 GST_TIME_ARGS (sess->next_rtcp_check_time));
2039 sess->next_rtcp_check_time += arrival->current_time;
2041 /* mark pending reconsider. We only want to signal the reconsideration
2042 * once after we handled all the source in the bye packet */
2048 on_new_ssrc (sess, source);
2050 on_bye_ssrc (sess, source);
2052 g_object_unref (source);
2055 RTP_SESSION_UNLOCK (sess);
2056 /* notify app of reconsideration */
2057 if (sess->callbacks.reconsider)
2058 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2059 RTP_SESSION_LOCK (sess);
2065 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
2066 RTPArrivalStats * arrival)
2068 GST_DEBUG ("received APP");
2072 rtp_session_request_local_key_unit (RTPSession * sess, RTPSource * src,
2073 gboolean fir, GstClockTime current_time)
2075 guint32 round_trip = 0;
2077 rtp_source_get_last_rb (src, NULL, NULL, NULL, NULL, NULL, NULL, &round_trip);
2079 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE && round_trip) {
2080 GstClockTime round_trip_in_ns = gst_util_uint64_scale (round_trip,
2083 if (current_time - sess->last_keyframe_request < 2 * round_trip_in_ns) {
2084 GST_DEBUG ("Ignoring %s request because one was send without one "
2085 "RTT (%" GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")",
2086 fir ? "FIR" : "PLI",
2087 GST_TIME_ARGS (current_time - sess->last_keyframe_request),
2088 GST_TIME_ARGS (round_trip_in_ns));;
2093 sess->last_keyframe_request = current_time;
2095 GST_LOG ("received %s request from %X %p(%p)", fir ? "FIR" : "PLI",
2096 rtp_source_get_ssrc (src), sess->callbacks.process_rtp,
2097 sess->callbacks.request_key_unit);
2099 RTP_SESSION_UNLOCK (sess);
2100 sess->callbacks.request_key_unit (sess, fir,
2101 sess->request_key_unit_user_data);
2102 RTP_SESSION_LOCK (sess);
2108 rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
2109 guint32 media_ssrc, GstClockTime current_time)
2113 if (!sess->callbacks.request_key_unit)
2116 src = find_source (sess, sender_ssrc);
2120 rtp_session_request_local_key_unit (sess, src, FALSE, current_time);
2124 rtp_session_process_fir (RTPSession * sess, guint32 sender_ssrc,
2125 guint8 * fci_data, guint fci_length, GstClockTime current_time)
2130 gboolean our_request = FALSE;
2132 if (!sess->callbacks.request_key_unit)
2138 src = find_source (sess, sender_ssrc);
2140 /* Hack because Google fails to set the sender_ssrc correctly */
2141 if (!src && sender_ssrc == 1) {
2142 GHashTableIter iter;
2144 /* we can't find the source if there are multiple */
2145 if (sess->stats.sender_sources > sess->stats.internal_sender_sources + 1)
2148 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
2149 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
2150 if (!src->internal && rtp_source_is_sender (src))
2158 for (position = 0; position < fci_length; position += 8) {
2159 guint8 *data = fci_data + position;
2162 ssrc = GST_READ_UINT32_BE (data);
2164 own = find_source (sess, ssrc);
2165 if (own->internal) {
2173 rtp_session_request_local_key_unit (sess, src, TRUE, current_time);
2177 rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
2178 RTPArrivalStats * arrival, GstClockTime current_time)
2180 GstRTCPType type = gst_rtcp_packet_get_type (packet);
2181 GstRTCPFBType fbtype = gst_rtcp_packet_fb_get_type (packet);
2182 guint32 sender_ssrc = gst_rtcp_packet_fb_get_sender_ssrc (packet);
2183 guint32 media_ssrc = gst_rtcp_packet_fb_get_media_ssrc (packet);
2184 guint8 *fci_data = gst_rtcp_packet_fb_get_fci (packet);
2185 guint fci_length = 4 * gst_rtcp_packet_fb_get_fci_length (packet);
2188 GST_DEBUG ("received feedback %d:%d from %08X about %08X with FCI of "
2189 "length %d", type, fbtype, sender_ssrc, media_ssrc, fci_length);
2191 if (g_signal_has_handler_pending (sess,
2192 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0, TRUE)) {
2193 GstBuffer *fci_buffer = NULL;
2195 if (fci_length > 0) {
2196 fci_buffer = gst_buffer_copy_region (packet->rtcp->buffer,
2197 GST_BUFFER_COPY_MEMORY, fci_data - packet->rtcp->map.data,
2199 GST_BUFFER_TIMESTAMP (fci_buffer) = arrival->running_time;
2202 RTP_SESSION_UNLOCK (sess);
2203 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0,
2204 type, fbtype, sender_ssrc, media_ssrc, fci_buffer);
2205 RTP_SESSION_LOCK (sess);
2208 gst_buffer_unref (fci_buffer);
2211 src = find_source (sess, media_ssrc);
2215 if (sess->rtcp_feedback_retention_window) {
2216 rtp_source_retain_rtcp_packet (src, packet, arrival->running_time);
2219 if (src->internal ||
2220 /* PSFB FIR puts the media ssrc inside the FCI */
2221 (type == GST_RTCP_TYPE_PSFB && fbtype == GST_RTCP_PSFB_TYPE_FIR)) {
2223 case GST_RTCP_TYPE_PSFB:
2225 case GST_RTCP_PSFB_TYPE_PLI:
2226 rtp_session_process_pli (sess, sender_ssrc, media_ssrc,
2229 case GST_RTCP_PSFB_TYPE_FIR:
2230 rtp_session_process_fir (sess, sender_ssrc, fci_data, fci_length,
2237 case GST_RTCP_TYPE_RTPFB:
2245 * rtp_session_process_rtcp:
2246 * @sess: and #RTPSession
2247 * @buffer: an RTCP buffer
2248 * @current_time: the current system time
2249 * @ntpnstime: the current NTP time in nanoseconds
2251 * Process an RTCP buffer in the session manager. This function takes ownership
2254 * Returns: a #GstFlowReturn.
2257 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
2258 GstClockTime current_time, guint64 ntpnstime)
2260 GstRTCPPacket packet;
2261 gboolean more, is_bye = FALSE, do_sync = FALSE;
2262 RTPArrivalStats arrival = { NULL, };
2263 GstFlowReturn result = GST_FLOW_OK;
2264 GstRTCPBuffer rtcp = { NULL, };
2266 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2267 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2269 if (!gst_rtcp_buffer_validate (buffer))
2270 goto invalid_packet;
2272 GST_DEBUG ("received RTCP packet");
2274 RTP_SESSION_LOCK (sess);
2275 /* update arrival stats */
2276 update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1,
2279 /* start processing the compound packet */
2280 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
2281 more = gst_rtcp_buffer_get_first_packet (&rtcp, &packet);
2285 type = gst_rtcp_packet_get_type (&packet);
2287 /* when we are leaving the session, we should ignore all non-BYE messages */
2288 if (sess->scheduled_bye && type != GST_RTCP_TYPE_BYE) {
2289 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
2294 case GST_RTCP_TYPE_SR:
2295 rtp_session_process_sr (sess, &packet, &arrival, &do_sync);
2297 case GST_RTCP_TYPE_RR:
2298 rtp_session_process_rr (sess, &packet, &arrival);
2300 case GST_RTCP_TYPE_SDES:
2301 rtp_session_process_sdes (sess, &packet, &arrival);
2303 case GST_RTCP_TYPE_BYE:
2305 /* don't try to attempt lip-sync anymore for streams with a BYE */
2307 rtp_session_process_bye (sess, &packet, &arrival);
2309 case GST_RTCP_TYPE_APP:
2310 rtp_session_process_app (sess, &packet, &arrival);
2312 case GST_RTCP_TYPE_RTPFB:
2313 case GST_RTCP_TYPE_PSFB:
2314 rtp_session_process_feedback (sess, &packet, &arrival, current_time);
2317 GST_WARNING ("got unknown RTCP packet");
2321 more = gst_rtcp_packet_move_to_next (&packet);
2324 gst_rtcp_buffer_unmap (&rtcp);
2326 /* if we are scheduling a BYE, we only want to count bye packets, else we
2327 * count everything */
2328 if (sess->scheduled_bye) {
2330 sess->stats.bye_members++;
2331 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2334 /* keep track of average packet size */
2335 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2337 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
2338 sess->stats.avg_rtcp_packet_size, arrival.bytes);
2339 RTP_SESSION_UNLOCK (sess);
2341 clean_arrival_stats (&arrival);
2343 /* notify caller of sr packets in the callback */
2344 if (do_sync && sess->callbacks.sync_rtcp) {
2345 result = sess->callbacks.sync_rtcp (sess, buffer,
2346 sess->sync_rtcp_user_data);
2348 gst_buffer_unref (buffer);
2355 GST_DEBUG ("invalid RTCP packet received");
2356 gst_buffer_unref (buffer);
2362 * rtp_session_update_send_caps:
2363 * @sess: an #RTPSession
2366 * Update the caps of the sender in the rtp session.
2369 rtp_session_update_send_caps (RTPSession * sess, GstCaps * caps)
2374 g_return_if_fail (RTP_IS_SESSION (sess));
2375 g_return_if_fail (GST_IS_CAPS (caps));
2377 GST_LOG ("received caps %" GST_PTR_FORMAT, caps);
2379 s = gst_caps_get_structure (caps, 0);
2381 if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
2385 RTP_SESSION_LOCK (sess);
2386 source = obtain_internal_source (sess, ssrc, &created);
2388 rtp_source_update_caps (source, caps);
2389 g_object_unref (source);
2391 RTP_SESSION_UNLOCK (sess);
2396 * rtp_session_send_rtp:
2397 * @sess: an #RTPSession
2398 * @data: pointer to either an RTP buffer or a list of RTP buffers
2399 * @is_list: TRUE when @data is a buffer list
2400 * @current_time: the current system time
2401 * @running_time: the running time of @data
2403 * Send the RTP buffer in the session manager. This function takes ownership of
2406 * Returns: a #GstFlowReturn.
2409 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
2410 GstClockTime current_time, GstClockTime running_time)
2412 GstFlowReturn result;
2414 gboolean prevsender;
2417 GstRTPBuffer rtp = { NULL };
2421 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2422 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
2424 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
2427 GstBufferList *list = GST_BUFFER_LIST_CAST (data);
2429 buffer = gst_buffer_list_get (list, 0);
2433 buffer = GST_BUFFER_CAST (data);
2436 if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp))
2437 goto invalid_packet;
2439 /* get SSRC and look up in session database */
2440 ssrc = gst_rtp_buffer_get_ssrc (&rtp);
2442 gst_rtp_buffer_unmap (&rtp);
2444 RTP_SESSION_LOCK (sess);
2445 source = obtain_internal_source (sess, ssrc, &created);
2447 /* update last activity */
2448 source->last_rtp_activity = current_time;
2450 prevsender = RTP_SOURCE_IS_SENDER (source);
2451 oldrate = source->bitrate;
2453 /* we use our own source to send */
2454 result = rtp_source_send_rtp (source, data, is_list, running_time);
2456 source_update_sender (sess, source, prevsender);
2458 if (oldrate != source->bitrate)
2459 sess->recalc_bandwidth = TRUE;
2460 RTP_SESSION_UNLOCK (sess);
2462 g_object_unref (source);
2468 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
2469 GST_DEBUG ("invalid RTP packet received");
2474 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
2475 GST_DEBUG ("no buffer in list");
2481 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
2483 *bandwidth += source->bitrate;
2486 /* must be called with session lock */
2488 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2491 GstClockTime result;
2493 /* recalculate bandwidth when it changed */
2494 if (sess->recalc_bandwidth) {
2497 if (sess->bandwidth > 0)
2498 bandwidth = sess->bandwidth;
2500 /* If it is <= 0, then try to estimate the actual bandwidth */
2503 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2504 (GHFunc) add_bitrates, &bandwidth);
2507 if (bandwidth < 8000)
2508 bandwidth = RTP_STATS_BANDWIDTH;
2510 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
2511 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
2513 sess->recalc_bandwidth = FALSE;
2516 if (sess->scheduled_bye) {
2517 result = rtp_stats_calculate_bye_interval (&sess->stats);
2519 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2520 sess->stats.internal_sender_sources > 0, first);
2523 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2524 GST_TIME_ARGS (result), first);
2526 if (!deterministic && result != GST_CLOCK_TIME_NONE)
2527 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2529 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2535 source_mark_bye (const gchar * key, RTPSource * source, const gchar * reason)
2537 if (source->internal)
2538 rtp_source_mark_bye (source, reason);
2542 * rtp_session_mark_all_bye:
2543 * @sess: an #RTPSession
2546 * Mark all internal sources of the session as BYE with @reason.
2549 rtp_session_mark_all_bye (RTPSession * sess, const gchar * reason)
2551 g_return_if_fail (RTP_IS_SESSION (sess));
2553 RTP_SESSION_LOCK (sess);
2554 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2555 (GHFunc) source_mark_bye, (gpointer) reason);
2556 RTP_SESSION_UNLOCK (sess);
2559 /* Stop the current @sess and schedule a BYE message for the other members.
2560 * One must have the session lock to call this function
2562 static GstFlowReturn
2563 rtp_session_schedule_bye_locked (RTPSession * sess, GstClockTime current_time)
2565 GstFlowReturn result = GST_FLOW_OK;
2566 GstClockTime interval;
2568 /* nothing to do it we already scheduled bye */
2569 if (sess->scheduled_bye)
2572 /* we schedule BYE now */
2573 sess->scheduled_bye = TRUE;
2574 /* at least one member wants to send a BYE */
2575 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
2576 sess->stats.bye_members = 1;
2577 sess->first_rtcp = TRUE;
2578 sess->allow_early = TRUE;
2580 /* reschedule transmission */
2581 sess->last_rtcp_send_time = current_time;
2582 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2584 if (interval != GST_CLOCK_TIME_NONE)
2585 sess->next_rtcp_check_time = current_time + interval;
2587 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
2589 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2590 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2592 RTP_SESSION_UNLOCK (sess);
2593 /* notify app of reconsideration */
2594 if (sess->callbacks.reconsider)
2595 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2596 RTP_SESSION_LOCK (sess);
2603 * rtp_session_schedule_bye:
2604 * @sess: an #RTPSession
2605 * @current_time: the current system time
2607 * Schedule a BYE message for all sources marked as BYE in @sess.
2609 * Returns: a #GstFlowReturn.
2612 rtp_session_schedule_bye (RTPSession * sess, GstClockTime current_time)
2614 GstFlowReturn result = GST_FLOW_OK;
2616 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2618 RTP_SESSION_LOCK (sess);
2619 result = rtp_session_schedule_bye_locked (sess, current_time);
2620 RTP_SESSION_UNLOCK (sess);
2626 * rtp_session_next_timeout:
2627 * @sess: an #RTPSession
2628 * @current_time: the current system time
2630 * Get the next time we should perform session maintenance tasks.
2632 * Returns: a time when rtp_session_on_timeout() should be called with the
2633 * current system time.
2636 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2638 GstClockTime result, interval = 0;
2640 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
2642 RTP_SESSION_LOCK (sess);
2644 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
2645 result = sess->next_early_rtcp_time;
2649 result = sess->next_rtcp_check_time;
2651 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2652 ", next time: %" GST_TIME_FORMAT,
2653 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2655 if (result == GST_CLOCK_TIME_NONE || result < current_time) {
2656 GST_DEBUG ("take current time as base");
2657 /* our previous check time expired, start counting from the current time
2659 result = current_time;
2662 if (sess->scheduled_bye) {
2663 if (sess->stats.active_sources >= 50) {
2664 GST_DEBUG ("reconsider BYE, more than 50 sources");
2665 /* reconsider BYE if members >= 50 */
2666 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2669 if (sess->first_rtcp) {
2670 GST_DEBUG ("first RTCP packet");
2671 /* we are called for the first time */
2672 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2673 } else if (sess->next_rtcp_check_time < current_time) {
2674 GST_DEBUG ("old check time expired, getting new timeout");
2675 /* get a new timeout when we need to */
2676 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
2680 if (interval != GST_CLOCK_TIME_NONE)
2683 result = GST_CLOCK_TIME_NONE;
2685 sess->next_rtcp_check_time = result;
2689 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2690 ", next time: %" GST_TIME_FORMAT,
2691 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2692 RTP_SESSION_UNLOCK (sess);
2706 GstRTCPBuffer rtcpbuf;
2709 guint num_to_report;
2713 GstClockTime current_time;
2715 GstClockTime running_time;
2716 GstClockTime interval;
2717 GstRTCPPacket packet;
2720 gboolean may_suppress;
2725 session_start_rtcp (RTPSession * sess, ReportData * data)
2727 GstRTCPPacket *packet = &data->packet;
2728 RTPSource *own = data->source;
2729 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2731 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2732 data->has_sdes = FALSE;
2734 gst_rtcp_buffer_map (data->rtcp, GST_MAP_READWRITE, rtcp);
2736 if (RTP_SOURCE_IS_SENDER (own)) {
2739 guint32 packet_count, octet_count;
2741 /* we are a sender, create SR */
2742 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2743 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SR, packet);
2745 /* get latest stats */
2746 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
2747 &ntptime, &rtptime, &packet_count, &octet_count);
2749 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2750 packet_count, octet_count);
2752 /* fill in sender report info */
2753 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2754 ntptime, rtptime, packet_count, octet_count);
2756 /* we are only receiver, create RR */
2757 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2758 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RR, packet);
2759 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2763 /* construct a Sender or Receiver Report */
2765 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2767 RTPSession *sess = data->sess;
2768 GstRTCPPacket *packet = &data->packet;
2769 guint8 fractionlost;
2771 guint32 exthighestseq, jitter;
2774 /* don't report for sources in future generations */
2775 if (((gint16) (source->generation - sess->generation)) > 0) {
2776 GST_DEBUG ("source %08x generation %u > %u", source->ssrc,
2777 source->generation, sess->generation);
2781 /* only report about other sender */
2782 if (source == data->source)
2785 if (gst_rtcp_packet_get_rb_count (packet) == GST_RTCP_MAX_RB_COUNT) {
2786 GST_DEBUG ("max RB count reached");
2790 if (!RTP_SOURCE_IS_SENDER (source)) {
2791 GST_DEBUG ("source %08x not sender", source->ssrc);
2795 GST_DEBUG ("create RB for SSRC %08x", source->ssrc);
2798 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2799 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2801 /* store last generated RR packet */
2802 source->last_rr.is_valid = TRUE;
2803 source->last_rr.fractionlost = fractionlost;
2804 source->last_rr.packetslost = packetslost;
2805 source->last_rr.exthighestseq = exthighestseq;
2806 source->last_rr.jitter = jitter;
2807 source->last_rr.lsr = lsr;
2808 source->last_rr.dlsr = dlsr;
2810 /* packet is not yet filled, add report block for this source. */
2811 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2812 exthighestseq, jitter, lsr, dlsr);
2815 /* source is reported, move to next generation */
2816 source->generation = sess->generation + 1;
2818 /* if we reported all sources in this generation, move to next */
2819 if (--data->num_to_report == 0) {
2821 GST_DEBUG ("all reported, generation now %u", sess->generation);
2827 session_add_fir (const gchar * key, RTPSource * source, ReportData * data)
2829 GstRTCPPacket *packet = &data->packet;
2833 if (!source->send_fir)
2836 len = gst_rtcp_packet_fb_get_fci_length (packet);
2837 if (!gst_rtcp_packet_fb_set_fci_length (packet, len + 2))
2838 /* exit because the packet is full, will put next request in a
2842 fci_data = gst_rtcp_packet_fb_get_fci (packet) + (len * 4);
2844 GST_WRITE_UINT32_BE (fci_data, source->ssrc);
2846 fci_data[0] = source->current_send_fir_seqnum;
2847 fci_data[1] = fci_data[2] = fci_data[3] = 0;
2849 source->send_fir = FALSE;
2853 session_fir (RTPSession * sess, ReportData * data)
2855 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2856 GstRTCPPacket *packet = &data->packet;
2858 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
2861 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_FIR);
2862 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
2863 gst_rtcp_packet_fb_set_media_ssrc (packet, 0);
2865 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2866 (GHFunc) session_add_fir, data);
2868 if (gst_rtcp_packet_fb_get_fci_length (packet) == 0)
2869 gst_rtcp_packet_remove (packet);
2871 data->may_suppress = FALSE;
2875 has_pli_compare_func (gconstpointer a, gconstpointer ignored)
2877 GstRTCPPacket packet;
2878 GstRTCPBuffer rtcp = { NULL, };
2879 gboolean ret = FALSE;
2881 gst_rtcp_buffer_map ((GstBuffer *) a, GST_MAP_READ, &rtcp);
2883 if (gst_rtcp_buffer_get_first_packet (&rtcp, &packet)) {
2884 if (gst_rtcp_packet_get_type (&packet) == GST_RTCP_TYPE_PSFB &&
2885 gst_rtcp_packet_fb_get_type (&packet) == GST_RTCP_PSFB_TYPE_PLI)
2889 gst_rtcp_buffer_unmap (&rtcp);
2896 session_pli (const gchar * key, RTPSource * source, ReportData * data)
2898 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2899 GstRTCPPacket *packet = &data->packet;
2901 if (!source->send_pli)
2904 if (rtp_source_has_retained (source, has_pli_compare_func, NULL))
2907 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
2908 /* exit because the packet is full, will put next request in a
2912 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_PLI);
2913 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
2914 gst_rtcp_packet_fb_set_media_ssrc (packet, source->ssrc);
2916 source->send_pli = FALSE;
2917 data->may_suppress = FALSE;
2920 /* perform cleanup of sources that timed out */
2922 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2924 gboolean remove = FALSE;
2925 gboolean byetimeout = FALSE;
2926 gboolean sendertimeout = FALSE;
2927 gboolean is_sender, is_active;
2928 RTPSession *sess = data->sess;
2929 GstClockTime interval, binterval;
2932 GST_DEBUG ("look at %08x, generation %u", source->ssrc, source->generation);
2934 /* check for outdated collisions */
2935 if (source->internal) {
2936 GST_DEBUG ("Timing out collisions for %x", source->ssrc);
2937 rtp_source_timeout (source, data->current_time,
2938 /* "a relatively long time" -- RFC 3550 section 8.2 */
2939 RTP_STATS_MIN_INTERVAL * GST_SECOND * 10,
2940 data->running_time - sess->rtcp_feedback_retention_window);
2943 /* nothing else to do when without RTCP */
2944 if (data->interval == GST_CLOCK_TIME_NONE)
2947 is_sender = RTP_SOURCE_IS_SENDER (source);
2948 is_active = RTP_SOURCE_IS_ACTIVE (source);
2950 /* our own rtcp interval may have been forced low by secondary configuration,
2951 * while sender side may still operate with higher interval,
2952 * so do not just take our interval to decide on timing out sender,
2953 * but take (if data->interval <= 5 * GST_SECOND):
2954 * interval = CLAMP (sender_interval, data->interval, 5 * GST_SECOND)
2955 * where sender_interval is difference between last 2 received RTCP reports
2957 if (data->interval >= 5 * GST_SECOND || source->internal) {
2958 binterval = data->interval;
2960 GST_LOG ("prev_rtcp %" GST_TIME_FORMAT ", last_rtcp %" GST_TIME_FORMAT,
2961 GST_TIME_ARGS (source->stats.prev_rtcptime),
2962 GST_TIME_ARGS (source->stats.last_rtcptime));
2963 /* if not received enough yet, fallback to larger default */
2964 if (source->stats.last_rtcptime > source->stats.prev_rtcptime)
2965 binterval = source->stats.last_rtcptime - source->stats.prev_rtcptime;
2967 binterval = 5 * GST_SECOND;
2968 binterval = CLAMP (binterval, data->interval, 5 * GST_SECOND);
2970 GST_LOG ("timeout base interval %" GST_TIME_FORMAT,
2971 GST_TIME_ARGS (binterval));
2973 if (!source->internal) {
2974 if (source->marked_bye) {
2975 /* if we received a BYE from the source, remove the source after some
2977 if (data->current_time > source->bye_time &&
2978 data->current_time - source->bye_time > sess->stats.bye_timeout) {
2979 GST_DEBUG ("removing BYE source %08x", source->ssrc);
2984 /* sources that were inactive for more than 5 times the deterministic reporting
2985 * interval get timed out. the min timeout is 5 seconds. */
2986 /* mind old time that might pre-date last time going to PLAYING */
2987 btime = MAX (source->last_activity, sess->start_time);
2988 if (data->current_time > btime) {
2989 interval = MAX (binterval * 5, 5 * GST_SECOND);
2990 if (data->current_time - btime > interval) {
2991 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2992 source->ssrc, GST_TIME_ARGS (btime));
2998 /* senders that did not send for a long time become a receiver, this also
2999 * holds for our own sources. */
3001 /* mind old time that might pre-date last time going to PLAYING */
3002 btime = MAX (source->last_rtp_activity, sess->start_time);
3003 if (data->current_time > btime) {
3004 interval = MAX (binterval * 2, 5 * GST_SECOND);
3005 if (data->current_time - btime > interval) {
3006 if (source->internal && source->sent_bye) {
3007 /* an internal source is BYE and stopped sending RTP, remove */
3008 GST_DEBUG ("internal BYE source %08x timed out, last %"
3009 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
3012 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
3013 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
3014 sendertimeout = TRUE;
3021 sess->total_sources--;
3023 sess->stats.sender_sources--;
3024 if (source->internal)
3025 sess->stats.internal_sender_sources--;
3028 sess->stats.active_sources--;
3030 if (source->internal)
3031 sess->stats.internal_sources--;
3034 on_bye_timeout (sess, source);
3036 on_timeout (sess, source);
3038 if (sendertimeout) {
3039 source->is_sender = FALSE;
3040 sess->stats.sender_sources--;
3041 if (source->internal)
3042 sess->stats.internal_sender_sources--;
3044 on_sender_timeout (sess, source);
3046 /* count how many source to report in this generation */
3047 if (((gint16) (source->generation - sess->generation)) <= 0)
3048 data->num_to_report++;
3050 source->closing = remove;
3054 session_sdes (RTPSession * sess, ReportData * data)
3056 GstRTCPPacket *packet = &data->packet;
3057 const GstStructure *sdes;
3059 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3061 /* add SDES packet */
3062 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SDES, packet);
3064 gst_rtcp_packet_sdes_add_item (packet, data->source->ssrc);
3066 sdes = rtp_source_get_sdes_struct (data->source);
3068 /* add all fields in the structure, the order is not important. */
3069 n_fields = gst_structure_n_fields (sdes);
3070 for (i = 0; i < n_fields; ++i) {
3073 GstRTCPSDESType type;
3075 field = gst_structure_nth_field_name (sdes, i);
3078 value = gst_structure_get_string (sdes, field);
3081 type = gst_rtcp_sdes_name_to_type (field);
3083 /* Early packets are minimal and only include the CNAME */
3084 if (data->is_early && type != GST_RTCP_SDES_CNAME)
3087 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
3088 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
3089 (const guint8 *) value);
3090 } else if (type == GST_RTCP_SDES_PRIV) {
3096 /* don't accept entries that are too big */
3097 prefix_len = strlen (field);
3098 if (prefix_len > 255)
3100 value_len = strlen (value);
3101 if (value_len > 255)
3103 data_len = 1 + prefix_len + value_len;
3107 data[0] = prefix_len;
3108 memcpy (&data[1], field, prefix_len);
3109 memcpy (&data[1 + prefix_len], value, value_len);
3111 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
3115 data->has_sdes = TRUE;
3118 /* schedule a BYE packet */
3120 make_source_bye (RTPSession * sess, RTPSource * source, ReportData * data)
3122 GstRTCPPacket *packet = &data->packet;
3123 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3126 session_sdes (sess, data);
3127 /* add a BYE packet */
3128 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_BYE, packet);
3129 gst_rtcp_packet_bye_add_ssrc (packet, source->ssrc);
3130 if (source->bye_reason)
3131 gst_rtcp_packet_bye_set_reason (packet, source->bye_reason);
3133 /* we have a BYE packet now */
3134 source->sent_bye = TRUE;
3138 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
3140 GstClockTime new_send_time, elapsed;
3142 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3143 data->is_early = TRUE;
3145 data->is_early = FALSE;
3147 if (data->is_early && sess->next_early_rtcp_time < current_time)
3150 /* no need to check yet */
3151 if (sess->next_rtcp_check_time == GST_CLOCK_TIME_NONE ||
3152 sess->next_rtcp_check_time > current_time) {
3153 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
3154 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
3155 GST_TIME_ARGS (current_time));
3159 /* get elapsed time since we last reported */
3160 elapsed = current_time - sess->last_rtcp_send_time;
3162 new_send_time = data->interval;
3163 /* perform forward reconsideration */
3164 if (new_send_time != GST_CLOCK_TIME_NONE) {
3165 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, new_send_time);
3167 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
3168 GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time),
3169 GST_TIME_ARGS (elapsed));
3171 new_send_time += sess->last_rtcp_send_time;
3174 /* check if reconsideration */
3175 if (new_send_time == GST_CLOCK_TIME_NONE || current_time < new_send_time) {
3176 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
3177 GST_TIME_ARGS (new_send_time));
3178 /* store new check time */
3179 sess->next_rtcp_check_time = new_send_time;
3185 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
3187 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
3188 GST_TIME_ARGS (new_send_time));
3190 sess->next_rtcp_check_time = new_send_time;
3191 if (new_send_time != GST_CLOCK_TIME_NONE) {
3192 sess->next_rtcp_check_time += current_time;
3194 /* Apply the rules from RFC 4585 section 3.5.3 */
3195 if (sess->stats.min_interval != 0 && !sess->first_rtcp) {
3196 GstClockTimeDiff T_rr_current_interval =
3197 g_random_double_range (0.5, 1.5) * sess->stats.min_interval;
3199 /* This will caused the RTCP to be suppressed if no FB packets are added */
3200 if (sess->last_rtcp_send_time + T_rr_current_interval >
3201 sess->next_rtcp_check_time) {
3202 GST_DEBUG ("RTCP packet could be suppressed min: %" GST_TIME_FORMAT
3203 " last: %" GST_TIME_FORMAT
3204 " + T_rr_current_interval: %" GST_TIME_FORMAT
3205 " > sess->next_rtcp_check_time: %" GST_TIME_FORMAT,
3206 GST_TIME_ARGS (sess->stats.min_interval),
3207 GST_TIME_ARGS (sess->last_rtcp_send_time),
3208 GST_TIME_ARGS (T_rr_current_interval),
3209 GST_TIME_ARGS (sess->next_rtcp_check_time));
3210 data->may_suppress = TRUE;
3219 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
3221 g_hash_table_insert (hash_table, key, g_object_ref (source));
3225 remove_closing_sources (const gchar * key, RTPSource * source,
3228 if (source->closing)
3231 if (source->send_fir)
3232 data->have_fir = TRUE;
3233 if (source->send_pli)
3234 data->have_pli = TRUE;
3240 generate_rtcp (const gchar * key, RTPSource * source, ReportData * data)
3242 RTPSession *sess = data->sess;
3243 gboolean is_bye = FALSE;
3244 ReportOutput *output;
3246 /* only generate RTCP for active internal sources */
3247 if (!source->internal || source->sent_bye)
3250 data->source = source;
3253 session_start_rtcp (sess, data);
3255 if (source->marked_bye) {
3257 make_source_bye (sess, source, data);
3259 } else if (!data->is_early) {
3260 /* loop over all known sources and add report blocks. If we are early, we
3261 * just make a minimal RTCP packet and skip this step */
3262 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3263 (GHFunc) session_report_blocks, data);
3265 if (!data->has_sdes)
3266 session_sdes (sess, data);
3269 session_fir (sess, data);
3272 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3273 (GHFunc) session_pli, data);
3275 gst_rtcp_buffer_unmap (&data->rtcpbuf);
3277 output = g_slice_new (ReportOutput);
3278 output->source = g_object_ref (source);
3279 output->is_bye = is_bye;
3280 output->buffer = data->rtcp;
3281 /* queue the RTCP packet to push later */
3282 g_queue_push_tail (&data->output, output);
3286 * rtp_session_on_timeout:
3287 * @sess: an #RTPSession
3288 * @current_time: the current system time
3289 * @ntpnstime: the current NTP time in nanoseconds
3290 * @running_time: the current running_time of the pipeline
3292 * Perform maintenance actions after the timeout obtained with
3293 * rtp_session_next_timeout() expired.
3295 * This function will perform timeouts of receivers and senders, send a BYE
3296 * packet or generate RTCP packets with current session stats.
3298 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
3299 * times, for each packet that should be processed.
3301 * Returns: a #GstFlowReturn.
3304 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
3305 guint64 ntpnstime, GstClockTime running_time)
3307 GstFlowReturn result = GST_FLOW_OK;
3308 ReportData data = { GST_RTCP_BUFFER_INIT };
3309 GHashTable *table_copy;
3310 ReportOutput *output;
3312 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3314 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT
3315 ", running-time %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
3316 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (running_time));
3319 data.current_time = current_time;
3320 data.ntpnstime = ntpnstime;
3321 data.running_time = running_time;
3322 data.num_to_report = 0;
3323 data.may_suppress = FALSE;
3324 g_queue_init (&data.output);
3326 RTP_SESSION_LOCK (sess);
3327 /* get a new interval, we need this for various cleanups etc */
3328 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
3330 /* we need an internal source now */
3331 if (sess->stats.internal_sources == 0) {
3335 source = obtain_internal_source (sess, sess->suggested_ssrc, &created);
3336 g_object_unref (source);
3339 /* Make a local copy of the hashtable. We need to do this because the
3340 * cleanup stage below releases the session lock. */
3341 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
3342 (GDestroyNotify) g_object_unref);
3343 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3344 (GHFunc) clone_ssrcs_hashtable, table_copy);
3346 /* Clean up the session, mark the source for removing, this might release the
3348 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
3349 g_hash_table_destroy (table_copy);
3351 /* Now remove the marked sources */
3352 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
3353 (GHRFunc) remove_closing_sources, NULL);
3355 /* see if we need to generate SR or RR packets */
3356 if (!is_rtcp_time (sess, current_time, &data))
3359 GST_DEBUG ("doing RTCP generation %u for %u sources", sess->generation,
3360 data.num_to_report);
3362 /* generate RTCP for all internal sources */
3363 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3364 (GHFunc) generate_rtcp, &data);
3366 /* we keep track of the last report time in order to timeout inactive
3367 * receivers or senders */
3368 if (!data.is_early && !data.may_suppress)
3369 sess->last_rtcp_send_time = data.current_time;
3370 sess->first_rtcp = FALSE;
3371 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
3374 RTP_SESSION_UNLOCK (sess);
3376 /* push out the RTCP packets */
3377 while ((output = g_queue_pop_head (&data.output))) {
3378 gboolean do_not_suppress;
3379 GstBuffer *buffer = output->buffer;
3380 RTPSource *source = output->source;
3382 /* Give the user a change to add its own packet */
3383 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
3384 buffer, data.is_early, &do_not_suppress);
3386 if (sess->callbacks.send_rtcp && (do_not_suppress || !data.may_suppress)) {
3389 packet_size = gst_buffer_get_size (buffer) + sess->header_len;
3391 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
3392 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
3393 sess->stats.avg_rtcp_packet_size, packet_size);
3395 sess->callbacks.send_rtcp (sess, source, buffer, output->is_bye,
3396 sess->send_rtcp_user_data);
3398 GST_DEBUG ("freeing packet callback: %p"
3399 " do_not_suppress: %d may_suppress: %d",
3400 sess->callbacks.send_rtcp, do_not_suppress, data.may_suppress);
3401 gst_buffer_unref (buffer);
3403 g_object_unref (source);
3404 g_slice_free (ReportOutput, output);
3410 * rtp_session_request_early_rtcp:
3411 * @sess: an #RTPSession
3412 * @current_time: the current system time
3413 * @max_delay: maximum delay
3415 * Request transmission of early RTCP
3418 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
3419 GstClockTimeDiff max_delay)
3421 GstClockTime T_dither_max;
3423 /* Implements the algorithm described in RFC 4585 section 3.5.2 */
3425 RTP_SESSION_LOCK (sess);
3427 /* Check if already requested */
3428 /* RFC 4585 section 3.5.2 step 2 */
3429 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3432 if (!GST_CLOCK_TIME_IS_VALID (sess->next_rtcp_check_time))
3435 /* Ignore the request a scheduled packet will be in time anyway */
3436 if (current_time + max_delay > sess->next_rtcp_check_time)
3439 /* RFC 4585 section 3.5.2 step 2b */
3440 /* If the total sources is <=2, then there is only us and one peer */
3441 if (sess->total_sources <= 2) {
3444 /* Divide by 2 because l = 0.5 */
3445 T_dither_max = sess->next_rtcp_check_time - sess->last_rtcp_send_time;
3449 /* RFC 4585 section 3.5.2 step 3 */
3450 if (current_time + T_dither_max > sess->next_rtcp_check_time)
3453 /* RFC 4585 section 3.5.2 step 4
3454 * Don't send if allow_early is FALSE, but not if we are in
3455 * immediate mode, meaning we are part of a group of at most the
3456 * application-specific threshold.
3458 if (sess->total_sources > sess->rtcp_immediate_feedback_threshold &&
3459 sess->allow_early == FALSE)
3463 /* Schedule an early transmission later */
3464 sess->next_early_rtcp_time = g_random_double () * T_dither_max +
3467 /* If no dithering, schedule it for NOW */
3468 sess->next_early_rtcp_time = current_time;
3471 RTP_SESSION_UNLOCK (sess);
3473 /* notify app of need to send packet early
3474 * and therefore of timeout change */
3475 if (sess->callbacks.reconsider)
3476 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
3482 RTP_SESSION_UNLOCK (sess);
3486 rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc, GstClockTime now,
3487 gboolean fir, gint count)
3489 RTPSource *src = find_source (sess, ssrc);
3495 src->send_pli = FALSE;
3496 src->send_fir = TRUE;
3498 if (count == -1 || count != src->last_fir_count)
3499 src->current_send_fir_seqnum++;
3500 src->last_fir_count = count;
3501 } else if (!src->send_fir) {
3502 src->send_pli = TRUE;
3505 rtp_session_request_early_rtcp (sess, now, 200 * GST_MSECOND);
3511 rtp_session_send_rtcp (RTPSession * sess, GstClockTimeDiff max_delay)
3515 if (!sess->callbacks.send_rtcp)
3518 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
3520 rtp_session_request_early_rtcp (sess, now, max_delay);