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,
93 /* update average packet size */
94 #define INIT_AVG(avg, val) \
96 #define UPDATE_AVG(avg, val) \
100 (avg) = ((val) + (15 * (avg))) >> 4;
103 /* The number RTCP intervals after which to timeout entries in the
106 #define RTCP_INTERVAL_COLLISION_TIMEOUT 10
108 /* GObject vmethods */
109 static void rtp_session_finalize (GObject * object);
110 static void rtp_session_set_property (GObject * object, guint prop_id,
111 const GValue * value, GParamSpec * pspec);
112 static void rtp_session_get_property (GObject * object, guint prop_id,
113 GValue * value, GParamSpec * pspec);
115 static void rtp_session_send_rtcp (RTPSession * sess, GstClockTime max_delay);
117 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
119 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
121 static guint32 rtp_session_create_new_ssrc (RTPSession * sess);
122 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
123 gboolean * created, RTPPacketInfo * pinfo, gboolean rtp);
124 static RTPSource *obtain_internal_source (RTPSession * sess,
125 guint32 ssrc, gboolean * created);
126 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
127 GstClockTime current_time);
128 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
129 gboolean deterministic, gboolean first);
132 accumulate_trues (GSignalInvocationHint * ihint, GValue * return_accu,
133 const GValue * handler_return, gpointer data)
135 if (g_value_get_boolean (handler_return))
136 g_value_set_boolean (return_accu, TRUE);
142 rtp_session_class_init (RTPSessionClass * klass)
144 GObjectClass *gobject_class;
146 gobject_class = (GObjectClass *) klass;
148 gobject_class->finalize = rtp_session_finalize;
149 gobject_class->set_property = rtp_session_set_property;
150 gobject_class->get_property = rtp_session_get_property;
153 * RTPSession::get-source-by-ssrc:
154 * @session: the object which received the signal
155 * @ssrc: the SSRC of the RTPSource
157 * Request the #RTPSource object with SSRC @ssrc in @session.
159 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
160 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
161 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
162 get_source_by_ssrc), NULL, NULL, g_cclosure_marshal_generic,
163 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
166 * RTPSession::on-new-ssrc:
167 * @session: the object which received the signal
168 * @src: the new RTPSource
170 * Notify of a new SSRC that entered @session.
172 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
173 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
174 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
175 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
178 * RTPSession::on-ssrc-collision:
179 * @session: the object which received the signal
180 * @src: the #RTPSource that caused a collision
182 * Notify when we have an SSRC collision
184 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
185 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
186 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
187 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
190 * RTPSession::on-ssrc-validated:
191 * @session: the object which received the signal
192 * @src: the new validated RTPSource
194 * Notify of a new SSRC that became validated.
196 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
197 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
198 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
199 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
202 * RTPSession::on-ssrc-active:
203 * @session: the object which received the signal
204 * @src: the active RTPSource
206 * Notify of a SSRC that is active, i.e., sending RTCP.
208 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
209 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
210 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
211 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
214 * RTPSession::on-ssrc-sdes:
215 * @session: the object which received the signal
216 * @src: the RTPSource
218 * Notify that a new SDES was received for SSRC.
220 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
221 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
222 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
223 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
226 * RTPSession::on-bye-ssrc:
227 * @session: the object which received the signal
228 * @src: the RTPSource that went away
230 * Notify of an SSRC that became inactive because of a BYE packet.
232 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
233 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
234 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
235 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
238 * RTPSession::on-bye-timeout:
239 * @session: the object which received the signal
240 * @src: the RTPSource that timed out
242 * Notify of an SSRC that has timed out because of BYE
244 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
245 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
246 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
247 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
250 * RTPSession::on-timeout:
251 * @session: the object which received the signal
252 * @src: the RTPSource that timed out
254 * Notify of an SSRC that has timed out
256 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
257 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
258 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
259 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
262 * RTPSession::on-sender-timeout:
263 * @session: the object which received the signal
264 * @src: the RTPSource that timed out
266 * Notify of an SSRC that was a sender but timed out and became a receiver.
268 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
269 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
270 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
271 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
275 * RTPSession::on-sending-rtcp
276 * @session: the object which received the signal
277 * @buffer: the #GstBuffer containing the RTCP packet about to be sent
278 * @early: %TRUE if the packet is early, %FALSE if it is regular
280 * This signal is emitted before sending an RTCP packet, it can be used
281 * to add extra RTCP Packets.
283 * Returns: %TRUE if the RTCP buffer should NOT be suppressed, %FALSE
284 * if suppressing it is acceptable
286 rtp_session_signals[SIGNAL_ON_SENDING_RTCP] =
287 g_signal_new ("on-sending-rtcp", G_TYPE_FROM_CLASS (klass),
288 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_rtcp),
289 accumulate_trues, NULL, g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2,
290 GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_BOOLEAN);
293 * RTPSession::on-feedback-rtcp:
294 * @session: the object which received the signal
295 * @type: Type of RTCP packet, will be %GST_RTCP_TYPE_RTPFB or
296 * %GST_RTCP_TYPE_RTPFB
297 * @fbtype: The type of RTCP FB packet, probably part of #GstRTCPFBType
298 * @sender_ssrc: The SSRC of the sender
299 * @media_ssrc: The SSRC of the media this refers to
300 * @fci: a #GstBuffer with the FCI data from the FB packet or %NULL if
303 * Notify that a RTCP feedback packet has been received
305 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP] =
306 g_signal_new ("on-feedback-rtcp", G_TYPE_FROM_CLASS (klass),
307 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_feedback_rtcp),
308 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 5, G_TYPE_UINT,
309 G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, GST_TYPE_BUFFER);
312 * RTPSession::send-rtcp:
313 * @session: the object which received the signal
314 * @max_delay: The maximum delay after which the feedback will not be useful
317 * Requests that the #RTPSession initiate a new RTCP packet as soon as
318 * possible within the requested delay.
320 rtp_session_signals[SIGNAL_SEND_RTCP] =
321 g_signal_new ("send-rtcp", G_TYPE_FROM_CLASS (klass),
322 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
323 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
324 g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_UINT64);
326 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
327 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
328 "The internal SSRC used for the session (deprecated)",
329 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
331 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
332 g_param_spec_object ("internal-source", "Internal Source",
333 "The internal source element of the session (deprecated)",
334 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
336 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
337 g_param_spec_double ("bandwidth", "Bandwidth",
338 "The bandwidth of the session (0 for auto-discover)",
339 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
340 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
342 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
343 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
344 "The fraction of the bandwidth used for RTCP (or as a real fraction of the RTP bandwidth if < 1)",
345 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
346 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
348 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
349 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
350 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
351 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
352 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
354 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
355 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
356 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
357 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
358 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
360 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
361 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
362 "The maximum size of the RTCP packets",
363 16, G_MAXINT16, DEFAULT_RTCP_MTU,
364 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
366 g_object_class_install_property (gobject_class, PROP_SDES,
367 g_param_spec_boxed ("sdes", "SDES",
368 "The SDES items of this session",
369 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
372 g_param_spec_uint ("num-sources", "Num Sources",
373 "The number of sources in the session", 0, G_MAXUINT,
374 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
376 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
377 g_param_spec_uint ("num-active-sources", "Num Active Sources",
378 "The number of active sources in the session", 0, G_MAXUINT,
379 DEFAULT_NUM_ACTIVE_SOURCES,
380 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
384 * Get a GValue Array of all sources in the session.
387 * <title>Getting the #RTPSources of a session
394 * g_object_get (sess, "sources", &arr, NULL);
396 * for (i = 0; i < arr->n_values; i++) {
399 * val = g_value_array_get_nth (arr, i);
400 * source = g_value_get_object (val);
402 * g_value_array_free (arr);
407 g_object_class_install_property (gobject_class, PROP_SOURCES,
408 g_param_spec_boxed ("sources", "Sources",
409 "An array of all known sources in the session",
410 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
412 g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
413 g_param_spec_boolean ("favor-new", "Favor new sources",
414 "Resolve SSRC conflict in favor of new sources", FALSE,
415 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
417 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
418 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
419 "Minimum interval between Regular RTCP packet (in ns)",
420 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
421 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
423 g_object_class_install_property (gobject_class,
424 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
425 g_param_spec_uint64 ("rtcp-feedback-retention-window",
426 "RTCP Feedback retention window",
427 "Duration during which RTCP Feedback packets are retained (in ns)",
428 0, G_MAXUINT64, DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW,
429 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
431 g_object_class_install_property (gobject_class,
432 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
433 g_param_spec_uint ("rtcp-immediate-feedback-threshold",
434 "RTCP Immediate Feedback threshold",
435 "The maximum number of members of a RTP session for which immediate"
437 0, G_MAXUINT, DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
438 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
440 g_object_class_install_property (gobject_class, PROP_PROBATION,
441 g_param_spec_uint ("probation", "Number of probations",
442 "Consecutive packet sequence numbers to accept the source",
443 0, G_MAXUINT, DEFAULT_PROBATION,
444 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
449 * Various session statistics. This property returns a GstStructure
450 * with name application/x-rtp-session-stats with the following fields:
452 * "rtx-drop-count" G_TYPE_UINT The number of retransmission events
453 * dropped (due to bandwidth constraints)
454 * "sent-nack-count" G_TYPE_UINT Number of NACKs sent
455 * "recv-nack-count" G_TYPE_UINT Number of NACKs received
459 g_object_class_install_property (gobject_class, PROP_STATS,
460 g_param_spec_boxed ("stats", "Statistics",
461 "Various statistics", GST_TYPE_STRUCTURE,
462 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
464 klass->get_source_by_ssrc =
465 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
466 klass->send_rtcp = GST_DEBUG_FUNCPTR (rtp_session_send_rtcp);
468 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
472 rtp_session_init (RTPSession * sess)
477 g_mutex_init (&sess->lock);
478 sess->key = g_random_int ();
482 for (i = 0; i < 32; i++) {
484 g_hash_table_new_full (NULL, NULL, NULL,
485 (GDestroyNotify) g_object_unref);
488 rtp_stats_init_defaults (&sess->stats);
489 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
490 rtp_stats_set_min_interval (&sess->stats,
491 (gdouble) DEFAULT_RTCP_MIN_INTERVAL / GST_SECOND);
493 sess->recalc_bandwidth = TRUE;
494 sess->bandwidth = DEFAULT_BANDWIDTH;
495 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
496 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
497 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
499 /* default UDP header length */
500 sess->header_len = 28;
501 sess->mtu = DEFAULT_RTCP_MTU;
503 sess->probation = DEFAULT_PROBATION;
505 /* some default SDES entries */
506 sess->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
508 /* we do not want to leak details like the username or hostname here */
509 str = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
510 gst_structure_set (sess->sdes, "cname", G_TYPE_STRING, str, NULL);
514 /* we do not want to leak the user's real name here */
515 str = g_strdup_printf ("Anon%u", g_random_int ());
516 gst_structure_set (sdes, "name", G_TYPE_STRING, str, NULL);
520 gst_structure_set (sess->sdes, "tool", G_TYPE_STRING, "GStreamer", NULL);
522 /* this is the SSRC we suggest */
523 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
525 sess->first_rtcp = TRUE;
526 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
528 sess->allow_early = TRUE;
529 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
530 sess->rtcp_feedback_retention_window = DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW;
531 sess->rtcp_immediate_feedback_threshold =
532 DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD;
534 sess->last_keyframe_request = GST_CLOCK_TIME_NONE;
538 rtp_session_finalize (GObject * object)
543 sess = RTP_SESSION_CAST (object);
545 gst_structure_free (sess->sdes);
547 for (i = 0; i < 32; i++)
548 g_hash_table_destroy (sess->ssrcs[i]);
550 g_mutex_clear (&sess->lock);
552 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
556 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
558 GValue value = { 0 };
560 g_value_init (&value, RTP_TYPE_SOURCE);
561 g_value_take_object (&value, source);
562 /* copies the value */
563 g_value_array_append (arr, &value);
567 rtp_session_create_sources (RTPSession * sess)
572 RTP_SESSION_LOCK (sess);
573 /* get number of elements in the table */
574 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
575 /* create the result value array */
576 res = g_value_array_new (size);
578 /* and copy all values into the array */
579 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
580 RTP_SESSION_UNLOCK (sess);
585 static GstStructure *
586 rtp_session_create_stats (RTPSession * sess)
590 s = gst_structure_new ("application/x-rtp-session-stats",
591 "rtx-drop-count", G_TYPE_UINT, sess->stats.nacks_dropped,
592 "sent-nack-count", G_TYPE_UINT, sess->stats.nacks_sent,
593 "recv-nack-count", G_TYPE_UINT, sess->stats.nacks_received, NULL);
599 rtp_session_set_property (GObject * object, guint prop_id,
600 const GValue * value, GParamSpec * pspec)
604 sess = RTP_SESSION (object);
607 case PROP_INTERNAL_SSRC:
610 RTP_SESSION_LOCK (sess);
611 sess->bandwidth = g_value_get_double (value);
612 sess->recalc_bandwidth = TRUE;
613 RTP_SESSION_UNLOCK (sess);
615 case PROP_RTCP_FRACTION:
616 RTP_SESSION_LOCK (sess);
617 sess->rtcp_bandwidth = g_value_get_double (value);
618 sess->recalc_bandwidth = TRUE;
619 RTP_SESSION_UNLOCK (sess);
621 case PROP_RTCP_RR_BANDWIDTH:
622 RTP_SESSION_LOCK (sess);
623 sess->rtcp_rr_bandwidth = g_value_get_int (value);
624 sess->recalc_bandwidth = TRUE;
625 RTP_SESSION_UNLOCK (sess);
627 case PROP_RTCP_RS_BANDWIDTH:
628 RTP_SESSION_LOCK (sess);
629 sess->rtcp_rs_bandwidth = g_value_get_int (value);
630 sess->recalc_bandwidth = TRUE;
631 RTP_SESSION_UNLOCK (sess);
634 sess->mtu = g_value_get_uint (value);
637 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
640 sess->favor_new = g_value_get_boolean (value);
642 case PROP_RTCP_MIN_INTERVAL:
643 rtp_stats_set_min_interval (&sess->stats,
644 (gdouble) g_value_get_uint64 (value) / GST_SECOND);
645 /* trigger reconsideration */
646 RTP_SESSION_LOCK (sess);
647 sess->next_rtcp_check_time = 0;
648 RTP_SESSION_UNLOCK (sess);
649 if (sess->callbacks.reconsider)
650 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
652 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
653 sess->rtcp_immediate_feedback_threshold = g_value_get_uint (value);
656 sess->probation = g_value_get_uint (value);
659 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
665 rtp_session_get_property (GObject * object, guint prop_id,
666 GValue * value, GParamSpec * pspec)
670 sess = RTP_SESSION (object);
673 case PROP_INTERNAL_SSRC:
674 g_value_set_uint (value, rtp_session_suggest_ssrc (sess));
676 case PROP_INTERNAL_SOURCE:
677 /* FIXME, return a random source */
678 g_value_set_object (value, NULL);
681 g_value_set_double (value, sess->bandwidth);
683 case PROP_RTCP_FRACTION:
684 g_value_set_double (value, sess->rtcp_bandwidth);
686 case PROP_RTCP_RR_BANDWIDTH:
687 g_value_set_int (value, sess->rtcp_rr_bandwidth);
689 case PROP_RTCP_RS_BANDWIDTH:
690 g_value_set_int (value, sess->rtcp_rs_bandwidth);
693 g_value_set_uint (value, sess->mtu);
696 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
698 case PROP_NUM_SOURCES:
699 g_value_set_uint (value, rtp_session_get_num_sources (sess));
701 case PROP_NUM_ACTIVE_SOURCES:
702 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
705 g_value_take_boxed (value, rtp_session_create_sources (sess));
708 g_value_set_boolean (value, sess->favor_new);
710 case PROP_RTCP_MIN_INTERVAL:
711 g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
713 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
714 g_value_set_uint (value, sess->rtcp_immediate_feedback_threshold);
717 g_value_set_uint (value, sess->probation);
720 g_value_take_boxed (value, rtp_session_create_stats (sess));
723 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
729 on_new_ssrc (RTPSession * sess, RTPSource * source)
731 g_object_ref (source);
732 RTP_SESSION_UNLOCK (sess);
733 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
734 RTP_SESSION_LOCK (sess);
735 g_object_unref (source);
739 on_ssrc_collision (RTPSession * sess, RTPSource * source)
741 g_object_ref (source);
742 RTP_SESSION_UNLOCK (sess);
743 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
745 RTP_SESSION_LOCK (sess);
746 g_object_unref (source);
750 on_ssrc_validated (RTPSession * sess, RTPSource * source)
752 g_object_ref (source);
753 RTP_SESSION_UNLOCK (sess);
754 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
756 RTP_SESSION_LOCK (sess);
757 g_object_unref (source);
761 on_ssrc_active (RTPSession * sess, RTPSource * source)
763 g_object_ref (source);
764 RTP_SESSION_UNLOCK (sess);
765 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
766 RTP_SESSION_LOCK (sess);
767 g_object_unref (source);
771 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
773 g_object_ref (source);
774 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
775 RTP_SESSION_UNLOCK (sess);
776 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
777 RTP_SESSION_LOCK (sess);
778 g_object_unref (source);
782 on_bye_ssrc (RTPSession * sess, RTPSource * source)
784 g_object_ref (source);
785 RTP_SESSION_UNLOCK (sess);
786 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
787 RTP_SESSION_LOCK (sess);
788 g_object_unref (source);
792 on_bye_timeout (RTPSession * sess, RTPSource * source)
794 g_object_ref (source);
795 RTP_SESSION_UNLOCK (sess);
796 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
797 RTP_SESSION_LOCK (sess);
798 g_object_unref (source);
802 on_timeout (RTPSession * sess, RTPSource * source)
804 g_object_ref (source);
805 RTP_SESSION_UNLOCK (sess);
806 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
807 RTP_SESSION_LOCK (sess);
808 g_object_unref (source);
812 on_sender_timeout (RTPSession * sess, RTPSource * source)
814 g_object_ref (source);
815 RTP_SESSION_UNLOCK (sess);
816 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
818 RTP_SESSION_LOCK (sess);
819 g_object_unref (source);
825 * Create a new session object.
827 * Returns: a new #RTPSession. g_object_unref() after usage.
830 rtp_session_new (void)
834 sess = g_object_new (RTP_TYPE_SESSION, NULL);
840 * rtp_session_set_callbacks:
841 * @sess: an #RTPSession
842 * @callbacks: callbacks to configure
843 * @user_data: user data passed in the callbacks
845 * Configure a set of callbacks to be notified of actions.
848 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
851 g_return_if_fail (RTP_IS_SESSION (sess));
853 if (callbacks->process_rtp) {
854 sess->callbacks.process_rtp = callbacks->process_rtp;
855 sess->process_rtp_user_data = user_data;
857 if (callbacks->send_rtp) {
858 sess->callbacks.send_rtp = callbacks->send_rtp;
859 sess->send_rtp_user_data = user_data;
861 if (callbacks->send_rtcp) {
862 sess->callbacks.send_rtcp = callbacks->send_rtcp;
863 sess->send_rtcp_user_data = user_data;
865 if (callbacks->sync_rtcp) {
866 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
867 sess->sync_rtcp_user_data = user_data;
869 if (callbacks->clock_rate) {
870 sess->callbacks.clock_rate = callbacks->clock_rate;
871 sess->clock_rate_user_data = user_data;
873 if (callbacks->reconsider) {
874 sess->callbacks.reconsider = callbacks->reconsider;
875 sess->reconsider_user_data = user_data;
877 if (callbacks->request_key_unit) {
878 sess->callbacks.request_key_unit = callbacks->request_key_unit;
879 sess->request_key_unit_user_data = user_data;
881 if (callbacks->request_time) {
882 sess->callbacks.request_time = callbacks->request_time;
883 sess->request_time_user_data = user_data;
885 if (callbacks->notify_nack) {
886 sess->callbacks.notify_nack = callbacks->notify_nack;
887 sess->notify_nack_user_data = user_data;
892 * rtp_session_set_process_rtp_callback:
893 * @sess: an #RTPSession
894 * @callback: callback to set
895 * @user_data: user data passed in the callback
897 * Configure only the process_rtp callback to be notified of the process_rtp action.
900 rtp_session_set_process_rtp_callback (RTPSession * sess,
901 RTPSessionProcessRTP callback, gpointer user_data)
903 g_return_if_fail (RTP_IS_SESSION (sess));
905 sess->callbacks.process_rtp = callback;
906 sess->process_rtp_user_data = user_data;
910 * rtp_session_set_send_rtp_callback:
911 * @sess: an #RTPSession
912 * @callback: callback to set
913 * @user_data: user data passed in the callback
915 * Configure only the send_rtp callback to be notified of the send_rtp action.
918 rtp_session_set_send_rtp_callback (RTPSession * sess,
919 RTPSessionSendRTP callback, gpointer user_data)
921 g_return_if_fail (RTP_IS_SESSION (sess));
923 sess->callbacks.send_rtp = callback;
924 sess->send_rtp_user_data = user_data;
928 * rtp_session_set_send_rtcp_callback:
929 * @sess: an #RTPSession
930 * @callback: callback to set
931 * @user_data: user data passed in the callback
933 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
936 rtp_session_set_send_rtcp_callback (RTPSession * sess,
937 RTPSessionSendRTCP callback, gpointer user_data)
939 g_return_if_fail (RTP_IS_SESSION (sess));
941 sess->callbacks.send_rtcp = callback;
942 sess->send_rtcp_user_data = user_data;
946 * rtp_session_set_sync_rtcp_callback:
947 * @sess: an #RTPSession
948 * @callback: callback to set
949 * @user_data: user data passed in the callback
951 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
954 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
955 RTPSessionSyncRTCP callback, gpointer user_data)
957 g_return_if_fail (RTP_IS_SESSION (sess));
959 sess->callbacks.sync_rtcp = callback;
960 sess->sync_rtcp_user_data = user_data;
964 * rtp_session_set_clock_rate_callback:
965 * @sess: an #RTPSession
966 * @callback: callback to set
967 * @user_data: user data passed in the callback
969 * Configure only the clock_rate callback to be notified of the clock_rate action.
972 rtp_session_set_clock_rate_callback (RTPSession * sess,
973 RTPSessionClockRate callback, gpointer user_data)
975 g_return_if_fail (RTP_IS_SESSION (sess));
977 sess->callbacks.clock_rate = callback;
978 sess->clock_rate_user_data = user_data;
982 * rtp_session_set_reconsider_callback:
983 * @sess: an #RTPSession
984 * @callback: callback to set
985 * @user_data: user data passed in the callback
987 * Configure only the reconsider callback to be notified of the reconsider action.
990 rtp_session_set_reconsider_callback (RTPSession * sess,
991 RTPSessionReconsider callback, gpointer user_data)
993 g_return_if_fail (RTP_IS_SESSION (sess));
995 sess->callbacks.reconsider = callback;
996 sess->reconsider_user_data = user_data;
1000 * rtp_session_set_request_time_callback:
1001 * @sess: an #RTPSession
1002 * @callback: callback to set
1003 * @user_data: user data passed in the callback
1005 * Configure only the request_time callback
1008 rtp_session_set_request_time_callback (RTPSession * sess,
1009 RTPSessionRequestTime callback, gpointer user_data)
1011 g_return_if_fail (RTP_IS_SESSION (sess));
1013 sess->callbacks.request_time = callback;
1014 sess->request_time_user_data = user_data;
1018 * rtp_session_set_bandwidth:
1019 * @sess: an #RTPSession
1020 * @bandwidth: the bandwidth allocated
1022 * Set the session bandwidth in bytes per second.
1025 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
1027 g_return_if_fail (RTP_IS_SESSION (sess));
1029 RTP_SESSION_LOCK (sess);
1030 sess->stats.bandwidth = bandwidth;
1031 RTP_SESSION_UNLOCK (sess);
1035 * rtp_session_get_bandwidth:
1036 * @sess: an #RTPSession
1038 * Get the session bandwidth.
1040 * Returns: the session bandwidth.
1043 rtp_session_get_bandwidth (RTPSession * sess)
1047 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1049 RTP_SESSION_LOCK (sess);
1050 result = sess->stats.bandwidth;
1051 RTP_SESSION_UNLOCK (sess);
1057 * rtp_session_set_rtcp_fraction:
1058 * @sess: an #RTPSession
1059 * @bandwidth: the RTCP bandwidth
1061 * Set the bandwidth in bytes per second that should be used for RTCP
1065 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
1067 g_return_if_fail (RTP_IS_SESSION (sess));
1069 RTP_SESSION_LOCK (sess);
1070 sess->stats.rtcp_bandwidth = bandwidth;
1071 RTP_SESSION_UNLOCK (sess);
1075 * rtp_session_get_rtcp_fraction:
1076 * @sess: an #RTPSession
1078 * Get the session bandwidth used for RTCP.
1080 * Returns: The bandwidth used for RTCP messages.
1083 rtp_session_get_rtcp_fraction (RTPSession * sess)
1087 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
1089 RTP_SESSION_LOCK (sess);
1090 result = sess->stats.rtcp_bandwidth;
1091 RTP_SESSION_UNLOCK (sess);
1097 * rtp_session_get_sdes_struct:
1098 * @sess: an #RTSPSession
1100 * Get the SDES data as a #GstStructure
1102 * Returns: a GstStructure with SDES items for @sess. This function returns a
1103 * copy of the SDES structure, use gst_structure_free() after usage.
1106 rtp_session_get_sdes_struct (RTPSession * sess)
1108 GstStructure *result = NULL;
1110 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1112 RTP_SESSION_LOCK (sess);
1114 result = gst_structure_copy (sess->sdes);
1115 RTP_SESSION_UNLOCK (sess);
1121 * rtp_session_set_sdes_struct:
1122 * @sess: an #RTSPSession
1123 * @sdes: a #GstStructure
1125 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1128 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
1130 g_return_if_fail (sdes);
1131 g_return_if_fail (RTP_IS_SESSION (sess));
1133 RTP_SESSION_LOCK (sess);
1135 gst_structure_free (sess->sdes);
1136 sess->sdes = gst_structure_copy (sdes);
1137 RTP_SESSION_UNLOCK (sess);
1140 static GstFlowReturn
1141 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1143 GstFlowReturn result = GST_FLOW_OK;
1145 if (source->internal) {
1146 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1148 RTP_SESSION_UNLOCK (session);
1150 if (session->callbacks.send_rtp)
1152 session->callbacks.send_rtp (session, source, data,
1153 session->send_rtp_user_data);
1155 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1158 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1159 RTP_SESSION_UNLOCK (session);
1161 if (session->callbacks.process_rtp)
1163 session->callbacks.process_rtp (session, source,
1164 GST_BUFFER_CAST (data), session->process_rtp_user_data);
1166 gst_buffer_unref (GST_BUFFER_CAST (data));
1168 RTP_SESSION_LOCK (session);
1174 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1178 RTP_SESSION_UNLOCK (session);
1180 if (session->callbacks.clock_rate)
1182 session->callbacks.clock_rate (session, pt,
1183 session->clock_rate_user_data);
1187 RTP_SESSION_LOCK (session);
1189 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1194 static RTPSourceCallbacks callbacks = {
1195 (RTPSourcePushRTP) source_push_rtp,
1196 (RTPSourceClockRate) source_clock_rate,
1200 check_collision (RTPSession * sess, RTPSource * source,
1201 RTPPacketInfo * pinfo, gboolean rtp)
1205 /* If we have no pinfo address, we can't do collision checking */
1206 if (!pinfo->address)
1209 ssrc = rtp_source_get_ssrc (source);
1211 if (!source->internal) {
1212 GSocketAddress *from;
1214 /* This is not our local source, but lets check if two remote
1217 from = source->rtp_from;
1219 from = source->rtcp_from;
1223 if (__g_socket_address_equal (from, pinfo->address)) {
1224 /* Address is the same */
1227 GST_LOG ("we have a third-party collision or loop ssrc:%x", ssrc);
1228 if (sess->favor_new) {
1229 if (rtp_source_find_conflicting_address (source,
1230 pinfo->address, pinfo->current_time)) {
1233 buf1 = __g_socket_address_to_string (pinfo->address);
1234 GST_LOG ("Known conflict on %x for %s, dropping packet", ssrc,
1242 /* Current address is not a known conflict, lets assume this is
1243 * a new source. Save old address in possible conflict list
1245 rtp_source_add_conflicting_address (source, from,
1246 pinfo->current_time);
1248 buf1 = __g_socket_address_to_string (from);
1249 buf2 = __g_socket_address_to_string (pinfo->address);
1251 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1252 " saving old as known conflict", ssrc, buf1, buf2);
1255 rtp_source_set_rtp_from (source, pinfo->address);
1257 rtp_source_set_rtcp_from (source, pinfo->address);
1265 /* Don't need to save old addresses, we ignore new sources */
1270 /* We don't already have a from address for RTP, just set it */
1272 rtp_source_set_rtp_from (source, pinfo->address);
1274 rtp_source_set_rtcp_from (source, pinfo->address);
1278 /* FIXME: Log 3rd party collision somehow
1279 * Maybe should be done in upper layer, only the SDES can tell us
1280 * if its a collision or a loop
1283 /* This is sending with our ssrc, is it an address we already know */
1284 if (rtp_source_find_conflicting_address (source, pinfo->address,
1285 pinfo->current_time)) {
1286 /* Its a known conflict, its probably a loop, not a collision
1287 * lets just drop the incoming packet
1289 GST_DEBUG ("Our packets are being looped back to us, dropping");
1291 /* Its a new collision, lets change our SSRC */
1292 rtp_source_add_conflicting_address (source, pinfo->address,
1293 pinfo->current_time);
1295 GST_DEBUG ("Collision for SSRC %x", ssrc);
1296 /* mark the source BYE */
1297 rtp_source_mark_bye (source, "SSRC Collision");
1298 /* if we were suggesting this SSRC, change to something else */
1299 if (sess->suggested_ssrc == ssrc)
1300 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
1302 on_ssrc_collision (sess, source);
1304 rtp_session_schedule_bye_locked (sess, pinfo->current_time);
1312 find_source (RTPSession * sess, guint32 ssrc)
1314 return g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1315 GINT_TO_POINTER (ssrc));
1319 add_source (RTPSession * sess, RTPSource * src)
1321 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1322 GINT_TO_POINTER (src->ssrc), src);
1323 /* report the new source ASAP */
1324 src->generation = sess->generation;
1325 /* we have one more source now */
1326 sess->total_sources++;
1327 if (RTP_SOURCE_IS_ACTIVE (src))
1328 sess->stats.active_sources++;
1329 if (src->internal) {
1330 sess->stats.internal_sources++;
1331 if (sess->suggested_ssrc != src->ssrc)
1332 sess->suggested_ssrc = src->ssrc;
1336 /* must be called with the session lock, the returned source needs to be
1337 * unreffed after usage. */
1339 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1340 RTPPacketInfo * pinfo, gboolean rtp)
1344 source = find_source (sess, ssrc);
1345 if (source == NULL) {
1346 /* make new Source in probation and insert */
1347 source = rtp_source_new (ssrc);
1349 GST_DEBUG ("creating new source %08x %p", ssrc, source);
1351 /* for RTP packets we need to set the source in probation. Receiving RTCP
1352 * packets of an SSRC, on the other hand, is a strong indication that we
1353 * are dealing with a valid source. */
1355 g_object_set (source, "probation", sess->probation, NULL);
1357 g_object_set (source, "probation", 0, NULL);
1359 /* store from address, if any */
1360 if (pinfo->address) {
1362 rtp_source_set_rtp_from (source, pinfo->address);
1364 rtp_source_set_rtcp_from (source, pinfo->address);
1367 /* configure a callback on the source */
1368 rtp_source_set_callbacks (source, &callbacks, sess);
1370 add_source (sess, source);
1374 /* check for collision, this updates the address when not previously set */
1375 if (check_collision (sess, source, pinfo, rtp)) {
1378 /* Receiving RTCP packets of an SSRC is a strong indication that we
1379 * are dealing with a valid source. */
1381 g_object_set (source, "probation", 0, NULL);
1383 /* update last activity */
1384 source->last_activity = pinfo->current_time;
1386 source->last_rtp_activity = pinfo->current_time;
1387 g_object_ref (source);
1392 /* must be called with the session lock, the returned source needs to be
1393 * unreffed after usage. */
1395 obtain_internal_source (RTPSession * sess, guint32 ssrc, gboolean * created)
1399 source = find_source (sess, ssrc);
1400 if (source == NULL) {
1401 /* make new internal Source and insert */
1402 source = rtp_source_new (ssrc);
1404 GST_DEBUG ("creating new internal source %08x %p", ssrc, source);
1406 source->validated = TRUE;
1407 source->internal = TRUE;
1408 source->probation = FALSE;
1409 rtp_source_set_sdes_struct (source, gst_structure_copy (sess->sdes));
1410 rtp_source_set_callbacks (source, &callbacks, sess);
1412 add_source (sess, source);
1417 g_object_ref (source);
1423 * rtp_session_suggest_ssrc:
1424 * @sess: a #RTPSession
1426 * Suggest an unused SSRC in @sess.
1428 * Returns: a free unused SSRC
1431 rtp_session_suggest_ssrc (RTPSession * sess)
1435 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1437 RTP_SESSION_LOCK (sess);
1438 result = sess->suggested_ssrc;
1439 RTP_SESSION_UNLOCK (sess);
1445 * rtp_session_add_source:
1446 * @sess: a #RTPSession
1447 * @src: #RTPSource to add
1449 * Add @src to @session.
1451 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1452 * existed in the session.
1455 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1457 gboolean result = FALSE;
1460 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1461 g_return_val_if_fail (src != NULL, FALSE);
1463 RTP_SESSION_LOCK (sess);
1464 find = find_source (sess, src->ssrc);
1466 add_source (sess, src);
1469 RTP_SESSION_UNLOCK (sess);
1475 * rtp_session_get_num_sources:
1476 * @sess: an #RTPSession
1478 * Get the number of sources in @sess.
1480 * Returns: The number of sources in @sess.
1483 rtp_session_get_num_sources (RTPSession * sess)
1487 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1489 RTP_SESSION_LOCK (sess);
1490 result = sess->total_sources;
1491 RTP_SESSION_UNLOCK (sess);
1497 * rtp_session_get_num_active_sources:
1498 * @sess: an #RTPSession
1500 * Get the number of active sources in @sess. A source is considered active when
1501 * it has been validated and has not yet received a BYE RTCP message.
1503 * Returns: The number of active sources in @sess.
1506 rtp_session_get_num_active_sources (RTPSession * sess)
1510 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1512 RTP_SESSION_LOCK (sess);
1513 result = sess->stats.active_sources;
1514 RTP_SESSION_UNLOCK (sess);
1520 * rtp_session_get_source_by_ssrc:
1521 * @sess: an #RTPSession
1524 * Find the source with @ssrc in @sess.
1526 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1527 * g_object_unref() after usage.
1530 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1534 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1536 RTP_SESSION_LOCK (sess);
1537 result = find_source (sess, ssrc);
1539 g_object_ref (result);
1540 RTP_SESSION_UNLOCK (sess);
1545 /* should be called with the SESSION lock */
1547 rtp_session_create_new_ssrc (RTPSession * sess)
1552 ssrc = g_random_int ();
1554 /* see if it exists in the session, we're done if it doesn't */
1555 if (find_source (sess, ssrc) == NULL)
1563 * rtp_session_create_source:
1564 * @sess: an #RTPSession
1566 * Create an #RTPSource for use in @sess. This function will create a source
1567 * with an ssrc that is currently not used by any participants in the session.
1569 * Returns: an #RTPSource.
1572 rtp_session_create_source (RTPSession * sess)
1577 RTP_SESSION_LOCK (sess);
1578 ssrc = rtp_session_create_new_ssrc (sess);
1579 source = rtp_source_new (ssrc);
1580 rtp_source_set_callbacks (source, &callbacks, sess);
1581 /* we need an additional ref for the source in the hashtable */
1582 g_object_ref (source);
1583 add_source (sess, source);
1584 RTP_SESSION_UNLOCK (sess);
1590 update_packet (GstBuffer ** buffer, guint idx, RTPPacketInfo * pinfo)
1592 GstNetAddressMeta *meta;
1594 /* get packet size including header overhead */
1595 pinfo->bytes += gst_buffer_get_size (*buffer) + pinfo->header_len;
1599 GstRTPBuffer rtp = { NULL };
1601 if (!gst_rtp_buffer_map (*buffer, GST_MAP_READ, &rtp))
1602 goto invalid_packet;
1604 pinfo->payload_len += gst_rtp_buffer_get_payload_len (&rtp);
1608 /* only keep info for first buffer */
1609 pinfo->ssrc = gst_rtp_buffer_get_ssrc (&rtp);
1610 pinfo->seqnum = gst_rtp_buffer_get_seq (&rtp);
1611 pinfo->pt = gst_rtp_buffer_get_payload_type (&rtp);
1612 pinfo->rtptime = gst_rtp_buffer_get_timestamp (&rtp);
1613 /* copy available csrc */
1614 pinfo->csrc_count = gst_rtp_buffer_get_csrc_count (&rtp);
1615 for (i = 0; i < pinfo->csrc_count; i++)
1616 pinfo->csrcs[i] = gst_rtp_buffer_get_csrc (&rtp, i);
1618 gst_rtp_buffer_unmap (&rtp);
1622 /* for netbuffer we can store the IP address to check for collisions */
1623 meta = gst_buffer_get_net_address_meta (*buffer);
1625 g_object_unref (pinfo->address);
1627 pinfo->address = G_SOCKET_ADDRESS (g_object_ref (meta->addr));
1629 pinfo->address = NULL;
1637 GST_DEBUG ("invalid RTP packet received");
1642 /* update the RTPPacketInfo structure with the current time and other bits
1643 * about the current buffer we are handling.
1644 * This function is typically called when a validated packet is received.
1645 * This function should be called with the SESSION_LOCK
1648 update_packet_info (RTPSession * sess, RTPPacketInfo * pinfo,
1649 gboolean send, gboolean rtp, gboolean is_list, gpointer data,
1650 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
1656 pinfo->is_list = is_list;
1658 pinfo->current_time = current_time;
1659 pinfo->running_time = running_time;
1660 pinfo->ntpnstime = ntpnstime;
1661 pinfo->header_len = sess->header_len;
1663 pinfo->payload_len = 0;
1667 GstBufferList *list = GST_BUFFER_LIST_CAST (data);
1669 gst_buffer_list_foreach (list, (GstBufferListFunc) update_packet,
1672 GstBuffer *buffer = GST_BUFFER_CAST (data);
1673 res = update_packet (&buffer, 0, pinfo);
1679 clean_packet_info (RTPPacketInfo * pinfo)
1682 g_object_unref (pinfo->address);
1684 gst_mini_object_unref (pinfo->data);
1690 source_update_active (RTPSession * sess, RTPSource * source,
1691 gboolean prevactive)
1693 gboolean active = RTP_SOURCE_IS_ACTIVE (source);
1694 guint32 ssrc = source->ssrc;
1696 if (prevactive == active)
1700 sess->stats.active_sources++;
1701 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1702 sess->stats.active_sources);
1704 sess->stats.active_sources--;
1705 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
1706 sess->stats.active_sources);
1712 source_update_sender (RTPSession * sess, RTPSource * source,
1713 gboolean prevsender)
1715 gboolean sender = RTP_SOURCE_IS_SENDER (source);
1716 guint32 ssrc = source->ssrc;
1718 if (prevsender == sender)
1722 sess->stats.sender_sources++;
1723 if (source->internal)
1724 sess->stats.internal_sender_sources++;
1725 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1726 sess->stats.sender_sources);
1728 sess->stats.sender_sources--;
1729 if (source->internal)
1730 sess->stats.internal_sender_sources--;
1731 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
1732 sess->stats.sender_sources);
1738 * rtp_session_process_rtp:
1739 * @sess: and #RTPSession
1740 * @buffer: an RTP buffer
1741 * @current_time: the current system time
1742 * @running_time: the running_time of @buffer
1744 * Process an RTP buffer in the session manager. This function takes ownership
1747 * Returns: a #GstFlowReturn.
1750 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1751 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
1753 GstFlowReturn result;
1757 gboolean prevsender, prevactive;
1758 RTPPacketInfo pinfo = { 0, };
1761 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1762 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1764 RTP_SESSION_LOCK (sess);
1766 /* update pinfo stats */
1767 if (!update_packet_info (sess, &pinfo, FALSE, TRUE, FALSE, buffer,
1768 current_time, running_time, ntpnstime)) {
1769 GST_DEBUG ("invalid RTP packet received");
1770 RTP_SESSION_UNLOCK (sess);
1771 return rtp_session_process_rtcp (sess, buffer, current_time, ntpnstime);
1776 source = obtain_source (sess, ssrc, &created, &pinfo, TRUE);
1780 prevsender = RTP_SOURCE_IS_SENDER (source);
1781 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1782 oldrate = source->bitrate;
1784 /* let source process the packet */
1785 result = rtp_source_process_rtp (source, &pinfo);
1787 /* source became active */
1788 if (source_update_active (sess, source, prevactive))
1789 on_ssrc_validated (sess, source);
1791 source_update_sender (sess, source, prevsender);
1793 if (oldrate != source->bitrate)
1794 sess->recalc_bandwidth = TRUE;
1797 on_new_ssrc (sess, source);
1799 if (source->validated) {
1803 /* for validated sources, we add the CSRCs as well */
1804 for (i = 0; i < pinfo.csrc_count; i++) {
1806 RTPSource *csrc_src;
1808 csrc = pinfo.csrcs[i];
1811 csrc_src = obtain_source (sess, csrc, &created, &pinfo, TRUE);
1816 GST_DEBUG ("created new CSRC: %08x", csrc);
1817 rtp_source_set_as_csrc (csrc_src);
1818 source_update_active (sess, csrc_src, FALSE);
1819 on_new_ssrc (sess, csrc_src);
1821 g_object_unref (csrc_src);
1824 g_object_unref (source);
1826 RTP_SESSION_UNLOCK (sess);
1828 clean_packet_info (&pinfo);
1835 RTP_SESSION_UNLOCK (sess);
1836 gst_buffer_unref (buffer);
1837 clean_packet_info (&pinfo);
1838 GST_DEBUG ("ignoring packet because its collisioning");
1844 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1845 GstRTCPPacket * packet, RTPPacketInfo * pinfo)
1849 count = gst_rtcp_packet_get_rb_count (packet);
1850 for (i = 0; i < count; i++) {
1851 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1852 guint8 fractionlost;
1856 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1857 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1859 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1861 /* find our own source */
1862 src = find_source (sess, ssrc);
1866 if (src->internal && RTP_SOURCE_IS_ACTIVE (src)) {
1867 /* only deal with report blocks for our session, we update the stats of
1868 * the sender of the RTCP message. We could also compare our stats against
1869 * the other sender to see if we are better or worse. */
1870 /* FIXME, need to keep track who the RB block is from */
1871 rtp_source_process_rb (source, pinfo->ntpnstime, fractionlost,
1872 packetslost, exthighestseq, jitter, lsr, dlsr);
1875 on_ssrc_active (sess, source);
1878 /* A Sender report contains statistics about how the sender is doing. This
1879 * includes timing informataion such as the relation between RTP and NTP
1880 * timestamps and the number of packets/bytes it sent to us.
1882 * In this report is also included a set of report blocks related to how this
1883 * sender is receiving data (in case we (or somebody else) is also sending stuff
1884 * to it). This info includes the packet loss, jitter and seqnum. It also
1885 * contains information to calculate the round trip time (LSR/DLSR).
1888 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1889 RTPPacketInfo * pinfo, gboolean * do_sync)
1891 guint32 senderssrc, rtptime, packet_count, octet_count;
1894 gboolean created, prevsender;
1896 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1897 &packet_count, &octet_count);
1899 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1900 senderssrc, GST_TIME_ARGS (pinfo->current_time));
1902 source = obtain_source (sess, senderssrc, &created, pinfo, FALSE);
1906 /* don't try to do lip-sync for sources that sent a BYE */
1907 if (RTP_SOURCE_IS_MARKED_BYE (source))
1912 prevsender = RTP_SOURCE_IS_SENDER (source);
1914 /* first update the source */
1915 rtp_source_process_sr (source, pinfo->current_time, ntptime, rtptime,
1916 packet_count, octet_count);
1918 source_update_sender (sess, source, prevsender);
1921 on_new_ssrc (sess, source);
1923 rtp_session_process_rb (sess, source, packet, pinfo);
1924 g_object_unref (source);
1927 /* A receiver report contains statistics about how a receiver is doing. It
1928 * includes stuff like packet loss, jitter and the seqnum it received last. It
1929 * also contains info to calculate the round trip time.
1931 * We are only interested in how the sender of this report is doing wrt to us.
1934 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1935 RTPPacketInfo * pinfo)
1941 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1943 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1945 source = obtain_source (sess, senderssrc, &created, pinfo, FALSE);
1950 on_new_ssrc (sess, source);
1952 rtp_session_process_rb (sess, source, packet, pinfo);
1953 g_object_unref (source);
1956 /* Get SDES items and store them in the SSRC */
1958 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1959 RTPPacketInfo * pinfo)
1962 gboolean more_items, more_entries;
1964 items = gst_rtcp_packet_sdes_get_item_count (packet);
1965 GST_DEBUG ("got SDES packet with %d items", items);
1967 more_items = gst_rtcp_packet_sdes_first_item (packet);
1969 while (more_items) {
1971 gboolean changed, created, prevactive;
1975 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1977 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1981 /* find src, no probation when dealing with RTCP */
1982 source = obtain_source (sess, ssrc, &created, pinfo, FALSE);
1986 sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
1988 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1990 while (more_entries) {
1991 GstRTCPSDESType type;
1997 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1999 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
2002 if (type == GST_RTCP_SDES_PRIV) {
2003 name = g_strndup ((const gchar *) &data[1], data[0]);
2005 data += data[0] + 1;
2007 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
2010 value = g_strndup ((const gchar *) data, len);
2012 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
2017 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
2021 /* takes ownership of sdes */
2022 changed = rtp_source_set_sdes_struct (source, sdes);
2024 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2025 source->validated = TRUE;
2028 on_new_ssrc (sess, source);
2030 /* source became active */
2031 if (source_update_active (sess, source, prevactive))
2032 on_ssrc_validated (sess, source);
2035 on_ssrc_sdes (sess, source);
2037 g_object_unref (source);
2039 more_items = gst_rtcp_packet_sdes_next_item (packet);
2044 /* BYE is sent when a client leaves the session
2047 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
2048 RTPPacketInfo * pinfo)
2052 gboolean reconsider = FALSE;
2054 reason = gst_rtcp_packet_bye_get_reason (packet);
2055 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
2057 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
2058 for (i = 0; i < count; i++) {
2061 gboolean created, prevactive, prevsender;
2062 guint pmembers, members;
2064 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
2065 GST_DEBUG ("SSRC: %08x", ssrc);
2067 /* find src and mark bye, no probation when dealing with RTCP */
2068 source = obtain_source (sess, ssrc, &created, pinfo, FALSE);
2072 if (source->internal) {
2073 /* our own source, something weird with this packet */
2074 g_object_unref (source);
2078 /* store time for when we need to time out this source */
2079 source->bye_time = pinfo->current_time;
2081 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2082 prevsender = RTP_SOURCE_IS_SENDER (source);
2084 /* mark the source BYE */
2085 rtp_source_mark_bye (source, reason);
2087 pmembers = sess->stats.active_sources;
2089 source_update_active (sess, source, prevactive);
2090 source_update_sender (sess, source, prevsender);
2092 members = sess->stats.active_sources;
2094 if (!sess->scheduled_bye && members < pmembers) {
2095 /* some members went away since the previous timeout estimate.
2096 * Perform reverse reconsideration but only when we are not scheduling a
2098 if (sess->next_rtcp_check_time != GST_CLOCK_TIME_NONE &&
2099 pinfo->current_time < sess->next_rtcp_check_time) {
2100 GstClockTime time_remaining;
2102 time_remaining = sess->next_rtcp_check_time - pinfo->current_time;
2103 sess->next_rtcp_check_time =
2104 gst_util_uint64_scale (time_remaining, members, pmembers);
2106 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
2107 GST_TIME_ARGS (sess->next_rtcp_check_time));
2109 sess->next_rtcp_check_time += pinfo->current_time;
2111 /* mark pending reconsider. We only want to signal the reconsideration
2112 * once after we handled all the source in the bye packet */
2118 on_new_ssrc (sess, source);
2120 on_bye_ssrc (sess, source);
2122 g_object_unref (source);
2125 RTP_SESSION_UNLOCK (sess);
2126 /* notify app of reconsideration */
2127 if (sess->callbacks.reconsider)
2128 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2129 RTP_SESSION_LOCK (sess);
2135 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
2136 RTPPacketInfo * pinfo)
2138 GST_DEBUG ("received APP");
2142 rtp_session_request_local_key_unit (RTPSession * sess, RTPSource * src,
2143 gboolean fir, GstClockTime current_time)
2145 guint32 round_trip = 0;
2147 rtp_source_get_last_rb (src, NULL, NULL, NULL, NULL, NULL, NULL, &round_trip);
2149 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE && round_trip) {
2150 GstClockTime round_trip_in_ns = gst_util_uint64_scale (round_trip,
2153 if (current_time - sess->last_keyframe_request < 2 * round_trip_in_ns) {
2154 GST_DEBUG ("Ignoring %s request because one was send without one "
2155 "RTT (%" GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")",
2156 fir ? "FIR" : "PLI",
2157 GST_TIME_ARGS (current_time - sess->last_keyframe_request),
2158 GST_TIME_ARGS (round_trip_in_ns));;
2163 sess->last_keyframe_request = current_time;
2165 GST_LOG ("received %s request from %X %p(%p)", fir ? "FIR" : "PLI",
2166 rtp_source_get_ssrc (src), sess->callbacks.process_rtp,
2167 sess->callbacks.request_key_unit);
2169 RTP_SESSION_UNLOCK (sess);
2170 sess->callbacks.request_key_unit (sess, fir,
2171 sess->request_key_unit_user_data);
2172 RTP_SESSION_LOCK (sess);
2178 rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
2179 guint32 media_ssrc, GstClockTime current_time)
2183 if (!sess->callbacks.request_key_unit)
2186 src = find_source (sess, sender_ssrc);
2190 rtp_session_request_local_key_unit (sess, src, FALSE, current_time);
2194 rtp_session_process_fir (RTPSession * sess, guint32 sender_ssrc,
2195 guint8 * fci_data, guint fci_length, GstClockTime current_time)
2200 gboolean our_request = FALSE;
2202 if (!sess->callbacks.request_key_unit)
2208 src = find_source (sess, sender_ssrc);
2210 /* Hack because Google fails to set the sender_ssrc correctly */
2211 if (!src && sender_ssrc == 1) {
2212 GHashTableIter iter;
2214 /* we can't find the source if there are multiple */
2215 if (sess->stats.sender_sources > sess->stats.internal_sender_sources + 1)
2218 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
2219 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
2220 if (!src->internal && rtp_source_is_sender (src))
2228 for (position = 0; position < fci_length; position += 8) {
2229 guint8 *data = fci_data + position;
2232 ssrc = GST_READ_UINT32_BE (data);
2234 own = find_source (sess, ssrc);
2235 if (own->internal) {
2243 rtp_session_request_local_key_unit (sess, src, TRUE, current_time);
2247 rtp_session_process_nack (RTPSession * sess, guint32 sender_ssrc,
2248 guint32 media_ssrc, guint8 * fci_data, guint fci_length,
2249 GstClockTime current_time)
2251 if (!sess->callbacks.notify_nack)
2254 while (fci_length > 0) {
2255 guint16 seqnum, blp;
2257 seqnum = GST_READ_UINT16_BE (fci_data);
2258 blp = GST_READ_UINT16_BE (fci_data + 2);
2260 GST_DEBUG ("NACK #%u, blp %04x", seqnum, blp);
2262 RTP_SESSION_UNLOCK (sess);
2263 sess->callbacks.notify_nack (sess, seqnum, blp,
2264 sess->notify_nack_user_data);
2265 RTP_SESSION_LOCK (sess);
2273 rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
2274 RTPPacketInfo * pinfo, GstClockTime current_time)
2276 GstRTCPType type = gst_rtcp_packet_get_type (packet);
2277 GstRTCPFBType fbtype = gst_rtcp_packet_fb_get_type (packet);
2278 guint32 sender_ssrc = gst_rtcp_packet_fb_get_sender_ssrc (packet);
2279 guint32 media_ssrc = gst_rtcp_packet_fb_get_media_ssrc (packet);
2280 guint8 *fci_data = gst_rtcp_packet_fb_get_fci (packet);
2281 guint fci_length = 4 * gst_rtcp_packet_fb_get_fci_length (packet);
2284 GST_DEBUG ("received feedback %d:%d from %08X about %08X with FCI of "
2285 "length %d", type, fbtype, sender_ssrc, media_ssrc, fci_length);
2287 if (g_signal_has_handler_pending (sess,
2288 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0, TRUE)) {
2289 GstBuffer *fci_buffer = NULL;
2291 if (fci_length > 0) {
2292 fci_buffer = gst_buffer_copy_region (packet->rtcp->buffer,
2293 GST_BUFFER_COPY_MEMORY, fci_data - packet->rtcp->map.data,
2295 GST_BUFFER_TIMESTAMP (fci_buffer) = pinfo->running_time;
2298 sess->stats.nacks_received++;
2300 RTP_SESSION_UNLOCK (sess);
2301 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0,
2302 type, fbtype, sender_ssrc, media_ssrc, fci_buffer);
2303 RTP_SESSION_LOCK (sess);
2306 gst_buffer_unref (fci_buffer);
2309 src = find_source (sess, media_ssrc);
2313 if (sess->rtcp_feedback_retention_window) {
2314 rtp_source_retain_rtcp_packet (src, packet, pinfo->running_time);
2317 if (src->internal ||
2318 /* PSFB FIR puts the media ssrc inside the FCI */
2319 (type == GST_RTCP_TYPE_PSFB && fbtype == GST_RTCP_PSFB_TYPE_FIR)) {
2321 case GST_RTCP_TYPE_PSFB:
2323 case GST_RTCP_PSFB_TYPE_PLI:
2324 rtp_session_process_pli (sess, sender_ssrc, media_ssrc,
2327 case GST_RTCP_PSFB_TYPE_FIR:
2328 rtp_session_process_fir (sess, sender_ssrc, fci_data, fci_length,
2335 case GST_RTCP_TYPE_RTPFB:
2337 case GST_RTCP_RTPFB_TYPE_NACK:
2338 rtp_session_process_nack (sess, sender_ssrc, media_ssrc,
2339 fci_data, fci_length, current_time);
2351 * rtp_session_process_rtcp:
2352 * @sess: and #RTPSession
2353 * @buffer: an RTCP buffer
2354 * @current_time: the current system time
2355 * @ntpnstime: the current NTP time in nanoseconds
2357 * Process an RTCP buffer in the session manager. This function takes ownership
2360 * Returns: a #GstFlowReturn.
2363 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
2364 GstClockTime current_time, guint64 ntpnstime)
2366 GstRTCPPacket packet;
2367 gboolean more, is_bye = FALSE, do_sync = FALSE;
2368 RTPPacketInfo pinfo = { 0, };
2369 GstFlowReturn result = GST_FLOW_OK;
2370 GstRTCPBuffer rtcp = { NULL, };
2372 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2373 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2375 if (!gst_rtcp_buffer_validate (buffer))
2376 goto invalid_packet;
2378 GST_DEBUG ("received RTCP packet");
2380 RTP_SESSION_LOCK (sess);
2381 /* update pinfo stats */
2382 update_packet_info (sess, &pinfo, FALSE, FALSE, FALSE, buffer, current_time,
2385 /* start processing the compound packet */
2386 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
2387 more = gst_rtcp_buffer_get_first_packet (&rtcp, &packet);
2391 type = gst_rtcp_packet_get_type (&packet);
2393 /* when we are leaving the session, we should ignore all non-BYE messages */
2394 if (sess->scheduled_bye && type != GST_RTCP_TYPE_BYE) {
2395 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
2400 case GST_RTCP_TYPE_SR:
2401 rtp_session_process_sr (sess, &packet, &pinfo, &do_sync);
2403 case GST_RTCP_TYPE_RR:
2404 rtp_session_process_rr (sess, &packet, &pinfo);
2406 case GST_RTCP_TYPE_SDES:
2407 rtp_session_process_sdes (sess, &packet, &pinfo);
2409 case GST_RTCP_TYPE_BYE:
2411 /* don't try to attempt lip-sync anymore for streams with a BYE */
2413 rtp_session_process_bye (sess, &packet, &pinfo);
2415 case GST_RTCP_TYPE_APP:
2416 rtp_session_process_app (sess, &packet, &pinfo);
2418 case GST_RTCP_TYPE_RTPFB:
2419 case GST_RTCP_TYPE_PSFB:
2420 rtp_session_process_feedback (sess, &packet, &pinfo, current_time);
2423 GST_WARNING ("got unknown RTCP packet");
2427 more = gst_rtcp_packet_move_to_next (&packet);
2430 gst_rtcp_buffer_unmap (&rtcp);
2432 /* if we are scheduling a BYE, we only want to count bye packets, else we
2433 * count everything */
2434 if (sess->scheduled_bye) {
2436 sess->stats.bye_members++;
2437 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, pinfo.bytes);
2440 /* keep track of average packet size */
2441 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, pinfo.bytes);
2443 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
2444 sess->stats.avg_rtcp_packet_size, pinfo.bytes);
2445 RTP_SESSION_UNLOCK (sess);
2448 clean_packet_info (&pinfo);
2450 /* notify caller of sr packets in the callback */
2451 if (do_sync && sess->callbacks.sync_rtcp) {
2452 result = sess->callbacks.sync_rtcp (sess, buffer,
2453 sess->sync_rtcp_user_data);
2455 gst_buffer_unref (buffer);
2462 GST_DEBUG ("invalid RTCP packet received");
2463 gst_buffer_unref (buffer);
2469 * rtp_session_update_send_caps:
2470 * @sess: an #RTPSession
2473 * Update the caps of the sender in the rtp session.
2476 rtp_session_update_send_caps (RTPSession * sess, GstCaps * caps)
2481 g_return_if_fail (RTP_IS_SESSION (sess));
2482 g_return_if_fail (GST_IS_CAPS (caps));
2484 GST_LOG ("received caps %" GST_PTR_FORMAT, caps);
2486 s = gst_caps_get_structure (caps, 0);
2488 if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
2492 RTP_SESSION_LOCK (sess);
2493 source = obtain_internal_source (sess, ssrc, &created);
2495 rtp_source_update_caps (source, caps);
2496 g_object_unref (source);
2498 RTP_SESSION_UNLOCK (sess);
2503 * rtp_session_send_rtp:
2504 * @sess: an #RTPSession
2505 * @data: pointer to either an RTP buffer or a list of RTP buffers
2506 * @is_list: TRUE when @data is a buffer list
2507 * @current_time: the current system time
2508 * @running_time: the running time of @data
2510 * Send the RTP buffer in the session manager. This function takes ownership of
2513 * Returns: a #GstFlowReturn.
2516 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
2517 GstClockTime current_time, GstClockTime running_time)
2519 GstFlowReturn result;
2521 gboolean prevsender;
2523 RTPPacketInfo pinfo = { 0, };
2526 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2527 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
2529 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
2531 RTP_SESSION_LOCK (sess);
2532 if (!update_packet_info (sess, &pinfo, TRUE, TRUE, is_list, data,
2533 current_time, running_time, -1))
2534 goto invalid_packet;
2536 source = obtain_internal_source (sess, pinfo.ssrc, &created);
2538 /* update last activity */
2539 source->last_rtp_activity = current_time;
2541 prevsender = RTP_SOURCE_IS_SENDER (source);
2542 oldrate = source->bitrate;
2544 /* we use our own source to send */
2545 result = rtp_source_send_rtp (source, &pinfo);
2547 source_update_sender (sess, source, prevsender);
2549 if (oldrate != source->bitrate)
2550 sess->recalc_bandwidth = TRUE;
2551 RTP_SESSION_UNLOCK (sess);
2553 g_object_unref (source);
2554 clean_packet_info (&pinfo);
2560 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
2561 RTP_SESSION_UNLOCK (sess);
2562 GST_DEBUG ("invalid RTP packet received");
2568 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
2570 *bandwidth += source->bitrate;
2573 /* must be called with session lock */
2575 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2578 GstClockTime result;
2580 /* recalculate bandwidth when it changed */
2581 if (sess->recalc_bandwidth) {
2584 if (sess->bandwidth > 0)
2585 bandwidth = sess->bandwidth;
2587 /* If it is <= 0, then try to estimate the actual bandwidth */
2590 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2591 (GHFunc) add_bitrates, &bandwidth);
2594 if (bandwidth < 8000)
2595 bandwidth = RTP_STATS_BANDWIDTH;
2597 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
2598 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
2600 sess->recalc_bandwidth = FALSE;
2603 if (sess->scheduled_bye) {
2604 result = rtp_stats_calculate_bye_interval (&sess->stats);
2606 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2607 sess->stats.internal_sender_sources > 0, first);
2610 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2611 GST_TIME_ARGS (result), first);
2613 if (!deterministic && result != GST_CLOCK_TIME_NONE)
2614 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2616 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2622 source_mark_bye (const gchar * key, RTPSource * source, const gchar * reason)
2624 if (source->internal)
2625 rtp_source_mark_bye (source, reason);
2629 * rtp_session_mark_all_bye:
2630 * @sess: an #RTPSession
2633 * Mark all internal sources of the session as BYE with @reason.
2636 rtp_session_mark_all_bye (RTPSession * sess, const gchar * reason)
2638 g_return_if_fail (RTP_IS_SESSION (sess));
2640 RTP_SESSION_LOCK (sess);
2641 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2642 (GHFunc) source_mark_bye, (gpointer) reason);
2643 RTP_SESSION_UNLOCK (sess);
2646 /* Stop the current @sess and schedule a BYE message for the other members.
2647 * One must have the session lock to call this function
2649 static GstFlowReturn
2650 rtp_session_schedule_bye_locked (RTPSession * sess, GstClockTime current_time)
2652 GstFlowReturn result = GST_FLOW_OK;
2653 GstClockTime interval;
2655 /* nothing to do it we already scheduled bye */
2656 if (sess->scheduled_bye)
2659 /* we schedule BYE now */
2660 sess->scheduled_bye = TRUE;
2661 /* at least one member wants to send a BYE */
2662 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
2663 sess->stats.bye_members = 1;
2664 sess->first_rtcp = TRUE;
2665 sess->allow_early = TRUE;
2667 /* reschedule transmission */
2668 sess->last_rtcp_send_time = current_time;
2669 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2671 if (interval != GST_CLOCK_TIME_NONE)
2672 sess->next_rtcp_check_time = current_time + interval;
2674 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
2676 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2677 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2679 RTP_SESSION_UNLOCK (sess);
2680 /* notify app of reconsideration */
2681 if (sess->callbacks.reconsider)
2682 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2683 RTP_SESSION_LOCK (sess);
2690 * rtp_session_schedule_bye:
2691 * @sess: an #RTPSession
2692 * @current_time: the current system time
2694 * Schedule a BYE message for all sources marked as BYE in @sess.
2696 * Returns: a #GstFlowReturn.
2699 rtp_session_schedule_bye (RTPSession * sess, GstClockTime current_time)
2701 GstFlowReturn result = GST_FLOW_OK;
2703 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2705 RTP_SESSION_LOCK (sess);
2706 result = rtp_session_schedule_bye_locked (sess, current_time);
2707 RTP_SESSION_UNLOCK (sess);
2713 * rtp_session_next_timeout:
2714 * @sess: an #RTPSession
2715 * @current_time: the current system time
2717 * Get the next time we should perform session maintenance tasks.
2719 * Returns: a time when rtp_session_on_timeout() should be called with the
2720 * current system time.
2723 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2725 GstClockTime result, interval = 0;
2727 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
2729 RTP_SESSION_LOCK (sess);
2731 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
2732 GST_DEBUG ("have early rtcp time");
2733 result = sess->next_early_rtcp_time;
2737 result = sess->next_rtcp_check_time;
2739 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2740 ", next time: %" GST_TIME_FORMAT,
2741 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2743 if (result == GST_CLOCK_TIME_NONE || result < current_time) {
2744 GST_DEBUG ("take current time as base");
2745 /* our previous check time expired, start counting from the current time
2747 result = current_time;
2750 if (sess->scheduled_bye) {
2751 if (sess->stats.active_sources >= 50) {
2752 GST_DEBUG ("reconsider BYE, more than 50 sources");
2753 /* reconsider BYE if members >= 50 */
2754 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2757 if (sess->first_rtcp) {
2758 GST_DEBUG ("first RTCP packet");
2759 /* we are called for the first time */
2760 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2761 } else if (sess->next_rtcp_check_time < current_time) {
2762 GST_DEBUG ("old check time expired, getting new timeout");
2763 /* get a new timeout when we need to */
2764 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
2768 if (interval != GST_CLOCK_TIME_NONE)
2771 result = GST_CLOCK_TIME_NONE;
2773 sess->next_rtcp_check_time = result;
2777 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2778 ", next time: %" GST_TIME_FORMAT,
2779 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2780 RTP_SESSION_UNLOCK (sess);
2794 GstRTCPBuffer rtcpbuf;
2797 guint num_to_report;
2802 GstClockTime current_time;
2804 GstClockTime running_time;
2805 GstClockTime interval;
2806 GstRTCPPacket packet;
2809 gboolean may_suppress;
2811 guint nacked_seqnums;
2815 session_start_rtcp (RTPSession * sess, ReportData * data)
2817 GstRTCPPacket *packet = &data->packet;
2818 RTPSource *own = data->source;
2819 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2821 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2822 data->has_sdes = FALSE;
2824 gst_rtcp_buffer_map (data->rtcp, GST_MAP_READWRITE, rtcp);
2826 if (RTP_SOURCE_IS_SENDER (own)) {
2829 guint32 packet_count, octet_count;
2831 /* we are a sender, create SR */
2832 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2833 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SR, packet);
2835 /* get latest stats */
2836 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
2837 &ntptime, &rtptime, &packet_count, &octet_count);
2839 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2840 packet_count, octet_count);
2842 /* fill in sender report info */
2843 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2844 ntptime, rtptime, packet_count, octet_count);
2846 /* we are only receiver, create RR */
2847 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2848 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RR, packet);
2849 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2853 /* construct a Sender or Receiver Report */
2855 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2857 RTPSession *sess = data->sess;
2858 GstRTCPPacket *packet = &data->packet;
2859 guint8 fractionlost;
2861 guint32 exthighestseq, jitter;
2864 /* don't report for sources in future generations */
2865 if (((gint16) (source->generation - sess->generation)) > 0) {
2866 GST_DEBUG ("source %08x generation %u > %u", source->ssrc,
2867 source->generation, sess->generation);
2871 /* only report about other sender */
2872 if (source == data->source)
2875 if (gst_rtcp_packet_get_rb_count (packet) == GST_RTCP_MAX_RB_COUNT) {
2876 GST_DEBUG ("max RB count reached");
2880 if (!RTP_SOURCE_IS_SENDER (source)) {
2881 GST_DEBUG ("source %08x not sender", source->ssrc);
2885 GST_DEBUG ("create RB for SSRC %08x", source->ssrc);
2888 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2889 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2891 /* store last generated RR packet */
2892 source->last_rr.is_valid = TRUE;
2893 source->last_rr.fractionlost = fractionlost;
2894 source->last_rr.packetslost = packetslost;
2895 source->last_rr.exthighestseq = exthighestseq;
2896 source->last_rr.jitter = jitter;
2897 source->last_rr.lsr = lsr;
2898 source->last_rr.dlsr = dlsr;
2900 /* packet is not yet filled, add report block for this source. */
2901 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2902 exthighestseq, jitter, lsr, dlsr);
2905 /* source is reported, move to next generation */
2906 source->generation = sess->generation + 1;
2908 /* if we reported all sources in this generation, move to next */
2909 if (--data->num_to_report == 0) {
2911 GST_DEBUG ("all reported, generation now %u", sess->generation);
2917 session_add_fir (const gchar * key, RTPSource * source, ReportData * data)
2919 GstRTCPPacket *packet = &data->packet;
2923 if (!source->send_fir)
2926 len = gst_rtcp_packet_fb_get_fci_length (packet);
2927 if (!gst_rtcp_packet_fb_set_fci_length (packet, len + 2))
2928 /* exit because the packet is full, will put next request in a
2932 fci_data = gst_rtcp_packet_fb_get_fci (packet) + (len * 4);
2934 GST_WRITE_UINT32_BE (fci_data, source->ssrc);
2936 fci_data[0] = source->current_send_fir_seqnum;
2937 fci_data[1] = fci_data[2] = fci_data[3] = 0;
2939 source->send_fir = FALSE;
2943 session_fir (RTPSession * sess, ReportData * data)
2945 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2946 GstRTCPPacket *packet = &data->packet;
2948 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
2951 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_FIR);
2952 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
2953 gst_rtcp_packet_fb_set_media_ssrc (packet, 0);
2955 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2956 (GHFunc) session_add_fir, data);
2958 if (gst_rtcp_packet_fb_get_fci_length (packet) == 0)
2959 gst_rtcp_packet_remove (packet);
2961 data->may_suppress = FALSE;
2965 has_pli_compare_func (gconstpointer a, gconstpointer ignored)
2967 GstRTCPPacket packet;
2968 GstRTCPBuffer rtcp = { NULL, };
2969 gboolean ret = FALSE;
2971 gst_rtcp_buffer_map ((GstBuffer *) a, GST_MAP_READ, &rtcp);
2973 if (gst_rtcp_buffer_get_first_packet (&rtcp, &packet)) {
2974 if (gst_rtcp_packet_get_type (&packet) == GST_RTCP_TYPE_PSFB &&
2975 gst_rtcp_packet_fb_get_type (&packet) == GST_RTCP_PSFB_TYPE_PLI)
2979 gst_rtcp_buffer_unmap (&rtcp);
2986 session_pli (const gchar * key, RTPSource * source, ReportData * data)
2988 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2989 GstRTCPPacket *packet = &data->packet;
2991 if (!source->send_pli)
2994 if (rtp_source_has_retained (source, has_pli_compare_func, NULL))
2997 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
2998 /* exit because the packet is full, will put next request in a
3002 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_PLI);
3003 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3004 gst_rtcp_packet_fb_set_media_ssrc (packet, source->ssrc);
3006 source->send_pli = FALSE;
3007 data->may_suppress = FALSE;
3010 /* construct NACK */
3012 session_nack (const gchar * key, RTPSource * source, ReportData * data)
3014 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3015 GstRTCPPacket *packet = &data->packet;
3020 if (!source->send_nack)
3023 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RTPFB, packet))
3024 /* exit because the packet is full, will put next request in a
3028 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_RTPFB_TYPE_NACK);
3029 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3030 gst_rtcp_packet_fb_set_media_ssrc (packet, source->ssrc);
3032 nacks = rtp_source_get_nacks (source, &n_nacks);
3033 GST_DEBUG ("%u NACKs", n_nacks);
3034 if (!gst_rtcp_packet_fb_set_fci_length (packet, n_nacks))
3037 fci_data = gst_rtcp_packet_fb_get_fci (packet);
3038 for (i = 0; i < n_nacks; i++) {
3039 GST_WRITE_UINT32_BE (fci_data, nacks[i]);
3041 data->nacked_seqnums++;
3044 rtp_source_clear_nacks (source);
3045 data->may_suppress = FALSE;
3048 /* perform cleanup of sources that timed out */
3050 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
3052 gboolean remove = FALSE;
3053 gboolean byetimeout = FALSE;
3054 gboolean sendertimeout = FALSE;
3055 gboolean is_sender, is_active;
3056 RTPSession *sess = data->sess;
3057 GstClockTime interval, binterval;
3060 GST_DEBUG ("look at %08x, generation %u", source->ssrc, source->generation);
3062 /* check for outdated collisions */
3063 if (source->internal) {
3064 GST_DEBUG ("Timing out collisions for %x", source->ssrc);
3065 rtp_source_timeout (source, data->current_time,
3066 /* "a relatively long time" -- RFC 3550 section 8.2 */
3067 RTP_STATS_MIN_INTERVAL * GST_SECOND * 10,
3068 data->running_time - sess->rtcp_feedback_retention_window);
3071 /* nothing else to do when without RTCP */
3072 if (data->interval == GST_CLOCK_TIME_NONE)
3075 is_sender = RTP_SOURCE_IS_SENDER (source);
3076 is_active = RTP_SOURCE_IS_ACTIVE (source);
3078 /* our own rtcp interval may have been forced low by secondary configuration,
3079 * while sender side may still operate with higher interval,
3080 * so do not just take our interval to decide on timing out sender,
3081 * but take (if data->interval <= 5 * GST_SECOND):
3082 * interval = CLAMP (sender_interval, data->interval, 5 * GST_SECOND)
3083 * where sender_interval is difference between last 2 received RTCP reports
3085 if (data->interval >= 5 * GST_SECOND || source->internal) {
3086 binterval = data->interval;
3088 GST_LOG ("prev_rtcp %" GST_TIME_FORMAT ", last_rtcp %" GST_TIME_FORMAT,
3089 GST_TIME_ARGS (source->stats.prev_rtcptime),
3090 GST_TIME_ARGS (source->stats.last_rtcptime));
3091 /* if not received enough yet, fallback to larger default */
3092 if (source->stats.last_rtcptime > source->stats.prev_rtcptime)
3093 binterval = source->stats.last_rtcptime - source->stats.prev_rtcptime;
3095 binterval = 5 * GST_SECOND;
3096 binterval = CLAMP (binterval, data->interval, 5 * GST_SECOND);
3098 GST_LOG ("timeout base interval %" GST_TIME_FORMAT,
3099 GST_TIME_ARGS (binterval));
3101 if (!source->internal) {
3102 if (source->marked_bye) {
3103 /* if we received a BYE from the source, remove the source after some
3105 if (data->current_time > source->bye_time &&
3106 data->current_time - source->bye_time > sess->stats.bye_timeout) {
3107 GST_DEBUG ("removing BYE source %08x", source->ssrc);
3112 /* sources that were inactive for more than 5 times the deterministic reporting
3113 * interval get timed out. the min timeout is 5 seconds. */
3114 /* mind old time that might pre-date last time going to PLAYING */
3115 btime = MAX (source->last_activity, sess->start_time);
3116 if (data->current_time > btime) {
3117 interval = MAX (binterval * 5, 5 * GST_SECOND);
3118 if (data->current_time - btime > interval) {
3119 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
3120 source->ssrc, GST_TIME_ARGS (btime));
3126 /* senders that did not send for a long time become a receiver, this also
3127 * holds for our own sources. */
3129 /* mind old time that might pre-date last time going to PLAYING */
3130 btime = MAX (source->last_rtp_activity, sess->start_time);
3131 if (data->current_time > btime) {
3132 interval = MAX (binterval * 2, 5 * GST_SECOND);
3133 if (data->current_time - btime > interval) {
3134 if (source->internal && source->sent_bye) {
3135 /* an internal source is BYE and stopped sending RTP, remove */
3136 GST_DEBUG ("internal BYE source %08x timed out, last %"
3137 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
3140 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
3141 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
3142 sendertimeout = TRUE;
3149 sess->total_sources--;
3151 sess->stats.sender_sources--;
3152 if (source->internal)
3153 sess->stats.internal_sender_sources--;
3156 sess->stats.active_sources--;
3158 if (source->internal)
3159 sess->stats.internal_sources--;
3162 on_bye_timeout (sess, source);
3164 on_timeout (sess, source);
3166 if (sendertimeout) {
3167 source->is_sender = FALSE;
3168 sess->stats.sender_sources--;
3169 if (source->internal)
3170 sess->stats.internal_sender_sources--;
3172 on_sender_timeout (sess, source);
3174 /* count how many source to report in this generation */
3175 if (((gint16) (source->generation - sess->generation)) <= 0)
3176 data->num_to_report++;
3178 source->closing = remove;
3182 session_sdes (RTPSession * sess, ReportData * data)
3184 GstRTCPPacket *packet = &data->packet;
3185 const GstStructure *sdes;
3187 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3189 /* add SDES packet */
3190 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SDES, packet);
3192 gst_rtcp_packet_sdes_add_item (packet, data->source->ssrc);
3194 sdes = rtp_source_get_sdes_struct (data->source);
3196 /* add all fields in the structure, the order is not important. */
3197 n_fields = gst_structure_n_fields (sdes);
3198 for (i = 0; i < n_fields; ++i) {
3201 GstRTCPSDESType type;
3203 field = gst_structure_nth_field_name (sdes, i);
3206 value = gst_structure_get_string (sdes, field);
3209 type = gst_rtcp_sdes_name_to_type (field);
3211 /* Early packets are minimal and only include the CNAME */
3212 if (data->is_early && type != GST_RTCP_SDES_CNAME)
3215 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
3216 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
3217 (const guint8 *) value);
3218 } else if (type == GST_RTCP_SDES_PRIV) {
3224 /* don't accept entries that are too big */
3225 prefix_len = strlen (field);
3226 if (prefix_len > 255)
3228 value_len = strlen (value);
3229 if (value_len > 255)
3231 data_len = 1 + prefix_len + value_len;
3235 data[0] = prefix_len;
3236 memcpy (&data[1], field, prefix_len);
3237 memcpy (&data[1 + prefix_len], value, value_len);
3239 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
3243 data->has_sdes = TRUE;
3246 /* schedule a BYE packet */
3248 make_source_bye (RTPSession * sess, RTPSource * source, ReportData * data)
3250 GstRTCPPacket *packet = &data->packet;
3251 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3254 session_sdes (sess, data);
3255 /* add a BYE packet */
3256 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_BYE, packet);
3257 gst_rtcp_packet_bye_add_ssrc (packet, source->ssrc);
3258 if (source->bye_reason)
3259 gst_rtcp_packet_bye_set_reason (packet, source->bye_reason);
3261 /* we have a BYE packet now */
3262 source->sent_bye = TRUE;
3266 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
3268 GstClockTime new_send_time, elapsed;
3269 GstClockTime interval;
3271 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3272 data->is_early = TRUE;
3274 data->is_early = FALSE;
3276 if (data->is_early && sess->next_early_rtcp_time < current_time) {
3277 GST_DEBUG ("early feedback %" GST_TIME_FORMAT " < now %"
3278 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_early_rtcp_time),
3279 GST_TIME_ARGS (current_time));
3283 /* no need to check yet */
3284 if (sess->next_rtcp_check_time == GST_CLOCK_TIME_NONE ||
3285 sess->next_rtcp_check_time > current_time) {
3286 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
3287 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
3288 GST_TIME_ARGS (current_time));
3293 /* get elapsed time since we last reported */
3294 elapsed = current_time - sess->last_rtcp_send_time;
3296 /* take interval and add jitter */
3297 interval = data->interval;
3298 if (interval != GST_CLOCK_TIME_NONE)
3299 interval = rtp_stats_add_rtcp_jitter (&sess->stats, interval);
3301 /* perform forward reconsideration */
3302 if (interval != GST_CLOCK_TIME_NONE) {
3303 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
3304 GST_TIME_FORMAT, GST_TIME_ARGS (interval), GST_TIME_ARGS (elapsed));
3305 new_send_time = interval + sess->last_rtcp_send_time;
3307 new_send_time = sess->last_rtcp_send_time;
3310 if (!data->is_early) {
3311 /* check if reconsideration */
3312 if (new_send_time == GST_CLOCK_TIME_NONE || current_time < new_send_time) {
3313 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
3314 GST_TIME_ARGS (new_send_time));
3315 /* store new check time */
3316 sess->next_rtcp_check_time = new_send_time;
3319 sess->next_rtcp_check_time = current_time + interval;
3320 } else if (interval != GST_CLOCK_TIME_NONE) {
3321 /* Apply the rules from RFC 4585 section 3.5.3 */
3322 if (sess->stats.min_interval != 0 && !sess->first_rtcp) {
3323 GstClockTime T_rr_current_interval =
3324 g_random_double_range (0.5, 1.5) * sess->stats.min_interval;
3326 /* This will caused the RTCP to be suppressed if no FB packets are added */
3327 if (sess->last_rtcp_send_time + T_rr_current_interval > new_send_time) {
3328 GST_DEBUG ("RTCP packet could be suppressed min: %" GST_TIME_FORMAT
3329 " last: %" GST_TIME_FORMAT
3330 " + T_rr_current_interval: %" GST_TIME_FORMAT
3331 " > new_send_time: %" GST_TIME_FORMAT,
3332 GST_TIME_ARGS (sess->stats.min_interval),
3333 GST_TIME_ARGS (sess->last_rtcp_send_time),
3334 GST_TIME_ARGS (T_rr_current_interval),
3335 GST_TIME_ARGS (new_send_time));
3336 data->may_suppress = TRUE;
3341 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
3342 GST_TIME_ARGS (new_send_time));
3348 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
3350 g_hash_table_insert (hash_table, key, g_object_ref (source));
3354 remove_closing_sources (const gchar * key, RTPSource * source,
3357 if (source->closing)
3360 if (source->send_fir)
3361 data->have_fir = TRUE;
3362 if (source->send_pli)
3363 data->have_pli = TRUE;
3364 if (source->send_nack)
3365 data->have_nack = TRUE;
3371 generate_rtcp (const gchar * key, RTPSource * source, ReportData * data)
3373 RTPSession *sess = data->sess;
3374 gboolean is_bye = FALSE;
3375 ReportOutput *output;
3377 /* only generate RTCP for active internal sources */
3378 if (!source->internal || source->sent_bye)
3381 data->source = source;
3384 session_start_rtcp (sess, data);
3386 if (source->marked_bye) {
3388 make_source_bye (sess, source, data);
3390 } else if (!data->is_early) {
3391 /* loop over all known sources and add report blocks. If we are early, we
3392 * just make a minimal RTCP packet and skip this step */
3393 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3394 (GHFunc) session_report_blocks, data);
3396 if (!data->has_sdes)
3397 session_sdes (sess, data);
3400 session_fir (sess, data);
3403 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3404 (GHFunc) session_pli, data);
3406 if (data->have_nack)
3407 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3408 (GHFunc) session_nack, data);
3410 gst_rtcp_buffer_unmap (&data->rtcpbuf);
3412 output = g_slice_new (ReportOutput);
3413 output->source = g_object_ref (source);
3414 output->is_bye = is_bye;
3415 output->buffer = data->rtcp;
3416 /* queue the RTCP packet to push later */
3417 g_queue_push_tail (&data->output, output);
3421 * rtp_session_on_timeout:
3422 * @sess: an #RTPSession
3423 * @current_time: the current system time
3424 * @ntpnstime: the current NTP time in nanoseconds
3425 * @running_time: the current running_time of the pipeline
3427 * Perform maintenance actions after the timeout obtained with
3428 * rtp_session_next_timeout() expired.
3430 * This function will perform timeouts of receivers and senders, send a BYE
3431 * packet or generate RTCP packets with current session stats.
3433 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
3434 * times, for each packet that should be processed.
3436 * Returns: a #GstFlowReturn.
3439 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
3440 guint64 ntpnstime, GstClockTime running_time)
3442 GstFlowReturn result = GST_FLOW_OK;
3443 ReportData data = { GST_RTCP_BUFFER_INIT };
3444 GHashTable *table_copy;
3445 ReportOutput *output;
3447 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3449 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT
3450 ", running-time %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
3451 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (running_time));
3454 data.current_time = current_time;
3455 data.ntpnstime = ntpnstime;
3456 data.running_time = running_time;
3457 data.num_to_report = 0;
3458 data.may_suppress = FALSE;
3459 data.nacked_seqnums = 0;
3460 g_queue_init (&data.output);
3462 RTP_SESSION_LOCK (sess);
3463 /* get a new interval, we need this for various cleanups etc */
3464 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
3466 GST_DEBUG ("interval %" GST_TIME_FORMAT, GST_TIME_ARGS (data.interval));
3468 /* we need an internal source now */
3469 if (sess->stats.internal_sources == 0) {
3473 source = obtain_internal_source (sess, sess->suggested_ssrc, &created);
3474 g_object_unref (source);
3477 /* Make a local copy of the hashtable. We need to do this because the
3478 * cleanup stage below releases the session lock. */
3479 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
3480 (GDestroyNotify) g_object_unref);
3481 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3482 (GHFunc) clone_ssrcs_hashtable, table_copy);
3484 /* Clean up the session, mark the source for removing, this might release the
3486 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
3487 g_hash_table_destroy (table_copy);
3489 /* Now remove the marked sources */
3490 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
3491 (GHRFunc) remove_closing_sources, &data);
3493 /* see if we need to generate SR or RR packets */
3494 if (!is_rtcp_time (sess, current_time, &data))
3497 GST_DEBUG ("doing RTCP generation %u for %u sources, early %d",
3498 sess->generation, data.num_to_report, data.is_early);
3500 /* generate RTCP for all internal sources */
3501 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3502 (GHFunc) generate_rtcp, &data);
3504 /* we keep track of the last report time in order to timeout inactive
3505 * receivers or senders */
3506 if (!data.is_early && !data.may_suppress)
3507 sess->last_rtcp_send_time = data.current_time;
3508 sess->first_rtcp = FALSE;
3509 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
3512 RTP_SESSION_UNLOCK (sess);
3514 /* push out the RTCP packets */
3515 while ((output = g_queue_pop_head (&data.output))) {
3516 gboolean do_not_suppress;
3517 GstBuffer *buffer = output->buffer;
3518 RTPSource *source = output->source;
3520 /* Give the user a change to add its own packet */
3521 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
3522 buffer, data.is_early, &do_not_suppress);
3524 if (sess->callbacks.send_rtcp && (do_not_suppress || !data.may_suppress)) {
3527 packet_size = gst_buffer_get_size (buffer) + sess->header_len;
3529 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
3530 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
3531 sess->stats.avg_rtcp_packet_size, packet_size);
3533 sess->callbacks.send_rtcp (sess, source, buffer, output->is_bye,
3534 sess->send_rtcp_user_data);
3535 sess->stats.nacks_sent += data.nacked_seqnums;
3537 GST_DEBUG ("freeing packet callback: %p"
3538 " do_not_suppress: %d may_suppress: %d",
3539 sess->callbacks.send_rtcp, do_not_suppress, data.may_suppress);
3540 sess->stats.nacks_dropped += data.nacked_seqnums;
3541 gst_buffer_unref (buffer);
3543 g_object_unref (source);
3544 g_slice_free (ReportOutput, output);
3550 * rtp_session_request_early_rtcp:
3551 * @sess: an #RTPSession
3552 * @current_time: the current system time
3553 * @max_delay: maximum delay
3555 * Request transmission of early RTCP
3558 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
3559 GstClockTime max_delay)
3561 GstClockTime T_dither_max;
3563 /* Implements the algorithm described in RFC 4585 section 3.5.2 */
3565 RTP_SESSION_LOCK (sess);
3567 /* Check if already requested */
3568 /* RFC 4585 section 3.5.2 step 2 */
3569 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
3570 GST_LOG_OBJECT (sess, "already have next early rtcp time");
3574 if (!GST_CLOCK_TIME_IS_VALID (sess->next_rtcp_check_time)) {
3575 GST_LOG_OBJECT (sess, "no next RTCP check time");
3579 /* Ignore the request a scheduled packet will be in time anyway */
3580 if (current_time + max_delay > sess->next_rtcp_check_time) {
3581 GST_LOG_OBJECT (sess, "next scheduled time is soon %" GST_TIME_FORMAT " + %"
3582 GST_TIME_FORMAT " > %" GST_TIME_FORMAT,
3583 GST_TIME_ARGS (current_time),
3584 GST_TIME_ARGS (max_delay), GST_TIME_ARGS (sess->next_rtcp_check_time));
3588 /* RFC 4585 section 3.5.2 step 2b */
3589 /* If the total sources is <=2, then there is only us and one peer */
3590 if (sess->total_sources <= 2) {
3593 /* Divide by 2 because l = 0.5 */
3594 T_dither_max = sess->next_rtcp_check_time - sess->last_rtcp_send_time;
3598 /* RFC 4585 section 3.5.2 step 3 */
3599 if (current_time + T_dither_max > sess->next_rtcp_check_time) {
3600 GST_LOG_OBJECT (sess, "don't send because of dither");
3604 /* RFC 4585 section 3.5.2 step 4
3605 * Don't send if allow_early is FALSE, but not if we are in
3606 * immediate mode, meaning we are part of a group of at most the
3607 * application-specific threshold.
3609 if (sess->total_sources > sess->rtcp_immediate_feedback_threshold &&
3610 sess->allow_early == FALSE) {
3611 GST_LOG_OBJECT (sess, "can't allow early feedback");
3616 /* Schedule an early transmission later */
3617 sess->next_early_rtcp_time = g_random_double () * T_dither_max +
3620 /* If no dithering, schedule it for NOW */
3621 sess->next_early_rtcp_time = current_time;
3624 GST_LOG_OBJECT (sess, "next early RTCP time %" GST_TIME_FORMAT,
3625 GST_TIME_ARGS (sess->next_early_rtcp_time));
3626 RTP_SESSION_UNLOCK (sess);
3628 /* notify app of need to send packet early
3629 * and therefore of timeout change */
3630 if (sess->callbacks.reconsider)
3631 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
3637 RTP_SESSION_UNLOCK (sess);
3641 rtp_session_send_rtcp (RTPSession * sess, GstClockTime max_delay)
3645 if (!sess->callbacks.send_rtcp)
3648 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
3650 rtp_session_request_early_rtcp (sess, now, max_delay);
3654 rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc,
3655 gboolean fir, gint count)
3659 RTP_SESSION_LOCK (sess);
3660 src = find_source (sess, ssrc);
3665 src->send_pli = FALSE;
3666 src->send_fir = TRUE;
3668 if (count == -1 || count != src->last_fir_count)
3669 src->current_send_fir_seqnum++;
3670 src->last_fir_count = count;
3671 } else if (!src->send_fir) {
3672 src->send_pli = TRUE;
3674 RTP_SESSION_UNLOCK (sess);
3676 rtp_session_send_rtcp (sess, 200 * GST_MSECOND);
3683 RTP_SESSION_UNLOCK (sess);
3689 * rtp_session_request_nack:
3690 * @sess: a #RTPSession
3692 * @seqnum: the missing seqnum
3693 * @max_delay: max delay to request NACK
3695 * Request scheduling of a NACK feedback packet for @seqnum in @ssrc.
3697 * Returns: %TRUE if the NACK feedback could be scheduled
3700 rtp_session_request_nack (RTPSession * sess, guint32 ssrc, guint16 seqnum,
3701 GstClockTime max_delay)
3705 RTP_SESSION_LOCK (sess);
3706 source = find_source (sess, ssrc);
3710 GST_DEBUG ("request NACK for %08x, #%u", ssrc, seqnum);
3711 rtp_source_register_nack (source, seqnum);
3712 RTP_SESSION_UNLOCK (sess);
3714 rtp_session_send_rtcp (sess, max_delay);
3721 RTP_SESSION_UNLOCK (sess);