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 "rtpsession.h"
33 GST_DEBUG_CATEGORY_STATIC (rtp_session_debug);
34 #define GST_CAT_DEFAULT rtp_session_debug
36 /* signals and args */
39 SIGNAL_GET_SOURCE_BY_SSRC,
41 SIGNAL_ON_SSRC_COLLISION,
42 SIGNAL_ON_SSRC_VALIDATED,
43 SIGNAL_ON_SSRC_ACTIVE,
46 SIGNAL_ON_BYE_TIMEOUT,
48 SIGNAL_ON_SENDER_TIMEOUT,
49 SIGNAL_ON_SENDING_RTCP,
50 SIGNAL_ON_FEEDBACK_RTCP,
52 SIGNAL_SEND_RTCP_FULL,
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,
94 /* update average packet size */
95 #define INIT_AVG(avg, val) \
97 #define UPDATE_AVG(avg, val) \
101 (avg) = ((val) + (15 * (avg))) >> 4;
104 /* GObject vmethods */
105 static void rtp_session_finalize (GObject * object);
106 static void rtp_session_set_property (GObject * object, guint prop_id,
107 const GValue * value, GParamSpec * pspec);
108 static void rtp_session_get_property (GObject * object, guint prop_id,
109 GValue * value, GParamSpec * pspec);
111 static gboolean rtp_session_send_rtcp (RTPSession * sess,
112 GstClockTime max_delay);
114 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
116 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
118 static guint32 rtp_session_create_new_ssrc (RTPSession * sess);
119 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
120 gboolean * created, RTPPacketInfo * pinfo, gboolean rtp);
121 static RTPSource *obtain_internal_source (RTPSession * sess,
122 guint32 ssrc, gboolean * created, GstClockTime current_time);
123 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
124 GstClockTime current_time);
125 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
126 gboolean deterministic, gboolean first);
129 accumulate_trues (GSignalInvocationHint * ihint, GValue * return_accu,
130 const GValue * handler_return, gpointer data)
132 if (g_value_get_boolean (handler_return))
133 g_value_set_boolean (return_accu, TRUE);
139 rtp_session_class_init (RTPSessionClass * klass)
141 GObjectClass *gobject_class;
143 gobject_class = (GObjectClass *) klass;
145 gobject_class->finalize = rtp_session_finalize;
146 gobject_class->set_property = rtp_session_set_property;
147 gobject_class->get_property = rtp_session_get_property;
150 * RTPSession::get-source-by-ssrc:
151 * @session: the object which received the signal
152 * @ssrc: the SSRC of the RTPSource
154 * Request the #RTPSource object with SSRC @ssrc in @session.
156 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
157 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
158 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
159 get_source_by_ssrc), NULL, NULL, g_cclosure_marshal_generic,
160 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
163 * RTPSession::on-new-ssrc:
164 * @session: the object which received the signal
165 * @src: the new RTPSource
167 * Notify of a new SSRC that entered @session.
169 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
170 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
171 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
172 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
175 * RTPSession::on-ssrc-collision:
176 * @session: the object which received the signal
177 * @src: the #RTPSource that caused a collision
179 * Notify when we have an SSRC collision
181 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
182 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
183 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
184 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
187 * RTPSession::on-ssrc-validated:
188 * @session: the object which received the signal
189 * @src: the new validated RTPSource
191 * Notify of a new SSRC that became validated.
193 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
194 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
195 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
196 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
199 * RTPSession::on-ssrc-active:
200 * @session: the object which received the signal
201 * @src: the active RTPSource
203 * Notify of a SSRC that is active, i.e., sending RTCP.
205 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
206 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
207 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
208 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
211 * RTPSession::on-ssrc-sdes:
212 * @session: the object which received the signal
213 * @src: the RTPSource
215 * Notify that a new SDES was received for SSRC.
217 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
218 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
219 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
220 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
223 * RTPSession::on-bye-ssrc:
224 * @session: the object which received the signal
225 * @src: the RTPSource that went away
227 * Notify of an SSRC that became inactive because of a BYE packet.
229 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
230 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
231 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
232 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
235 * RTPSession::on-bye-timeout:
236 * @session: the object which received the signal
237 * @src: the RTPSource that timed out
239 * Notify of an SSRC that has timed out because of BYE
241 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
242 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
243 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
244 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
247 * RTPSession::on-timeout:
248 * @session: the object which received the signal
249 * @src: the RTPSource that timed out
251 * Notify of an SSRC that has timed out
253 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
254 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
255 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
256 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
259 * RTPSession::on-sender-timeout:
260 * @session: the object which received the signal
261 * @src: the RTPSource that timed out
263 * Notify of an SSRC that was a sender but timed out and became a receiver.
265 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
266 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
267 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
268 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
272 * RTPSession::on-sending-rtcp
273 * @session: the object which received the signal
274 * @buffer: the #GstBuffer containing the RTCP packet about to be sent
275 * @early: %TRUE if the packet is early, %FALSE if it is regular
277 * This signal is emitted before sending an RTCP packet, it can be used
278 * to add extra RTCP Packets.
280 * Returns: %TRUE if the RTCP buffer should NOT be suppressed, %FALSE
281 * if suppressing it is acceptable
283 rtp_session_signals[SIGNAL_ON_SENDING_RTCP] =
284 g_signal_new ("on-sending-rtcp", G_TYPE_FROM_CLASS (klass),
285 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_rtcp),
286 accumulate_trues, NULL, g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2,
287 GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_BOOLEAN);
290 * RTPSession::on-feedback-rtcp:
291 * @session: the object which received the signal
292 * @type: Type of RTCP packet, will be %GST_RTCP_TYPE_RTPFB or
293 * %GST_RTCP_TYPE_RTPFB
294 * @fbtype: The type of RTCP FB packet, probably part of #GstRTCPFBType
295 * @sender_ssrc: The SSRC of the sender
296 * @media_ssrc: The SSRC of the media this refers to
297 * @fci: a #GstBuffer with the FCI data from the FB packet or %NULL if
300 * Notify that a RTCP feedback packet has been received
302 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP] =
303 g_signal_new ("on-feedback-rtcp", G_TYPE_FROM_CLASS (klass),
304 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_feedback_rtcp),
305 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 5, G_TYPE_UINT,
306 G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, GST_TYPE_BUFFER);
309 * RTPSession::send-rtcp:
310 * @session: the object which received the signal
311 * @max_delay: The maximum delay after which the feedback will not be useful
314 * Requests that the #RTPSession initiate a new RTCP packet as soon as
315 * possible within the requested delay.
317 rtp_session_signals[SIGNAL_SEND_RTCP] =
318 g_signal_new ("send-rtcp", G_TYPE_FROM_CLASS (klass),
319 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
320 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
321 g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_UINT64);
324 * RTPSession::send-rtcp-full:
325 * @session: the object which received the signal
326 * @max_delay: The maximum delay after which the feedback will not be useful
329 * Requests that the #RTPSession initiate a new RTCP packet as soon as
330 * possible within the requested delay.
332 * Returns: TRUE if the new RTCP packet could be scheduled within the
333 * requested delay, FALSE otherwise.
337 rtp_session_signals[SIGNAL_SEND_RTCP_FULL] =
338 g_signal_new ("send-rtcp-full", G_TYPE_FROM_CLASS (klass),
339 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
340 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
341 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 1, G_TYPE_UINT64);
343 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
344 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
345 "The internal SSRC used for the session (deprecated)",
346 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
348 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
349 g_param_spec_object ("internal-source", "Internal Source",
350 "The internal source element of the session (deprecated)",
351 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
353 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
354 g_param_spec_double ("bandwidth", "Bandwidth",
355 "The bandwidth of the session (0 for auto-discover)",
356 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
357 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
360 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
361 "The fraction of the bandwidth used for RTCP (or as a real fraction of the RTP bandwidth if < 1)",
362 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
363 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
365 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
366 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
367 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
368 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
369 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
372 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
373 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
374 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
375 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
377 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
378 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
379 "The maximum size of the RTCP packets",
380 16, G_MAXINT16, DEFAULT_RTCP_MTU,
381 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
383 g_object_class_install_property (gobject_class, PROP_SDES,
384 g_param_spec_boxed ("sdes", "SDES",
385 "The SDES items of this session",
386 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
388 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
389 g_param_spec_uint ("num-sources", "Num Sources",
390 "The number of sources in the session", 0, G_MAXUINT,
391 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
393 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
394 g_param_spec_uint ("num-active-sources", "Num Active Sources",
395 "The number of active sources in the session", 0, G_MAXUINT,
396 DEFAULT_NUM_ACTIVE_SOURCES,
397 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
401 * Get a GValue Array of all sources in the session.
404 * <title>Getting the #RTPSources of a session
411 * g_object_get (sess, "sources", &arr, NULL);
413 * for (i = 0; i < arr->n_values; i++) {
416 * val = g_value_array_get_nth (arr, i);
417 * source = g_value_get_object (val);
419 * g_value_array_free (arr);
424 g_object_class_install_property (gobject_class, PROP_SOURCES,
425 g_param_spec_boxed ("sources", "Sources",
426 "An array of all known sources in the session",
427 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
429 g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
430 g_param_spec_boolean ("favor-new", "Favor new sources",
431 "Resolve SSRC conflict in favor of new sources", FALSE,
432 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
434 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
435 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
436 "Minimum interval between Regular RTCP packet (in ns)",
437 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
438 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
440 g_object_class_install_property (gobject_class,
441 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
442 g_param_spec_uint64 ("rtcp-feedback-retention-window",
443 "RTCP Feedback retention window",
444 "Duration during which RTCP Feedback packets are retained (in ns)",
445 0, G_MAXUINT64, DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW,
446 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
448 g_object_class_install_property (gobject_class,
449 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
450 g_param_spec_uint ("rtcp-immediate-feedback-threshold",
451 "RTCP Immediate Feedback threshold",
452 "The maximum number of members of a RTP session for which immediate"
454 0, G_MAXUINT, DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
455 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
457 g_object_class_install_property (gobject_class, PROP_PROBATION,
458 g_param_spec_uint ("probation", "Number of probations",
459 "Consecutive packet sequence numbers to accept the source",
460 0, G_MAXUINT, DEFAULT_PROBATION,
461 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
466 * Various session statistics. This property returns a GstStructure
467 * with name application/x-rtp-session-stats with the following fields:
469 * "rtx-drop-count" G_TYPE_UINT The number of retransmission events
470 * dropped (due to bandwidth constraints)
471 * "sent-nack-count" G_TYPE_UINT Number of NACKs sent
472 * "recv-nack-count" G_TYPE_UINT Number of NACKs received
476 g_object_class_install_property (gobject_class, PROP_STATS,
477 g_param_spec_boxed ("stats", "Statistics",
478 "Various statistics", GST_TYPE_STRUCTURE,
479 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
481 klass->get_source_by_ssrc =
482 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
483 klass->send_rtcp = GST_DEBUG_FUNCPTR (rtp_session_send_rtcp);
485 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
489 rtp_session_init (RTPSession * sess)
494 g_mutex_init (&sess->lock);
495 sess->key = g_random_int ();
499 for (i = 0; i < 32; i++) {
501 g_hash_table_new_full (NULL, NULL, NULL,
502 (GDestroyNotify) g_object_unref);
505 rtp_stats_init_defaults (&sess->stats);
506 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
507 rtp_stats_set_min_interval (&sess->stats,
508 (gdouble) DEFAULT_RTCP_MIN_INTERVAL / GST_SECOND);
510 sess->recalc_bandwidth = TRUE;
511 sess->bandwidth = DEFAULT_BANDWIDTH;
512 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
513 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
514 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
516 /* default UDP header length */
517 sess->header_len = 28;
518 sess->mtu = DEFAULT_RTCP_MTU;
520 sess->probation = DEFAULT_PROBATION;
522 /* some default SDES entries */
523 sess->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
525 /* we do not want to leak details like the username or hostname here */
526 str = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
527 gst_structure_set (sess->sdes, "cname", G_TYPE_STRING, str, NULL);
531 /* we do not want to leak the user's real name here */
532 str = g_strdup_printf ("Anon%u", g_random_int ());
533 gst_structure_set (sdes, "name", G_TYPE_STRING, str, NULL);
537 gst_structure_set (sess->sdes, "tool", G_TYPE_STRING, "GStreamer", NULL);
539 /* this is the SSRC we suggest */
540 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
542 sess->first_rtcp = TRUE;
543 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
545 sess->allow_early = TRUE;
546 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
547 sess->rtcp_feedback_retention_window = DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW;
548 sess->rtcp_immediate_feedback_threshold =
549 DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD;
551 sess->last_keyframe_request = GST_CLOCK_TIME_NONE;
553 sess->is_doing_ptp = TRUE;
557 rtp_session_finalize (GObject * object)
562 sess = RTP_SESSION_CAST (object);
564 gst_structure_free (sess->sdes);
566 g_list_free_full (sess->conflicting_addresses,
567 (GDestroyNotify) rtp_conflicting_address_free);
569 for (i = 0; i < 32; i++)
570 g_hash_table_destroy (sess->ssrcs[i]);
572 g_mutex_clear (&sess->lock);
574 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
578 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
580 GValue value = { 0 };
582 g_value_init (&value, RTP_TYPE_SOURCE);
583 g_value_take_object (&value, source);
584 /* copies the value */
585 g_value_array_append (arr, &value);
589 rtp_session_create_sources (RTPSession * sess)
594 RTP_SESSION_LOCK (sess);
595 /* get number of elements in the table */
596 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
597 /* create the result value array */
598 res = g_value_array_new (size);
600 /* and copy all values into the array */
601 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
602 RTP_SESSION_UNLOCK (sess);
607 static GstStructure *
608 rtp_session_create_stats (RTPSession * sess)
612 s = gst_structure_new ("application/x-rtp-session-stats",
613 "rtx-drop-count", G_TYPE_UINT, sess->stats.nacks_dropped,
614 "sent-nack-count", G_TYPE_UINT, sess->stats.nacks_sent,
615 "recv-nack-count", G_TYPE_UINT, sess->stats.nacks_received, NULL);
621 rtp_session_set_property (GObject * object, guint prop_id,
622 const GValue * value, GParamSpec * pspec)
626 sess = RTP_SESSION (object);
629 case PROP_INTERNAL_SSRC:
630 RTP_SESSION_LOCK (sess);
631 sess->suggested_ssrc = g_value_get_uint (value);
632 RTP_SESSION_UNLOCK (sess);
633 if (sess->callbacks.reconfigure)
634 sess->callbacks.reconfigure (sess, sess->reconfigure_user_data);
637 RTP_SESSION_LOCK (sess);
638 sess->bandwidth = g_value_get_double (value);
639 sess->recalc_bandwidth = TRUE;
640 RTP_SESSION_UNLOCK (sess);
642 case PROP_RTCP_FRACTION:
643 RTP_SESSION_LOCK (sess);
644 sess->rtcp_bandwidth = g_value_get_double (value);
645 sess->recalc_bandwidth = TRUE;
646 RTP_SESSION_UNLOCK (sess);
648 case PROP_RTCP_RR_BANDWIDTH:
649 RTP_SESSION_LOCK (sess);
650 sess->rtcp_rr_bandwidth = g_value_get_int (value);
651 sess->recalc_bandwidth = TRUE;
652 RTP_SESSION_UNLOCK (sess);
654 case PROP_RTCP_RS_BANDWIDTH:
655 RTP_SESSION_LOCK (sess);
656 sess->rtcp_rs_bandwidth = g_value_get_int (value);
657 sess->recalc_bandwidth = TRUE;
658 RTP_SESSION_UNLOCK (sess);
661 sess->mtu = g_value_get_uint (value);
664 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
667 sess->favor_new = g_value_get_boolean (value);
669 case PROP_RTCP_MIN_INTERVAL:
670 rtp_stats_set_min_interval (&sess->stats,
671 (gdouble) g_value_get_uint64 (value) / GST_SECOND);
672 /* trigger reconsideration */
673 RTP_SESSION_LOCK (sess);
674 sess->next_rtcp_check_time = 0;
675 RTP_SESSION_UNLOCK (sess);
676 if (sess->callbacks.reconsider)
677 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
679 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
680 sess->rtcp_immediate_feedback_threshold = g_value_get_uint (value);
683 sess->probation = g_value_get_uint (value);
686 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
692 rtp_session_get_property (GObject * object, guint prop_id,
693 GValue * value, GParamSpec * pspec)
697 sess = RTP_SESSION (object);
700 case PROP_INTERNAL_SSRC:
701 g_value_set_uint (value, rtp_session_suggest_ssrc (sess));
703 case PROP_INTERNAL_SOURCE:
704 /* FIXME, return a random source */
705 g_value_set_object (value, NULL);
708 g_value_set_double (value, sess->bandwidth);
710 case PROP_RTCP_FRACTION:
711 g_value_set_double (value, sess->rtcp_bandwidth);
713 case PROP_RTCP_RR_BANDWIDTH:
714 g_value_set_int (value, sess->rtcp_rr_bandwidth);
716 case PROP_RTCP_RS_BANDWIDTH:
717 g_value_set_int (value, sess->rtcp_rs_bandwidth);
720 g_value_set_uint (value, sess->mtu);
723 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
725 case PROP_NUM_SOURCES:
726 g_value_set_uint (value, rtp_session_get_num_sources (sess));
728 case PROP_NUM_ACTIVE_SOURCES:
729 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
732 g_value_take_boxed (value, rtp_session_create_sources (sess));
735 g_value_set_boolean (value, sess->favor_new);
737 case PROP_RTCP_MIN_INTERVAL:
738 g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
740 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
741 g_value_set_uint (value, sess->rtcp_immediate_feedback_threshold);
744 g_value_set_uint (value, sess->probation);
747 g_value_take_boxed (value, rtp_session_create_stats (sess));
750 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
756 on_new_ssrc (RTPSession * sess, RTPSource * source)
758 g_object_ref (source);
759 RTP_SESSION_UNLOCK (sess);
760 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
761 RTP_SESSION_LOCK (sess);
762 g_object_unref (source);
766 on_ssrc_collision (RTPSession * sess, RTPSource * source)
768 g_object_ref (source);
769 RTP_SESSION_UNLOCK (sess);
770 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
772 RTP_SESSION_LOCK (sess);
773 g_object_unref (source);
777 on_ssrc_validated (RTPSession * sess, RTPSource * source)
779 g_object_ref (source);
780 RTP_SESSION_UNLOCK (sess);
781 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
783 RTP_SESSION_LOCK (sess);
784 g_object_unref (source);
788 on_ssrc_active (RTPSession * sess, RTPSource * source)
790 g_object_ref (source);
791 RTP_SESSION_UNLOCK (sess);
792 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
793 RTP_SESSION_LOCK (sess);
794 g_object_unref (source);
798 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
800 g_object_ref (source);
801 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
802 RTP_SESSION_UNLOCK (sess);
803 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
804 RTP_SESSION_LOCK (sess);
805 g_object_unref (source);
809 on_bye_ssrc (RTPSession * sess, RTPSource * source)
811 g_object_ref (source);
812 RTP_SESSION_UNLOCK (sess);
813 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
814 RTP_SESSION_LOCK (sess);
815 g_object_unref (source);
819 on_bye_timeout (RTPSession * sess, RTPSource * source)
821 g_object_ref (source);
822 RTP_SESSION_UNLOCK (sess);
823 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
824 RTP_SESSION_LOCK (sess);
825 g_object_unref (source);
829 on_timeout (RTPSession * sess, RTPSource * source)
831 g_object_ref (source);
832 RTP_SESSION_UNLOCK (sess);
833 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
834 RTP_SESSION_LOCK (sess);
835 g_object_unref (source);
839 on_sender_timeout (RTPSession * sess, RTPSource * source)
841 g_object_ref (source);
842 RTP_SESSION_UNLOCK (sess);
843 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
845 RTP_SESSION_LOCK (sess);
846 g_object_unref (source);
852 * Create a new session object.
854 * Returns: a new #RTPSession. g_object_unref() after usage.
857 rtp_session_new (void)
861 sess = g_object_new (RTP_TYPE_SESSION, NULL);
867 * rtp_session_set_callbacks:
868 * @sess: an #RTPSession
869 * @callbacks: callbacks to configure
870 * @user_data: user data passed in the callbacks
872 * Configure a set of callbacks to be notified of actions.
875 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
878 g_return_if_fail (RTP_IS_SESSION (sess));
880 if (callbacks->process_rtp) {
881 sess->callbacks.process_rtp = callbacks->process_rtp;
882 sess->process_rtp_user_data = user_data;
884 if (callbacks->send_rtp) {
885 sess->callbacks.send_rtp = callbacks->send_rtp;
886 sess->send_rtp_user_data = user_data;
888 if (callbacks->send_rtcp) {
889 sess->callbacks.send_rtcp = callbacks->send_rtcp;
890 sess->send_rtcp_user_data = user_data;
892 if (callbacks->sync_rtcp) {
893 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
894 sess->sync_rtcp_user_data = user_data;
896 if (callbacks->clock_rate) {
897 sess->callbacks.clock_rate = callbacks->clock_rate;
898 sess->clock_rate_user_data = user_data;
900 if (callbacks->reconsider) {
901 sess->callbacks.reconsider = callbacks->reconsider;
902 sess->reconsider_user_data = user_data;
904 if (callbacks->request_key_unit) {
905 sess->callbacks.request_key_unit = callbacks->request_key_unit;
906 sess->request_key_unit_user_data = user_data;
908 if (callbacks->request_time) {
909 sess->callbacks.request_time = callbacks->request_time;
910 sess->request_time_user_data = user_data;
912 if (callbacks->notify_nack) {
913 sess->callbacks.notify_nack = callbacks->notify_nack;
914 sess->notify_nack_user_data = user_data;
916 if (callbacks->reconfigure) {
917 sess->callbacks.reconfigure = callbacks->reconfigure;
918 sess->reconfigure_user_data = user_data;
923 * rtp_session_set_process_rtp_callback:
924 * @sess: an #RTPSession
925 * @callback: callback to set
926 * @user_data: user data passed in the callback
928 * Configure only the process_rtp callback to be notified of the process_rtp action.
931 rtp_session_set_process_rtp_callback (RTPSession * sess,
932 RTPSessionProcessRTP callback, gpointer user_data)
934 g_return_if_fail (RTP_IS_SESSION (sess));
936 sess->callbacks.process_rtp = callback;
937 sess->process_rtp_user_data = user_data;
941 * rtp_session_set_send_rtp_callback:
942 * @sess: an #RTPSession
943 * @callback: callback to set
944 * @user_data: user data passed in the callback
946 * Configure only the send_rtp callback to be notified of the send_rtp action.
949 rtp_session_set_send_rtp_callback (RTPSession * sess,
950 RTPSessionSendRTP callback, gpointer user_data)
952 g_return_if_fail (RTP_IS_SESSION (sess));
954 sess->callbacks.send_rtp = callback;
955 sess->send_rtp_user_data = user_data;
959 * rtp_session_set_send_rtcp_callback:
960 * @sess: an #RTPSession
961 * @callback: callback to set
962 * @user_data: user data passed in the callback
964 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
967 rtp_session_set_send_rtcp_callback (RTPSession * sess,
968 RTPSessionSendRTCP callback, gpointer user_data)
970 g_return_if_fail (RTP_IS_SESSION (sess));
972 sess->callbacks.send_rtcp = callback;
973 sess->send_rtcp_user_data = user_data;
977 * rtp_session_set_sync_rtcp_callback:
978 * @sess: an #RTPSession
979 * @callback: callback to set
980 * @user_data: user data passed in the callback
982 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
985 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
986 RTPSessionSyncRTCP callback, gpointer user_data)
988 g_return_if_fail (RTP_IS_SESSION (sess));
990 sess->callbacks.sync_rtcp = callback;
991 sess->sync_rtcp_user_data = user_data;
995 * rtp_session_set_clock_rate_callback:
996 * @sess: an #RTPSession
997 * @callback: callback to set
998 * @user_data: user data passed in the callback
1000 * Configure only the clock_rate callback to be notified of the clock_rate action.
1003 rtp_session_set_clock_rate_callback (RTPSession * sess,
1004 RTPSessionClockRate callback, gpointer user_data)
1006 g_return_if_fail (RTP_IS_SESSION (sess));
1008 sess->callbacks.clock_rate = callback;
1009 sess->clock_rate_user_data = user_data;
1013 * rtp_session_set_reconsider_callback:
1014 * @sess: an #RTPSession
1015 * @callback: callback to set
1016 * @user_data: user data passed in the callback
1018 * Configure only the reconsider callback to be notified of the reconsider action.
1021 rtp_session_set_reconsider_callback (RTPSession * sess,
1022 RTPSessionReconsider callback, gpointer user_data)
1024 g_return_if_fail (RTP_IS_SESSION (sess));
1026 sess->callbacks.reconsider = callback;
1027 sess->reconsider_user_data = user_data;
1031 * rtp_session_set_request_time_callback:
1032 * @sess: an #RTPSession
1033 * @callback: callback to set
1034 * @user_data: user data passed in the callback
1036 * Configure only the request_time callback
1039 rtp_session_set_request_time_callback (RTPSession * sess,
1040 RTPSessionRequestTime callback, gpointer user_data)
1042 g_return_if_fail (RTP_IS_SESSION (sess));
1044 sess->callbacks.request_time = callback;
1045 sess->request_time_user_data = user_data;
1049 * rtp_session_set_bandwidth:
1050 * @sess: an #RTPSession
1051 * @bandwidth: the bandwidth allocated
1053 * Set the session bandwidth in bytes per second.
1056 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
1058 g_return_if_fail (RTP_IS_SESSION (sess));
1060 RTP_SESSION_LOCK (sess);
1061 sess->stats.bandwidth = bandwidth;
1062 RTP_SESSION_UNLOCK (sess);
1066 * rtp_session_get_bandwidth:
1067 * @sess: an #RTPSession
1069 * Get the session bandwidth.
1071 * Returns: the session bandwidth.
1074 rtp_session_get_bandwidth (RTPSession * sess)
1078 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1080 RTP_SESSION_LOCK (sess);
1081 result = sess->stats.bandwidth;
1082 RTP_SESSION_UNLOCK (sess);
1088 * rtp_session_set_rtcp_fraction:
1089 * @sess: an #RTPSession
1090 * @bandwidth: the RTCP bandwidth
1092 * Set the bandwidth in bytes per second that should be used for RTCP
1096 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
1098 g_return_if_fail (RTP_IS_SESSION (sess));
1100 RTP_SESSION_LOCK (sess);
1101 sess->stats.rtcp_bandwidth = bandwidth;
1102 RTP_SESSION_UNLOCK (sess);
1106 * rtp_session_get_rtcp_fraction:
1107 * @sess: an #RTPSession
1109 * Get the session bandwidth used for RTCP.
1111 * Returns: The bandwidth used for RTCP messages.
1114 rtp_session_get_rtcp_fraction (RTPSession * sess)
1118 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
1120 RTP_SESSION_LOCK (sess);
1121 result = sess->stats.rtcp_bandwidth;
1122 RTP_SESSION_UNLOCK (sess);
1128 * rtp_session_get_sdes_struct:
1129 * @sess: an #RTSPSession
1131 * Get the SDES data as a #GstStructure
1133 * Returns: a GstStructure with SDES items for @sess. This function returns a
1134 * copy of the SDES structure, use gst_structure_free() after usage.
1137 rtp_session_get_sdes_struct (RTPSession * sess)
1139 GstStructure *result = NULL;
1141 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1143 RTP_SESSION_LOCK (sess);
1145 result = gst_structure_copy (sess->sdes);
1146 RTP_SESSION_UNLOCK (sess);
1152 * rtp_session_set_sdes_struct:
1153 * @sess: an #RTSPSession
1154 * @sdes: a #GstStructure
1156 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1159 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
1161 g_return_if_fail (sdes);
1162 g_return_if_fail (RTP_IS_SESSION (sess));
1164 RTP_SESSION_LOCK (sess);
1166 gst_structure_free (sess->sdes);
1167 sess->sdes = gst_structure_copy (sdes);
1168 RTP_SESSION_UNLOCK (sess);
1171 static GstFlowReturn
1172 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1174 GstFlowReturn result = GST_FLOW_OK;
1176 if (source->internal) {
1177 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1179 RTP_SESSION_UNLOCK (session);
1181 if (session->callbacks.send_rtp)
1183 session->callbacks.send_rtp (session, source, data,
1184 session->send_rtp_user_data);
1186 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1189 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1190 RTP_SESSION_UNLOCK (session);
1192 if (session->callbacks.process_rtp)
1194 session->callbacks.process_rtp (session, source,
1195 GST_BUFFER_CAST (data), session->process_rtp_user_data);
1197 gst_buffer_unref (GST_BUFFER_CAST (data));
1199 RTP_SESSION_LOCK (session);
1205 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1209 RTP_SESSION_UNLOCK (session);
1211 if (session->callbacks.clock_rate)
1213 session->callbacks.clock_rate (session, pt,
1214 session->clock_rate_user_data);
1218 RTP_SESSION_LOCK (session);
1220 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1225 static RTPSourceCallbacks callbacks = {
1226 (RTPSourcePushRTP) source_push_rtp,
1227 (RTPSourceClockRate) source_clock_rate,
1232 * rtp_session_find_conflicting_address:
1233 * @session: The session the packet came in
1234 * @address: address to check for
1235 * @time: The time when the packet that is possibly in conflict arrived
1237 * Checks if an address which has a conflict is already known. If it is
1238 * a known conflict, remember the time
1240 * Returns: TRUE if it was a known conflict, FALSE otherwise
1243 rtp_session_find_conflicting_address (RTPSession * session,
1244 GSocketAddress * address, GstClockTime time)
1246 return find_conflicting_address (session->conflicting_addresses, address,
1251 * rtp_session_add_conflicting_address:
1252 * @session: The session the packet came in
1253 * @address: address to remember
1254 * @time: The time when the packet that is in conflict arrived
1256 * Adds a new conflict address
1259 rtp_session_add_conflicting_address (RTPSession * sess,
1260 GSocketAddress * address, GstClockTime time)
1262 sess->conflicting_addresses =
1263 add_conflicting_address (sess->conflicting_addresses, address, time);
1268 check_collision (RTPSession * sess, RTPSource * source,
1269 RTPPacketInfo * pinfo, gboolean rtp)
1273 /* If we have no pinfo address, we can't do collision checking */
1274 if (!pinfo->address)
1277 ssrc = rtp_source_get_ssrc (source);
1279 if (!source->internal) {
1280 GSocketAddress *from;
1282 /* This is not our local source, but lets check if two remote
1285 from = source->rtp_from;
1287 from = source->rtcp_from;
1291 if (__g_socket_address_equal (from, pinfo->address)) {
1292 /* Address is the same */
1295 GST_LOG ("we have a third-party collision or loop ssrc:%x", ssrc);
1296 if (sess->favor_new) {
1297 if (rtp_source_find_conflicting_address (source,
1298 pinfo->address, pinfo->current_time)) {
1301 buf1 = __g_socket_address_to_string (pinfo->address);
1302 GST_LOG ("Known conflict on %x for %s, dropping packet", ssrc,
1310 /* Current address is not a known conflict, lets assume this is
1311 * a new source. Save old address in possible conflict list
1313 rtp_source_add_conflicting_address (source, from,
1314 pinfo->current_time);
1316 buf1 = __g_socket_address_to_string (from);
1317 buf2 = __g_socket_address_to_string (pinfo->address);
1319 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1320 " saving old as known conflict", ssrc, buf1, buf2);
1323 rtp_source_set_rtp_from (source, pinfo->address);
1325 rtp_source_set_rtcp_from (source, pinfo->address);
1333 /* Don't need to save old addresses, we ignore new sources */
1338 /* We don't already have a from address for RTP, just set it */
1340 rtp_source_set_rtp_from (source, pinfo->address);
1342 rtp_source_set_rtcp_from (source, pinfo->address);
1346 /* FIXME: Log 3rd party collision somehow
1347 * Maybe should be done in upper layer, only the SDES can tell us
1348 * if its a collision or a loop
1351 /* This is sending with our ssrc, is it an address we already know */
1352 if (rtp_session_find_conflicting_address (sess, pinfo->address,
1353 pinfo->current_time)) {
1354 /* Its a known conflict, its probably a loop, not a collision
1355 * lets just drop the incoming packet
1357 GST_DEBUG ("Our packets are being looped back to us, dropping");
1359 /* Its a new collision, lets change our SSRC */
1360 rtp_session_add_conflicting_address (sess, pinfo->address,
1361 pinfo->current_time);
1363 GST_DEBUG ("Collision for SSRC %x", ssrc);
1364 /* mark the source BYE */
1365 rtp_source_mark_bye (source, "SSRC Collision");
1366 /* if we were suggesting this SSRC, change to something else */
1367 if (sess->suggested_ssrc == ssrc)
1368 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
1370 on_ssrc_collision (sess, source);
1372 rtp_session_schedule_bye_locked (sess, pinfo->current_time);
1381 gboolean is_doing_ptp;
1382 GSocketAddress *new_addr;
1385 /* check if the two given ip addr are the same (do not care about the port) */
1387 ip_addr_equal (GSocketAddress * a, GSocketAddress * b)
1390 g_inet_address_equal (g_inet_socket_address_get_address
1391 (G_INET_SOCKET_ADDRESS (a)),
1392 g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (b)));
1396 compare_rtp_source_addr (const gchar * key, RTPSource * source,
1397 CompareAddrData * data)
1399 /* only compare ip addr of remote sources which are also not closing */
1400 if (!source->internal && !source->closing && source->rtp_from) {
1401 /* look for the first rtp source */
1402 if (!data->new_addr)
1403 data->new_addr = source->rtp_from;
1404 /* compare current ip addr with the first one */
1406 data->is_doing_ptp &= ip_addr_equal (data->new_addr, source->rtp_from);
1411 compare_rtcp_source_addr (const gchar * key, RTPSource * source,
1412 CompareAddrData * data)
1414 /* only compare ip addr of remote sources which are also not closing */
1415 if (!source->internal && !source->closing && source->rtcp_from) {
1416 /* look for the first rtcp source */
1417 if (!data->new_addr)
1418 data->new_addr = source->rtcp_from;
1420 /* compare current ip addr with the first one */
1421 data->is_doing_ptp &= ip_addr_equal (data->new_addr, source->rtcp_from);
1425 /* loop over our non-internal source to know if the session
1426 * is doing point-to-point */
1428 session_update_ptp (RTPSession * sess)
1430 /* to know if the session is doing point to point, the ip addr
1431 * of each non-internal (=remotes) source have to be compared
1434 gboolean is_doing_rtp_ptp;
1435 gboolean is_doing_rtcp_ptp;
1436 CompareAddrData data;
1438 /* compare the first remote source's ip addr that receive rtp packets
1439 * with other remote rtp source.
1440 * it's enough because the session just needs to know if they are all
1443 data.is_doing_ptp = TRUE;
1444 data.new_addr = NULL;
1445 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
1446 (GHFunc) compare_rtp_source_addr, (gpointer) & data);
1447 is_doing_rtp_ptp = data.is_doing_ptp;
1449 /* same but about rtcp */
1450 data.is_doing_ptp = TRUE;
1451 data.new_addr = NULL;
1452 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
1453 (GHFunc) compare_rtcp_source_addr, (gpointer) & data);
1454 is_doing_rtcp_ptp = data.is_doing_ptp;
1456 /* the session is doing point-to-point if all rtp remote have the same
1457 * ip addr and if all rtcp remote sources have the same ip addr */
1458 sess->is_doing_ptp = is_doing_rtp_ptp && is_doing_rtcp_ptp;
1460 GST_DEBUG ("doing point-to-point: %d", sess->is_doing_ptp);
1464 add_source (RTPSession * sess, RTPSource * src)
1466 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1467 GINT_TO_POINTER (src->ssrc), src);
1468 /* report the new source ASAP */
1469 src->generation = sess->generation;
1470 /* we have one more source now */
1471 sess->total_sources++;
1472 if (RTP_SOURCE_IS_ACTIVE (src))
1473 sess->stats.active_sources++;
1474 if (src->internal) {
1475 sess->stats.internal_sources++;
1476 if (sess->suggested_ssrc != src->ssrc)
1477 sess->suggested_ssrc = src->ssrc;
1480 /* update point-to-point status */
1482 session_update_ptp (sess);
1486 find_source (RTPSession * sess, guint32 ssrc)
1488 return g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1489 GINT_TO_POINTER (ssrc));
1492 /* must be called with the session lock, the returned source needs to be
1493 * unreffed after usage. */
1495 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1496 RTPPacketInfo * pinfo, gboolean rtp)
1500 source = find_source (sess, ssrc);
1501 if (source == NULL) {
1502 /* make new Source in probation and insert */
1503 source = rtp_source_new (ssrc);
1505 GST_DEBUG ("creating new source %08x %p", ssrc, source);
1507 /* for RTP packets we need to set the source in probation. Receiving RTCP
1508 * packets of an SSRC, on the other hand, is a strong indication that we
1509 * are dealing with a valid source. */
1511 g_object_set (source, "probation", sess->probation, NULL);
1513 g_object_set (source, "probation", 0, NULL);
1515 /* store from address, if any */
1516 if (pinfo->address) {
1518 rtp_source_set_rtp_from (source, pinfo->address);
1520 rtp_source_set_rtcp_from (source, pinfo->address);
1523 /* configure a callback on the source */
1524 rtp_source_set_callbacks (source, &callbacks, sess);
1526 add_source (sess, source);
1530 /* check for collision, this updates the address when not previously set */
1531 if (check_collision (sess, source, pinfo, rtp)) {
1534 /* Receiving RTCP packets of an SSRC is a strong indication that we
1535 * are dealing with a valid source. */
1537 g_object_set (source, "probation", 0, NULL);
1539 /* update last activity */
1540 source->last_activity = pinfo->current_time;
1542 source->last_rtp_activity = pinfo->current_time;
1543 g_object_ref (source);
1548 /* must be called with the session lock, the returned source needs to be
1549 * unreffed after usage. */
1551 obtain_internal_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1552 GstClockTime current_time)
1556 source = find_source (sess, ssrc);
1557 if (source == NULL) {
1558 /* make new internal Source and insert */
1559 source = rtp_source_new (ssrc);
1561 GST_DEBUG ("creating new internal source %08x %p", ssrc, source);
1563 source->validated = TRUE;
1564 source->internal = TRUE;
1565 source->probation = FALSE;
1566 rtp_source_set_sdes_struct (source, gst_structure_copy (sess->sdes));
1567 rtp_source_set_callbacks (source, &callbacks, sess);
1569 add_source (sess, source);
1574 /* update last activity */
1575 if (current_time != GST_CLOCK_TIME_NONE) {
1576 source->last_activity = current_time;
1577 source->last_rtp_activity = current_time;
1579 g_object_ref (source);
1585 * rtp_session_suggest_ssrc:
1586 * @sess: a #RTPSession
1588 * Suggest an unused SSRC in @sess.
1590 * Returns: a free unused SSRC
1593 rtp_session_suggest_ssrc (RTPSession * sess)
1597 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1599 RTP_SESSION_LOCK (sess);
1600 result = sess->suggested_ssrc;
1601 RTP_SESSION_UNLOCK (sess);
1607 * rtp_session_add_source:
1608 * @sess: a #RTPSession
1609 * @src: #RTPSource to add
1611 * Add @src to @session.
1613 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1614 * existed in the session.
1617 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1619 gboolean result = FALSE;
1622 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1623 g_return_val_if_fail (src != NULL, FALSE);
1625 RTP_SESSION_LOCK (sess);
1626 find = find_source (sess, src->ssrc);
1628 add_source (sess, src);
1631 RTP_SESSION_UNLOCK (sess);
1637 * rtp_session_get_num_sources:
1638 * @sess: an #RTPSession
1640 * Get the number of sources in @sess.
1642 * Returns: The number of sources in @sess.
1645 rtp_session_get_num_sources (RTPSession * sess)
1649 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1651 RTP_SESSION_LOCK (sess);
1652 result = sess->total_sources;
1653 RTP_SESSION_UNLOCK (sess);
1659 * rtp_session_get_num_active_sources:
1660 * @sess: an #RTPSession
1662 * Get the number of active sources in @sess. A source is considered active when
1663 * it has been validated and has not yet received a BYE RTCP message.
1665 * Returns: The number of active sources in @sess.
1668 rtp_session_get_num_active_sources (RTPSession * sess)
1672 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1674 RTP_SESSION_LOCK (sess);
1675 result = sess->stats.active_sources;
1676 RTP_SESSION_UNLOCK (sess);
1682 * rtp_session_get_source_by_ssrc:
1683 * @sess: an #RTPSession
1686 * Find the source with @ssrc in @sess.
1688 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1689 * g_object_unref() after usage.
1692 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1696 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1698 RTP_SESSION_LOCK (sess);
1699 result = find_source (sess, ssrc);
1701 g_object_ref (result);
1702 RTP_SESSION_UNLOCK (sess);
1707 /* should be called with the SESSION lock */
1709 rtp_session_create_new_ssrc (RTPSession * sess)
1714 ssrc = g_random_int ();
1716 /* see if it exists in the session, we're done if it doesn't */
1717 if (find_source (sess, ssrc) == NULL)
1725 * rtp_session_create_source:
1726 * @sess: an #RTPSession
1728 * Create an #RTPSource for use in @sess. This function will create a source
1729 * with an ssrc that is currently not used by any participants in the session.
1731 * Returns: an #RTPSource.
1734 rtp_session_create_source (RTPSession * sess)
1739 RTP_SESSION_LOCK (sess);
1740 ssrc = rtp_session_create_new_ssrc (sess);
1741 source = rtp_source_new (ssrc);
1742 rtp_source_set_callbacks (source, &callbacks, sess);
1743 /* we need an additional ref for the source in the hashtable */
1744 g_object_ref (source);
1745 add_source (sess, source);
1746 RTP_SESSION_UNLOCK (sess);
1752 update_packet (GstBuffer ** buffer, guint idx, RTPPacketInfo * pinfo)
1754 GstNetAddressMeta *meta;
1756 /* get packet size including header overhead */
1757 pinfo->bytes += gst_buffer_get_size (*buffer) + pinfo->header_len;
1761 GstRTPBuffer rtp = { NULL };
1763 if (!gst_rtp_buffer_map (*buffer, GST_MAP_READ, &rtp))
1764 goto invalid_packet;
1766 pinfo->payload_len += gst_rtp_buffer_get_payload_len (&rtp);
1770 /* only keep info for first buffer */
1771 pinfo->ssrc = gst_rtp_buffer_get_ssrc (&rtp);
1772 pinfo->seqnum = gst_rtp_buffer_get_seq (&rtp);
1773 pinfo->pt = gst_rtp_buffer_get_payload_type (&rtp);
1774 pinfo->rtptime = gst_rtp_buffer_get_timestamp (&rtp);
1775 /* copy available csrc */
1776 pinfo->csrc_count = gst_rtp_buffer_get_csrc_count (&rtp);
1777 for (i = 0; i < pinfo->csrc_count; i++)
1778 pinfo->csrcs[i] = gst_rtp_buffer_get_csrc (&rtp, i);
1780 gst_rtp_buffer_unmap (&rtp);
1784 /* for netbuffer we can store the IP address to check for collisions */
1785 meta = gst_buffer_get_net_address_meta (*buffer);
1787 g_object_unref (pinfo->address);
1789 pinfo->address = G_SOCKET_ADDRESS (g_object_ref (meta->addr));
1791 pinfo->address = NULL;
1799 GST_DEBUG ("invalid RTP packet received");
1804 /* update the RTPPacketInfo structure with the current time and other bits
1805 * about the current buffer we are handling.
1806 * This function is typically called when a validated packet is received.
1807 * This function should be called with the SESSION_LOCK
1810 update_packet_info (RTPSession * sess, RTPPacketInfo * pinfo,
1811 gboolean send, gboolean rtp, gboolean is_list, gpointer data,
1812 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
1818 pinfo->is_list = is_list;
1820 pinfo->current_time = current_time;
1821 pinfo->running_time = running_time;
1822 pinfo->ntpnstime = ntpnstime;
1823 pinfo->header_len = sess->header_len;
1825 pinfo->payload_len = 0;
1829 GstBufferList *list = GST_BUFFER_LIST_CAST (data);
1831 gst_buffer_list_foreach (list, (GstBufferListFunc) update_packet,
1834 GstBuffer *buffer = GST_BUFFER_CAST (data);
1835 res = update_packet (&buffer, 0, pinfo);
1841 clean_packet_info (RTPPacketInfo * pinfo)
1844 g_object_unref (pinfo->address);
1846 gst_mini_object_unref (pinfo->data);
1852 source_update_active (RTPSession * sess, RTPSource * source,
1853 gboolean prevactive)
1855 gboolean active = RTP_SOURCE_IS_ACTIVE (source);
1856 guint32 ssrc = source->ssrc;
1858 if (prevactive == active)
1862 sess->stats.active_sources++;
1863 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1864 sess->stats.active_sources);
1866 sess->stats.active_sources--;
1867 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
1868 sess->stats.active_sources);
1874 source_update_sender (RTPSession * sess, RTPSource * source,
1875 gboolean prevsender)
1877 gboolean sender = RTP_SOURCE_IS_SENDER (source);
1878 guint32 ssrc = source->ssrc;
1880 if (prevsender == sender)
1884 sess->stats.sender_sources++;
1885 if (source->internal)
1886 sess->stats.internal_sender_sources++;
1887 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1888 sess->stats.sender_sources);
1890 sess->stats.sender_sources--;
1891 if (source->internal)
1892 sess->stats.internal_sender_sources--;
1893 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
1894 sess->stats.sender_sources);
1900 * rtp_session_process_rtp:
1901 * @sess: and #RTPSession
1902 * @buffer: an RTP buffer
1903 * @current_time: the current system time
1904 * @running_time: the running_time of @buffer
1906 * Process an RTP buffer in the session manager. This function takes ownership
1909 * Returns: a #GstFlowReturn.
1912 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1913 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
1915 GstFlowReturn result;
1919 gboolean prevsender, prevactive;
1920 RTPPacketInfo pinfo = { 0, };
1923 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1924 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1926 RTP_SESSION_LOCK (sess);
1928 /* update pinfo stats */
1929 if (!update_packet_info (sess, &pinfo, FALSE, TRUE, FALSE, buffer,
1930 current_time, running_time, ntpnstime)) {
1931 GST_DEBUG ("invalid RTP packet received");
1932 RTP_SESSION_UNLOCK (sess);
1933 return rtp_session_process_rtcp (sess, buffer, current_time, ntpnstime);
1938 source = obtain_source (sess, ssrc, &created, &pinfo, TRUE);
1942 prevsender = RTP_SOURCE_IS_SENDER (source);
1943 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1944 oldrate = source->bitrate;
1946 /* let source process the packet */
1947 result = rtp_source_process_rtp (source, &pinfo);
1949 /* source became active */
1950 if (source_update_active (sess, source, prevactive))
1951 on_ssrc_validated (sess, source);
1953 source_update_sender (sess, source, prevsender);
1955 if (oldrate != source->bitrate)
1956 sess->recalc_bandwidth = TRUE;
1959 on_new_ssrc (sess, source);
1961 if (source->validated) {
1965 /* for validated sources, we add the CSRCs as well */
1966 for (i = 0; i < pinfo.csrc_count; i++) {
1968 RTPSource *csrc_src;
1970 csrc = pinfo.csrcs[i];
1973 csrc_src = obtain_source (sess, csrc, &created, &pinfo, TRUE);
1978 GST_DEBUG ("created new CSRC: %08x", csrc);
1979 rtp_source_set_as_csrc (csrc_src);
1980 source_update_active (sess, csrc_src, FALSE);
1981 on_new_ssrc (sess, csrc_src);
1983 g_object_unref (csrc_src);
1986 g_object_unref (source);
1988 RTP_SESSION_UNLOCK (sess);
1990 clean_packet_info (&pinfo);
1997 RTP_SESSION_UNLOCK (sess);
1998 clean_packet_info (&pinfo);
1999 GST_DEBUG ("ignoring packet because its collisioning");
2005 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
2006 GstRTCPPacket * packet, RTPPacketInfo * pinfo)
2010 count = gst_rtcp_packet_get_rb_count (packet);
2011 for (i = 0; i < count; i++) {
2012 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
2013 guint8 fractionlost;
2017 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
2018 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2020 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
2022 /* find our own source */
2023 src = find_source (sess, ssrc);
2027 if (src->internal && RTP_SOURCE_IS_ACTIVE (src)) {
2028 /* only deal with report blocks for our session, we update the stats of
2029 * the sender of the RTCP message. We could also compare our stats against
2030 * the other sender to see if we are better or worse. */
2031 /* FIXME, need to keep track who the RB block is from */
2032 rtp_source_process_rb (source, pinfo->ntpnstime, fractionlost,
2033 packetslost, exthighestseq, jitter, lsr, dlsr);
2036 on_ssrc_active (sess, source);
2039 /* A Sender report contains statistics about how the sender is doing. This
2040 * includes timing informataion such as the relation between RTP and NTP
2041 * timestamps and the number of packets/bytes it sent to us.
2043 * In this report is also included a set of report blocks related to how this
2044 * sender is receiving data (in case we (or somebody else) is also sending stuff
2045 * to it). This info includes the packet loss, jitter and seqnum. It also
2046 * contains information to calculate the round trip time (LSR/DLSR).
2049 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
2050 RTPPacketInfo * pinfo, gboolean * do_sync)
2052 guint32 senderssrc, rtptime, packet_count, octet_count;
2055 gboolean created, prevsender;
2057 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
2058 &packet_count, &octet_count);
2060 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
2061 senderssrc, GST_TIME_ARGS (pinfo->current_time));
2063 source = obtain_source (sess, senderssrc, &created, pinfo, FALSE);
2067 /* skip non-bye packets for sources that are marked BYE */
2068 if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
2071 /* don't try to do lip-sync for sources that sent a BYE */
2072 if (RTP_SOURCE_IS_MARKED_BYE (source))
2077 prevsender = RTP_SOURCE_IS_SENDER (source);
2079 /* first update the source */
2080 rtp_source_process_sr (source, pinfo->current_time, ntptime, rtptime,
2081 packet_count, octet_count);
2083 source_update_sender (sess, source, prevsender);
2086 on_new_ssrc (sess, source);
2088 rtp_session_process_rb (sess, source, packet, pinfo);
2091 g_object_unref (source);
2094 /* A receiver report contains statistics about how a receiver is doing. It
2095 * includes stuff like packet loss, jitter and the seqnum it received last. It
2096 * also contains info to calculate the round trip time.
2098 * We are only interested in how the sender of this report is doing wrt to us.
2101 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
2102 RTPPacketInfo * pinfo)
2108 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
2110 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
2112 source = obtain_source (sess, senderssrc, &created, pinfo, FALSE);
2116 /* skip non-bye packets for sources that are marked BYE */
2117 if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
2121 on_new_ssrc (sess, source);
2123 rtp_session_process_rb (sess, source, packet, pinfo);
2126 g_object_unref (source);
2129 /* Get SDES items and store them in the SSRC */
2131 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
2132 RTPPacketInfo * pinfo)
2135 gboolean more_items, more_entries;
2137 items = gst_rtcp_packet_sdes_get_item_count (packet);
2138 GST_DEBUG ("got SDES packet with %d items", items);
2140 more_items = gst_rtcp_packet_sdes_first_item (packet);
2142 while (more_items) {
2144 gboolean changed, created, prevactive;
2148 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
2150 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
2154 /* find src, no probation when dealing with RTCP */
2155 source = obtain_source (sess, ssrc, &created, pinfo, FALSE);
2159 /* skip non-bye packets for sources that are marked BYE */
2160 if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
2163 sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
2165 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
2167 while (more_entries) {
2168 GstRTCPSDESType type;
2174 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
2176 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
2179 if (type == GST_RTCP_SDES_PRIV) {
2180 name = g_strndup ((const gchar *) &data[1], data[0]);
2182 data += data[0] + 1;
2184 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
2187 value = g_strndup ((const gchar *) data, len);
2189 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
2194 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
2198 /* takes ownership of sdes */
2199 changed = rtp_source_set_sdes_struct (source, sdes);
2201 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2202 source->validated = TRUE;
2205 on_new_ssrc (sess, source);
2207 /* source became active */
2208 if (source_update_active (sess, source, prevactive))
2209 on_ssrc_validated (sess, source);
2212 on_ssrc_sdes (sess, source);
2215 g_object_unref (source);
2217 more_items = gst_rtcp_packet_sdes_next_item (packet);
2222 /* BYE is sent when a client leaves the session
2225 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
2226 RTPPacketInfo * pinfo)
2230 gboolean reconsider = FALSE;
2232 reason = gst_rtcp_packet_bye_get_reason (packet);
2233 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
2235 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
2236 for (i = 0; i < count; i++) {
2239 gboolean created, prevactive, prevsender;
2240 guint pmembers, members;
2242 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
2243 GST_DEBUG ("SSRC: %08x", ssrc);
2245 /* find src and mark bye, no probation when dealing with RTCP */
2246 source = obtain_source (sess, ssrc, &created, pinfo, FALSE);
2250 if (source->internal) {
2251 /* our own source, something weird with this packet */
2252 g_object_unref (source);
2256 /* store time for when we need to time out this source */
2257 source->bye_time = pinfo->current_time;
2259 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2260 prevsender = RTP_SOURCE_IS_SENDER (source);
2262 /* mark the source BYE */
2263 rtp_source_mark_bye (source, reason);
2265 pmembers = sess->stats.active_sources;
2267 source_update_active (sess, source, prevactive);
2268 source_update_sender (sess, source, prevsender);
2270 members = sess->stats.active_sources;
2272 if (!sess->scheduled_bye && members < pmembers) {
2273 /* some members went away since the previous timeout estimate.
2274 * Perform reverse reconsideration but only when we are not scheduling a
2276 if (sess->next_rtcp_check_time != GST_CLOCK_TIME_NONE &&
2277 pinfo->current_time < sess->next_rtcp_check_time) {
2278 GstClockTime time_remaining;
2280 time_remaining = sess->next_rtcp_check_time - pinfo->current_time;
2281 sess->next_rtcp_check_time =
2282 gst_util_uint64_scale (time_remaining, members, pmembers);
2284 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
2285 GST_TIME_ARGS (sess->next_rtcp_check_time));
2287 sess->next_rtcp_check_time += pinfo->current_time;
2289 /* mark pending reconsider. We only want to signal the reconsideration
2290 * once after we handled all the source in the bye packet */
2296 on_new_ssrc (sess, source);
2298 on_bye_ssrc (sess, source);
2300 g_object_unref (source);
2303 RTP_SESSION_UNLOCK (sess);
2304 /* notify app of reconsideration */
2305 if (sess->callbacks.reconsider)
2306 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2307 RTP_SESSION_LOCK (sess);
2313 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
2314 RTPPacketInfo * pinfo)
2316 GST_DEBUG ("received APP");
2320 rtp_session_request_local_key_unit (RTPSession * sess, RTPSource * src,
2321 gboolean fir, GstClockTime current_time)
2323 guint32 round_trip = 0;
2325 rtp_source_get_last_rb (src, NULL, NULL, NULL, NULL, NULL, NULL, &round_trip);
2327 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE && round_trip) {
2328 GstClockTime round_trip_in_ns = gst_util_uint64_scale (round_trip,
2331 if (current_time - sess->last_keyframe_request < 2 * round_trip_in_ns) {
2332 GST_DEBUG ("Ignoring %s request because one was send without one "
2333 "RTT (%" GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")",
2334 fir ? "FIR" : "PLI",
2335 GST_TIME_ARGS (current_time - sess->last_keyframe_request),
2336 GST_TIME_ARGS (round_trip_in_ns));;
2341 sess->last_keyframe_request = current_time;
2343 GST_LOG ("received %s request from %X %p(%p)", fir ? "FIR" : "PLI",
2344 rtp_source_get_ssrc (src), sess->callbacks.process_rtp,
2345 sess->callbacks.request_key_unit);
2347 RTP_SESSION_UNLOCK (sess);
2348 sess->callbacks.request_key_unit (sess, fir,
2349 sess->request_key_unit_user_data);
2350 RTP_SESSION_LOCK (sess);
2356 rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
2357 guint32 media_ssrc, GstClockTime current_time)
2361 if (!sess->callbacks.request_key_unit)
2364 src = find_source (sess, sender_ssrc);
2368 rtp_session_request_local_key_unit (sess, src, FALSE, current_time);
2372 rtp_session_process_fir (RTPSession * sess, guint32 sender_ssrc,
2373 guint8 * fci_data, guint fci_length, GstClockTime current_time)
2378 gboolean our_request = FALSE;
2380 if (!sess->callbacks.request_key_unit)
2386 src = find_source (sess, sender_ssrc);
2388 /* Hack because Google fails to set the sender_ssrc correctly */
2389 if (!src && sender_ssrc == 1) {
2390 GHashTableIter iter;
2392 /* we can't find the source if there are multiple */
2393 if (sess->stats.sender_sources > sess->stats.internal_sender_sources + 1)
2396 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
2397 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
2398 if (!src->internal && rtp_source_is_sender (src))
2406 for (position = 0; position < fci_length; position += 8) {
2407 guint8 *data = fci_data + position;
2410 ssrc = GST_READ_UINT32_BE (data);
2412 own = find_source (sess, ssrc);
2416 if (own->internal) {
2424 rtp_session_request_local_key_unit (sess, src, TRUE, current_time);
2428 rtp_session_process_nack (RTPSession * sess, guint32 sender_ssrc,
2429 guint32 media_ssrc, guint8 * fci_data, guint fci_length,
2430 GstClockTime current_time)
2432 sess->stats.nacks_received++;
2434 if (!sess->callbacks.notify_nack)
2437 while (fci_length > 0) {
2438 guint16 seqnum, blp;
2440 seqnum = GST_READ_UINT16_BE (fci_data);
2441 blp = GST_READ_UINT16_BE (fci_data + 2);
2443 GST_DEBUG ("NACK #%u, blp %04x, SSRC 0x%08x", seqnum, blp, media_ssrc);
2445 RTP_SESSION_UNLOCK (sess);
2446 sess->callbacks.notify_nack (sess, seqnum, blp, media_ssrc,
2447 sess->notify_nack_user_data);
2448 RTP_SESSION_LOCK (sess);
2456 rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
2457 RTPPacketInfo * pinfo, GstClockTime current_time)
2459 GstRTCPType type = gst_rtcp_packet_get_type (packet);
2460 GstRTCPFBType fbtype = gst_rtcp_packet_fb_get_type (packet);
2461 guint32 sender_ssrc = gst_rtcp_packet_fb_get_sender_ssrc (packet);
2462 guint32 media_ssrc = gst_rtcp_packet_fb_get_media_ssrc (packet);
2463 guint8 *fci_data = gst_rtcp_packet_fb_get_fci (packet);
2464 guint fci_length = 4 * gst_rtcp_packet_fb_get_fci_length (packet);
2467 src = find_source (sess, media_ssrc);
2469 /* skip non-bye packets for sources that are marked BYE */
2470 if (sess->scheduled_bye && src && RTP_SOURCE_IS_MARKED_BYE (src))
2473 GST_DEBUG ("received feedback %d:%d from %08X about %08X with FCI of "
2474 "length %d", type, fbtype, sender_ssrc, media_ssrc, fci_length);
2476 if (g_signal_has_handler_pending (sess,
2477 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0, TRUE)) {
2478 GstBuffer *fci_buffer = NULL;
2480 if (fci_length > 0) {
2481 fci_buffer = gst_buffer_copy_region (packet->rtcp->buffer,
2482 GST_BUFFER_COPY_MEMORY, fci_data - packet->rtcp->map.data,
2484 GST_BUFFER_TIMESTAMP (fci_buffer) = pinfo->running_time;
2487 RTP_SESSION_UNLOCK (sess);
2488 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0,
2489 type, fbtype, sender_ssrc, media_ssrc, fci_buffer);
2490 RTP_SESSION_LOCK (sess);
2493 gst_buffer_unref (fci_buffer);
2496 if (src && sess->rtcp_feedback_retention_window) {
2497 rtp_source_retain_rtcp_packet (src, packet, pinfo->running_time);
2500 if ((src && src->internal) ||
2501 /* PSFB FIR puts the media ssrc inside the FCI */
2502 (type == GST_RTCP_TYPE_PSFB && fbtype == GST_RTCP_PSFB_TYPE_FIR)) {
2504 case GST_RTCP_TYPE_PSFB:
2506 case GST_RTCP_PSFB_TYPE_PLI:
2507 rtp_session_process_pli (sess, sender_ssrc, media_ssrc,
2510 case GST_RTCP_PSFB_TYPE_FIR:
2511 rtp_session_process_fir (sess, sender_ssrc, fci_data, fci_length,
2518 case GST_RTCP_TYPE_RTPFB:
2520 case GST_RTCP_RTPFB_TYPE_NACK:
2521 rtp_session_process_nack (sess, sender_ssrc, media_ssrc,
2522 fci_data, fci_length, current_time);
2534 * rtp_session_process_rtcp:
2535 * @sess: and #RTPSession
2536 * @buffer: an RTCP buffer
2537 * @current_time: the current system time
2538 * @ntpnstime: the current NTP time in nanoseconds
2540 * Process an RTCP buffer in the session manager. This function takes ownership
2543 * Returns: a #GstFlowReturn.
2546 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
2547 GstClockTime current_time, guint64 ntpnstime)
2549 GstRTCPPacket packet;
2550 gboolean more, is_bye = FALSE, do_sync = FALSE;
2551 RTPPacketInfo pinfo = { 0, };
2552 GstFlowReturn result = GST_FLOW_OK;
2553 GstRTCPBuffer rtcp = { NULL, };
2555 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2556 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2558 if (!gst_rtcp_buffer_validate (buffer))
2559 goto invalid_packet;
2561 GST_DEBUG ("received RTCP packet");
2563 RTP_SESSION_LOCK (sess);
2564 /* update pinfo stats */
2565 update_packet_info (sess, &pinfo, FALSE, FALSE, FALSE, buffer, current_time,
2568 /* start processing the compound packet */
2569 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
2570 more = gst_rtcp_buffer_get_first_packet (&rtcp, &packet);
2574 type = gst_rtcp_packet_get_type (&packet);
2577 case GST_RTCP_TYPE_SR:
2578 rtp_session_process_sr (sess, &packet, &pinfo, &do_sync);
2580 case GST_RTCP_TYPE_RR:
2581 rtp_session_process_rr (sess, &packet, &pinfo);
2583 case GST_RTCP_TYPE_SDES:
2584 rtp_session_process_sdes (sess, &packet, &pinfo);
2586 case GST_RTCP_TYPE_BYE:
2588 /* don't try to attempt lip-sync anymore for streams with a BYE */
2590 rtp_session_process_bye (sess, &packet, &pinfo);
2592 case GST_RTCP_TYPE_APP:
2593 rtp_session_process_app (sess, &packet, &pinfo);
2595 case GST_RTCP_TYPE_RTPFB:
2596 case GST_RTCP_TYPE_PSFB:
2597 rtp_session_process_feedback (sess, &packet, &pinfo, current_time);
2600 GST_WARNING ("got unknown RTCP packet");
2603 more = gst_rtcp_packet_move_to_next (&packet);
2606 gst_rtcp_buffer_unmap (&rtcp);
2608 /* if we are scheduling a BYE, we only want to count bye packets, else we
2609 * count everything */
2610 if (sess->scheduled_bye && is_bye) {
2611 sess->bye_stats.bye_members++;
2612 UPDATE_AVG (sess->bye_stats.avg_rtcp_packet_size, pinfo.bytes);
2615 /* keep track of average packet size */
2616 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, pinfo.bytes);
2618 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
2619 sess->stats.avg_rtcp_packet_size, pinfo.bytes);
2620 RTP_SESSION_UNLOCK (sess);
2623 clean_packet_info (&pinfo);
2625 /* notify caller of sr packets in the callback */
2626 if (do_sync && sess->callbacks.sync_rtcp) {
2627 result = sess->callbacks.sync_rtcp (sess, buffer,
2628 sess->sync_rtcp_user_data);
2630 gst_buffer_unref (buffer);
2637 GST_DEBUG ("invalid RTCP packet received");
2638 gst_buffer_unref (buffer);
2644 * rtp_session_update_send_caps:
2645 * @sess: an #RTPSession
2648 * Update the caps of the sender in the rtp session.
2651 rtp_session_update_send_caps (RTPSession * sess, GstCaps * caps)
2656 g_return_if_fail (RTP_IS_SESSION (sess));
2657 g_return_if_fail (GST_IS_CAPS (caps));
2659 GST_LOG ("received caps %" GST_PTR_FORMAT, caps);
2661 s = gst_caps_get_structure (caps, 0);
2663 if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
2667 RTP_SESSION_LOCK (sess);
2668 source = obtain_internal_source (sess, ssrc, &created, GST_CLOCK_TIME_NONE);
2670 rtp_source_update_caps (source, caps);
2671 g_object_unref (source);
2673 RTP_SESSION_UNLOCK (sess);
2678 * rtp_session_send_rtp:
2679 * @sess: an #RTPSession
2680 * @data: pointer to either an RTP buffer or a list of RTP buffers
2681 * @is_list: TRUE when @data is a buffer list
2682 * @current_time: the current system time
2683 * @running_time: the running time of @data
2685 * Send the RTP buffer in the session manager. This function takes ownership of
2688 * Returns: a #GstFlowReturn.
2691 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
2692 GstClockTime current_time, GstClockTime running_time)
2694 GstFlowReturn result;
2696 gboolean prevsender;
2698 RTPPacketInfo pinfo = { 0, };
2701 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2702 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
2704 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
2706 RTP_SESSION_LOCK (sess);
2707 if (!update_packet_info (sess, &pinfo, TRUE, TRUE, is_list, data,
2708 current_time, running_time, -1))
2709 goto invalid_packet;
2711 source = obtain_internal_source (sess, pinfo.ssrc, &created, current_time);
2713 prevsender = RTP_SOURCE_IS_SENDER (source);
2714 oldrate = source->bitrate;
2716 /* we use our own source to send */
2717 result = rtp_source_send_rtp (source, &pinfo);
2719 source_update_sender (sess, source, prevsender);
2721 if (oldrate != source->bitrate)
2722 sess->recalc_bandwidth = TRUE;
2723 RTP_SESSION_UNLOCK (sess);
2725 g_object_unref (source);
2726 clean_packet_info (&pinfo);
2732 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
2733 RTP_SESSION_UNLOCK (sess);
2734 GST_DEBUG ("invalid RTP packet received");
2740 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
2742 *bandwidth += source->bitrate;
2745 /* must be called with session lock */
2747 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2750 GstClockTime result;
2751 RTPSessionStats *stats;
2753 /* recalculate bandwidth when it changed */
2754 if (sess->recalc_bandwidth) {
2757 if (sess->bandwidth > 0)
2758 bandwidth = sess->bandwidth;
2760 /* If it is <= 0, then try to estimate the actual bandwidth */
2763 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2764 (GHFunc) add_bitrates, &bandwidth);
2767 if (bandwidth < 8000)
2768 bandwidth = RTP_STATS_BANDWIDTH;
2770 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
2771 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
2773 sess->recalc_bandwidth = FALSE;
2776 if (sess->scheduled_bye) {
2777 stats = &sess->bye_stats;
2778 result = rtp_stats_calculate_bye_interval (stats);
2780 stats = &sess->stats;
2781 result = rtp_stats_calculate_rtcp_interval (stats,
2782 stats->internal_sender_sources > 0, first);
2785 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2786 GST_TIME_ARGS (result), first);
2788 if (!deterministic && result != GST_CLOCK_TIME_NONE)
2789 result = rtp_stats_add_rtcp_jitter (stats, result);
2791 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2797 source_mark_bye (const gchar * key, RTPSource * source, const gchar * reason)
2799 if (source->internal)
2800 rtp_source_mark_bye (source, reason);
2804 * rtp_session_mark_all_bye:
2805 * @sess: an #RTPSession
2808 * Mark all internal sources of the session as BYE with @reason.
2811 rtp_session_mark_all_bye (RTPSession * sess, const gchar * reason)
2813 g_return_if_fail (RTP_IS_SESSION (sess));
2815 RTP_SESSION_LOCK (sess);
2816 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2817 (GHFunc) source_mark_bye, (gpointer) reason);
2818 RTP_SESSION_UNLOCK (sess);
2821 /* Stop the current @sess and schedule a BYE message for the other members.
2822 * One must have the session lock to call this function
2824 static GstFlowReturn
2825 rtp_session_schedule_bye_locked (RTPSession * sess, GstClockTime current_time)
2827 GstFlowReturn result = GST_FLOW_OK;
2828 GstClockTime interval;
2830 /* nothing to do it we already scheduled bye */
2831 if (sess->scheduled_bye)
2834 /* we schedule BYE now */
2835 sess->scheduled_bye = TRUE;
2836 /* at least one member wants to send a BYE */
2837 memcpy (&sess->bye_stats, &sess->stats, sizeof (RTPSessionStats));
2838 INIT_AVG (sess->bye_stats.avg_rtcp_packet_size, 100);
2839 sess->bye_stats.bye_members = 1;
2840 sess->first_rtcp = TRUE;
2841 sess->allow_early = TRUE;
2843 /* reschedule transmission */
2844 sess->last_rtcp_send_time = current_time;
2845 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2847 if (interval != GST_CLOCK_TIME_NONE)
2848 sess->next_rtcp_check_time = current_time + interval;
2850 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
2852 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2853 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2855 RTP_SESSION_UNLOCK (sess);
2856 /* notify app of reconsideration */
2857 if (sess->callbacks.reconsider)
2858 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2859 RTP_SESSION_LOCK (sess);
2866 * rtp_session_schedule_bye:
2867 * @sess: an #RTPSession
2868 * @current_time: the current system time
2870 * Schedule a BYE message for all sources marked as BYE in @sess.
2872 * Returns: a #GstFlowReturn.
2875 rtp_session_schedule_bye (RTPSession * sess, GstClockTime current_time)
2877 GstFlowReturn result;
2879 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2881 RTP_SESSION_LOCK (sess);
2882 result = rtp_session_schedule_bye_locked (sess, current_time);
2883 RTP_SESSION_UNLOCK (sess);
2889 * rtp_session_next_timeout:
2890 * @sess: an #RTPSession
2891 * @current_time: the current system time
2893 * Get the next time we should perform session maintenance tasks.
2895 * Returns: a time when rtp_session_on_timeout() should be called with the
2896 * current system time.
2899 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2901 GstClockTime result, interval = 0;
2903 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
2905 RTP_SESSION_LOCK (sess);
2907 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
2908 GST_DEBUG ("have early rtcp time");
2909 result = sess->next_early_rtcp_time;
2913 result = sess->next_rtcp_check_time;
2915 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2916 ", next time: %" GST_TIME_FORMAT,
2917 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2919 if (result == GST_CLOCK_TIME_NONE || result < current_time) {
2920 GST_DEBUG ("take current time as base");
2921 /* our previous check time expired, start counting from the current time
2923 result = current_time;
2926 if (sess->scheduled_bye) {
2927 if (sess->bye_stats.active_sources >= 50) {
2928 GST_DEBUG ("reconsider BYE, more than 50 sources");
2929 /* reconsider BYE if members >= 50 */
2930 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2933 if (sess->first_rtcp) {
2934 GST_DEBUG ("first RTCP packet");
2935 /* we are called for the first time */
2936 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2937 } else if (sess->next_rtcp_check_time < current_time) {
2938 GST_DEBUG ("old check time expired, getting new timeout");
2939 /* get a new timeout when we need to */
2940 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
2944 if (interval != GST_CLOCK_TIME_NONE)
2947 result = GST_CLOCK_TIME_NONE;
2949 sess->next_rtcp_check_time = result;
2953 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2954 ", next time: %" GST_TIME_FORMAT,
2955 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2956 RTP_SESSION_UNLOCK (sess);
2970 GstRTCPBuffer rtcpbuf;
2973 guint num_to_report;
2978 GstClockTime current_time;
2980 GstClockTime running_time;
2981 GstClockTime interval;
2982 GstRTCPPacket packet;
2985 gboolean may_suppress;
2987 guint nacked_seqnums;
2991 session_start_rtcp (RTPSession * sess, ReportData * data)
2993 GstRTCPPacket *packet = &data->packet;
2994 RTPSource *own = data->source;
2995 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2997 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2998 data->has_sdes = FALSE;
3000 gst_rtcp_buffer_map (data->rtcp, GST_MAP_READWRITE, rtcp);
3002 if (RTP_SOURCE_IS_SENDER (own)) {
3005 guint32 packet_count, octet_count;
3007 /* we are a sender, create SR */
3008 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
3009 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SR, packet);
3011 /* get latest stats */
3012 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
3013 &ntptime, &rtptime, &packet_count, &octet_count);
3015 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
3016 packet_count, octet_count);
3018 /* fill in sender report info */
3019 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
3020 ntptime, rtptime, packet_count, octet_count);
3022 /* we are only receiver, create RR */
3023 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
3024 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RR, packet);
3025 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
3029 /* construct a Sender or Receiver Report */
3031 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
3033 RTPSession *sess = data->sess;
3034 GstRTCPPacket *packet = &data->packet;
3035 guint8 fractionlost;
3037 guint32 exthighestseq, jitter;
3040 /* don't report for sources in future generations */
3041 if (((gint16) (source->generation - sess->generation)) > 0) {
3042 GST_DEBUG ("source %08x generation %u > %u", source->ssrc,
3043 source->generation, sess->generation);
3047 if (g_hash_table_contains (source->reported_in_sr_of,
3048 GUINT_TO_POINTER (data->source->ssrc))) {
3049 GST_DEBUG ("source %08x already reported in this generation", source->ssrc);
3053 if (gst_rtcp_packet_get_rb_count (packet) == GST_RTCP_MAX_RB_COUNT) {
3054 GST_DEBUG ("max RB count reached");
3058 /* only report about other sender */
3059 if (source == data->source)
3062 if (!RTP_SOURCE_IS_SENDER (source)) {
3063 GST_DEBUG ("source %08x not sender", source->ssrc);
3067 GST_DEBUG ("create RB for SSRC %08x", source->ssrc);
3070 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
3071 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
3073 /* store last generated RR packet */
3074 source->last_rr.is_valid = TRUE;
3075 source->last_rr.fractionlost = fractionlost;
3076 source->last_rr.packetslost = packetslost;
3077 source->last_rr.exthighestseq = exthighestseq;
3078 source->last_rr.jitter = jitter;
3079 source->last_rr.lsr = lsr;
3080 source->last_rr.dlsr = dlsr;
3082 /* packet is not yet filled, add report block for this source. */
3083 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
3084 exthighestseq, jitter, lsr, dlsr);
3087 g_hash_table_add (source->reported_in_sr_of,
3088 GUINT_TO_POINTER (data->source->ssrc));
3093 session_add_fir (const gchar * key, RTPSource * source, ReportData * data)
3095 GstRTCPPacket *packet = &data->packet;
3099 if (!source->send_fir)
3102 len = gst_rtcp_packet_fb_get_fci_length (packet);
3103 if (!gst_rtcp_packet_fb_set_fci_length (packet, len + 2))
3104 /* exit because the packet is full, will put next request in a
3108 fci_data = gst_rtcp_packet_fb_get_fci (packet) + (len * 4);
3110 GST_WRITE_UINT32_BE (fci_data, source->ssrc);
3112 fci_data[0] = source->current_send_fir_seqnum;
3113 fci_data[1] = fci_data[2] = fci_data[3] = 0;
3115 source->send_fir = FALSE;
3119 session_fir (RTPSession * sess, ReportData * data)
3121 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3122 GstRTCPPacket *packet = &data->packet;
3124 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
3127 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_FIR);
3128 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3129 gst_rtcp_packet_fb_set_media_ssrc (packet, 0);
3131 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3132 (GHFunc) session_add_fir, data);
3134 if (gst_rtcp_packet_fb_get_fci_length (packet) == 0)
3135 gst_rtcp_packet_remove (packet);
3137 data->may_suppress = FALSE;
3141 has_pli_compare_func (gconstpointer a, gconstpointer ignored)
3143 GstRTCPPacket packet;
3144 GstRTCPBuffer rtcp = { NULL, };
3145 gboolean ret = FALSE;
3147 gst_rtcp_buffer_map ((GstBuffer *) a, GST_MAP_READ, &rtcp);
3149 if (gst_rtcp_buffer_get_first_packet (&rtcp, &packet)) {
3150 if (gst_rtcp_packet_get_type (&packet) == GST_RTCP_TYPE_PSFB &&
3151 gst_rtcp_packet_fb_get_type (&packet) == GST_RTCP_PSFB_TYPE_PLI)
3155 gst_rtcp_buffer_unmap (&rtcp);
3162 session_pli (const gchar * key, RTPSource * source, ReportData * data)
3164 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3165 GstRTCPPacket *packet = &data->packet;
3167 if (!source->send_pli)
3170 if (rtp_source_has_retained (source, has_pli_compare_func, NULL))
3173 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
3174 /* exit because the packet is full, will put next request in a
3178 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_PLI);
3179 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3180 gst_rtcp_packet_fb_set_media_ssrc (packet, source->ssrc);
3182 source->send_pli = FALSE;
3183 data->may_suppress = FALSE;
3186 /* construct NACK */
3188 session_nack (const gchar * key, RTPSource * source, ReportData * data)
3190 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3191 GstRTCPPacket *packet = &data->packet;
3196 if (!source->send_nack)
3199 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RTPFB, packet))
3200 /* exit because the packet is full, will put next request in a
3204 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_RTPFB_TYPE_NACK);
3205 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3206 gst_rtcp_packet_fb_set_media_ssrc (packet, source->ssrc);
3208 nacks = rtp_source_get_nacks (source, &n_nacks);
3209 GST_DEBUG ("%u NACKs", n_nacks);
3210 if (!gst_rtcp_packet_fb_set_fci_length (packet, n_nacks))
3213 fci_data = gst_rtcp_packet_fb_get_fci (packet);
3214 for (i = 0; i < n_nacks; i++) {
3215 GST_WRITE_UINT32_BE (fci_data, nacks[i]);
3217 data->nacked_seqnums++;
3220 rtp_source_clear_nacks (source);
3221 data->may_suppress = FALSE;
3224 /* perform cleanup of sources that timed out */
3226 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
3228 gboolean remove = FALSE;
3229 gboolean byetimeout = FALSE;
3230 gboolean sendertimeout = FALSE;
3231 gboolean is_sender, is_active;
3232 RTPSession *sess = data->sess;
3233 GstClockTime interval, binterval;
3236 GST_DEBUG ("look at %08x, generation %u", source->ssrc, source->generation);
3238 /* check for outdated collisions */
3239 if (source->internal) {
3240 GST_DEBUG ("Timing out collisions for %x", source->ssrc);
3241 rtp_source_timeout (source, data->current_time,
3242 data->running_time - sess->rtcp_feedback_retention_window);
3245 /* nothing else to do when without RTCP */
3246 if (data->interval == GST_CLOCK_TIME_NONE)
3249 is_sender = RTP_SOURCE_IS_SENDER (source);
3250 is_active = RTP_SOURCE_IS_ACTIVE (source);
3252 /* our own rtcp interval may have been forced low by secondary configuration,
3253 * while sender side may still operate with higher interval,
3254 * so do not just take our interval to decide on timing out sender,
3255 * but take (if data->interval <= 5 * GST_SECOND):
3256 * interval = CLAMP (sender_interval, data->interval, 5 * GST_SECOND)
3257 * where sender_interval is difference between last 2 received RTCP reports
3259 if (data->interval >= 5 * GST_SECOND || source->internal) {
3260 binterval = data->interval;
3262 GST_LOG ("prev_rtcp %" GST_TIME_FORMAT ", last_rtcp %" GST_TIME_FORMAT,
3263 GST_TIME_ARGS (source->stats.prev_rtcptime),
3264 GST_TIME_ARGS (source->stats.last_rtcptime));
3265 /* if not received enough yet, fallback to larger default */
3266 if (source->stats.last_rtcptime > source->stats.prev_rtcptime)
3267 binterval = source->stats.last_rtcptime - source->stats.prev_rtcptime;
3269 binterval = 5 * GST_SECOND;
3270 binterval = CLAMP (binterval, data->interval, 5 * GST_SECOND);
3272 GST_LOG ("timeout base interval %" GST_TIME_FORMAT,
3273 GST_TIME_ARGS (binterval));
3275 if (!source->internal && source->marked_bye) {
3276 /* if we received a BYE from the source, remove the source after some
3278 if (data->current_time > source->bye_time &&
3279 data->current_time - source->bye_time > sess->stats.bye_timeout) {
3280 GST_DEBUG ("removing BYE source %08x", source->ssrc);
3286 if (source->internal && source->sent_bye) {
3287 GST_DEBUG ("removing internal source that has sent BYE %08x", source->ssrc);
3291 /* sources that were inactive for more than 5 times the deterministic reporting
3292 * interval get timed out. the min timeout is 5 seconds. */
3293 /* mind old time that might pre-date last time going to PLAYING */
3294 btime = MAX (source->last_activity, sess->start_time);
3295 if (data->current_time > btime) {
3296 interval = MAX (binterval * 5, 5 * GST_SECOND);
3297 if (data->current_time - btime > interval) {
3298 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
3299 source->ssrc, GST_TIME_ARGS (btime));
3300 if (source->internal) {
3301 /* this is an internal source that is not using our suggested ssrc.
3302 * since there must be another source using this ssrc, we can remove
3303 * this one instead of making it a receiver forever */
3304 if (source->ssrc != sess->suggested_ssrc) {
3305 rtp_source_mark_bye (source, "timed out");
3306 /* do not schedule bye here, since we are inside the RTCP timeout
3307 * processing and scheduling bye will interfere with SR/RR sending */
3315 /* senders that did not send for a long time become a receiver, this also
3316 * holds for our own sources. */
3318 /* mind old time that might pre-date last time going to PLAYING */
3319 btime = MAX (source->last_rtp_activity, sess->start_time);
3320 if (data->current_time > btime) {
3321 interval = MAX (binterval * 2, 5 * GST_SECOND);
3322 if (data->current_time - btime > interval) {
3323 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
3324 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
3325 sendertimeout = TRUE;
3331 sess->total_sources--;
3333 sess->stats.sender_sources--;
3334 if (source->internal)
3335 sess->stats.internal_sender_sources--;
3338 sess->stats.active_sources--;
3340 if (source->internal)
3341 sess->stats.internal_sources--;
3344 on_bye_timeout (sess, source);
3346 on_timeout (sess, source);
3348 if (sendertimeout) {
3349 source->is_sender = FALSE;
3350 sess->stats.sender_sources--;
3351 if (source->internal)
3352 sess->stats.internal_sender_sources--;
3354 on_sender_timeout (sess, source);
3356 /* count how many source to report in this generation */
3357 if (((gint16) (source->generation - sess->generation)) <= 0)
3358 data->num_to_report++;
3360 source->closing = remove;
3364 session_sdes (RTPSession * sess, ReportData * data)
3366 GstRTCPPacket *packet = &data->packet;
3367 const GstStructure *sdes;
3369 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3371 /* add SDES packet */
3372 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SDES, packet);
3374 gst_rtcp_packet_sdes_add_item (packet, data->source->ssrc);
3376 sdes = rtp_source_get_sdes_struct (data->source);
3378 /* add all fields in the structure, the order is not important. */
3379 n_fields = gst_structure_n_fields (sdes);
3380 for (i = 0; i < n_fields; ++i) {
3383 GstRTCPSDESType type;
3385 field = gst_structure_nth_field_name (sdes, i);
3388 value = gst_structure_get_string (sdes, field);
3391 type = gst_rtcp_sdes_name_to_type (field);
3393 /* Early packets are minimal and only include the CNAME */
3394 if (data->is_early && type != GST_RTCP_SDES_CNAME)
3397 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
3398 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
3399 (const guint8 *) value);
3400 } else if (type == GST_RTCP_SDES_PRIV) {
3406 /* don't accept entries that are too big */
3407 prefix_len = strlen (field);
3408 if (prefix_len > 255)
3410 value_len = strlen (value);
3411 if (value_len > 255)
3413 data_len = 1 + prefix_len + value_len;
3417 data[0] = prefix_len;
3418 memcpy (&data[1], field, prefix_len);
3419 memcpy (&data[1 + prefix_len], value, value_len);
3421 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
3425 data->has_sdes = TRUE;
3428 /* schedule a BYE packet */
3430 make_source_bye (RTPSession * sess, RTPSource * source, ReportData * data)
3432 GstRTCPPacket *packet = &data->packet;
3433 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3436 session_sdes (sess, data);
3437 /* add a BYE packet */
3438 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_BYE, packet);
3439 gst_rtcp_packet_bye_add_ssrc (packet, source->ssrc);
3440 if (source->bye_reason)
3441 gst_rtcp_packet_bye_set_reason (packet, source->bye_reason);
3443 /* we have a BYE packet now */
3444 source->sent_bye = TRUE;
3448 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
3450 GstClockTime new_send_time, elapsed;
3451 GstClockTime interval;
3452 RTPSessionStats *stats;
3454 if (sess->scheduled_bye)
3455 stats = &sess->bye_stats;
3457 stats = &sess->stats;
3459 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3460 data->is_early = TRUE;
3462 data->is_early = FALSE;
3464 if (data->is_early && sess->next_early_rtcp_time < current_time) {
3465 GST_DEBUG ("early feedback %" GST_TIME_FORMAT " < now %"
3466 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_early_rtcp_time),
3467 GST_TIME_ARGS (current_time));
3471 /* no need to check yet */
3472 if (sess->next_rtcp_check_time == GST_CLOCK_TIME_NONE ||
3473 sess->next_rtcp_check_time > current_time) {
3474 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
3475 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
3476 GST_TIME_ARGS (current_time));
3481 /* get elapsed time since we last reported */
3482 elapsed = current_time - sess->last_rtcp_send_time;
3484 /* take interval and add jitter */
3485 interval = data->interval;
3486 if (interval != GST_CLOCK_TIME_NONE)
3487 interval = rtp_stats_add_rtcp_jitter (stats, interval);
3489 /* perform forward reconsideration */
3490 if (interval != GST_CLOCK_TIME_NONE) {
3491 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
3492 GST_TIME_FORMAT, GST_TIME_ARGS (interval), GST_TIME_ARGS (elapsed));
3493 new_send_time = interval + sess->last_rtcp_send_time;
3495 new_send_time = sess->last_rtcp_send_time;
3498 if (!data->is_early) {
3499 /* check if reconsideration */
3500 if (new_send_time == GST_CLOCK_TIME_NONE || current_time < new_send_time) {
3501 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
3502 GST_TIME_ARGS (new_send_time));
3503 /* store new check time */
3504 sess->next_rtcp_check_time = new_send_time;
3507 sess->next_rtcp_check_time = current_time + interval;
3508 } else if (interval != GST_CLOCK_TIME_NONE) {
3509 /* Apply the rules from RFC 4585 section 3.5.3 */
3510 if (stats->min_interval != 0 && !sess->first_rtcp) {
3511 GstClockTime T_rr_current_interval =
3512 g_random_double_range (0.5, 1.5) * stats->min_interval;
3514 /* This will caused the RTCP to be suppressed if no FB packets are added */
3515 if (sess->last_rtcp_send_time + T_rr_current_interval > new_send_time) {
3516 GST_DEBUG ("RTCP packet could be suppressed min: %" GST_TIME_FORMAT
3517 " last: %" GST_TIME_FORMAT
3518 " + T_rr_current_interval: %" GST_TIME_FORMAT
3519 " > new_send_time: %" GST_TIME_FORMAT,
3520 GST_TIME_ARGS (stats->min_interval),
3521 GST_TIME_ARGS (sess->last_rtcp_send_time),
3522 GST_TIME_ARGS (T_rr_current_interval),
3523 GST_TIME_ARGS (new_send_time));
3524 data->may_suppress = TRUE;
3529 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
3530 GST_TIME_ARGS (new_send_time));
3536 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
3538 g_hash_table_insert (hash_table, key, g_object_ref (source));
3542 remove_closing_sources (const gchar * key, RTPSource * source,
3545 if (source->closing)
3548 if (source->send_fir)
3549 data->have_fir = TRUE;
3550 if (source->send_pli)
3551 data->have_pli = TRUE;
3552 if (source->send_nack)
3553 data->have_nack = TRUE;
3559 generate_rtcp (const gchar * key, RTPSource * source, ReportData * data)
3561 RTPSession *sess = data->sess;
3562 gboolean is_bye = FALSE;
3563 ReportOutput *output;
3565 /* only generate RTCP for active internal sources */
3566 if (!source->internal || source->sent_bye)
3569 /* ignore other sources when we do the timeout after a scheduled BYE */
3570 if (sess->scheduled_bye && !source->marked_bye)
3573 data->source = source;
3576 session_start_rtcp (sess, data);
3578 if (source->marked_bye) {
3580 make_source_bye (sess, source, data);
3582 } else if (!data->is_early) {
3583 /* loop over all known sources and add report blocks. If we are early, we
3584 * just make a minimal RTCP packet and skip this step */
3585 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3586 (GHFunc) session_report_blocks, data);
3588 if (!data->has_sdes)
3589 session_sdes (sess, data);
3592 session_fir (sess, data);
3595 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3596 (GHFunc) session_pli, data);
3598 if (data->have_nack)
3599 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3600 (GHFunc) session_nack, data);
3602 gst_rtcp_buffer_unmap (&data->rtcpbuf);
3604 output = g_slice_new (ReportOutput);
3605 output->source = g_object_ref (source);
3606 output->is_bye = is_bye;
3607 output->buffer = data->rtcp;
3608 /* queue the RTCP packet to push later */
3609 g_queue_push_tail (&data->output, output);
3613 update_generation (const gchar * key, RTPSource * source, ReportData * data)
3615 RTPSession *sess = data->sess;
3617 if (g_hash_table_size (source->reported_in_sr_of) >=
3618 sess->stats.internal_sources) {
3619 /* source is reported, move to next generation */
3620 source->generation = sess->generation + 1;
3621 g_hash_table_remove_all (source->reported_in_sr_of);
3623 GST_LOG ("reported source %x, new generation: %d", source->ssrc,
3624 source->generation);
3626 /* if we reported all sources in this generation, move to next */
3627 if (--data->num_to_report == 0) {
3629 GST_DEBUG ("all reported, generation now %u", sess->generation);
3635 * rtp_session_on_timeout:
3636 * @sess: an #RTPSession
3637 * @current_time: the current system time
3638 * @ntpnstime: the current NTP time in nanoseconds
3639 * @running_time: the current running_time of the pipeline
3641 * Perform maintenance actions after the timeout obtained with
3642 * rtp_session_next_timeout() expired.
3644 * This function will perform timeouts of receivers and senders, send a BYE
3645 * packet or generate RTCP packets with current session stats.
3647 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
3648 * times, for each packet that should be processed.
3650 * Returns: a #GstFlowReturn.
3653 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
3654 guint64 ntpnstime, GstClockTime running_time)
3656 GstFlowReturn result = GST_FLOW_OK;
3657 ReportData data = { GST_RTCP_BUFFER_INIT };
3658 GHashTable *table_copy;
3659 ReportOutput *output;
3661 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3663 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT
3664 ", running-time %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
3665 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (running_time));
3668 data.current_time = current_time;
3669 data.ntpnstime = ntpnstime;
3670 data.running_time = running_time;
3671 data.num_to_report = 0;
3672 data.may_suppress = FALSE;
3673 data.nacked_seqnums = 0;
3674 g_queue_init (&data.output);
3676 RTP_SESSION_LOCK (sess);
3677 /* get a new interval, we need this for various cleanups etc */
3678 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
3680 GST_DEBUG ("interval %" GST_TIME_FORMAT, GST_TIME_ARGS (data.interval));
3682 /* we need an internal source now */
3683 if (sess->stats.internal_sources == 0) {
3687 source = obtain_internal_source (sess, sess->suggested_ssrc, &created,
3689 g_object_unref (source);
3692 sess->conflicting_addresses =
3693 timeout_conflicting_addresses (sess->conflicting_addresses, current_time);
3695 /* Make a local copy of the hashtable. We need to do this because the
3696 * cleanup stage below releases the session lock. */
3697 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
3698 (GDestroyNotify) g_object_unref);
3699 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3700 (GHFunc) clone_ssrcs_hashtable, table_copy);
3702 /* Clean up the session, mark the source for removing, this might release the
3704 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
3705 g_hash_table_destroy (table_copy);
3707 /* Now remove the marked sources */
3708 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
3709 (GHRFunc) remove_closing_sources, &data);
3711 /* update point-to-point status */
3712 session_update_ptp (sess);
3714 /* see if we need to generate SR or RR packets */
3715 if (!is_rtcp_time (sess, current_time, &data))
3718 GST_DEBUG ("doing RTCP generation %u for %u sources, early %d",
3719 sess->generation, data.num_to_report, data.is_early);
3721 /* generate RTCP for all internal sources */
3722 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3723 (GHFunc) generate_rtcp, &data);
3725 /* update the generation for all the sources that have been reported */
3726 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3727 (GHFunc) update_generation, &data);
3729 /* we keep track of the last report time in order to timeout inactive
3730 * receivers or senders */
3731 if (!data.is_early && !data.may_suppress)
3732 sess->last_rtcp_send_time = data.current_time;
3733 sess->first_rtcp = FALSE;
3734 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
3735 sess->scheduled_bye = FALSE;
3737 /* RFC 4585 section 3.5.2 step 6 */
3738 if (!data.is_early) {
3739 sess->allow_early = TRUE;
3743 RTP_SESSION_UNLOCK (sess);
3745 /* push out the RTCP packets */
3746 while ((output = g_queue_pop_head (&data.output))) {
3747 gboolean do_not_suppress;
3748 GstBuffer *buffer = output->buffer;
3749 RTPSource *source = output->source;
3751 /* Give the user a change to add its own packet */
3752 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
3753 buffer, data.is_early, &do_not_suppress);
3755 if (sess->callbacks.send_rtcp && (do_not_suppress || !data.may_suppress)) {
3758 packet_size = gst_buffer_get_size (buffer) + sess->header_len;
3760 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
3761 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
3762 sess->stats.avg_rtcp_packet_size, packet_size);
3764 sess->callbacks.send_rtcp (sess, source, buffer, output->is_bye,
3765 sess->send_rtcp_user_data);
3766 sess->stats.nacks_sent += data.nacked_seqnums;
3768 GST_DEBUG ("freeing packet callback: %p"
3769 " do_not_suppress: %d may_suppress: %d",
3770 sess->callbacks.send_rtcp, do_not_suppress, data.may_suppress);
3771 sess->stats.nacks_dropped += data.nacked_seqnums;
3772 gst_buffer_unref (buffer);
3774 g_object_unref (source);
3775 g_slice_free (ReportOutput, output);
3781 * rtp_session_request_early_rtcp:
3782 * @sess: an #RTPSession
3783 * @current_time: the current system time
3784 * @max_delay: maximum delay
3786 * Request transmission of early RTCP
3788 * Returns: %TRUE if the related RTCP can be scheduled.
3791 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
3792 GstClockTime max_delay)
3794 GstClockTime T_dither_max, T_rr;
3797 /* Implements the algorithm described in RFC 4585 section 3.5.2 */
3799 RTP_SESSION_LOCK (sess);
3801 /* Check if already requested */
3802 /* RFC 4585 section 3.5.2 step 2 */
3803 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
3804 GST_LOG_OBJECT (sess, "already have next early rtcp time");
3809 if (!GST_CLOCK_TIME_IS_VALID (sess->next_rtcp_check_time)) {
3810 GST_LOG_OBJECT (sess, "no next RTCP check time");
3815 T_rr = sess->next_rtcp_check_time - sess->last_rtcp_send_time;
3817 /* RFC 4585 section 3.5.2 step 2b */
3818 /* If the total sources is <=2, then there is only us and one peer */
3819 /* When there is one auxiliary stream the session can still do point
3822 if (sess->is_doing_ptp) {
3825 /* Divide by 2 because l = 0.5 */
3826 T_dither_max = T_rr;
3830 /* RFC 4585 section 3.5.2 step 3 */
3831 if (current_time + T_dither_max > sess->next_rtcp_check_time) {
3832 GST_LOG_OBJECT (sess, "don't send because of dither");
3837 /* RFC 4585 section 3.5.2 step 4a */
3838 if (sess->allow_early == FALSE) {
3839 /* Ignore the request a scheduled packet will be in time anyway */
3840 if (current_time + max_delay > sess->next_rtcp_check_time) {
3841 GST_LOG_OBJECT (sess,
3842 "next scheduled time is soon %" GST_TIME_FORMAT " + %" GST_TIME_FORMAT
3843 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
3844 GST_TIME_ARGS (max_delay),
3845 GST_TIME_ARGS (sess->next_rtcp_check_time));
3848 GST_LOG_OBJECT (sess, "can't allow early feedback");
3854 /* RFC 4585 section 3.5.2 step 4b */
3856 /* Schedule an early transmission later */
3857 sess->next_early_rtcp_time = g_random_double () * T_dither_max +
3860 /* If no dithering, schedule it for NOW */
3861 sess->next_early_rtcp_time = current_time;
3864 /* RFC 4585 section 3.5.2 step 6 */
3865 sess->allow_early = FALSE;
3866 /* Delay next regular RTCP packet to not exceed the short-term
3867 * RTCP bandwidth when using early feedback as compared to
3869 sess->next_rtcp_check_time = sess->last_rtcp_send_time + 2 * T_rr;
3870 sess->last_rtcp_send_time += T_rr;
3872 GST_LOG_OBJECT (sess, "next early RTCP time %" GST_TIME_FORMAT,
3873 GST_TIME_ARGS (sess->next_early_rtcp_time));
3874 RTP_SESSION_UNLOCK (sess);
3876 /* notify app of need to send packet early
3877 * and therefore of timeout change */
3878 if (sess->callbacks.reconsider)
3879 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
3885 RTP_SESSION_UNLOCK (sess);
3891 rtp_session_send_rtcp (RTPSession * sess, GstClockTime max_delay)
3895 if (!sess->callbacks.send_rtcp)
3898 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
3900 return rtp_session_request_early_rtcp (sess, now, max_delay);
3904 rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc,
3905 gboolean fir, gint count)
3909 if (!rtp_session_send_rtcp (sess, 5 * GST_SECOND)) {
3910 GST_DEBUG ("FIR/PLI not sent");
3914 RTP_SESSION_LOCK (sess);
3915 src = find_source (sess, ssrc);
3920 src->send_pli = FALSE;
3921 src->send_fir = TRUE;
3923 if (count == -1 || count != src->last_fir_count)
3924 src->current_send_fir_seqnum++;
3925 src->last_fir_count = count;
3926 } else if (!src->send_fir) {
3927 src->send_pli = TRUE;
3929 RTP_SESSION_UNLOCK (sess);
3936 RTP_SESSION_UNLOCK (sess);
3942 * rtp_session_request_nack:
3943 * @sess: a #RTPSession
3945 * @seqnum: the missing seqnum
3946 * @max_delay: max delay to request NACK
3948 * Request scheduling of a NACK feedback packet for @seqnum in @ssrc.
3950 * Returns: %TRUE if the NACK feedback could be scheduled
3953 rtp_session_request_nack (RTPSession * sess, guint32 ssrc, guint16 seqnum,
3954 GstClockTime max_delay)
3958 if (!rtp_session_send_rtcp (sess, max_delay)) {
3959 GST_DEBUG ("NACK not sent");
3963 RTP_SESSION_LOCK (sess);
3964 source = find_source (sess, ssrc);
3968 GST_DEBUG ("request NACK for %08x, #%u", ssrc, seqnum);
3969 rtp_source_register_nack (source, seqnum);
3970 RTP_SESSION_UNLOCK (sess);
3977 RTP_SESSION_UNLOCK (sess);