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,
55 #define DEFAULT_INTERNAL_SOURCE NULL
56 #define DEFAULT_BANDWIDTH RTP_STATS_BANDWIDTH
57 #define DEFAULT_RTCP_FRACTION (RTP_STATS_RTCP_FRACTION * RTP_STATS_BANDWIDTH)
58 #define DEFAULT_RTCP_RR_BANDWIDTH -1
59 #define DEFAULT_RTCP_RS_BANDWIDTH -1
60 #define DEFAULT_RTCP_MTU 1400
61 #define DEFAULT_SDES NULL
62 #define DEFAULT_NUM_SOURCES 0
63 #define DEFAULT_NUM_ACTIVE_SOURCES 0
64 #define DEFAULT_SOURCES NULL
65 #define DEFAULT_RTCP_MIN_INTERVAL (RTP_STATS_MIN_INTERVAL * GST_SECOND)
66 #define DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW (2 * GST_SECOND)
67 #define DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD (3)
68 #define DEFAULT_PROBATION RTP_DEFAULT_PROBATION
77 PROP_RTCP_RR_BANDWIDTH,
78 PROP_RTCP_RS_BANDWIDTH,
82 PROP_NUM_ACTIVE_SOURCES,
85 PROP_RTCP_MIN_INTERVAL,
86 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
87 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
92 /* update average packet size */
93 #define INIT_AVG(avg, val) \
95 #define UPDATE_AVG(avg, val) \
99 (avg) = ((val) + (15 * (avg))) >> 4;
102 /* The number RTCP intervals after which to timeout entries in the
105 #define RTCP_INTERVAL_COLLISION_TIMEOUT 10
107 /* GObject vmethods */
108 static void rtp_session_finalize (GObject * object);
109 static void rtp_session_set_property (GObject * object, guint prop_id,
110 const GValue * value, GParamSpec * pspec);
111 static void rtp_session_get_property (GObject * object, guint prop_id,
112 GValue * value, GParamSpec * pspec);
114 static void rtp_session_send_rtcp (RTPSession * sess, GstClockTime max_delay);
116 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
118 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
120 static guint32 rtp_session_create_new_ssrc (RTPSession * sess);
121 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
122 gboolean * created, RTPPacketInfo * pinfo, gboolean rtp);
123 static RTPSource *obtain_internal_source (RTPSession * sess,
124 guint32 ssrc, gboolean * created);
125 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
126 GstClockTime current_time);
127 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
128 gboolean deterministic, gboolean first);
131 accumulate_trues (GSignalInvocationHint * ihint, GValue * return_accu,
132 const GValue * handler_return, gpointer data)
134 if (g_value_get_boolean (handler_return))
135 g_value_set_boolean (return_accu, TRUE);
141 rtp_session_class_init (RTPSessionClass * klass)
143 GObjectClass *gobject_class;
145 gobject_class = (GObjectClass *) klass;
147 gobject_class->finalize = rtp_session_finalize;
148 gobject_class->set_property = rtp_session_set_property;
149 gobject_class->get_property = rtp_session_get_property;
152 * RTPSession::get-source-by-ssrc:
153 * @session: the object which received the signal
154 * @ssrc: the SSRC of the RTPSource
156 * Request the #RTPSource object with SSRC @ssrc in @session.
158 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
159 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
160 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
161 get_source_by_ssrc), NULL, NULL, g_cclosure_marshal_generic,
162 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
165 * RTPSession::on-new-ssrc:
166 * @session: the object which received the signal
167 * @src: the new RTPSource
169 * Notify of a new SSRC that entered @session.
171 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
172 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
173 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
174 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
177 * RTPSession::on-ssrc-collision:
178 * @session: the object which received the signal
179 * @src: the #RTPSource that caused a collision
181 * Notify when we have an SSRC collision
183 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
184 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
185 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
186 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
189 * RTPSession::on-ssrc-validated:
190 * @session: the object which received the signal
191 * @src: the new validated RTPSource
193 * Notify of a new SSRC that became validated.
195 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
196 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
197 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
198 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
201 * RTPSession::on-ssrc-active:
202 * @session: the object which received the signal
203 * @src: the active RTPSource
205 * Notify of a SSRC that is active, i.e., sending RTCP.
207 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
208 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
209 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
210 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
213 * RTPSession::on-ssrc-sdes:
214 * @session: the object which received the signal
215 * @src: the RTPSource
217 * Notify that a new SDES was received for SSRC.
219 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
220 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
221 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
222 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
225 * RTPSession::on-bye-ssrc:
226 * @session: the object which received the signal
227 * @src: the RTPSource that went away
229 * Notify of an SSRC that became inactive because of a BYE packet.
231 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
232 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
233 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
234 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
237 * RTPSession::on-bye-timeout:
238 * @session: the object which received the signal
239 * @src: the RTPSource that timed out
241 * Notify of an SSRC that has timed out because of BYE
243 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
244 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
245 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
246 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
249 * RTPSession::on-timeout:
250 * @session: the object which received the signal
251 * @src: the RTPSource that timed out
253 * Notify of an SSRC that has timed out
255 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
256 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
257 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
258 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
261 * RTPSession::on-sender-timeout:
262 * @session: the object which received the signal
263 * @src: the RTPSource that timed out
265 * Notify of an SSRC that was a sender but timed out and became a receiver.
267 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
268 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
269 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
270 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
274 * RTPSession::on-sending-rtcp
275 * @session: the object which received the signal
276 * @buffer: the #GstBuffer containing the RTCP packet about to be sent
277 * @early: %TRUE if the packet is early, %FALSE if it is regular
279 * This signal is emitted before sending an RTCP packet, it can be used
280 * to add extra RTCP Packets.
282 * Returns: %TRUE if the RTCP buffer should NOT be suppressed, %FALSE
283 * if suppressing it is acceptable
285 rtp_session_signals[SIGNAL_ON_SENDING_RTCP] =
286 g_signal_new ("on-sending-rtcp", G_TYPE_FROM_CLASS (klass),
287 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_rtcp),
288 accumulate_trues, NULL, g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2,
289 GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_BOOLEAN);
292 * RTPSession::on-feedback-rtcp:
293 * @session: the object which received the signal
294 * @type: Type of RTCP packet, will be %GST_RTCP_TYPE_RTPFB or
295 * %GST_RTCP_TYPE_RTPFB
296 * @fbtype: The type of RTCP FB packet, probably part of #GstRTCPFBType
297 * @sender_ssrc: The SSRC of the sender
298 * @media_ssrc: The SSRC of the media this refers to
299 * @fci: a #GstBuffer with the FCI data from the FB packet or %NULL if
302 * Notify that a RTCP feedback packet has been received
304 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP] =
305 g_signal_new ("on-feedback-rtcp", G_TYPE_FROM_CLASS (klass),
306 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_feedback_rtcp),
307 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 5, G_TYPE_UINT,
308 G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, GST_TYPE_BUFFER);
311 * RTPSession::send-rtcp:
312 * @session: the object which received the signal
313 * @max_delay: The maximum delay after which the feedback will not be useful
316 * Requests that the #RTPSession initiate a new RTCP packet as soon as
317 * possible within the requested delay.
319 rtp_session_signals[SIGNAL_SEND_RTCP] =
320 g_signal_new ("send-rtcp", G_TYPE_FROM_CLASS (klass),
321 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
322 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
323 g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_UINT64);
325 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
326 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
327 "The internal SSRC used for the session (deprecated)",
328 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
330 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
331 g_param_spec_object ("internal-source", "Internal Source",
332 "The internal source element of the session (deprecated)",
333 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
335 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
336 g_param_spec_double ("bandwidth", "Bandwidth",
337 "The bandwidth of the session (0 for auto-discover)",
338 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
339 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
341 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
342 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
343 "The fraction of the bandwidth used for RTCP (or as a real fraction of the RTP bandwidth if < 1)",
344 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
345 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
347 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
348 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
349 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
350 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
351 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
353 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
354 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
355 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
356 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
357 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
360 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
361 "The maximum size of the RTCP packets",
362 16, G_MAXINT16, DEFAULT_RTCP_MTU,
363 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
365 g_object_class_install_property (gobject_class, PROP_SDES,
366 g_param_spec_boxed ("sdes", "SDES",
367 "The SDES items of this session",
368 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
370 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
371 g_param_spec_uint ("num-sources", "Num Sources",
372 "The number of sources in the session", 0, G_MAXUINT,
373 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
375 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
376 g_param_spec_uint ("num-active-sources", "Num Active Sources",
377 "The number of active sources in the session", 0, G_MAXUINT,
378 DEFAULT_NUM_ACTIVE_SOURCES,
379 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
383 * Get a GValue Array of all sources in the session.
386 * <title>Getting the #RTPSources of a session
393 * g_object_get (sess, "sources", &arr, NULL);
395 * for (i = 0; i < arr->n_values; i++) {
398 * val = g_value_array_get_nth (arr, i);
399 * source = g_value_get_object (val);
401 * g_value_array_free (arr);
406 g_object_class_install_property (gobject_class, PROP_SOURCES,
407 g_param_spec_boxed ("sources", "Sources",
408 "An array of all known sources in the session",
409 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
411 g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
412 g_param_spec_boolean ("favor-new", "Favor new sources",
413 "Resolve SSRC conflict in favor of new sources", FALSE,
414 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
416 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
417 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
418 "Minimum interval between Regular RTCP packet (in ns)",
419 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
420 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
422 g_object_class_install_property (gobject_class,
423 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
424 g_param_spec_uint64 ("rtcp-feedback-retention-window",
425 "RTCP Feedback retention window",
426 "Duration during which RTCP Feedback packets are retained (in ns)",
427 0, G_MAXUINT64, DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW,
428 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
430 g_object_class_install_property (gobject_class,
431 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
432 g_param_spec_uint ("rtcp-immediate-feedback-threshold",
433 "RTCP Immediate Feedback threshold",
434 "The maximum number of members of a RTP session for which immediate"
436 0, G_MAXUINT, DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
437 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
439 g_object_class_install_property (gobject_class, PROP_PROBATION,
440 g_param_spec_uint ("probation", "Number of probations",
441 "Consecutive packet sequence numbers to accept the source",
442 0, G_MAXUINT, DEFAULT_PROBATION,
443 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
445 klass->get_source_by_ssrc =
446 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
447 klass->send_rtcp = GST_DEBUG_FUNCPTR (rtp_session_send_rtcp);
449 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
453 rtp_session_init (RTPSession * sess)
458 g_mutex_init (&sess->lock);
459 sess->key = g_random_int ();
463 for (i = 0; i < 32; i++) {
465 g_hash_table_new_full (NULL, NULL, NULL,
466 (GDestroyNotify) g_object_unref);
469 rtp_stats_init_defaults (&sess->stats);
470 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
471 rtp_stats_set_min_interval (&sess->stats,
472 (gdouble) DEFAULT_RTCP_MIN_INTERVAL / GST_SECOND);
474 sess->recalc_bandwidth = TRUE;
475 sess->bandwidth = DEFAULT_BANDWIDTH;
476 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
477 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
478 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
480 /* default UDP header length */
481 sess->header_len = 28;
482 sess->mtu = DEFAULT_RTCP_MTU;
484 sess->probation = DEFAULT_PROBATION;
486 /* some default SDES entries */
487 sess->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
489 /* we do not want to leak details like the username or hostname here */
490 str = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
491 gst_structure_set (sess->sdes, "cname", G_TYPE_STRING, str, NULL);
495 /* we do not want to leak the user's real name here */
496 str = g_strdup_printf ("Anon%u", g_random_int ());
497 gst_structure_set (sdes, "name", G_TYPE_STRING, str, NULL);
501 gst_structure_set (sess->sdes, "tool", G_TYPE_STRING, "GStreamer", NULL);
503 /* this is the SSRC we suggest */
504 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
506 sess->first_rtcp = TRUE;
507 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
509 sess->allow_early = TRUE;
510 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
511 sess->rtcp_feedback_retention_window = DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW;
512 sess->rtcp_immediate_feedback_threshold =
513 DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD;
515 sess->last_keyframe_request = GST_CLOCK_TIME_NONE;
519 rtp_session_finalize (GObject * object)
524 sess = RTP_SESSION_CAST (object);
526 gst_structure_free (sess->sdes);
528 for (i = 0; i < 32; i++)
529 g_hash_table_destroy (sess->ssrcs[i]);
531 g_mutex_clear (&sess->lock);
533 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
537 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
539 GValue value = { 0 };
541 g_value_init (&value, RTP_TYPE_SOURCE);
542 g_value_take_object (&value, source);
543 /* copies the value */
544 g_value_array_append (arr, &value);
548 rtp_session_create_sources (RTPSession * sess)
553 RTP_SESSION_LOCK (sess);
554 /* get number of elements in the table */
555 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
556 /* create the result value array */
557 res = g_value_array_new (size);
559 /* and copy all values into the array */
560 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
561 RTP_SESSION_UNLOCK (sess);
567 rtp_session_set_property (GObject * object, guint prop_id,
568 const GValue * value, GParamSpec * pspec)
572 sess = RTP_SESSION (object);
575 case PROP_INTERNAL_SSRC:
578 RTP_SESSION_LOCK (sess);
579 sess->bandwidth = g_value_get_double (value);
580 sess->recalc_bandwidth = TRUE;
581 RTP_SESSION_UNLOCK (sess);
583 case PROP_RTCP_FRACTION:
584 RTP_SESSION_LOCK (sess);
585 sess->rtcp_bandwidth = g_value_get_double (value);
586 sess->recalc_bandwidth = TRUE;
587 RTP_SESSION_UNLOCK (sess);
589 case PROP_RTCP_RR_BANDWIDTH:
590 RTP_SESSION_LOCK (sess);
591 sess->rtcp_rr_bandwidth = g_value_get_int (value);
592 sess->recalc_bandwidth = TRUE;
593 RTP_SESSION_UNLOCK (sess);
595 case PROP_RTCP_RS_BANDWIDTH:
596 RTP_SESSION_LOCK (sess);
597 sess->rtcp_rs_bandwidth = g_value_get_int (value);
598 sess->recalc_bandwidth = TRUE;
599 RTP_SESSION_UNLOCK (sess);
602 sess->mtu = g_value_get_uint (value);
605 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
608 sess->favor_new = g_value_get_boolean (value);
610 case PROP_RTCP_MIN_INTERVAL:
611 rtp_stats_set_min_interval (&sess->stats,
612 (gdouble) g_value_get_uint64 (value) / GST_SECOND);
613 /* trigger reconsideration */
614 RTP_SESSION_LOCK (sess);
615 sess->next_rtcp_check_time = 0;
616 RTP_SESSION_UNLOCK (sess);
617 if (sess->callbacks.reconsider)
618 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
620 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
621 sess->rtcp_immediate_feedback_threshold = g_value_get_uint (value);
624 sess->probation = g_value_get_uint (value);
627 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
633 rtp_session_get_property (GObject * object, guint prop_id,
634 GValue * value, GParamSpec * pspec)
638 sess = RTP_SESSION (object);
641 case PROP_INTERNAL_SSRC:
642 g_value_set_uint (value, rtp_session_suggest_ssrc (sess));
644 case PROP_INTERNAL_SOURCE:
645 /* FIXME, return a random source */
646 g_value_set_object (value, NULL);
649 g_value_set_double (value, sess->bandwidth);
651 case PROP_RTCP_FRACTION:
652 g_value_set_double (value, sess->rtcp_bandwidth);
654 case PROP_RTCP_RR_BANDWIDTH:
655 g_value_set_int (value, sess->rtcp_rr_bandwidth);
657 case PROP_RTCP_RS_BANDWIDTH:
658 g_value_set_int (value, sess->rtcp_rs_bandwidth);
661 g_value_set_uint (value, sess->mtu);
664 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
666 case PROP_NUM_SOURCES:
667 g_value_set_uint (value, rtp_session_get_num_sources (sess));
669 case PROP_NUM_ACTIVE_SOURCES:
670 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
673 g_value_take_boxed (value, rtp_session_create_sources (sess));
676 g_value_set_boolean (value, sess->favor_new);
678 case PROP_RTCP_MIN_INTERVAL:
679 g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
681 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
682 g_value_set_uint (value, sess->rtcp_immediate_feedback_threshold);
685 g_value_set_uint (value, sess->probation);
688 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
694 on_new_ssrc (RTPSession * sess, RTPSource * source)
696 g_object_ref (source);
697 RTP_SESSION_UNLOCK (sess);
698 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
699 RTP_SESSION_LOCK (sess);
700 g_object_unref (source);
704 on_ssrc_collision (RTPSession * sess, RTPSource * source)
706 g_object_ref (source);
707 RTP_SESSION_UNLOCK (sess);
708 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
710 RTP_SESSION_LOCK (sess);
711 g_object_unref (source);
715 on_ssrc_validated (RTPSession * sess, RTPSource * source)
717 g_object_ref (source);
718 RTP_SESSION_UNLOCK (sess);
719 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
721 RTP_SESSION_LOCK (sess);
722 g_object_unref (source);
726 on_ssrc_active (RTPSession * sess, RTPSource * source)
728 g_object_ref (source);
729 RTP_SESSION_UNLOCK (sess);
730 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
731 RTP_SESSION_LOCK (sess);
732 g_object_unref (source);
736 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
738 g_object_ref (source);
739 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
740 RTP_SESSION_UNLOCK (sess);
741 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
742 RTP_SESSION_LOCK (sess);
743 g_object_unref (source);
747 on_bye_ssrc (RTPSession * sess, RTPSource * source)
749 g_object_ref (source);
750 RTP_SESSION_UNLOCK (sess);
751 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
752 RTP_SESSION_LOCK (sess);
753 g_object_unref (source);
757 on_bye_timeout (RTPSession * sess, RTPSource * source)
759 g_object_ref (source);
760 RTP_SESSION_UNLOCK (sess);
761 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
762 RTP_SESSION_LOCK (sess);
763 g_object_unref (source);
767 on_timeout (RTPSession * sess, RTPSource * source)
769 g_object_ref (source);
770 RTP_SESSION_UNLOCK (sess);
771 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
772 RTP_SESSION_LOCK (sess);
773 g_object_unref (source);
777 on_sender_timeout (RTPSession * sess, RTPSource * source)
779 g_object_ref (source);
780 RTP_SESSION_UNLOCK (sess);
781 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
783 RTP_SESSION_LOCK (sess);
784 g_object_unref (source);
790 * Create a new session object.
792 * Returns: a new #RTPSession. g_object_unref() after usage.
795 rtp_session_new (void)
799 sess = g_object_new (RTP_TYPE_SESSION, NULL);
805 * rtp_session_set_callbacks:
806 * @sess: an #RTPSession
807 * @callbacks: callbacks to configure
808 * @user_data: user data passed in the callbacks
810 * Configure a set of callbacks to be notified of actions.
813 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
816 g_return_if_fail (RTP_IS_SESSION (sess));
818 if (callbacks->process_rtp) {
819 sess->callbacks.process_rtp = callbacks->process_rtp;
820 sess->process_rtp_user_data = user_data;
822 if (callbacks->send_rtp) {
823 sess->callbacks.send_rtp = callbacks->send_rtp;
824 sess->send_rtp_user_data = user_data;
826 if (callbacks->send_rtcp) {
827 sess->callbacks.send_rtcp = callbacks->send_rtcp;
828 sess->send_rtcp_user_data = user_data;
830 if (callbacks->sync_rtcp) {
831 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
832 sess->sync_rtcp_user_data = user_data;
834 if (callbacks->clock_rate) {
835 sess->callbacks.clock_rate = callbacks->clock_rate;
836 sess->clock_rate_user_data = user_data;
838 if (callbacks->reconsider) {
839 sess->callbacks.reconsider = callbacks->reconsider;
840 sess->reconsider_user_data = user_data;
842 if (callbacks->request_key_unit) {
843 sess->callbacks.request_key_unit = callbacks->request_key_unit;
844 sess->request_key_unit_user_data = user_data;
846 if (callbacks->request_time) {
847 sess->callbacks.request_time = callbacks->request_time;
848 sess->request_time_user_data = user_data;
850 if (callbacks->notify_nack) {
851 sess->callbacks.notify_nack = callbacks->notify_nack;
852 sess->notify_nack_user_data = user_data;
857 * rtp_session_set_process_rtp_callback:
858 * @sess: an #RTPSession
859 * @callback: callback to set
860 * @user_data: user data passed in the callback
862 * Configure only the process_rtp callback to be notified of the process_rtp action.
865 rtp_session_set_process_rtp_callback (RTPSession * sess,
866 RTPSessionProcessRTP callback, gpointer user_data)
868 g_return_if_fail (RTP_IS_SESSION (sess));
870 sess->callbacks.process_rtp = callback;
871 sess->process_rtp_user_data = user_data;
875 * rtp_session_set_send_rtp_callback:
876 * @sess: an #RTPSession
877 * @callback: callback to set
878 * @user_data: user data passed in the callback
880 * Configure only the send_rtp callback to be notified of the send_rtp action.
883 rtp_session_set_send_rtp_callback (RTPSession * sess,
884 RTPSessionSendRTP callback, gpointer user_data)
886 g_return_if_fail (RTP_IS_SESSION (sess));
888 sess->callbacks.send_rtp = callback;
889 sess->send_rtp_user_data = user_data;
893 * rtp_session_set_send_rtcp_callback:
894 * @sess: an #RTPSession
895 * @callback: callback to set
896 * @user_data: user data passed in the callback
898 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
901 rtp_session_set_send_rtcp_callback (RTPSession * sess,
902 RTPSessionSendRTCP callback, gpointer user_data)
904 g_return_if_fail (RTP_IS_SESSION (sess));
906 sess->callbacks.send_rtcp = callback;
907 sess->send_rtcp_user_data = user_data;
911 * rtp_session_set_sync_rtcp_callback:
912 * @sess: an #RTPSession
913 * @callback: callback to set
914 * @user_data: user data passed in the callback
916 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
919 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
920 RTPSessionSyncRTCP callback, gpointer user_data)
922 g_return_if_fail (RTP_IS_SESSION (sess));
924 sess->callbacks.sync_rtcp = callback;
925 sess->sync_rtcp_user_data = user_data;
929 * rtp_session_set_clock_rate_callback:
930 * @sess: an #RTPSession
931 * @callback: callback to set
932 * @user_data: user data passed in the callback
934 * Configure only the clock_rate callback to be notified of the clock_rate action.
937 rtp_session_set_clock_rate_callback (RTPSession * sess,
938 RTPSessionClockRate callback, gpointer user_data)
940 g_return_if_fail (RTP_IS_SESSION (sess));
942 sess->callbacks.clock_rate = callback;
943 sess->clock_rate_user_data = user_data;
947 * rtp_session_set_reconsider_callback:
948 * @sess: an #RTPSession
949 * @callback: callback to set
950 * @user_data: user data passed in the callback
952 * Configure only the reconsider callback to be notified of the reconsider action.
955 rtp_session_set_reconsider_callback (RTPSession * sess,
956 RTPSessionReconsider callback, gpointer user_data)
958 g_return_if_fail (RTP_IS_SESSION (sess));
960 sess->callbacks.reconsider = callback;
961 sess->reconsider_user_data = user_data;
965 * rtp_session_set_request_time_callback:
966 * @sess: an #RTPSession
967 * @callback: callback to set
968 * @user_data: user data passed in the callback
970 * Configure only the request_time callback
973 rtp_session_set_request_time_callback (RTPSession * sess,
974 RTPSessionRequestTime callback, gpointer user_data)
976 g_return_if_fail (RTP_IS_SESSION (sess));
978 sess->callbacks.request_time = callback;
979 sess->request_time_user_data = user_data;
983 * rtp_session_set_bandwidth:
984 * @sess: an #RTPSession
985 * @bandwidth: the bandwidth allocated
987 * Set the session bandwidth in bytes per second.
990 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
992 g_return_if_fail (RTP_IS_SESSION (sess));
994 RTP_SESSION_LOCK (sess);
995 sess->stats.bandwidth = bandwidth;
996 RTP_SESSION_UNLOCK (sess);
1000 * rtp_session_get_bandwidth:
1001 * @sess: an #RTPSession
1003 * Get the session bandwidth.
1005 * Returns: the session bandwidth.
1008 rtp_session_get_bandwidth (RTPSession * sess)
1012 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1014 RTP_SESSION_LOCK (sess);
1015 result = sess->stats.bandwidth;
1016 RTP_SESSION_UNLOCK (sess);
1022 * rtp_session_set_rtcp_fraction:
1023 * @sess: an #RTPSession
1024 * @bandwidth: the RTCP bandwidth
1026 * Set the bandwidth in bytes per second that should be used for RTCP
1030 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
1032 g_return_if_fail (RTP_IS_SESSION (sess));
1034 RTP_SESSION_LOCK (sess);
1035 sess->stats.rtcp_bandwidth = bandwidth;
1036 RTP_SESSION_UNLOCK (sess);
1040 * rtp_session_get_rtcp_fraction:
1041 * @sess: an #RTPSession
1043 * Get the session bandwidth used for RTCP.
1045 * Returns: The bandwidth used for RTCP messages.
1048 rtp_session_get_rtcp_fraction (RTPSession * sess)
1052 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
1054 RTP_SESSION_LOCK (sess);
1055 result = sess->stats.rtcp_bandwidth;
1056 RTP_SESSION_UNLOCK (sess);
1062 * rtp_session_get_sdes_struct:
1063 * @sess: an #RTSPSession
1065 * Get the SDES data as a #GstStructure
1067 * Returns: a GstStructure with SDES items for @sess. This function returns a
1068 * copy of the SDES structure, use gst_structure_free() after usage.
1071 rtp_session_get_sdes_struct (RTPSession * sess)
1073 GstStructure *result = NULL;
1075 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1077 RTP_SESSION_LOCK (sess);
1079 result = gst_structure_copy (sess->sdes);
1080 RTP_SESSION_UNLOCK (sess);
1086 * rtp_session_set_sdes_struct:
1087 * @sess: an #RTSPSession
1088 * @sdes: a #GstStructure
1090 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1093 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
1095 g_return_if_fail (sdes);
1096 g_return_if_fail (RTP_IS_SESSION (sess));
1098 RTP_SESSION_LOCK (sess);
1100 gst_structure_free (sess->sdes);
1101 sess->sdes = gst_structure_copy (sdes);
1102 RTP_SESSION_UNLOCK (sess);
1105 static GstFlowReturn
1106 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1108 GstFlowReturn result = GST_FLOW_OK;
1110 if (source->internal) {
1111 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1113 RTP_SESSION_UNLOCK (session);
1115 if (session->callbacks.send_rtp)
1117 session->callbacks.send_rtp (session, source, data,
1118 session->send_rtp_user_data);
1120 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1123 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1124 RTP_SESSION_UNLOCK (session);
1126 if (session->callbacks.process_rtp)
1128 session->callbacks.process_rtp (session, source,
1129 GST_BUFFER_CAST (data), session->process_rtp_user_data);
1131 gst_buffer_unref (GST_BUFFER_CAST (data));
1133 RTP_SESSION_LOCK (session);
1139 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1143 RTP_SESSION_UNLOCK (session);
1145 if (session->callbacks.clock_rate)
1147 session->callbacks.clock_rate (session, pt,
1148 session->clock_rate_user_data);
1152 RTP_SESSION_LOCK (session);
1154 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1159 static RTPSourceCallbacks callbacks = {
1160 (RTPSourcePushRTP) source_push_rtp,
1161 (RTPSourceClockRate) source_clock_rate,
1165 check_collision (RTPSession * sess, RTPSource * source,
1166 RTPPacketInfo * pinfo, gboolean rtp)
1170 /* If we have no pinfo address, we can't do collision checking */
1171 if (!pinfo->address)
1174 ssrc = rtp_source_get_ssrc (source);
1176 if (!source->internal) {
1177 GSocketAddress *from;
1179 /* This is not our local source, but lets check if two remote
1182 from = source->rtp_from;
1184 from = source->rtcp_from;
1188 if (__g_socket_address_equal (from, pinfo->address)) {
1189 /* Address is the same */
1192 GST_LOG ("we have a third-party collision or loop ssrc:%x", ssrc);
1193 if (sess->favor_new) {
1194 if (rtp_source_find_conflicting_address (source,
1195 pinfo->address, pinfo->current_time)) {
1198 buf1 = __g_socket_address_to_string (pinfo->address);
1199 GST_LOG ("Known conflict on %x for %s, dropping packet", ssrc,
1207 /* Current address is not a known conflict, lets assume this is
1208 * a new source. Save old address in possible conflict list
1210 rtp_source_add_conflicting_address (source, from,
1211 pinfo->current_time);
1213 buf1 = __g_socket_address_to_string (from);
1214 buf2 = __g_socket_address_to_string (pinfo->address);
1216 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1217 " saving old as known conflict", ssrc, buf1, buf2);
1220 rtp_source_set_rtp_from (source, pinfo->address);
1222 rtp_source_set_rtcp_from (source, pinfo->address);
1230 /* Don't need to save old addresses, we ignore new sources */
1235 /* We don't already have a from address for RTP, just set it */
1237 rtp_source_set_rtp_from (source, pinfo->address);
1239 rtp_source_set_rtcp_from (source, pinfo->address);
1243 /* FIXME: Log 3rd party collision somehow
1244 * Maybe should be done in upper layer, only the SDES can tell us
1245 * if its a collision or a loop
1248 /* This is sending with our ssrc, is it an address we already know */
1249 if (rtp_source_find_conflicting_address (source, pinfo->address,
1250 pinfo->current_time)) {
1251 /* Its a known conflict, its probably a loop, not a collision
1252 * lets just drop the incoming packet
1254 GST_DEBUG ("Our packets are being looped back to us, dropping");
1256 /* Its a new collision, lets change our SSRC */
1257 rtp_source_add_conflicting_address (source, pinfo->address,
1258 pinfo->current_time);
1260 GST_DEBUG ("Collision for SSRC %x", ssrc);
1261 /* mark the source BYE */
1262 rtp_source_mark_bye (source, "SSRC Collision");
1263 /* if we were suggesting this SSRC, change to something else */
1264 if (sess->suggested_ssrc == ssrc)
1265 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
1267 on_ssrc_collision (sess, source);
1269 rtp_session_schedule_bye_locked (sess, pinfo->current_time);
1277 find_source (RTPSession * sess, guint32 ssrc)
1279 return g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1280 GINT_TO_POINTER (ssrc));
1284 add_source (RTPSession * sess, RTPSource * src)
1286 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1287 GINT_TO_POINTER (src->ssrc), src);
1288 /* report the new source ASAP */
1289 src->generation = sess->generation;
1290 /* we have one more source now */
1291 sess->total_sources++;
1292 if (RTP_SOURCE_IS_ACTIVE (src))
1293 sess->stats.active_sources++;
1294 if (src->internal) {
1295 sess->stats.internal_sources++;
1296 if (sess->suggested_ssrc != src->ssrc)
1297 sess->suggested_ssrc = src->ssrc;
1301 /* must be called with the session lock, the returned source needs to be
1302 * unreffed after usage. */
1304 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1305 RTPPacketInfo * pinfo, gboolean rtp)
1309 source = find_source (sess, ssrc);
1310 if (source == NULL) {
1311 /* make new Source in probation and insert */
1312 source = rtp_source_new (ssrc);
1314 GST_DEBUG ("creating new source %08x %p", ssrc, source);
1316 /* for RTP packets we need to set the source in probation. Receiving RTCP
1317 * packets of an SSRC, on the other hand, is a strong indication that we
1318 * are dealing with a valid source. */
1320 g_object_set (source, "probation", sess->probation, NULL);
1322 g_object_set (source, "probation", 0, NULL);
1324 /* store from address, if any */
1325 if (pinfo->address) {
1327 rtp_source_set_rtp_from (source, pinfo->address);
1329 rtp_source_set_rtcp_from (source, pinfo->address);
1332 /* configure a callback on the source */
1333 rtp_source_set_callbacks (source, &callbacks, sess);
1335 add_source (sess, source);
1339 /* check for collision, this updates the address when not previously set */
1340 if (check_collision (sess, source, pinfo, rtp)) {
1343 /* Receiving RTCP packets of an SSRC is a strong indication that we
1344 * are dealing with a valid source. */
1346 g_object_set (source, "probation", 0, NULL);
1348 /* update last activity */
1349 source->last_activity = pinfo->current_time;
1351 source->last_rtp_activity = pinfo->current_time;
1352 g_object_ref (source);
1357 /* must be called with the session lock, the returned source needs to be
1358 * unreffed after usage. */
1360 obtain_internal_source (RTPSession * sess, guint32 ssrc, gboolean * created)
1364 source = find_source (sess, ssrc);
1365 if (source == NULL) {
1366 /* make new internal Source and insert */
1367 source = rtp_source_new (ssrc);
1369 GST_DEBUG ("creating new internal source %08x %p", ssrc, source);
1371 source->validated = TRUE;
1372 source->internal = TRUE;
1373 source->probation = FALSE;
1374 rtp_source_set_sdes_struct (source, gst_structure_copy (sess->sdes));
1375 rtp_source_set_callbacks (source, &callbacks, sess);
1377 add_source (sess, source);
1382 g_object_ref (source);
1388 * rtp_session_suggest_ssrc:
1389 * @sess: a #RTPSession
1391 * Suggest an unused SSRC in @sess.
1393 * Returns: a free unused SSRC
1396 rtp_session_suggest_ssrc (RTPSession * sess)
1400 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1402 RTP_SESSION_LOCK (sess);
1403 result = sess->suggested_ssrc;
1404 RTP_SESSION_UNLOCK (sess);
1410 * rtp_session_add_source:
1411 * @sess: a #RTPSession
1412 * @src: #RTPSource to add
1414 * Add @src to @session.
1416 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1417 * existed in the session.
1420 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1422 gboolean result = FALSE;
1425 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1426 g_return_val_if_fail (src != NULL, FALSE);
1428 RTP_SESSION_LOCK (sess);
1429 find = find_source (sess, src->ssrc);
1431 add_source (sess, src);
1434 RTP_SESSION_UNLOCK (sess);
1440 * rtp_session_get_num_sources:
1441 * @sess: an #RTPSession
1443 * Get the number of sources in @sess.
1445 * Returns: The number of sources in @sess.
1448 rtp_session_get_num_sources (RTPSession * sess)
1452 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1454 RTP_SESSION_LOCK (sess);
1455 result = sess->total_sources;
1456 RTP_SESSION_UNLOCK (sess);
1462 * rtp_session_get_num_active_sources:
1463 * @sess: an #RTPSession
1465 * Get the number of active sources in @sess. A source is considered active when
1466 * it has been validated and has not yet received a BYE RTCP message.
1468 * Returns: The number of active sources in @sess.
1471 rtp_session_get_num_active_sources (RTPSession * sess)
1475 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1477 RTP_SESSION_LOCK (sess);
1478 result = sess->stats.active_sources;
1479 RTP_SESSION_UNLOCK (sess);
1485 * rtp_session_get_source_by_ssrc:
1486 * @sess: an #RTPSession
1489 * Find the source with @ssrc in @sess.
1491 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1492 * g_object_unref() after usage.
1495 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1499 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1501 RTP_SESSION_LOCK (sess);
1502 result = find_source (sess, ssrc);
1504 g_object_ref (result);
1505 RTP_SESSION_UNLOCK (sess);
1510 /* should be called with the SESSION lock */
1512 rtp_session_create_new_ssrc (RTPSession * sess)
1517 ssrc = g_random_int ();
1519 /* see if it exists in the session, we're done if it doesn't */
1520 if (find_source (sess, ssrc) == NULL)
1528 * rtp_session_create_source:
1529 * @sess: an #RTPSession
1531 * Create an #RTPSource for use in @sess. This function will create a source
1532 * with an ssrc that is currently not used by any participants in the session.
1534 * Returns: an #RTPSource.
1537 rtp_session_create_source (RTPSession * sess)
1542 RTP_SESSION_LOCK (sess);
1543 ssrc = rtp_session_create_new_ssrc (sess);
1544 source = rtp_source_new (ssrc);
1545 rtp_source_set_callbacks (source, &callbacks, sess);
1546 /* we need an additional ref for the source in the hashtable */
1547 g_object_ref (source);
1548 add_source (sess, source);
1549 RTP_SESSION_UNLOCK (sess);
1555 update_packet (GstBuffer ** buffer, guint idx, RTPPacketInfo * pinfo)
1557 GstNetAddressMeta *meta;
1559 /* get packet size including header overhead */
1560 pinfo->bytes += gst_buffer_get_size (*buffer) + pinfo->header_len;
1564 GstRTPBuffer rtp = { NULL };
1566 if (!gst_rtp_buffer_map (*buffer, GST_MAP_READ, &rtp))
1567 goto invalid_packet;
1569 pinfo->payload_len += gst_rtp_buffer_get_payload_len (&rtp);
1573 /* only keep info for first buffer */
1574 pinfo->ssrc = gst_rtp_buffer_get_ssrc (&rtp);
1575 pinfo->seqnum = gst_rtp_buffer_get_seq (&rtp);
1576 pinfo->pt = gst_rtp_buffer_get_payload_type (&rtp);
1577 pinfo->rtptime = gst_rtp_buffer_get_timestamp (&rtp);
1578 /* copy available csrc */
1579 pinfo->csrc_count = gst_rtp_buffer_get_csrc_count (&rtp);
1580 for (i = 0; i < pinfo->csrc_count; i++)
1581 pinfo->csrcs[i] = gst_rtp_buffer_get_csrc (&rtp, i);
1583 gst_rtp_buffer_unmap (&rtp);
1587 /* for netbuffer we can store the IP address to check for collisions */
1588 meta = gst_buffer_get_net_address_meta (*buffer);
1590 g_object_unref (pinfo->address);
1592 pinfo->address = G_SOCKET_ADDRESS (g_object_ref (meta->addr));
1594 pinfo->address = NULL;
1602 GST_DEBUG ("invalid RTP packet received");
1607 /* update the RTPPacketInfo structure with the current time and other bits
1608 * about the current buffer we are handling.
1609 * This function is typically called when a validated packet is received.
1610 * This function should be called with the SESSION_LOCK
1613 update_packet_info (RTPSession * sess, RTPPacketInfo * pinfo,
1614 gboolean send, gboolean rtp, gboolean is_list, gpointer data,
1615 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
1621 pinfo->is_list = is_list;
1623 pinfo->current_time = current_time;
1624 pinfo->running_time = running_time;
1625 pinfo->ntpnstime = ntpnstime;
1626 pinfo->header_len = sess->header_len;
1628 pinfo->payload_len = 0;
1632 GstBufferList *list = GST_BUFFER_LIST_CAST (data);
1634 gst_buffer_list_foreach (list, (GstBufferListFunc) update_packet,
1637 GstBuffer *buffer = GST_BUFFER_CAST (data);
1638 res = update_packet (&buffer, 0, pinfo);
1644 clean_packet_info (RTPPacketInfo * pinfo)
1647 g_object_unref (pinfo->address);
1649 gst_mini_object_unref (pinfo->data);
1655 source_update_active (RTPSession * sess, RTPSource * source,
1656 gboolean prevactive)
1658 gboolean active = RTP_SOURCE_IS_ACTIVE (source);
1659 guint32 ssrc = source->ssrc;
1661 if (prevactive == active)
1665 sess->stats.active_sources++;
1666 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1667 sess->stats.active_sources);
1669 sess->stats.active_sources--;
1670 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
1671 sess->stats.active_sources);
1677 source_update_sender (RTPSession * sess, RTPSource * source,
1678 gboolean prevsender)
1680 gboolean sender = RTP_SOURCE_IS_SENDER (source);
1681 guint32 ssrc = source->ssrc;
1683 if (prevsender == sender)
1687 sess->stats.sender_sources++;
1688 if (source->internal)
1689 sess->stats.internal_sender_sources++;
1690 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1691 sess->stats.sender_sources);
1693 sess->stats.sender_sources--;
1694 if (source->internal)
1695 sess->stats.internal_sender_sources--;
1696 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
1697 sess->stats.sender_sources);
1703 * rtp_session_process_rtp:
1704 * @sess: and #RTPSession
1705 * @buffer: an RTP buffer
1706 * @current_time: the current system time
1707 * @running_time: the running_time of @buffer
1709 * Process an RTP buffer in the session manager. This function takes ownership
1712 * Returns: a #GstFlowReturn.
1715 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1716 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
1718 GstFlowReturn result;
1722 gboolean prevsender, prevactive;
1723 RTPPacketInfo pinfo = { 0, };
1726 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1727 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1729 RTP_SESSION_LOCK (sess);
1731 /* update pinfo stats */
1732 if (!update_packet_info (sess, &pinfo, FALSE, TRUE, FALSE, buffer,
1733 current_time, running_time, ntpnstime)) {
1734 GST_DEBUG ("invalid RTP packet received");
1735 RTP_SESSION_UNLOCK (sess);
1736 return rtp_session_process_rtcp (sess, buffer, current_time, ntpnstime);
1741 source = obtain_source (sess, ssrc, &created, &pinfo, TRUE);
1745 prevsender = RTP_SOURCE_IS_SENDER (source);
1746 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1747 oldrate = source->bitrate;
1749 /* let source process the packet */
1750 result = rtp_source_process_rtp (source, &pinfo);
1752 /* source became active */
1753 if (source_update_active (sess, source, prevactive))
1754 on_ssrc_validated (sess, source);
1756 source_update_sender (sess, source, prevsender);
1758 if (oldrate != source->bitrate)
1759 sess->recalc_bandwidth = TRUE;
1762 on_new_ssrc (sess, source);
1764 if (source->validated) {
1768 /* for validated sources, we add the CSRCs as well */
1769 for (i = 0; i < pinfo.csrc_count; i++) {
1771 RTPSource *csrc_src;
1773 csrc = pinfo.csrcs[i];
1776 csrc_src = obtain_source (sess, csrc, &created, &pinfo, TRUE);
1781 GST_DEBUG ("created new CSRC: %08x", csrc);
1782 rtp_source_set_as_csrc (csrc_src);
1783 source_update_active (sess, csrc_src, FALSE);
1784 on_new_ssrc (sess, csrc_src);
1786 g_object_unref (csrc_src);
1789 g_object_unref (source);
1791 RTP_SESSION_UNLOCK (sess);
1793 clean_packet_info (&pinfo);
1800 RTP_SESSION_UNLOCK (sess);
1801 gst_buffer_unref (buffer);
1802 clean_packet_info (&pinfo);
1803 GST_DEBUG ("ignoring packet because its collisioning");
1809 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1810 GstRTCPPacket * packet, RTPPacketInfo * pinfo)
1814 count = gst_rtcp_packet_get_rb_count (packet);
1815 for (i = 0; i < count; i++) {
1816 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1817 guint8 fractionlost;
1821 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1822 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1824 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1826 /* find our own source */
1827 src = find_source (sess, ssrc);
1831 if (src->internal && RTP_SOURCE_IS_ACTIVE (src)) {
1832 /* only deal with report blocks for our session, we update the stats of
1833 * the sender of the RTCP message. We could also compare our stats against
1834 * the other sender to see if we are better or worse. */
1835 /* FIXME, need to keep track who the RB block is from */
1836 rtp_source_process_rb (source, pinfo->ntpnstime, fractionlost,
1837 packetslost, exthighestseq, jitter, lsr, dlsr);
1840 on_ssrc_active (sess, source);
1843 /* A Sender report contains statistics about how the sender is doing. This
1844 * includes timing informataion such as the relation between RTP and NTP
1845 * timestamps and the number of packets/bytes it sent to us.
1847 * In this report is also included a set of report blocks related to how this
1848 * sender is receiving data (in case we (or somebody else) is also sending stuff
1849 * to it). This info includes the packet loss, jitter and seqnum. It also
1850 * contains information to calculate the round trip time (LSR/DLSR).
1853 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1854 RTPPacketInfo * pinfo, gboolean * do_sync)
1856 guint32 senderssrc, rtptime, packet_count, octet_count;
1859 gboolean created, prevsender;
1861 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1862 &packet_count, &octet_count);
1864 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1865 senderssrc, GST_TIME_ARGS (pinfo->current_time));
1867 source = obtain_source (sess, senderssrc, &created, pinfo, FALSE);
1871 /* don't try to do lip-sync for sources that sent a BYE */
1872 if (RTP_SOURCE_IS_MARKED_BYE (source))
1877 prevsender = RTP_SOURCE_IS_SENDER (source);
1879 /* first update the source */
1880 rtp_source_process_sr (source, pinfo->current_time, ntptime, rtptime,
1881 packet_count, octet_count);
1883 source_update_sender (sess, source, prevsender);
1886 on_new_ssrc (sess, source);
1888 rtp_session_process_rb (sess, source, packet, pinfo);
1889 g_object_unref (source);
1892 /* A receiver report contains statistics about how a receiver is doing. It
1893 * includes stuff like packet loss, jitter and the seqnum it received last. It
1894 * also contains info to calculate the round trip time.
1896 * We are only interested in how the sender of this report is doing wrt to us.
1899 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1900 RTPPacketInfo * pinfo)
1906 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1908 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1910 source = obtain_source (sess, senderssrc, &created, pinfo, FALSE);
1915 on_new_ssrc (sess, source);
1917 rtp_session_process_rb (sess, source, packet, pinfo);
1918 g_object_unref (source);
1921 /* Get SDES items and store them in the SSRC */
1923 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1924 RTPPacketInfo * pinfo)
1927 gboolean more_items, more_entries;
1929 items = gst_rtcp_packet_sdes_get_item_count (packet);
1930 GST_DEBUG ("got SDES packet with %d items", items);
1932 more_items = gst_rtcp_packet_sdes_first_item (packet);
1934 while (more_items) {
1936 gboolean changed, created, prevactive;
1940 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1942 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1946 /* find src, no probation when dealing with RTCP */
1947 source = obtain_source (sess, ssrc, &created, pinfo, FALSE);
1951 sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
1953 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1955 while (more_entries) {
1956 GstRTCPSDESType type;
1962 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1964 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1967 if (type == GST_RTCP_SDES_PRIV) {
1968 name = g_strndup ((const gchar *) &data[1], data[0]);
1970 data += data[0] + 1;
1972 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
1975 value = g_strndup ((const gchar *) data, len);
1977 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
1982 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1986 /* takes ownership of sdes */
1987 changed = rtp_source_set_sdes_struct (source, sdes);
1989 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1990 source->validated = TRUE;
1993 on_new_ssrc (sess, source);
1995 /* source became active */
1996 if (source_update_active (sess, source, prevactive))
1997 on_ssrc_validated (sess, source);
2000 on_ssrc_sdes (sess, source);
2002 g_object_unref (source);
2004 more_items = gst_rtcp_packet_sdes_next_item (packet);
2009 /* BYE is sent when a client leaves the session
2012 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
2013 RTPPacketInfo * pinfo)
2017 gboolean reconsider = FALSE;
2019 reason = gst_rtcp_packet_bye_get_reason (packet);
2020 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
2022 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
2023 for (i = 0; i < count; i++) {
2026 gboolean created, prevactive, prevsender;
2027 guint pmembers, members;
2029 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
2030 GST_DEBUG ("SSRC: %08x", ssrc);
2032 /* find src and mark bye, no probation when dealing with RTCP */
2033 source = obtain_source (sess, ssrc, &created, pinfo, FALSE);
2037 if (source->internal) {
2038 /* our own source, something weird with this packet */
2039 g_object_unref (source);
2043 /* store time for when we need to time out this source */
2044 source->bye_time = pinfo->current_time;
2046 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2047 prevsender = RTP_SOURCE_IS_SENDER (source);
2049 /* mark the source BYE */
2050 rtp_source_mark_bye (source, reason);
2052 pmembers = sess->stats.active_sources;
2054 source_update_active (sess, source, prevactive);
2055 source_update_sender (sess, source, prevsender);
2057 members = sess->stats.active_sources;
2059 if (!sess->scheduled_bye && members < pmembers) {
2060 /* some members went away since the previous timeout estimate.
2061 * Perform reverse reconsideration but only when we are not scheduling a
2063 if (sess->next_rtcp_check_time != GST_CLOCK_TIME_NONE &&
2064 pinfo->current_time < sess->next_rtcp_check_time) {
2065 GstClockTime time_remaining;
2067 time_remaining = sess->next_rtcp_check_time - pinfo->current_time;
2068 sess->next_rtcp_check_time =
2069 gst_util_uint64_scale (time_remaining, members, pmembers);
2071 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
2072 GST_TIME_ARGS (sess->next_rtcp_check_time));
2074 sess->next_rtcp_check_time += pinfo->current_time;
2076 /* mark pending reconsider. We only want to signal the reconsideration
2077 * once after we handled all the source in the bye packet */
2083 on_new_ssrc (sess, source);
2085 on_bye_ssrc (sess, source);
2087 g_object_unref (source);
2090 RTP_SESSION_UNLOCK (sess);
2091 /* notify app of reconsideration */
2092 if (sess->callbacks.reconsider)
2093 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2094 RTP_SESSION_LOCK (sess);
2100 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
2101 RTPPacketInfo * pinfo)
2103 GST_DEBUG ("received APP");
2107 rtp_session_request_local_key_unit (RTPSession * sess, RTPSource * src,
2108 gboolean fir, GstClockTime current_time)
2110 guint32 round_trip = 0;
2112 rtp_source_get_last_rb (src, NULL, NULL, NULL, NULL, NULL, NULL, &round_trip);
2114 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE && round_trip) {
2115 GstClockTime round_trip_in_ns = gst_util_uint64_scale (round_trip,
2118 if (current_time - sess->last_keyframe_request < 2 * round_trip_in_ns) {
2119 GST_DEBUG ("Ignoring %s request because one was send without one "
2120 "RTT (%" GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")",
2121 fir ? "FIR" : "PLI",
2122 GST_TIME_ARGS (current_time - sess->last_keyframe_request),
2123 GST_TIME_ARGS (round_trip_in_ns));;
2128 sess->last_keyframe_request = current_time;
2130 GST_LOG ("received %s request from %X %p(%p)", fir ? "FIR" : "PLI",
2131 rtp_source_get_ssrc (src), sess->callbacks.process_rtp,
2132 sess->callbacks.request_key_unit);
2134 RTP_SESSION_UNLOCK (sess);
2135 sess->callbacks.request_key_unit (sess, fir,
2136 sess->request_key_unit_user_data);
2137 RTP_SESSION_LOCK (sess);
2143 rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
2144 guint32 media_ssrc, GstClockTime current_time)
2148 if (!sess->callbacks.request_key_unit)
2151 src = find_source (sess, sender_ssrc);
2155 rtp_session_request_local_key_unit (sess, src, FALSE, current_time);
2159 rtp_session_process_fir (RTPSession * sess, guint32 sender_ssrc,
2160 guint8 * fci_data, guint fci_length, GstClockTime current_time)
2165 gboolean our_request = FALSE;
2167 if (!sess->callbacks.request_key_unit)
2173 src = find_source (sess, sender_ssrc);
2175 /* Hack because Google fails to set the sender_ssrc correctly */
2176 if (!src && sender_ssrc == 1) {
2177 GHashTableIter iter;
2179 /* we can't find the source if there are multiple */
2180 if (sess->stats.sender_sources > sess->stats.internal_sender_sources + 1)
2183 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
2184 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
2185 if (!src->internal && rtp_source_is_sender (src))
2193 for (position = 0; position < fci_length; position += 8) {
2194 guint8 *data = fci_data + position;
2197 ssrc = GST_READ_UINT32_BE (data);
2199 own = find_source (sess, ssrc);
2200 if (own->internal) {
2208 rtp_session_request_local_key_unit (sess, src, TRUE, current_time);
2212 rtp_session_process_nack (RTPSession * sess, guint32 sender_ssrc,
2213 guint32 media_ssrc, guint8 * fci_data, guint fci_length,
2214 GstClockTime current_time)
2216 if (!sess->callbacks.notify_nack)
2219 while (fci_length > 0) {
2220 guint16 seqnum, blp;
2222 seqnum = GST_READ_UINT16_BE (fci_data);
2223 blp = GST_READ_UINT16_BE (fci_data + 2);
2225 GST_DEBUG ("NACK #%u, blp %04x", seqnum, blp);
2227 RTP_SESSION_UNLOCK (sess);
2228 sess->callbacks.notify_nack (sess, seqnum, blp,
2229 sess->notify_nack_user_data);
2230 RTP_SESSION_LOCK (sess);
2238 rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
2239 RTPPacketInfo * pinfo, GstClockTime current_time)
2241 GstRTCPType type = gst_rtcp_packet_get_type (packet);
2242 GstRTCPFBType fbtype = gst_rtcp_packet_fb_get_type (packet);
2243 guint32 sender_ssrc = gst_rtcp_packet_fb_get_sender_ssrc (packet);
2244 guint32 media_ssrc = gst_rtcp_packet_fb_get_media_ssrc (packet);
2245 guint8 *fci_data = gst_rtcp_packet_fb_get_fci (packet);
2246 guint fci_length = 4 * gst_rtcp_packet_fb_get_fci_length (packet);
2249 GST_DEBUG ("received feedback %d:%d from %08X about %08X with FCI of "
2250 "length %d", type, fbtype, sender_ssrc, media_ssrc, fci_length);
2252 if (g_signal_has_handler_pending (sess,
2253 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0, TRUE)) {
2254 GstBuffer *fci_buffer = NULL;
2256 if (fci_length > 0) {
2257 fci_buffer = gst_buffer_copy_region (packet->rtcp->buffer,
2258 GST_BUFFER_COPY_MEMORY, fci_data - packet->rtcp->map.data,
2260 GST_BUFFER_TIMESTAMP (fci_buffer) = pinfo->running_time;
2263 RTP_SESSION_UNLOCK (sess);
2264 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0,
2265 type, fbtype, sender_ssrc, media_ssrc, fci_buffer);
2266 RTP_SESSION_LOCK (sess);
2269 gst_buffer_unref (fci_buffer);
2272 src = find_source (sess, media_ssrc);
2276 if (sess->rtcp_feedback_retention_window) {
2277 rtp_source_retain_rtcp_packet (src, packet, pinfo->running_time);
2280 if (src->internal ||
2281 /* PSFB FIR puts the media ssrc inside the FCI */
2282 (type == GST_RTCP_TYPE_PSFB && fbtype == GST_RTCP_PSFB_TYPE_FIR)) {
2284 case GST_RTCP_TYPE_PSFB:
2286 case GST_RTCP_PSFB_TYPE_PLI:
2287 rtp_session_process_pli (sess, sender_ssrc, media_ssrc,
2290 case GST_RTCP_PSFB_TYPE_FIR:
2291 rtp_session_process_fir (sess, sender_ssrc, fci_data, fci_length,
2298 case GST_RTCP_TYPE_RTPFB:
2300 case GST_RTCP_RTPFB_TYPE_NACK:
2301 rtp_session_process_nack (sess, sender_ssrc, media_ssrc,
2302 fci_data, fci_length, current_time);
2314 * rtp_session_process_rtcp:
2315 * @sess: and #RTPSession
2316 * @buffer: an RTCP buffer
2317 * @current_time: the current system time
2318 * @ntpnstime: the current NTP time in nanoseconds
2320 * Process an RTCP buffer in the session manager. This function takes ownership
2323 * Returns: a #GstFlowReturn.
2326 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
2327 GstClockTime current_time, guint64 ntpnstime)
2329 GstRTCPPacket packet;
2330 gboolean more, is_bye = FALSE, do_sync = FALSE;
2331 RTPPacketInfo pinfo = { 0, };
2332 GstFlowReturn result = GST_FLOW_OK;
2333 GstRTCPBuffer rtcp = { NULL, };
2335 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2336 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2338 if (!gst_rtcp_buffer_validate (buffer))
2339 goto invalid_packet;
2341 GST_DEBUG ("received RTCP packet");
2343 RTP_SESSION_LOCK (sess);
2344 /* update pinfo stats */
2345 update_packet_info (sess, &pinfo, FALSE, FALSE, FALSE, buffer, current_time,
2348 /* start processing the compound packet */
2349 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
2350 more = gst_rtcp_buffer_get_first_packet (&rtcp, &packet);
2354 type = gst_rtcp_packet_get_type (&packet);
2356 /* when we are leaving the session, we should ignore all non-BYE messages */
2357 if (sess->scheduled_bye && type != GST_RTCP_TYPE_BYE) {
2358 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
2363 case GST_RTCP_TYPE_SR:
2364 rtp_session_process_sr (sess, &packet, &pinfo, &do_sync);
2366 case GST_RTCP_TYPE_RR:
2367 rtp_session_process_rr (sess, &packet, &pinfo);
2369 case GST_RTCP_TYPE_SDES:
2370 rtp_session_process_sdes (sess, &packet, &pinfo);
2372 case GST_RTCP_TYPE_BYE:
2374 /* don't try to attempt lip-sync anymore for streams with a BYE */
2376 rtp_session_process_bye (sess, &packet, &pinfo);
2378 case GST_RTCP_TYPE_APP:
2379 rtp_session_process_app (sess, &packet, &pinfo);
2381 case GST_RTCP_TYPE_RTPFB:
2382 case GST_RTCP_TYPE_PSFB:
2383 rtp_session_process_feedback (sess, &packet, &pinfo, current_time);
2386 GST_WARNING ("got unknown RTCP packet");
2390 more = gst_rtcp_packet_move_to_next (&packet);
2393 gst_rtcp_buffer_unmap (&rtcp);
2395 /* if we are scheduling a BYE, we only want to count bye packets, else we
2396 * count everything */
2397 if (sess->scheduled_bye) {
2399 sess->stats.bye_members++;
2400 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, pinfo.bytes);
2403 /* keep track of average packet size */
2404 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, pinfo.bytes);
2406 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
2407 sess->stats.avg_rtcp_packet_size, pinfo.bytes);
2408 RTP_SESSION_UNLOCK (sess);
2411 clean_packet_info (&pinfo);
2413 /* notify caller of sr packets in the callback */
2414 if (do_sync && sess->callbacks.sync_rtcp) {
2415 result = sess->callbacks.sync_rtcp (sess, buffer,
2416 sess->sync_rtcp_user_data);
2418 gst_buffer_unref (buffer);
2425 GST_DEBUG ("invalid RTCP packet received");
2426 gst_buffer_unref (buffer);
2432 * rtp_session_update_send_caps:
2433 * @sess: an #RTPSession
2436 * Update the caps of the sender in the rtp session.
2439 rtp_session_update_send_caps (RTPSession * sess, GstCaps * caps)
2444 g_return_if_fail (RTP_IS_SESSION (sess));
2445 g_return_if_fail (GST_IS_CAPS (caps));
2447 GST_LOG ("received caps %" GST_PTR_FORMAT, caps);
2449 s = gst_caps_get_structure (caps, 0);
2451 if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
2455 RTP_SESSION_LOCK (sess);
2456 source = obtain_internal_source (sess, ssrc, &created);
2458 rtp_source_update_caps (source, caps);
2459 g_object_unref (source);
2461 RTP_SESSION_UNLOCK (sess);
2466 * rtp_session_send_rtp:
2467 * @sess: an #RTPSession
2468 * @data: pointer to either an RTP buffer or a list of RTP buffers
2469 * @is_list: TRUE when @data is a buffer list
2470 * @current_time: the current system time
2471 * @running_time: the running time of @data
2473 * Send the RTP buffer in the session manager. This function takes ownership of
2476 * Returns: a #GstFlowReturn.
2479 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
2480 GstClockTime current_time, GstClockTime running_time)
2482 GstFlowReturn result;
2484 gboolean prevsender;
2486 RTPPacketInfo pinfo = { 0, };
2489 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2490 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
2492 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
2494 RTP_SESSION_LOCK (sess);
2495 if (!update_packet_info (sess, &pinfo, TRUE, TRUE, is_list, data,
2496 current_time, running_time, -1))
2497 goto invalid_packet;
2499 source = obtain_internal_source (sess, pinfo.ssrc, &created);
2501 /* update last activity */
2502 source->last_rtp_activity = current_time;
2504 prevsender = RTP_SOURCE_IS_SENDER (source);
2505 oldrate = source->bitrate;
2507 /* we use our own source to send */
2508 result = rtp_source_send_rtp (source, &pinfo);
2510 source_update_sender (sess, source, prevsender);
2512 if (oldrate != source->bitrate)
2513 sess->recalc_bandwidth = TRUE;
2514 RTP_SESSION_UNLOCK (sess);
2516 g_object_unref (source);
2517 clean_packet_info (&pinfo);
2523 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
2524 RTP_SESSION_UNLOCK (sess);
2525 GST_DEBUG ("invalid RTP packet received");
2531 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
2533 *bandwidth += source->bitrate;
2536 /* must be called with session lock */
2538 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2541 GstClockTime result;
2543 /* recalculate bandwidth when it changed */
2544 if (sess->recalc_bandwidth) {
2547 if (sess->bandwidth > 0)
2548 bandwidth = sess->bandwidth;
2550 /* If it is <= 0, then try to estimate the actual bandwidth */
2553 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2554 (GHFunc) add_bitrates, &bandwidth);
2557 if (bandwidth < 8000)
2558 bandwidth = RTP_STATS_BANDWIDTH;
2560 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
2561 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
2563 sess->recalc_bandwidth = FALSE;
2566 if (sess->scheduled_bye) {
2567 result = rtp_stats_calculate_bye_interval (&sess->stats);
2569 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2570 sess->stats.internal_sender_sources > 0, first);
2573 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2574 GST_TIME_ARGS (result), first);
2576 if (!deterministic && result != GST_CLOCK_TIME_NONE)
2577 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2579 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2585 source_mark_bye (const gchar * key, RTPSource * source, const gchar * reason)
2587 if (source->internal)
2588 rtp_source_mark_bye (source, reason);
2592 * rtp_session_mark_all_bye:
2593 * @sess: an #RTPSession
2596 * Mark all internal sources of the session as BYE with @reason.
2599 rtp_session_mark_all_bye (RTPSession * sess, const gchar * reason)
2601 g_return_if_fail (RTP_IS_SESSION (sess));
2603 RTP_SESSION_LOCK (sess);
2604 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2605 (GHFunc) source_mark_bye, (gpointer) reason);
2606 RTP_SESSION_UNLOCK (sess);
2609 /* Stop the current @sess and schedule a BYE message for the other members.
2610 * One must have the session lock to call this function
2612 static GstFlowReturn
2613 rtp_session_schedule_bye_locked (RTPSession * sess, GstClockTime current_time)
2615 GstFlowReturn result = GST_FLOW_OK;
2616 GstClockTime interval;
2618 /* nothing to do it we already scheduled bye */
2619 if (sess->scheduled_bye)
2622 /* we schedule BYE now */
2623 sess->scheduled_bye = TRUE;
2624 /* at least one member wants to send a BYE */
2625 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
2626 sess->stats.bye_members = 1;
2627 sess->first_rtcp = TRUE;
2628 sess->allow_early = TRUE;
2630 /* reschedule transmission */
2631 sess->last_rtcp_send_time = current_time;
2632 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2634 if (interval != GST_CLOCK_TIME_NONE)
2635 sess->next_rtcp_check_time = current_time + interval;
2637 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
2639 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2640 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2642 RTP_SESSION_UNLOCK (sess);
2643 /* notify app of reconsideration */
2644 if (sess->callbacks.reconsider)
2645 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2646 RTP_SESSION_LOCK (sess);
2653 * rtp_session_schedule_bye:
2654 * @sess: an #RTPSession
2655 * @current_time: the current system time
2657 * Schedule a BYE message for all sources marked as BYE in @sess.
2659 * Returns: a #GstFlowReturn.
2662 rtp_session_schedule_bye (RTPSession * sess, GstClockTime current_time)
2664 GstFlowReturn result = GST_FLOW_OK;
2666 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2668 RTP_SESSION_LOCK (sess);
2669 result = rtp_session_schedule_bye_locked (sess, current_time);
2670 RTP_SESSION_UNLOCK (sess);
2676 * rtp_session_next_timeout:
2677 * @sess: an #RTPSession
2678 * @current_time: the current system time
2680 * Get the next time we should perform session maintenance tasks.
2682 * Returns: a time when rtp_session_on_timeout() should be called with the
2683 * current system time.
2686 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2688 GstClockTime result, interval = 0;
2690 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
2692 RTP_SESSION_LOCK (sess);
2694 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
2695 GST_DEBUG ("have early rtcp time");
2696 result = sess->next_early_rtcp_time;
2700 result = sess->next_rtcp_check_time;
2702 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2703 ", next time: %" GST_TIME_FORMAT,
2704 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2706 if (result == GST_CLOCK_TIME_NONE || result < current_time) {
2707 GST_DEBUG ("take current time as base");
2708 /* our previous check time expired, start counting from the current time
2710 result = current_time;
2713 if (sess->scheduled_bye) {
2714 if (sess->stats.active_sources >= 50) {
2715 GST_DEBUG ("reconsider BYE, more than 50 sources");
2716 /* reconsider BYE if members >= 50 */
2717 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2720 if (sess->first_rtcp) {
2721 GST_DEBUG ("first RTCP packet");
2722 /* we are called for the first time */
2723 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2724 } else if (sess->next_rtcp_check_time < current_time) {
2725 GST_DEBUG ("old check time expired, getting new timeout");
2726 /* get a new timeout when we need to */
2727 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
2731 if (interval != GST_CLOCK_TIME_NONE)
2734 result = GST_CLOCK_TIME_NONE;
2736 sess->next_rtcp_check_time = result;
2740 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2741 ", next time: %" GST_TIME_FORMAT,
2742 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2743 RTP_SESSION_UNLOCK (sess);
2757 GstRTCPBuffer rtcpbuf;
2760 guint num_to_report;
2765 GstClockTime current_time;
2767 GstClockTime running_time;
2768 GstClockTime interval;
2769 GstRTCPPacket packet;
2772 gboolean may_suppress;
2777 session_start_rtcp (RTPSession * sess, ReportData * data)
2779 GstRTCPPacket *packet = &data->packet;
2780 RTPSource *own = data->source;
2781 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2783 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2784 data->has_sdes = FALSE;
2786 gst_rtcp_buffer_map (data->rtcp, GST_MAP_READWRITE, rtcp);
2788 if (RTP_SOURCE_IS_SENDER (own)) {
2791 guint32 packet_count, octet_count;
2793 /* we are a sender, create SR */
2794 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2795 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SR, packet);
2797 /* get latest stats */
2798 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
2799 &ntptime, &rtptime, &packet_count, &octet_count);
2801 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2802 packet_count, octet_count);
2804 /* fill in sender report info */
2805 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2806 ntptime, rtptime, packet_count, octet_count);
2808 /* we are only receiver, create RR */
2809 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2810 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RR, packet);
2811 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2815 /* construct a Sender or Receiver Report */
2817 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2819 RTPSession *sess = data->sess;
2820 GstRTCPPacket *packet = &data->packet;
2821 guint8 fractionlost;
2823 guint32 exthighestseq, jitter;
2826 /* don't report for sources in future generations */
2827 if (((gint16) (source->generation - sess->generation)) > 0) {
2828 GST_DEBUG ("source %08x generation %u > %u", source->ssrc,
2829 source->generation, sess->generation);
2833 /* only report about other sender */
2834 if (source == data->source)
2837 if (gst_rtcp_packet_get_rb_count (packet) == GST_RTCP_MAX_RB_COUNT) {
2838 GST_DEBUG ("max RB count reached");
2842 if (!RTP_SOURCE_IS_SENDER (source)) {
2843 GST_DEBUG ("source %08x not sender", source->ssrc);
2847 GST_DEBUG ("create RB for SSRC %08x", source->ssrc);
2850 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2851 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2853 /* store last generated RR packet */
2854 source->last_rr.is_valid = TRUE;
2855 source->last_rr.fractionlost = fractionlost;
2856 source->last_rr.packetslost = packetslost;
2857 source->last_rr.exthighestseq = exthighestseq;
2858 source->last_rr.jitter = jitter;
2859 source->last_rr.lsr = lsr;
2860 source->last_rr.dlsr = dlsr;
2862 /* packet is not yet filled, add report block for this source. */
2863 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2864 exthighestseq, jitter, lsr, dlsr);
2867 /* source is reported, move to next generation */
2868 source->generation = sess->generation + 1;
2870 /* if we reported all sources in this generation, move to next */
2871 if (--data->num_to_report == 0) {
2873 GST_DEBUG ("all reported, generation now %u", sess->generation);
2879 session_add_fir (const gchar * key, RTPSource * source, ReportData * data)
2881 GstRTCPPacket *packet = &data->packet;
2885 if (!source->send_fir)
2888 len = gst_rtcp_packet_fb_get_fci_length (packet);
2889 if (!gst_rtcp_packet_fb_set_fci_length (packet, len + 2))
2890 /* exit because the packet is full, will put next request in a
2894 fci_data = gst_rtcp_packet_fb_get_fci (packet) + (len * 4);
2896 GST_WRITE_UINT32_BE (fci_data, source->ssrc);
2898 fci_data[0] = source->current_send_fir_seqnum;
2899 fci_data[1] = fci_data[2] = fci_data[3] = 0;
2901 source->send_fir = FALSE;
2905 session_fir (RTPSession * sess, ReportData * data)
2907 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2908 GstRTCPPacket *packet = &data->packet;
2910 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
2913 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_FIR);
2914 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
2915 gst_rtcp_packet_fb_set_media_ssrc (packet, 0);
2917 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2918 (GHFunc) session_add_fir, data);
2920 if (gst_rtcp_packet_fb_get_fci_length (packet) == 0)
2921 gst_rtcp_packet_remove (packet);
2923 data->may_suppress = FALSE;
2927 has_pli_compare_func (gconstpointer a, gconstpointer ignored)
2929 GstRTCPPacket packet;
2930 GstRTCPBuffer rtcp = { NULL, };
2931 gboolean ret = FALSE;
2933 gst_rtcp_buffer_map ((GstBuffer *) a, GST_MAP_READ, &rtcp);
2935 if (gst_rtcp_buffer_get_first_packet (&rtcp, &packet)) {
2936 if (gst_rtcp_packet_get_type (&packet) == GST_RTCP_TYPE_PSFB &&
2937 gst_rtcp_packet_fb_get_type (&packet) == GST_RTCP_PSFB_TYPE_PLI)
2941 gst_rtcp_buffer_unmap (&rtcp);
2948 session_pli (const gchar * key, RTPSource * source, ReportData * data)
2950 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2951 GstRTCPPacket *packet = &data->packet;
2953 if (!source->send_pli)
2956 if (rtp_source_has_retained (source, has_pli_compare_func, NULL))
2959 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
2960 /* exit because the packet is full, will put next request in a
2964 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_PLI);
2965 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
2966 gst_rtcp_packet_fb_set_media_ssrc (packet, source->ssrc);
2968 source->send_pli = FALSE;
2969 data->may_suppress = FALSE;
2972 /* construct NACK */
2974 session_nack (const gchar * key, RTPSource * source, ReportData * data)
2976 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2977 GstRTCPPacket *packet = &data->packet;
2982 if (!source->send_nack)
2985 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RTPFB, packet))
2986 /* exit because the packet is full, will put next request in a
2990 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_RTPFB_TYPE_NACK);
2991 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
2992 gst_rtcp_packet_fb_set_media_ssrc (packet, source->ssrc);
2994 nacks = rtp_source_get_nacks (source, &n_nacks);
2995 GST_DEBUG ("%u NACKs", n_nacks);
2996 if (!gst_rtcp_packet_fb_set_fci_length (packet, n_nacks))
2999 fci_data = gst_rtcp_packet_fb_get_fci (packet);
3000 for (i = 0; i < n_nacks; i++) {
3001 GST_WRITE_UINT32_BE (fci_data, nacks[i]);
3005 rtp_source_clear_nacks (source);
3006 data->may_suppress = FALSE;
3009 /* perform cleanup of sources that timed out */
3011 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
3013 gboolean remove = FALSE;
3014 gboolean byetimeout = FALSE;
3015 gboolean sendertimeout = FALSE;
3016 gboolean is_sender, is_active;
3017 RTPSession *sess = data->sess;
3018 GstClockTime interval, binterval;
3021 GST_DEBUG ("look at %08x, generation %u", source->ssrc, source->generation);
3023 /* check for outdated collisions */
3024 if (source->internal) {
3025 GST_DEBUG ("Timing out collisions for %x", source->ssrc);
3026 rtp_source_timeout (source, data->current_time,
3027 /* "a relatively long time" -- RFC 3550 section 8.2 */
3028 RTP_STATS_MIN_INTERVAL * GST_SECOND * 10,
3029 data->running_time - sess->rtcp_feedback_retention_window);
3032 /* nothing else to do when without RTCP */
3033 if (data->interval == GST_CLOCK_TIME_NONE)
3036 is_sender = RTP_SOURCE_IS_SENDER (source);
3037 is_active = RTP_SOURCE_IS_ACTIVE (source);
3039 /* our own rtcp interval may have been forced low by secondary configuration,
3040 * while sender side may still operate with higher interval,
3041 * so do not just take our interval to decide on timing out sender,
3042 * but take (if data->interval <= 5 * GST_SECOND):
3043 * interval = CLAMP (sender_interval, data->interval, 5 * GST_SECOND)
3044 * where sender_interval is difference between last 2 received RTCP reports
3046 if (data->interval >= 5 * GST_SECOND || source->internal) {
3047 binterval = data->interval;
3049 GST_LOG ("prev_rtcp %" GST_TIME_FORMAT ", last_rtcp %" GST_TIME_FORMAT,
3050 GST_TIME_ARGS (source->stats.prev_rtcptime),
3051 GST_TIME_ARGS (source->stats.last_rtcptime));
3052 /* if not received enough yet, fallback to larger default */
3053 if (source->stats.last_rtcptime > source->stats.prev_rtcptime)
3054 binterval = source->stats.last_rtcptime - source->stats.prev_rtcptime;
3056 binterval = 5 * GST_SECOND;
3057 binterval = CLAMP (binterval, data->interval, 5 * GST_SECOND);
3059 GST_LOG ("timeout base interval %" GST_TIME_FORMAT,
3060 GST_TIME_ARGS (binterval));
3062 if (!source->internal) {
3063 if (source->marked_bye) {
3064 /* if we received a BYE from the source, remove the source after some
3066 if (data->current_time > source->bye_time &&
3067 data->current_time - source->bye_time > sess->stats.bye_timeout) {
3068 GST_DEBUG ("removing BYE source %08x", source->ssrc);
3073 /* sources that were inactive for more than 5 times the deterministic reporting
3074 * interval get timed out. the min timeout is 5 seconds. */
3075 /* mind old time that might pre-date last time going to PLAYING */
3076 btime = MAX (source->last_activity, sess->start_time);
3077 if (data->current_time > btime) {
3078 interval = MAX (binterval * 5, 5 * GST_SECOND);
3079 if (data->current_time - btime > interval) {
3080 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
3081 source->ssrc, GST_TIME_ARGS (btime));
3087 /* senders that did not send for a long time become a receiver, this also
3088 * holds for our own sources. */
3090 /* mind old time that might pre-date last time going to PLAYING */
3091 btime = MAX (source->last_rtp_activity, sess->start_time);
3092 if (data->current_time > btime) {
3093 interval = MAX (binterval * 2, 5 * GST_SECOND);
3094 if (data->current_time - btime > interval) {
3095 if (source->internal && source->sent_bye) {
3096 /* an internal source is BYE and stopped sending RTP, remove */
3097 GST_DEBUG ("internal BYE source %08x timed out, last %"
3098 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
3101 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
3102 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
3103 sendertimeout = TRUE;
3110 sess->total_sources--;
3112 sess->stats.sender_sources--;
3113 if (source->internal)
3114 sess->stats.internal_sender_sources--;
3117 sess->stats.active_sources--;
3119 if (source->internal)
3120 sess->stats.internal_sources--;
3123 on_bye_timeout (sess, source);
3125 on_timeout (sess, source);
3127 if (sendertimeout) {
3128 source->is_sender = FALSE;
3129 sess->stats.sender_sources--;
3130 if (source->internal)
3131 sess->stats.internal_sender_sources--;
3133 on_sender_timeout (sess, source);
3135 /* count how many source to report in this generation */
3136 if (((gint16) (source->generation - sess->generation)) <= 0)
3137 data->num_to_report++;
3139 source->closing = remove;
3143 session_sdes (RTPSession * sess, ReportData * data)
3145 GstRTCPPacket *packet = &data->packet;
3146 const GstStructure *sdes;
3148 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3150 /* add SDES packet */
3151 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SDES, packet);
3153 gst_rtcp_packet_sdes_add_item (packet, data->source->ssrc);
3155 sdes = rtp_source_get_sdes_struct (data->source);
3157 /* add all fields in the structure, the order is not important. */
3158 n_fields = gst_structure_n_fields (sdes);
3159 for (i = 0; i < n_fields; ++i) {
3162 GstRTCPSDESType type;
3164 field = gst_structure_nth_field_name (sdes, i);
3167 value = gst_structure_get_string (sdes, field);
3170 type = gst_rtcp_sdes_name_to_type (field);
3172 /* Early packets are minimal and only include the CNAME */
3173 if (data->is_early && type != GST_RTCP_SDES_CNAME)
3176 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
3177 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
3178 (const guint8 *) value);
3179 } else if (type == GST_RTCP_SDES_PRIV) {
3185 /* don't accept entries that are too big */
3186 prefix_len = strlen (field);
3187 if (prefix_len > 255)
3189 value_len = strlen (value);
3190 if (value_len > 255)
3192 data_len = 1 + prefix_len + value_len;
3196 data[0] = prefix_len;
3197 memcpy (&data[1], field, prefix_len);
3198 memcpy (&data[1 + prefix_len], value, value_len);
3200 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
3204 data->has_sdes = TRUE;
3207 /* schedule a BYE packet */
3209 make_source_bye (RTPSession * sess, RTPSource * source, ReportData * data)
3211 GstRTCPPacket *packet = &data->packet;
3212 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3215 session_sdes (sess, data);
3216 /* add a BYE packet */
3217 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_BYE, packet);
3218 gst_rtcp_packet_bye_add_ssrc (packet, source->ssrc);
3219 if (source->bye_reason)
3220 gst_rtcp_packet_bye_set_reason (packet, source->bye_reason);
3222 /* we have a BYE packet now */
3223 source->sent_bye = TRUE;
3227 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
3229 GstClockTime new_send_time, elapsed;
3230 GstClockTime interval;
3232 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3233 data->is_early = TRUE;
3235 data->is_early = FALSE;
3237 if (data->is_early && sess->next_early_rtcp_time < current_time) {
3238 GST_DEBUG ("early feedback %" GST_TIME_FORMAT " < now %"
3239 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_early_rtcp_time),
3240 GST_TIME_ARGS (current_time));
3244 /* no need to check yet */
3245 if (sess->next_rtcp_check_time == GST_CLOCK_TIME_NONE ||
3246 sess->next_rtcp_check_time > current_time) {
3247 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
3248 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
3249 GST_TIME_ARGS (current_time));
3254 /* get elapsed time since we last reported */
3255 elapsed = current_time - sess->last_rtcp_send_time;
3257 /* take interval and add jitter */
3258 interval = data->interval;
3259 if (interval != GST_CLOCK_TIME_NONE)
3260 interval = rtp_stats_add_rtcp_jitter (&sess->stats, interval);
3262 /* perform forward reconsideration */
3263 if (interval != GST_CLOCK_TIME_NONE) {
3264 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
3265 GST_TIME_FORMAT, GST_TIME_ARGS (interval), GST_TIME_ARGS (elapsed));
3266 new_send_time = interval + sess->last_rtcp_send_time;
3268 new_send_time = sess->last_rtcp_send_time;
3271 if (!data->is_early) {
3272 /* check if reconsideration */
3273 if (new_send_time == GST_CLOCK_TIME_NONE || current_time < new_send_time) {
3274 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
3275 GST_TIME_ARGS (new_send_time));
3276 /* store new check time */
3277 sess->next_rtcp_check_time = new_send_time;
3280 sess->next_rtcp_check_time = current_time + interval;
3281 } else if (interval != GST_CLOCK_TIME_NONE) {
3282 /* Apply the rules from RFC 4585 section 3.5.3 */
3283 if (sess->stats.min_interval != 0 && !sess->first_rtcp) {
3284 GstClockTime T_rr_current_interval =
3285 g_random_double_range (0.5, 1.5) * sess->stats.min_interval;
3287 /* This will caused the RTCP to be suppressed if no FB packets are added */
3288 if (sess->last_rtcp_send_time + T_rr_current_interval > new_send_time) {
3289 GST_DEBUG ("RTCP packet could be suppressed min: %" GST_TIME_FORMAT
3290 " last: %" GST_TIME_FORMAT
3291 " + T_rr_current_interval: %" GST_TIME_FORMAT
3292 " > new_send_time: %" GST_TIME_FORMAT,
3293 GST_TIME_ARGS (sess->stats.min_interval),
3294 GST_TIME_ARGS (sess->last_rtcp_send_time),
3295 GST_TIME_ARGS (T_rr_current_interval),
3296 GST_TIME_ARGS (new_send_time));
3297 data->may_suppress = TRUE;
3302 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
3303 GST_TIME_ARGS (new_send_time));
3309 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
3311 g_hash_table_insert (hash_table, key, g_object_ref (source));
3315 remove_closing_sources (const gchar * key, RTPSource * source,
3318 if (source->closing)
3321 if (source->send_fir)
3322 data->have_fir = TRUE;
3323 if (source->send_pli)
3324 data->have_pli = TRUE;
3325 if (source->send_nack)
3326 data->have_nack = TRUE;
3332 generate_rtcp (const gchar * key, RTPSource * source, ReportData * data)
3334 RTPSession *sess = data->sess;
3335 gboolean is_bye = FALSE;
3336 ReportOutput *output;
3338 /* only generate RTCP for active internal sources */
3339 if (!source->internal || source->sent_bye)
3342 data->source = source;
3345 session_start_rtcp (sess, data);
3347 if (source->marked_bye) {
3349 make_source_bye (sess, source, data);
3351 } else if (!data->is_early) {
3352 /* loop over all known sources and add report blocks. If we are early, we
3353 * just make a minimal RTCP packet and skip this step */
3354 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3355 (GHFunc) session_report_blocks, data);
3357 if (!data->has_sdes)
3358 session_sdes (sess, data);
3361 session_fir (sess, data);
3364 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3365 (GHFunc) session_pli, data);
3367 if (data->have_nack)
3368 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3369 (GHFunc) session_nack, data);
3371 gst_rtcp_buffer_unmap (&data->rtcpbuf);
3373 output = g_slice_new (ReportOutput);
3374 output->source = g_object_ref (source);
3375 output->is_bye = is_bye;
3376 output->buffer = data->rtcp;
3377 /* queue the RTCP packet to push later */
3378 g_queue_push_tail (&data->output, output);
3382 * rtp_session_on_timeout:
3383 * @sess: an #RTPSession
3384 * @current_time: the current system time
3385 * @ntpnstime: the current NTP time in nanoseconds
3386 * @running_time: the current running_time of the pipeline
3388 * Perform maintenance actions after the timeout obtained with
3389 * rtp_session_next_timeout() expired.
3391 * This function will perform timeouts of receivers and senders, send a BYE
3392 * packet or generate RTCP packets with current session stats.
3394 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
3395 * times, for each packet that should be processed.
3397 * Returns: a #GstFlowReturn.
3400 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
3401 guint64 ntpnstime, GstClockTime running_time)
3403 GstFlowReturn result = GST_FLOW_OK;
3404 ReportData data = { GST_RTCP_BUFFER_INIT };
3405 GHashTable *table_copy;
3406 ReportOutput *output;
3408 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3410 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT
3411 ", running-time %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
3412 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (running_time));
3415 data.current_time = current_time;
3416 data.ntpnstime = ntpnstime;
3417 data.running_time = running_time;
3418 data.num_to_report = 0;
3419 data.may_suppress = FALSE;
3420 g_queue_init (&data.output);
3422 RTP_SESSION_LOCK (sess);
3423 /* get a new interval, we need this for various cleanups etc */
3424 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
3426 GST_DEBUG ("interval %" GST_TIME_FORMAT, GST_TIME_ARGS (data.interval));
3428 /* we need an internal source now */
3429 if (sess->stats.internal_sources == 0) {
3433 source = obtain_internal_source (sess, sess->suggested_ssrc, &created);
3434 g_object_unref (source);
3437 /* Make a local copy of the hashtable. We need to do this because the
3438 * cleanup stage below releases the session lock. */
3439 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
3440 (GDestroyNotify) g_object_unref);
3441 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3442 (GHFunc) clone_ssrcs_hashtable, table_copy);
3444 /* Clean up the session, mark the source for removing, this might release the
3446 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
3447 g_hash_table_destroy (table_copy);
3449 /* Now remove the marked sources */
3450 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
3451 (GHRFunc) remove_closing_sources, &data);
3453 /* see if we need to generate SR or RR packets */
3454 if (!is_rtcp_time (sess, current_time, &data))
3457 GST_DEBUG ("doing RTCP generation %u for %u sources, early %d",
3458 sess->generation, data.num_to_report, data.is_early);
3460 /* generate RTCP for all internal sources */
3461 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3462 (GHFunc) generate_rtcp, &data);
3464 /* we keep track of the last report time in order to timeout inactive
3465 * receivers or senders */
3466 if (!data.is_early && !data.may_suppress)
3467 sess->last_rtcp_send_time = data.current_time;
3468 sess->first_rtcp = FALSE;
3469 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
3472 RTP_SESSION_UNLOCK (sess);
3474 /* push out the RTCP packets */
3475 while ((output = g_queue_pop_head (&data.output))) {
3476 gboolean do_not_suppress;
3477 GstBuffer *buffer = output->buffer;
3478 RTPSource *source = output->source;
3480 /* Give the user a change to add its own packet */
3481 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
3482 buffer, data.is_early, &do_not_suppress);
3484 if (sess->callbacks.send_rtcp && (do_not_suppress || !data.may_suppress)) {
3487 packet_size = gst_buffer_get_size (buffer) + sess->header_len;
3489 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
3490 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
3491 sess->stats.avg_rtcp_packet_size, packet_size);
3493 sess->callbacks.send_rtcp (sess, source, buffer, output->is_bye,
3494 sess->send_rtcp_user_data);
3496 GST_DEBUG ("freeing packet callback: %p"
3497 " do_not_suppress: %d may_suppress: %d",
3498 sess->callbacks.send_rtcp, do_not_suppress, data.may_suppress);
3499 gst_buffer_unref (buffer);
3501 g_object_unref (source);
3502 g_slice_free (ReportOutput, output);
3508 * rtp_session_request_early_rtcp:
3509 * @sess: an #RTPSession
3510 * @current_time: the current system time
3511 * @max_delay: maximum delay
3513 * Request transmission of early RTCP
3516 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
3517 GstClockTime max_delay)
3519 GstClockTime T_dither_max;
3521 /* Implements the algorithm described in RFC 4585 section 3.5.2 */
3523 RTP_SESSION_LOCK (sess);
3525 /* Check if already requested */
3526 /* RFC 4585 section 3.5.2 step 2 */
3527 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
3528 GST_LOG_OBJECT (sess, "already have next early rtcp time");
3532 if (!GST_CLOCK_TIME_IS_VALID (sess->next_rtcp_check_time)) {
3533 GST_LOG_OBJECT (sess, "no next RTCP check time");
3537 /* Ignore the request a scheduled packet will be in time anyway */
3538 if (current_time + max_delay > sess->next_rtcp_check_time) {
3539 GST_LOG_OBJECT (sess, "next scheduled time is soon %" GST_TIME_FORMAT " + %"
3540 GST_TIME_FORMAT " > %" GST_TIME_FORMAT,
3541 GST_TIME_ARGS (current_time),
3542 GST_TIME_ARGS (max_delay), GST_TIME_ARGS (sess->next_rtcp_check_time));
3546 /* RFC 4585 section 3.5.2 step 2b */
3547 /* If the total sources is <=2, then there is only us and one peer */
3548 if (sess->total_sources <= 2) {
3551 /* Divide by 2 because l = 0.5 */
3552 T_dither_max = sess->next_rtcp_check_time - sess->last_rtcp_send_time;
3556 /* RFC 4585 section 3.5.2 step 3 */
3557 if (current_time + T_dither_max > sess->next_rtcp_check_time) {
3558 GST_LOG_OBJECT (sess, "don't send because of dither");
3562 /* RFC 4585 section 3.5.2 step 4
3563 * Don't send if allow_early is FALSE, but not if we are in
3564 * immediate mode, meaning we are part of a group of at most the
3565 * application-specific threshold.
3567 if (sess->total_sources > sess->rtcp_immediate_feedback_threshold &&
3568 sess->allow_early == FALSE) {
3569 GST_LOG_OBJECT (sess, "can't allow early feedback");
3574 /* Schedule an early transmission later */
3575 sess->next_early_rtcp_time = g_random_double () * T_dither_max +
3578 /* If no dithering, schedule it for NOW */
3579 sess->next_early_rtcp_time = current_time;
3582 GST_LOG_OBJECT (sess, "next early RTCP time %" GST_TIME_FORMAT,
3583 GST_TIME_ARGS (sess->next_early_rtcp_time));
3584 RTP_SESSION_UNLOCK (sess);
3586 /* notify app of need to send packet early
3587 * and therefore of timeout change */
3588 if (sess->callbacks.reconsider)
3589 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
3595 RTP_SESSION_UNLOCK (sess);
3599 rtp_session_send_rtcp (RTPSession * sess, GstClockTime max_delay)
3603 if (!sess->callbacks.send_rtcp)
3606 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
3608 rtp_session_request_early_rtcp (sess, now, max_delay);
3612 rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc,
3613 gboolean fir, gint count)
3617 RTP_SESSION_LOCK (sess);
3618 src = find_source (sess, ssrc);
3623 src->send_pli = FALSE;
3624 src->send_fir = TRUE;
3626 if (count == -1 || count != src->last_fir_count)
3627 src->current_send_fir_seqnum++;
3628 src->last_fir_count = count;
3629 } else if (!src->send_fir) {
3630 src->send_pli = TRUE;
3632 RTP_SESSION_UNLOCK (sess);
3634 rtp_session_send_rtcp (sess, 200 * GST_MSECOND);
3641 RTP_SESSION_UNLOCK (sess);
3647 * rtp_session_request_nack:
3648 * @sess: a #RTPSession
3650 * @seqnum: the missing seqnum
3651 * @max_delay: max delay to request NACK
3653 * Request scheduling of a NACK feedback packet for @seqnum in @ssrc.
3655 * Returns: %TRUE if the NACK feedback could be scheduled
3658 rtp_session_request_nack (RTPSession * sess, guint32 ssrc, guint16 seqnum,
3659 GstClockTime max_delay)
3663 RTP_SESSION_LOCK (sess);
3664 source = find_source (sess, ssrc);
3668 GST_DEBUG ("request NACK for %08x, #%u", ssrc, seqnum);
3669 rtp_source_register_nack (source, seqnum);
3670 RTP_SESSION_UNLOCK (sess);
3672 rtp_session_send_rtcp (sess, max_delay);
3679 RTP_SESSION_UNLOCK (sess);