2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
21 * with newer GLib versions (>= 2.31.0) */
22 #define GLIB_DISABLE_DEPRECATION_WARNINGS
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/rtp/gstrtcpbuffer.h>
29 #include <gst/glib-compat-private.h>
31 #include "gstrtpbin-marshal.h"
32 #include "rtpsession.h"
34 GST_DEBUG_CATEGORY_STATIC (rtp_session_debug);
35 #define GST_CAT_DEFAULT rtp_session_debug
37 /* signals and args */
40 SIGNAL_GET_SOURCE_BY_SSRC,
42 SIGNAL_ON_SSRC_COLLISION,
43 SIGNAL_ON_SSRC_VALIDATED,
44 SIGNAL_ON_SSRC_ACTIVE,
47 SIGNAL_ON_BYE_TIMEOUT,
49 SIGNAL_ON_SENDER_TIMEOUT,
50 SIGNAL_ON_SENDING_RTCP,
51 SIGNAL_ON_FEEDBACK_RTCP,
56 #define DEFAULT_INTERNAL_SOURCE NULL
57 #define DEFAULT_BANDWIDTH RTP_STATS_BANDWIDTH
58 #define DEFAULT_RTCP_FRACTION (RTP_STATS_RTCP_FRACTION * RTP_STATS_BANDWIDTH)
59 #define DEFAULT_RTCP_RR_BANDWIDTH -1
60 #define DEFAULT_RTCP_RS_BANDWIDTH -1
61 #define DEFAULT_RTCP_MTU 1400
62 #define DEFAULT_SDES NULL
63 #define DEFAULT_NUM_SOURCES 0
64 #define DEFAULT_NUM_ACTIVE_SOURCES 0
65 #define DEFAULT_SOURCES NULL
66 #define DEFAULT_RTCP_MIN_INTERVAL (RTP_STATS_MIN_INTERVAL * GST_SECOND)
67 #define DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW (2 * GST_SECOND)
68 #define DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD (3)
69 #define DEFAULT_PROBATION RTP_DEFAULT_PROBATION
78 PROP_RTCP_RR_BANDWIDTH,
79 PROP_RTCP_RS_BANDWIDTH,
83 PROP_NUM_ACTIVE_SOURCES,
86 PROP_RTCP_MIN_INTERVAL,
87 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
88 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
93 /* update average packet size */
94 #define INIT_AVG(avg, val) \
96 #define UPDATE_AVG(avg, val) \
100 (avg) = ((val) + (15 * (avg))) >> 4;
103 /* The number RTCP intervals after which to timeout entries in the
106 #define RTCP_INTERVAL_COLLISION_TIMEOUT 10
108 /* GObject vmethods */
109 static void rtp_session_finalize (GObject * object);
110 static void rtp_session_set_property (GObject * object, guint prop_id,
111 const GValue * value, GParamSpec * pspec);
112 static void rtp_session_get_property (GObject * object, guint prop_id,
113 GValue * value, GParamSpec * pspec);
115 static gboolean rtp_session_on_sending_rtcp (RTPSession * sess,
116 GstBuffer * buffer, gboolean early);
117 static void rtp_session_send_rtcp (RTPSession * sess,
118 GstClockTimeDiff max_delay);
121 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
123 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
125 static guint32 rtp_session_create_new_ssrc (RTPSession * sess);
126 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
127 gboolean * created, RTPArrivalStats * arrival, gboolean rtp);
128 static RTPSource *obtain_internal_source (RTPSession * sess,
129 guint32 ssrc, gboolean * created);
130 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
131 GstClockTime current_time);
132 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
133 gboolean deterministic, gboolean first);
136 accumulate_trues (GSignalInvocationHint * ihint, GValue * return_accu,
137 const GValue * handler_return, gpointer data)
139 if (g_value_get_boolean (handler_return))
140 g_value_set_boolean (return_accu, TRUE);
146 rtp_session_class_init (RTPSessionClass * klass)
148 GObjectClass *gobject_class;
150 gobject_class = (GObjectClass *) klass;
152 gobject_class->finalize = rtp_session_finalize;
153 gobject_class->set_property = rtp_session_set_property;
154 gobject_class->get_property = rtp_session_get_property;
157 * RTPSession::get-source-by-ssrc:
158 * @session: the object which received the signal
159 * @ssrc: the SSRC of the RTPSource
161 * Request the #RTPSource object with SSRC @ssrc in @session.
163 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
164 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
165 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
166 get_source_by_ssrc), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
167 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
170 * RTPSession::on-new-ssrc:
171 * @session: the object which received the signal
172 * @src: the new RTPSource
174 * Notify of a new SSRC that entered @session.
176 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
177 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
178 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
179 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
182 * RTPSession::on-ssrc-collision:
183 * @session: the object which received the signal
184 * @src: the #RTPSource that caused a collision
186 * Notify when we have an SSRC collision
188 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
189 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
190 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
191 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
194 * RTPSession::on-ssrc-validated:
195 * @session: the object which received the signal
196 * @src: the new validated RTPSource
198 * Notify of a new SSRC that became validated.
200 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
201 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
202 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
203 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
206 * RTPSession::on-ssrc-active:
207 * @session: the object which received the signal
208 * @src: the active RTPSource
210 * Notify of a SSRC that is active, i.e., sending RTCP.
212 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
213 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
214 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
215 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
218 * RTPSession::on-ssrc-sdes:
219 * @session: the object which received the signal
220 * @src: the RTPSource
222 * Notify that a new SDES was received for SSRC.
224 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
225 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
226 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
227 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
230 * RTPSession::on-bye-ssrc:
231 * @session: the object which received the signal
232 * @src: the RTPSource that went away
234 * Notify of an SSRC that became inactive because of a BYE packet.
236 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
237 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
238 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
239 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
242 * RTPSession::on-bye-timeout:
243 * @session: the object which received the signal
244 * @src: the RTPSource that timed out
246 * Notify of an SSRC that has timed out because of BYE
248 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
249 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
250 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
251 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
254 * RTPSession::on-timeout:
255 * @session: the object which received the signal
256 * @src: the RTPSource that timed out
258 * Notify of an SSRC that has timed out
260 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
261 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
262 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
263 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
266 * RTPSession::on-sender-timeout:
267 * @session: the object which received the signal
268 * @src: the RTPSource that timed out
270 * Notify of an SSRC that was a sender but timed out and became a receiver.
272 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
273 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
274 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
275 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
279 * RTPSession::on-sending-rtcp
280 * @session: the object which received the signal
281 * @buffer: the #GstBuffer containing the RTCP packet about to be sent
282 * @early: %TRUE if the packet is early, %FALSE if it is regular
284 * This signal is emitted before sending an RTCP packet, it can be used
285 * to add extra RTCP Packets.
287 * Returns: %TRUE if the RTCP buffer should NOT be suppressed, %FALSE
288 * if suppressing it is acceptable
290 rtp_session_signals[SIGNAL_ON_SENDING_RTCP] =
291 g_signal_new ("on-sending-rtcp", G_TYPE_FROM_CLASS (klass),
292 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_rtcp),
293 accumulate_trues, NULL, gst_rtp_bin_marshal_BOOLEAN__BOXED_BOOLEAN,
294 G_TYPE_BOOLEAN, 2, GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE,
298 * RTPSession::on-feedback-rtcp:
299 * @session: the object which received the signal
300 * @type: Type of RTCP packet, will be %GST_RTCP_TYPE_RTPFB or
301 * %GST_RTCP_TYPE_RTPFB
302 * @fbtype: The type of RTCP FB packet, probably part of #GstRTCPFBType
303 * @sender_ssrc: The SSRC of the sender
304 * @media_ssrc: The SSRC of the media this refers to
305 * @fci: a #GstBuffer with the FCI data from the FB packet or %NULL if
308 * Notify that a RTCP feedback packet has been received
310 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP] =
311 g_signal_new ("on-feedback-rtcp", G_TYPE_FROM_CLASS (klass),
312 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_feedback_rtcp),
313 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT_UINT_UINT_BOXED,
314 G_TYPE_NONE, 5, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT,
318 * RTPSession::send-rtcp:
319 * @session: the object which received the signal
320 * @max_delay: The maximum delay after which the feedback will not be useful
323 * Requests that the #RTPSession initiate a new RTCP packet as soon as
324 * possible within the requested delay.
326 rtp_session_signals[SIGNAL_SEND_RTCP] =
327 g_signal_new ("send-rtcp", G_TYPE_FROM_CLASS (klass),
328 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
329 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
330 gst_rtp_bin_marshal_VOID__UINT64, G_TYPE_NONE, 1, G_TYPE_UINT64);
332 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
333 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
334 "The internal SSRC used for the session (deprecated)",
335 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
337 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
338 g_param_spec_object ("internal-source", "Internal Source",
339 "The internal source element of the session (deprecated)",
340 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
342 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
343 g_param_spec_double ("bandwidth", "Bandwidth",
344 "The bandwidth of the session (0 for auto-discover)",
345 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
346 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
348 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
349 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
350 "The fraction of the bandwidth used for RTCP (or as a real fraction of the RTP bandwidth if < 1)",
351 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
352 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
354 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
355 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
356 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
357 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
358 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
360 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
361 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
362 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
363 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
364 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
366 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
367 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
368 "The maximum size of the RTCP packets",
369 16, G_MAXINT16, DEFAULT_RTCP_MTU,
370 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
372 g_object_class_install_property (gobject_class, PROP_SDES,
373 g_param_spec_boxed ("sdes", "SDES",
374 "The SDES items of this session",
375 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
377 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
378 g_param_spec_uint ("num-sources", "Num Sources",
379 "The number of sources in the session", 0, G_MAXUINT,
380 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
382 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
383 g_param_spec_uint ("num-active-sources", "Num Active Sources",
384 "The number of active sources in the session", 0, G_MAXUINT,
385 DEFAULT_NUM_ACTIVE_SOURCES,
386 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
390 * Get a GValue Array of all sources in the session.
393 * <title>Getting the #RTPSources of a session
400 * g_object_get (sess, "sources", &arr, NULL);
402 * for (i = 0; i < arr->n_values; i++) {
405 * val = g_value_array_get_nth (arr, i);
406 * source = g_value_get_object (val);
408 * g_value_array_free (arr);
413 g_object_class_install_property (gobject_class, PROP_SOURCES,
414 g_param_spec_boxed ("sources", "Sources",
415 "An array of all known sources in the session",
416 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
418 g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
419 g_param_spec_boolean ("favor-new", "Favor new sources",
420 "Resolve SSRC conflict in favor of new sources", FALSE,
421 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
423 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
424 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
425 "Minimum interval between Regular RTCP packet (in ns)",
426 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
427 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
429 g_object_class_install_property (gobject_class,
430 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
431 g_param_spec_uint64 ("rtcp-feedback-retention-window",
432 "RTCP Feedback retention window",
433 "Duration during which RTCP Feedback packets are retained (in ns)",
434 0, G_MAXUINT64, DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW,
435 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
437 g_object_class_install_property (gobject_class,
438 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
439 g_param_spec_uint ("rtcp-immediate-feedback-threshold",
440 "RTCP Immediate Feedback threshold",
441 "The maximum number of members of a RTP session for which immediate"
443 0, G_MAXUINT, DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
444 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
446 g_object_class_install_property (gobject_class, PROP_PROBATION,
447 g_param_spec_uint ("probation", "Number of probations",
448 "Consecutive packet sequence numbers to accept the source",
449 0, G_MAXUINT, DEFAULT_PROBATION,
450 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
452 klass->get_source_by_ssrc =
453 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
454 klass->on_sending_rtcp = GST_DEBUG_FUNCPTR (rtp_session_on_sending_rtcp);
455 klass->send_rtcp = GST_DEBUG_FUNCPTR (rtp_session_send_rtcp);
457 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
461 rtp_session_init (RTPSession * sess)
466 g_mutex_init (&sess->lock);
467 sess->key = g_random_int ();
471 for (i = 0; i < 32; i++) {
473 g_hash_table_new_full (NULL, NULL, NULL,
474 (GDestroyNotify) g_object_unref);
477 rtp_stats_init_defaults (&sess->stats);
478 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
479 rtp_stats_set_min_interval (&sess->stats,
480 (gdouble) DEFAULT_RTCP_MIN_INTERVAL / GST_SECOND);
482 sess->recalc_bandwidth = TRUE;
483 sess->bandwidth = DEFAULT_BANDWIDTH;
484 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
485 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
486 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
488 /* default UDP header length */
489 sess->header_len = 28;
490 sess->mtu = DEFAULT_RTCP_MTU;
492 sess->probation = DEFAULT_PROBATION;
494 /* some default SDES entries */
495 sess->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
497 /* we do not want to leak details like the username or hostname here */
498 str = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
499 gst_structure_set (sess->sdes, "cname", G_TYPE_STRING, str, NULL);
503 /* we do not want to leak the user's real name here */
504 str = g_strdup_printf ("Anon%u", g_random_int ());
505 gst_structure_set (sdes, "name", G_TYPE_STRING, str, NULL);
509 gst_structure_set (sess->sdes, "tool", G_TYPE_STRING, "GStreamer", NULL);
511 /* this is the SSRC we suggest */
512 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
514 sess->first_rtcp = TRUE;
515 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
517 sess->allow_early = TRUE;
518 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
519 sess->rtcp_feedback_retention_window = DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW;
520 sess->rtcp_immediate_feedback_threshold =
521 DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD;
523 sess->last_keyframe_request = GST_CLOCK_TIME_NONE;
527 rtp_session_finalize (GObject * object)
532 sess = RTP_SESSION_CAST (object);
534 gst_structure_free (sess->sdes);
536 for (i = 0; i < 32; i++)
537 g_hash_table_destroy (sess->ssrcs[i]);
539 g_mutex_clear (&sess->lock);
541 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
545 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
547 GValue value = { 0 };
549 g_value_init (&value, RTP_TYPE_SOURCE);
550 g_value_take_object (&value, source);
551 /* copies the value */
552 g_value_array_append (arr, &value);
556 rtp_session_create_sources (RTPSession * sess)
561 RTP_SESSION_LOCK (sess);
562 /* get number of elements in the table */
563 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
564 /* create the result value array */
565 res = g_value_array_new (size);
567 /* and copy all values into the array */
568 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
569 RTP_SESSION_UNLOCK (sess);
575 rtp_session_set_property (GObject * object, guint prop_id,
576 const GValue * value, GParamSpec * pspec)
580 sess = RTP_SESSION (object);
583 case PROP_INTERNAL_SSRC:
586 RTP_SESSION_LOCK (sess);
587 sess->bandwidth = g_value_get_double (value);
588 sess->recalc_bandwidth = TRUE;
589 RTP_SESSION_UNLOCK (sess);
591 case PROP_RTCP_FRACTION:
592 RTP_SESSION_LOCK (sess);
593 sess->rtcp_bandwidth = g_value_get_double (value);
594 sess->recalc_bandwidth = TRUE;
595 RTP_SESSION_UNLOCK (sess);
597 case PROP_RTCP_RR_BANDWIDTH:
598 RTP_SESSION_LOCK (sess);
599 sess->rtcp_rr_bandwidth = g_value_get_int (value);
600 sess->recalc_bandwidth = TRUE;
601 RTP_SESSION_UNLOCK (sess);
603 case PROP_RTCP_RS_BANDWIDTH:
604 RTP_SESSION_LOCK (sess);
605 sess->rtcp_rs_bandwidth = g_value_get_int (value);
606 sess->recalc_bandwidth = TRUE;
607 RTP_SESSION_UNLOCK (sess);
610 sess->mtu = g_value_get_uint (value);
613 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
616 sess->favor_new = g_value_get_boolean (value);
618 case PROP_RTCP_MIN_INTERVAL:
619 rtp_stats_set_min_interval (&sess->stats,
620 (gdouble) g_value_get_uint64 (value) / GST_SECOND);
621 /* trigger reconsideration */
622 RTP_SESSION_LOCK (sess);
623 sess->next_rtcp_check_time = 0;
624 RTP_SESSION_UNLOCK (sess);
625 if (sess->callbacks.reconsider)
626 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
628 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
629 sess->rtcp_immediate_feedback_threshold = g_value_get_uint (value);
632 sess->probation = g_value_get_uint (value);
635 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
641 rtp_session_get_property (GObject * object, guint prop_id,
642 GValue * value, GParamSpec * pspec)
646 sess = RTP_SESSION (object);
649 case PROP_INTERNAL_SSRC:
650 g_value_set_uint (value, rtp_session_suggest_ssrc (sess));
652 case PROP_INTERNAL_SOURCE:
653 /* FIXME, return a random source */
654 g_value_set_object (value, NULL);
657 g_value_set_double (value, sess->bandwidth);
659 case PROP_RTCP_FRACTION:
660 g_value_set_double (value, sess->rtcp_bandwidth);
662 case PROP_RTCP_RR_BANDWIDTH:
663 g_value_set_int (value, sess->rtcp_rr_bandwidth);
665 case PROP_RTCP_RS_BANDWIDTH:
666 g_value_set_int (value, sess->rtcp_rs_bandwidth);
669 g_value_set_uint (value, sess->mtu);
672 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
674 case PROP_NUM_SOURCES:
675 g_value_set_uint (value, rtp_session_get_num_sources (sess));
677 case PROP_NUM_ACTIVE_SOURCES:
678 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
681 g_value_take_boxed (value, rtp_session_create_sources (sess));
684 g_value_set_boolean (value, sess->favor_new);
686 case PROP_RTCP_MIN_INTERVAL:
687 g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
689 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
690 g_value_set_uint (value, sess->rtcp_immediate_feedback_threshold);
693 g_value_set_uint (value, sess->probation);
696 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
702 on_new_ssrc (RTPSession * sess, RTPSource * source)
704 g_object_ref (source);
705 RTP_SESSION_UNLOCK (sess);
706 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
707 RTP_SESSION_LOCK (sess);
708 g_object_unref (source);
712 on_ssrc_collision (RTPSession * sess, RTPSource * source)
714 g_object_ref (source);
715 RTP_SESSION_UNLOCK (sess);
716 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
718 RTP_SESSION_LOCK (sess);
719 g_object_unref (source);
723 on_ssrc_validated (RTPSession * sess, RTPSource * source)
725 g_object_ref (source);
726 RTP_SESSION_UNLOCK (sess);
727 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
729 RTP_SESSION_LOCK (sess);
730 g_object_unref (source);
734 on_ssrc_active (RTPSession * sess, RTPSource * source)
736 g_object_ref (source);
737 RTP_SESSION_UNLOCK (sess);
738 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
739 RTP_SESSION_LOCK (sess);
740 g_object_unref (source);
744 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
746 g_object_ref (source);
747 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
748 RTP_SESSION_UNLOCK (sess);
749 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
750 RTP_SESSION_LOCK (sess);
751 g_object_unref (source);
755 on_bye_ssrc (RTPSession * sess, RTPSource * source)
757 g_object_ref (source);
758 RTP_SESSION_UNLOCK (sess);
759 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
760 RTP_SESSION_LOCK (sess);
761 g_object_unref (source);
765 on_bye_timeout (RTPSession * sess, RTPSource * source)
767 g_object_ref (source);
768 RTP_SESSION_UNLOCK (sess);
769 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
770 RTP_SESSION_LOCK (sess);
771 g_object_unref (source);
775 on_timeout (RTPSession * sess, RTPSource * source)
777 g_object_ref (source);
778 RTP_SESSION_UNLOCK (sess);
779 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
780 RTP_SESSION_LOCK (sess);
781 g_object_unref (source);
785 on_sender_timeout (RTPSession * sess, RTPSource * source)
787 g_object_ref (source);
788 RTP_SESSION_UNLOCK (sess);
789 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
791 RTP_SESSION_LOCK (sess);
792 g_object_unref (source);
798 * Create a new session object.
800 * Returns: a new #RTPSession. g_object_unref() after usage.
803 rtp_session_new (void)
807 sess = g_object_new (RTP_TYPE_SESSION, NULL);
813 * rtp_session_set_callbacks:
814 * @sess: an #RTPSession
815 * @callbacks: callbacks to configure
816 * @user_data: user data passed in the callbacks
818 * Configure a set of callbacks to be notified of actions.
821 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
824 g_return_if_fail (RTP_IS_SESSION (sess));
826 if (callbacks->process_rtp) {
827 sess->callbacks.process_rtp = callbacks->process_rtp;
828 sess->process_rtp_user_data = user_data;
830 if (callbacks->send_rtp) {
831 sess->callbacks.send_rtp = callbacks->send_rtp;
832 sess->send_rtp_user_data = user_data;
834 if (callbacks->send_rtcp) {
835 sess->callbacks.send_rtcp = callbacks->send_rtcp;
836 sess->send_rtcp_user_data = user_data;
838 if (callbacks->sync_rtcp) {
839 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
840 sess->sync_rtcp_user_data = user_data;
842 if (callbacks->clock_rate) {
843 sess->callbacks.clock_rate = callbacks->clock_rate;
844 sess->clock_rate_user_data = user_data;
846 if (callbacks->reconsider) {
847 sess->callbacks.reconsider = callbacks->reconsider;
848 sess->reconsider_user_data = user_data;
850 if (callbacks->request_key_unit) {
851 sess->callbacks.request_key_unit = callbacks->request_key_unit;
852 sess->request_key_unit_user_data = user_data;
854 if (callbacks->request_time) {
855 sess->callbacks.request_time = callbacks->request_time;
856 sess->request_time_user_data = user_data;
861 * rtp_session_set_process_rtp_callback:
862 * @sess: an #RTPSession
863 * @callback: callback to set
864 * @user_data: user data passed in the callback
866 * Configure only the process_rtp callback to be notified of the process_rtp action.
869 rtp_session_set_process_rtp_callback (RTPSession * sess,
870 RTPSessionProcessRTP callback, gpointer user_data)
872 g_return_if_fail (RTP_IS_SESSION (sess));
874 sess->callbacks.process_rtp = callback;
875 sess->process_rtp_user_data = user_data;
879 * rtp_session_set_send_rtp_callback:
880 * @sess: an #RTPSession
881 * @callback: callback to set
882 * @user_data: user data passed in the callback
884 * Configure only the send_rtp callback to be notified of the send_rtp action.
887 rtp_session_set_send_rtp_callback (RTPSession * sess,
888 RTPSessionSendRTP callback, gpointer user_data)
890 g_return_if_fail (RTP_IS_SESSION (sess));
892 sess->callbacks.send_rtp = callback;
893 sess->send_rtp_user_data = user_data;
897 * rtp_session_set_send_rtcp_callback:
898 * @sess: an #RTPSession
899 * @callback: callback to set
900 * @user_data: user data passed in the callback
902 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
905 rtp_session_set_send_rtcp_callback (RTPSession * sess,
906 RTPSessionSendRTCP callback, gpointer user_data)
908 g_return_if_fail (RTP_IS_SESSION (sess));
910 sess->callbacks.send_rtcp = callback;
911 sess->send_rtcp_user_data = user_data;
915 * rtp_session_set_sync_rtcp_callback:
916 * @sess: an #RTPSession
917 * @callback: callback to set
918 * @user_data: user data passed in the callback
920 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
923 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
924 RTPSessionSyncRTCP callback, gpointer user_data)
926 g_return_if_fail (RTP_IS_SESSION (sess));
928 sess->callbacks.sync_rtcp = callback;
929 sess->sync_rtcp_user_data = user_data;
933 * rtp_session_set_clock_rate_callback:
934 * @sess: an #RTPSession
935 * @callback: callback to set
936 * @user_data: user data passed in the callback
938 * Configure only the clock_rate callback to be notified of the clock_rate action.
941 rtp_session_set_clock_rate_callback (RTPSession * sess,
942 RTPSessionClockRate callback, gpointer user_data)
944 g_return_if_fail (RTP_IS_SESSION (sess));
946 sess->callbacks.clock_rate = callback;
947 sess->clock_rate_user_data = user_data;
951 * rtp_session_set_reconsider_callback:
952 * @sess: an #RTPSession
953 * @callback: callback to set
954 * @user_data: user data passed in the callback
956 * Configure only the reconsider callback to be notified of the reconsider action.
959 rtp_session_set_reconsider_callback (RTPSession * sess,
960 RTPSessionReconsider callback, gpointer user_data)
962 g_return_if_fail (RTP_IS_SESSION (sess));
964 sess->callbacks.reconsider = callback;
965 sess->reconsider_user_data = user_data;
969 * rtp_session_set_request_time_callback:
970 * @sess: an #RTPSession
971 * @callback: callback to set
972 * @user_data: user data passed in the callback
974 * Configure only the request_time callback
977 rtp_session_set_request_time_callback (RTPSession * sess,
978 RTPSessionRequestTime callback, gpointer user_data)
980 g_return_if_fail (RTP_IS_SESSION (sess));
982 sess->callbacks.request_time = callback;
983 sess->request_time_user_data = user_data;
987 * rtp_session_set_bandwidth:
988 * @sess: an #RTPSession
989 * @bandwidth: the bandwidth allocated
991 * Set the session bandwidth in bytes per second.
994 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
996 g_return_if_fail (RTP_IS_SESSION (sess));
998 RTP_SESSION_LOCK (sess);
999 sess->stats.bandwidth = bandwidth;
1000 RTP_SESSION_UNLOCK (sess);
1004 * rtp_session_get_bandwidth:
1005 * @sess: an #RTPSession
1007 * Get the session bandwidth.
1009 * Returns: the session bandwidth.
1012 rtp_session_get_bandwidth (RTPSession * sess)
1016 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1018 RTP_SESSION_LOCK (sess);
1019 result = sess->stats.bandwidth;
1020 RTP_SESSION_UNLOCK (sess);
1026 * rtp_session_set_rtcp_fraction:
1027 * @sess: an #RTPSession
1028 * @bandwidth: the RTCP bandwidth
1030 * Set the bandwidth in bytes per second that should be used for RTCP
1034 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
1036 g_return_if_fail (RTP_IS_SESSION (sess));
1038 RTP_SESSION_LOCK (sess);
1039 sess->stats.rtcp_bandwidth = bandwidth;
1040 RTP_SESSION_UNLOCK (sess);
1044 * rtp_session_get_rtcp_fraction:
1045 * @sess: an #RTPSession
1047 * Get the session bandwidth used for RTCP.
1049 * Returns: The bandwidth used for RTCP messages.
1052 rtp_session_get_rtcp_fraction (RTPSession * sess)
1056 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
1058 RTP_SESSION_LOCK (sess);
1059 result = sess->stats.rtcp_bandwidth;
1060 RTP_SESSION_UNLOCK (sess);
1066 * rtp_session_get_sdes_struct:
1067 * @sess: an #RTSPSession
1069 * Get the SDES data as a #GstStructure
1071 * Returns: a GstStructure with SDES items for @sess. This function returns a
1072 * copy of the SDES structure, use gst_structure_free() after usage.
1075 rtp_session_get_sdes_struct (RTPSession * sess)
1077 GstStructure *result = NULL;
1079 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1081 RTP_SESSION_LOCK (sess);
1083 result = gst_structure_copy (sess->sdes);
1084 RTP_SESSION_UNLOCK (sess);
1090 * rtp_session_set_sdes_struct:
1091 * @sess: an #RTSPSession
1092 * @sdes: a #GstStructure
1094 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1097 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
1099 g_return_if_fail (sdes);
1100 g_return_if_fail (RTP_IS_SESSION (sess));
1102 RTP_SESSION_LOCK (sess);
1104 gst_structure_free (sess->sdes);
1105 sess->sdes = gst_structure_copy (sdes);
1106 RTP_SESSION_UNLOCK (sess);
1109 static GstFlowReturn
1110 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1112 GstFlowReturn result = GST_FLOW_OK;
1114 if (source->internal) {
1115 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1117 RTP_SESSION_UNLOCK (session);
1119 if (session->callbacks.send_rtp)
1121 session->callbacks.send_rtp (session, source, data,
1122 session->send_rtp_user_data);
1124 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1127 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1128 RTP_SESSION_UNLOCK (session);
1130 if (session->callbacks.process_rtp)
1132 session->callbacks.process_rtp (session, source,
1133 GST_BUFFER_CAST (data), session->process_rtp_user_data);
1135 gst_buffer_unref (GST_BUFFER_CAST (data));
1137 RTP_SESSION_LOCK (session);
1143 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1147 RTP_SESSION_UNLOCK (session);
1149 if (session->callbacks.clock_rate)
1151 session->callbacks.clock_rate (session, pt,
1152 session->clock_rate_user_data);
1156 RTP_SESSION_LOCK (session);
1158 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1163 static RTPSourceCallbacks callbacks = {
1164 (RTPSourcePushRTP) source_push_rtp,
1165 (RTPSourceClockRate) source_clock_rate,
1169 check_collision (RTPSession * sess, RTPSource * source,
1170 RTPArrivalStats * arrival, gboolean rtp)
1174 /* If we have no arrival address, we can't do collision checking */
1175 if (!arrival->address)
1178 ssrc = rtp_source_get_ssrc (source);
1180 if (!source->internal) {
1181 GSocketAddress *from;
1183 /* This is not our local source, but lets check if two remote
1186 from = source->rtp_from;
1188 from = source->rtcp_from;
1192 if (__g_socket_address_equal (from, arrival->address)) {
1193 /* Address is the same */
1196 GST_LOG ("we have a third-party collision or loop ssrc:%x", ssrc);
1197 if (sess->favor_new) {
1198 if (rtp_source_find_conflicting_address (source,
1199 arrival->address, arrival->current_time)) {
1202 buf1 = __g_socket_address_to_string (arrival->address);
1203 GST_LOG ("Known conflict on %x for %s, dropping packet", ssrc,
1211 /* Current address is not a known conflict, lets assume this is
1212 * a new source. Save old address in possible conflict list
1214 rtp_source_add_conflicting_address (source, from,
1215 arrival->current_time);
1217 buf1 = __g_socket_address_to_string (from);
1218 buf2 = __g_socket_address_to_string (arrival->address);
1220 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1221 " saving old as known conflict", ssrc, buf1, buf2);
1224 rtp_source_set_rtp_from (source, arrival->address);
1226 rtp_source_set_rtcp_from (source, arrival->address);
1234 /* Don't need to save old addresses, we ignore new sources */
1239 /* We don't already have a from address for RTP, just set it */
1241 rtp_source_set_rtp_from (source, arrival->address);
1243 rtp_source_set_rtcp_from (source, arrival->address);
1247 /* FIXME: Log 3rd party collision somehow
1248 * Maybe should be done in upper layer, only the SDES can tell us
1249 * if its a collision or a loop
1252 /* This is sending with our ssrc, is it an address we already know */
1253 if (rtp_source_find_conflicting_address (source, arrival->address,
1254 arrival->current_time)) {
1255 /* Its a known conflict, its probably a loop, not a collision
1256 * lets just drop the incoming packet
1258 GST_DEBUG ("Our packets are being looped back to us, dropping");
1260 /* Its a new collision, lets change our SSRC */
1261 rtp_source_add_conflicting_address (source, arrival->address,
1262 arrival->current_time);
1264 GST_DEBUG ("Collision for SSRC %x", ssrc);
1265 /* mark the source BYE */
1266 rtp_source_mark_bye (source, "SSRC Collision");
1267 /* if we were suggesting this SSRC, change to something else */
1268 if (sess->suggested_ssrc == ssrc)
1269 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
1271 on_ssrc_collision (sess, source);
1273 rtp_session_schedule_bye_locked (sess, arrival->current_time);
1281 find_source (RTPSession * sess, guint32 ssrc)
1283 return g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1284 GINT_TO_POINTER (ssrc));
1288 add_source (RTPSession * sess, RTPSource * src)
1290 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1291 GINT_TO_POINTER (src->ssrc), src);
1292 /* report the new source ASAP */
1293 src->generation = sess->generation;
1294 /* we have one more source now */
1295 sess->total_sources++;
1296 if (RTP_SOURCE_IS_ACTIVE (src))
1297 sess->stats.active_sources++;
1298 if (src->internal) {
1299 sess->stats.internal_sources++;
1300 if (sess->suggested_ssrc != src->ssrc)
1301 sess->suggested_ssrc = src->ssrc;
1305 /* must be called with the session lock, the returned source needs to be
1306 * unreffed after usage. */
1308 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1309 RTPArrivalStats * arrival, gboolean rtp)
1313 source = find_source (sess, ssrc);
1314 if (source == NULL) {
1315 /* make new Source in probation and insert */
1316 source = rtp_source_new (ssrc);
1318 GST_DEBUG ("creating new source %08x %p", ssrc, source);
1320 /* for RTP packets we need to set the source in probation. Receiving RTCP
1321 * packets of an SSRC, on the other hand, is a strong indication that we
1322 * are dealing with a valid source. */
1324 g_object_set (source, "probation", sess->probation, NULL);
1326 g_object_set (source, "probation", 0, NULL);
1328 /* store from address, if any */
1329 if (arrival->address) {
1331 rtp_source_set_rtp_from (source, arrival->address);
1333 rtp_source_set_rtcp_from (source, arrival->address);
1336 /* configure a callback on the source */
1337 rtp_source_set_callbacks (source, &callbacks, sess);
1339 add_source (sess, source);
1343 /* check for collision, this updates the address when not previously set */
1344 if (check_collision (sess, source, arrival, rtp)) {
1347 /* Receiving RTCP packets of an SSRC is a strong indication that we
1348 * are dealing with a valid source. */
1350 g_object_set (source, "probation", 0, NULL);
1352 /* update last activity */
1353 source->last_activity = arrival->current_time;
1355 source->last_rtp_activity = arrival->current_time;
1356 g_object_ref (source);
1361 /* must be called with the session lock, the returned source needs to be
1362 * unreffed after usage. */
1364 obtain_internal_source (RTPSession * sess, guint32 ssrc, gboolean * created)
1368 source = find_source (sess, ssrc);
1369 if (source == NULL) {
1370 /* make new internal Source and insert */
1371 source = rtp_source_new (ssrc);
1373 GST_DEBUG ("creating new internal source %08x %p", ssrc, source);
1375 source->validated = TRUE;
1376 source->internal = TRUE;
1377 rtp_source_set_sdes_struct (source, gst_structure_copy (sess->sdes));
1378 rtp_source_set_callbacks (source, &callbacks, sess);
1380 add_source (sess, source);
1385 g_object_ref (source);
1391 * rtp_session_suggest_ssrc:
1392 * @sess: a #RTPSession
1394 * Suggest an unused SSRC in @sess.
1396 * Returns: a free unused SSRC
1399 rtp_session_suggest_ssrc (RTPSession * sess)
1403 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1405 RTP_SESSION_LOCK (sess);
1406 result = sess->suggested_ssrc;
1407 RTP_SESSION_UNLOCK (sess);
1413 * rtp_session_add_source:
1414 * @sess: a #RTPSession
1415 * @src: #RTPSource to add
1417 * Add @src to @session.
1419 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1420 * existed in the session.
1423 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1425 gboolean result = FALSE;
1428 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1429 g_return_val_if_fail (src != NULL, FALSE);
1431 RTP_SESSION_LOCK (sess);
1432 find = find_source (sess, src->ssrc);
1434 add_source (sess, src);
1437 RTP_SESSION_UNLOCK (sess);
1443 * rtp_session_get_num_sources:
1444 * @sess: an #RTPSession
1446 * Get the number of sources in @sess.
1448 * Returns: The number of sources in @sess.
1451 rtp_session_get_num_sources (RTPSession * sess)
1455 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1457 RTP_SESSION_LOCK (sess);
1458 result = sess->total_sources;
1459 RTP_SESSION_UNLOCK (sess);
1465 * rtp_session_get_num_active_sources:
1466 * @sess: an #RTPSession
1468 * Get the number of active sources in @sess. A source is considered active when
1469 * it has been validated and has not yet received a BYE RTCP message.
1471 * Returns: The number of active sources in @sess.
1474 rtp_session_get_num_active_sources (RTPSession * sess)
1478 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1480 RTP_SESSION_LOCK (sess);
1481 result = sess->stats.active_sources;
1482 RTP_SESSION_UNLOCK (sess);
1488 * rtp_session_get_source_by_ssrc:
1489 * @sess: an #RTPSession
1492 * Find the source with @ssrc in @sess.
1494 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1495 * g_object_unref() after usage.
1498 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1502 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1504 RTP_SESSION_LOCK (sess);
1505 result = find_source (sess, ssrc);
1507 g_object_ref (result);
1508 RTP_SESSION_UNLOCK (sess);
1513 /* should be called with the SESSION lock */
1515 rtp_session_create_new_ssrc (RTPSession * sess)
1520 ssrc = g_random_int ();
1522 /* see if it exists in the session, we're done if it doesn't */
1523 if (find_source (sess, ssrc) == NULL)
1531 * rtp_session_create_source:
1532 * @sess: an #RTPSession
1534 * Create an #RTPSource for use in @sess. This function will create a source
1535 * with an ssrc that is currently not used by any participants in the session.
1537 * Returns: an #RTPSource.
1540 rtp_session_create_source (RTPSession * sess)
1545 RTP_SESSION_LOCK (sess);
1546 ssrc = rtp_session_create_new_ssrc (sess);
1547 source = rtp_source_new (ssrc);
1548 rtp_source_set_callbacks (source, &callbacks, sess);
1549 /* we need an additional ref for the source in the hashtable */
1550 g_object_ref (source);
1551 add_source (sess, source);
1552 RTP_SESSION_UNLOCK (sess);
1557 /* update the RTPArrivalStats structure with the current time and other bits
1558 * about the current buffer we are handling.
1559 * This function is typically called when a validated packet is received.
1560 * This function should be called with the SESSION_LOCK
1563 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
1564 gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
1565 GstClockTime running_time, guint64 ntpnstime)
1567 GstNetAddressMeta *meta;
1568 GstRTPBuffer rtpb = { NULL };
1570 /* get time of arrival */
1571 arrival->current_time = current_time;
1572 arrival->running_time = running_time;
1573 arrival->ntpnstime = ntpnstime;
1575 /* get packet size including header overhead */
1576 arrival->bytes = gst_buffer_get_size (buffer) + sess->header_len;
1579 gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtpb);
1580 arrival->payload_len = gst_rtp_buffer_get_payload_len (&rtpb);
1581 gst_rtp_buffer_unmap (&rtpb);
1583 arrival->payload_len = 0;
1586 /* for netbuffer we can store the IP address to check for collisions */
1587 meta = gst_buffer_get_net_address_meta (buffer);
1588 if (arrival->address)
1589 g_object_unref (arrival->address);
1591 arrival->address = G_SOCKET_ADDRESS (g_object_ref (meta->addr));
1593 arrival->address = NULL;
1598 clean_arrival_stats (RTPArrivalStats * arrival)
1600 if (arrival->address)
1601 g_object_unref (arrival->address);
1605 source_update_active (RTPSession * sess, RTPSource * source,
1606 gboolean prevactive)
1608 gboolean active = RTP_SOURCE_IS_ACTIVE (source);
1609 guint32 ssrc = source->ssrc;
1611 if (prevactive == active)
1615 sess->stats.active_sources++;
1616 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1617 sess->stats.active_sources);
1619 sess->stats.active_sources--;
1620 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
1621 sess->stats.active_sources);
1627 source_update_sender (RTPSession * sess, RTPSource * source,
1628 gboolean prevsender)
1630 gboolean sender = RTP_SOURCE_IS_SENDER (source);
1631 guint32 ssrc = source->ssrc;
1633 if (prevsender == sender)
1637 sess->stats.sender_sources++;
1638 if (source->internal)
1639 sess->stats.internal_sender_sources++;
1640 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1641 sess->stats.sender_sources);
1643 sess->stats.sender_sources--;
1644 if (source->internal)
1645 sess->stats.internal_sender_sources--;
1646 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
1647 sess->stats.sender_sources);
1653 * rtp_session_process_rtp:
1654 * @sess: and #RTPSession
1655 * @buffer: an RTP buffer
1656 * @current_time: the current system time
1657 * @running_time: the running_time of @buffer
1659 * Process an RTP buffer in the session manager. This function takes ownership
1662 * Returns: a #GstFlowReturn.
1665 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1666 GstClockTime current_time, GstClockTime running_time)
1668 GstFlowReturn result;
1672 gboolean prevsender, prevactive;
1673 RTPArrivalStats arrival = { NULL, };
1677 GstRTPBuffer rtp = { NULL };
1679 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1680 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1682 if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp))
1683 goto invalid_packet;
1685 /* get SSRC to look up in session database */
1686 ssrc = gst_rtp_buffer_get_ssrc (&rtp);
1687 /* copy available csrc for later */
1688 count = gst_rtp_buffer_get_csrc_count (&rtp);
1689 /* make sure to not overflow our array. An RTP buffer can maximally contain
1691 count = MIN (count, 16);
1693 for (i = 0; i < count; i++)
1694 csrcs[i] = gst_rtp_buffer_get_csrc (&rtp, i);
1696 gst_rtp_buffer_unmap (&rtp);
1698 RTP_SESSION_LOCK (sess);
1700 /* FIXME, we should simply not update any stats on the BYE
1701 * internal sources */
1702 /* ignore more RTP packets when we left the session */
1703 if (sess->source->marked_bye)
1707 /* update arrival stats */
1708 update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1711 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1715 prevsender = RTP_SOURCE_IS_SENDER (source);
1716 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1717 oldrate = source->bitrate;
1719 /* let source process the packet */
1720 result = rtp_source_process_rtp (source, buffer, &arrival);
1722 /* source became active */
1723 if (source_update_active (sess, source, prevactive))
1724 on_ssrc_validated (sess, source);
1726 source_update_sender (sess, source, prevsender);
1728 if (oldrate != source->bitrate)
1729 sess->recalc_bandwidth = TRUE;
1732 on_new_ssrc (sess, source);
1734 if (source->validated) {
1737 /* for validated sources, we add the CSRCs as well */
1738 for (i = 0; i < count; i++) {
1740 RTPSource *csrc_src;
1745 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1750 GST_DEBUG ("created new CSRC: %08x", csrc);
1751 rtp_source_set_as_csrc (csrc_src);
1752 if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1753 sess->stats.active_sources++;
1754 on_new_ssrc (sess, csrc_src);
1756 g_object_unref (csrc_src);
1759 g_object_unref (source);
1761 RTP_SESSION_UNLOCK (sess);
1763 clean_arrival_stats (&arrival);
1770 gst_buffer_unref (buffer);
1771 GST_DEBUG ("invalid RTP packet received");
1777 RTP_SESSION_UNLOCK (sess);
1778 gst_buffer_unref (buffer);
1779 GST_DEBUG ("ignoring RTP packet because we are leaving");
1785 RTP_SESSION_UNLOCK (sess);
1786 gst_buffer_unref (buffer);
1787 clean_arrival_stats (&arrival);
1788 GST_DEBUG ("ignoring packet because its collisioning");
1794 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1795 GstRTCPPacket * packet, RTPArrivalStats * arrival)
1799 count = gst_rtcp_packet_get_rb_count (packet);
1800 for (i = 0; i < count; i++) {
1801 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1802 guint8 fractionlost;
1806 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1807 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1809 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1811 /* find our own source */
1812 src = find_source (sess, ssrc);
1816 if (src->internal) {
1817 /* only deal with report blocks for our session, we update the stats of
1818 * the sender of the RTCP message. We could also compare our stats against
1819 * the other sender to see if we are better or worse. */
1820 /* FIXME, need to keep track who the RB block is from */
1821 rtp_source_process_rb (source, arrival->ntpnstime, fractionlost,
1822 packetslost, exthighestseq, jitter, lsr, dlsr);
1825 on_ssrc_active (sess, source);
1828 /* A Sender report contains statistics about how the sender is doing. This
1829 * includes timing informataion such as the relation between RTP and NTP
1830 * timestamps and the number of packets/bytes it sent to us.
1832 * In this report is also included a set of report blocks related to how this
1833 * sender is receiving data (in case we (or somebody else) is also sending stuff
1834 * to it). This info includes the packet loss, jitter and seqnum. It also
1835 * contains information to calculate the round trip time (LSR/DLSR).
1838 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1839 RTPArrivalStats * arrival, gboolean * do_sync)
1841 guint32 senderssrc, rtptime, packet_count, octet_count;
1844 gboolean created, prevsender;
1846 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1847 &packet_count, &octet_count);
1849 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1850 senderssrc, GST_TIME_ARGS (arrival->current_time));
1852 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1856 /* don't try to do lip-sync for sources that sent a BYE */
1857 if (RTP_SOURCE_IS_MARKED_BYE (source))
1862 prevsender = RTP_SOURCE_IS_SENDER (source);
1864 /* first update the source */
1865 rtp_source_process_sr (source, arrival->current_time, ntptime, rtptime,
1866 packet_count, octet_count);
1868 source_update_sender (sess, source, prevsender);
1871 on_new_ssrc (sess, source);
1873 rtp_session_process_rb (sess, source, packet, arrival);
1874 g_object_unref (source);
1877 /* A receiver report contains statistics about how a receiver is doing. It
1878 * includes stuff like packet loss, jitter and the seqnum it received last. It
1879 * also contains info to calculate the round trip time.
1881 * We are only interested in how the sender of this report is doing wrt to us.
1884 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1885 RTPArrivalStats * arrival)
1891 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1893 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1895 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1900 on_new_ssrc (sess, source);
1902 rtp_session_process_rb (sess, source, packet, arrival);
1903 g_object_unref (source);
1906 /* Get SDES items and store them in the SSRC */
1908 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1909 RTPArrivalStats * arrival)
1912 gboolean more_items, more_entries;
1914 items = gst_rtcp_packet_sdes_get_item_count (packet);
1915 GST_DEBUG ("got SDES packet with %d items", items);
1917 more_items = gst_rtcp_packet_sdes_first_item (packet);
1919 while (more_items) {
1921 gboolean changed, created, prevactive;
1925 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1927 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1931 /* find src, no probation when dealing with RTCP */
1932 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1936 sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
1938 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1940 while (more_entries) {
1941 GstRTCPSDESType type;
1947 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1949 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1952 if (type == GST_RTCP_SDES_PRIV) {
1953 name = g_strndup ((const gchar *) &data[1], data[0]);
1955 data += data[0] + 1;
1957 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
1960 value = g_strndup ((const gchar *) data, len);
1962 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
1967 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1971 /* takes ownership of sdes */
1972 changed = rtp_source_set_sdes_struct (source, sdes);
1974 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1975 source->validated = TRUE;
1978 on_new_ssrc (sess, source);
1980 /* source became active */
1981 if (source_update_active (sess, source, prevactive))
1982 on_ssrc_validated (sess, source);
1985 on_ssrc_sdes (sess, source);
1987 g_object_unref (source);
1989 more_items = gst_rtcp_packet_sdes_next_item (packet);
1994 /* BYE is sent when a client leaves the session
1997 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
1998 RTPArrivalStats * arrival)
2002 gboolean reconsider = FALSE;
2004 reason = gst_rtcp_packet_bye_get_reason (packet);
2005 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
2007 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
2008 for (i = 0; i < count; i++) {
2011 gboolean created, prevactive, prevsender;
2012 guint pmembers, members;
2014 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
2015 GST_DEBUG ("SSRC: %08x", ssrc);
2017 /* find src and mark bye, no probation when dealing with RTCP */
2018 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
2022 if (source->internal) {
2023 /* our own source, something weird with this packet */
2024 g_object_unref (source);
2028 /* store time for when we need to time out this source */
2029 source->bye_time = arrival->current_time;
2031 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2032 prevsender = RTP_SOURCE_IS_SENDER (source);
2034 /* mark the source BYE */
2035 rtp_source_mark_bye (source, reason);
2037 pmembers = sess->stats.active_sources;
2039 source_update_active (sess, source, prevactive);
2040 source_update_sender (sess, source, prevsender);
2042 members = sess->stats.active_sources;
2044 if (!sess->scheduled_bye && members < pmembers) {
2045 /* some members went away since the previous timeout estimate.
2046 * Perform reverse reconsideration but only when we are not scheduling a
2048 if (sess->next_rtcp_check_time != GST_CLOCK_TIME_NONE &&
2049 arrival->current_time < sess->next_rtcp_check_time) {
2050 GstClockTime time_remaining;
2052 time_remaining = sess->next_rtcp_check_time - arrival->current_time;
2053 sess->next_rtcp_check_time =
2054 gst_util_uint64_scale (time_remaining, members, pmembers);
2056 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
2057 GST_TIME_ARGS (sess->next_rtcp_check_time));
2059 sess->next_rtcp_check_time += arrival->current_time;
2061 /* mark pending reconsider. We only want to signal the reconsideration
2062 * once after we handled all the source in the bye packet */
2068 on_new_ssrc (sess, source);
2070 on_bye_ssrc (sess, source);
2072 g_object_unref (source);
2075 RTP_SESSION_UNLOCK (sess);
2076 /* notify app of reconsideration */
2077 if (sess->callbacks.reconsider)
2078 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2079 RTP_SESSION_LOCK (sess);
2085 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
2086 RTPArrivalStats * arrival)
2088 GST_DEBUG ("received APP");
2092 rtp_session_request_local_key_unit (RTPSession * sess, RTPSource * src,
2093 gboolean fir, GstClockTime current_time)
2095 guint32 round_trip = 0;
2097 rtp_source_get_last_rb (src, NULL, NULL, NULL, NULL, NULL, NULL, &round_trip);
2099 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE && round_trip) {
2100 GstClockTime round_trip_in_ns = gst_util_uint64_scale (round_trip,
2103 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE &&
2104 current_time - sess->last_keyframe_request < 2 * round_trip_in_ns) {
2105 GST_DEBUG ("Ignoring %s request because one was send without one "
2106 "RTT (%" GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")",
2107 fir ? "FIR" : "PLI",
2108 GST_TIME_ARGS (current_time - sess->last_keyframe_request),
2109 GST_TIME_ARGS (round_trip_in_ns));;
2114 sess->last_keyframe_request = current_time;
2116 GST_LOG ("received %s request from %X %p(%p)", fir ? "FIR" : "PLI",
2117 rtp_source_get_ssrc (src), sess->callbacks.process_rtp,
2118 sess->callbacks.request_key_unit);
2120 RTP_SESSION_UNLOCK (sess);
2121 sess->callbacks.request_key_unit (sess, fir,
2122 sess->request_key_unit_user_data);
2123 RTP_SESSION_LOCK (sess);
2129 rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
2130 guint32 media_ssrc, GstClockTime current_time)
2134 if (!sess->callbacks.request_key_unit)
2137 src = find_source (sess, sender_ssrc);
2141 rtp_session_request_local_key_unit (sess, src, FALSE, current_time);
2145 rtp_session_process_fir (RTPSession * sess, guint32 sender_ssrc,
2146 guint8 * fci_data, guint fci_length, GstClockTime current_time)
2151 gboolean our_request = FALSE;
2153 if (!sess->callbacks.request_key_unit)
2159 src = find_source (sess, sender_ssrc);
2161 /* Hack because Google fails to set the sender_ssrc correctly */
2162 if (!src && sender_ssrc == 1) {
2163 GHashTableIter iter;
2165 /* we can't find the source if there are multiple */
2166 if (sess->stats.sender_sources > sess->stats.internal_sender_sources + 1)
2169 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
2170 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
2171 if (!src->internal && rtp_source_is_sender (src))
2179 for (position = 0; position < fci_length; position += 8) {
2180 guint8 *data = fci_data + position;
2183 ssrc = GST_READ_UINT32_BE (data);
2185 own = find_source (sess, ssrc);
2186 if (own->internal) {
2194 rtp_session_request_local_key_unit (sess, src, TRUE, current_time);
2198 rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
2199 RTPArrivalStats * arrival, GstClockTime current_time)
2201 GstRTCPType type = gst_rtcp_packet_get_type (packet);
2202 GstRTCPFBType fbtype = gst_rtcp_packet_fb_get_type (packet);
2203 guint32 sender_ssrc = gst_rtcp_packet_fb_get_sender_ssrc (packet);
2204 guint32 media_ssrc = gst_rtcp_packet_fb_get_media_ssrc (packet);
2205 guint8 *fci_data = gst_rtcp_packet_fb_get_fci (packet);
2206 guint fci_length = 4 * gst_rtcp_packet_fb_get_fci_length (packet);
2209 GST_DEBUG ("received feedback %d:%d from %08X about %08X with FCI of "
2210 "length %d", type, fbtype, sender_ssrc, media_ssrc, fci_length);
2212 if (g_signal_has_handler_pending (sess,
2213 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0, TRUE)) {
2214 GstBuffer *fci_buffer = NULL;
2216 if (fci_length > 0) {
2217 fci_buffer = gst_buffer_copy_region (packet->rtcp->buffer,
2218 GST_BUFFER_COPY_MEMORY, fci_data - packet->rtcp->map.data,
2220 GST_BUFFER_TIMESTAMP (fci_buffer) = arrival->running_time;
2223 RTP_SESSION_UNLOCK (sess);
2224 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0,
2225 type, fbtype, sender_ssrc, media_ssrc, fci_buffer);
2226 RTP_SESSION_LOCK (sess);
2229 gst_buffer_unref (fci_buffer);
2232 src = find_source (sess, media_ssrc);
2236 if (sess->rtcp_feedback_retention_window) {
2237 rtp_source_retain_rtcp_packet (src, packet, arrival->running_time);
2240 if (src->internal ||
2241 /* PSFB FIR puts the media ssrc inside the FCI */
2242 (type == GST_RTCP_TYPE_PSFB && fbtype == GST_RTCP_PSFB_TYPE_FIR)) {
2244 case GST_RTCP_TYPE_PSFB:
2246 case GST_RTCP_PSFB_TYPE_PLI:
2247 rtp_session_process_pli (sess, sender_ssrc, media_ssrc,
2250 case GST_RTCP_PSFB_TYPE_FIR:
2251 rtp_session_process_fir (sess, sender_ssrc, fci_data, fci_length,
2258 case GST_RTCP_TYPE_RTPFB:
2266 * rtp_session_process_rtcp:
2267 * @sess: and #RTPSession
2268 * @buffer: an RTCP buffer
2269 * @current_time: the current system time
2270 * @ntpnstime: the current NTP time in nanoseconds
2272 * Process an RTCP buffer in the session manager. This function takes ownership
2275 * Returns: a #GstFlowReturn.
2278 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
2279 GstClockTime current_time, guint64 ntpnstime)
2281 GstRTCPPacket packet;
2282 gboolean more, is_bye = FALSE, do_sync = FALSE;
2283 RTPArrivalStats arrival = { NULL, };
2284 GstFlowReturn result = GST_FLOW_OK;
2285 GstRTCPBuffer rtcp = { NULL, };
2287 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2288 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2290 if (!gst_rtcp_buffer_validate (buffer))
2291 goto invalid_packet;
2293 GST_DEBUG ("received RTCP packet");
2295 RTP_SESSION_LOCK (sess);
2296 /* update arrival stats */
2297 update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1,
2301 /* FIXME, simply ignore RTCP for iternal sources with BYE */
2302 if (sess->source->sent_bye)
2306 /* start processing the compound packet */
2307 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
2308 more = gst_rtcp_buffer_get_first_packet (&rtcp, &packet);
2312 type = gst_rtcp_packet_get_type (&packet);
2314 /* when we are leaving the session, we should ignore all non-BYE messages */
2315 if (sess->scheduled_bye && type != GST_RTCP_TYPE_BYE) {
2316 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
2321 case GST_RTCP_TYPE_SR:
2322 rtp_session_process_sr (sess, &packet, &arrival, &do_sync);
2324 case GST_RTCP_TYPE_RR:
2325 rtp_session_process_rr (sess, &packet, &arrival);
2327 case GST_RTCP_TYPE_SDES:
2328 rtp_session_process_sdes (sess, &packet, &arrival);
2330 case GST_RTCP_TYPE_BYE:
2332 /* don't try to attempt lip-sync anymore for streams with a BYE */
2334 rtp_session_process_bye (sess, &packet, &arrival);
2336 case GST_RTCP_TYPE_APP:
2337 rtp_session_process_app (sess, &packet, &arrival);
2339 case GST_RTCP_TYPE_RTPFB:
2340 case GST_RTCP_TYPE_PSFB:
2341 rtp_session_process_feedback (sess, &packet, &arrival, current_time);
2344 GST_WARNING ("got unknown RTCP packet");
2348 more = gst_rtcp_packet_move_to_next (&packet);
2351 gst_rtcp_buffer_unmap (&rtcp);
2353 /* if we are scheduling a BYE, we only want to count bye packets, else we
2354 * count everything */
2355 if (sess->scheduled_bye) {
2357 sess->stats.bye_members++;
2358 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2361 /* keep track of average packet size */
2362 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2364 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
2365 sess->stats.avg_rtcp_packet_size, arrival.bytes);
2366 RTP_SESSION_UNLOCK (sess);
2368 clean_arrival_stats (&arrival);
2370 /* notify caller of sr packets in the callback */
2371 if (do_sync && sess->callbacks.sync_rtcp) {
2372 /* make writable, we might want to change the buffer */
2373 buffer = gst_buffer_make_writable (buffer);
2375 result = sess->callbacks.sync_rtcp (sess, buffer,
2376 sess->sync_rtcp_user_data);
2378 gst_buffer_unref (buffer);
2385 GST_DEBUG ("invalid RTCP packet received");
2386 gst_buffer_unref (buffer);
2392 RTP_SESSION_UNLOCK (sess);
2393 gst_buffer_unref (buffer);
2394 clean_arrival_stats (&arrival);
2395 GST_DEBUG ("ignoring RTCP packet because we left");
2402 * rtp_session_update_send_caps:
2403 * @sess: an #RTPSession
2406 * Update the caps of the sender in the rtp session.
2409 rtp_session_update_send_caps (RTPSession * sess, GstCaps * caps)
2414 g_return_if_fail (RTP_IS_SESSION (sess));
2415 g_return_if_fail (GST_IS_CAPS (caps));
2417 GST_LOG ("received caps %" GST_PTR_FORMAT, caps);
2419 s = gst_caps_get_structure (caps, 0);
2421 if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
2425 RTP_SESSION_LOCK (sess);
2426 source = obtain_internal_source (sess, ssrc, &created);
2428 rtp_source_update_caps (source, caps);
2429 g_object_unref (source);
2431 RTP_SESSION_UNLOCK (sess);
2436 * rtp_session_send_rtp:
2437 * @sess: an #RTPSession
2438 * @data: pointer to either an RTP buffer or a list of RTP buffers
2439 * @is_list: TRUE when @data is a buffer list
2440 * @current_time: the current system time
2441 * @running_time: the running time of @data
2443 * Send the RTP buffer in the session manager. This function takes ownership of
2446 * Returns: a #GstFlowReturn.
2449 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
2450 GstClockTime current_time, GstClockTime running_time)
2452 GstFlowReturn result;
2454 gboolean prevsender;
2457 GstRTPBuffer rtp = { NULL };
2461 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2462 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
2464 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
2467 GstBufferList *list = GST_BUFFER_LIST_CAST (data);
2469 buffer = gst_buffer_list_get (list, 0);
2473 buffer = GST_BUFFER_CAST (data);
2476 if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp))
2477 goto invalid_packet;
2479 /* get SSRC and look up in session database */
2480 ssrc = gst_rtp_buffer_get_ssrc (&rtp);
2482 gst_rtp_buffer_unmap (&rtp);
2484 RTP_SESSION_LOCK (sess);
2485 source = obtain_internal_source (sess, ssrc, &created);
2487 /* update last activity */
2488 source->last_rtp_activity = current_time;
2490 prevsender = RTP_SOURCE_IS_SENDER (source);
2491 oldrate = source->bitrate;
2493 /* we use our own source to send */
2494 result = rtp_source_send_rtp (source, data, is_list, running_time);
2496 source_update_sender (sess, source, prevsender);
2498 if (oldrate != source->bitrate)
2499 sess->recalc_bandwidth = TRUE;
2500 RTP_SESSION_UNLOCK (sess);
2502 g_object_unref (source);
2508 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
2509 GST_DEBUG ("invalid RTP packet received");
2514 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
2515 GST_DEBUG ("no buffer in list");
2521 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
2523 *bandwidth += source->bitrate;
2526 /* must be called with session lock */
2528 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2531 GstClockTime result;
2533 /* recalculate bandwidth when it changed */
2534 if (sess->recalc_bandwidth) {
2537 if (sess->bandwidth > 0)
2538 bandwidth = sess->bandwidth;
2540 /* If it is <= 0, then try to estimate the actual bandwidth */
2543 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2544 (GHFunc) add_bitrates, &bandwidth);
2547 if (bandwidth < 8000)
2548 bandwidth = RTP_STATS_BANDWIDTH;
2550 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
2551 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
2553 sess->recalc_bandwidth = FALSE;
2556 if (sess->scheduled_bye) {
2557 result = rtp_stats_calculate_bye_interval (&sess->stats);
2559 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2560 sess->stats.internal_sender_sources > 0, first);
2563 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2564 GST_TIME_ARGS (result), first);
2566 if (!deterministic && result != GST_CLOCK_TIME_NONE)
2567 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2569 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2575 source_mark_bye (const gchar * key, RTPSource * source, const gchar * reason)
2577 if (source->internal)
2578 rtp_source_mark_bye (source, reason);
2582 * rtp_session_mark_all_bye:
2583 * @sess: an #RTPSession
2586 * Mark all internal sources of the session as BYE with @reason.
2589 rtp_session_mark_all_bye (RTPSession * sess, const gchar * reason)
2591 g_return_if_fail (RTP_IS_SESSION (sess));
2593 RTP_SESSION_LOCK (sess);
2594 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2595 (GHFunc) source_mark_bye, (gpointer) reason);
2596 RTP_SESSION_UNLOCK (sess);
2599 /* Stop the current @sess and schedule a BYE message for the other members.
2600 * One must have the session lock to call this function
2602 static GstFlowReturn
2603 rtp_session_schedule_bye_locked (RTPSession * sess, GstClockTime current_time)
2605 GstFlowReturn result = GST_FLOW_OK;
2606 GstClockTime interval;
2608 /* nothing to do it we already scheduled bye */
2609 if (sess->scheduled_bye)
2612 /* we schedule BYE now */
2613 sess->scheduled_bye = TRUE;
2614 /* at least one member wants to send a BYE */
2615 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
2616 sess->stats.bye_members = 1;
2617 sess->first_rtcp = TRUE;
2618 sess->allow_early = TRUE;
2620 /* reschedule transmission */
2621 sess->last_rtcp_send_time = current_time;
2622 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2624 if (interval != GST_CLOCK_TIME_NONE)
2625 sess->next_rtcp_check_time = current_time + interval;
2627 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
2629 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2630 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2632 RTP_SESSION_UNLOCK (sess);
2633 /* notify app of reconsideration */
2634 if (sess->callbacks.reconsider)
2635 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2636 RTP_SESSION_LOCK (sess);
2643 * rtp_session_schedule_bye:
2644 * @sess: an #RTPSession
2645 * @current_time: the current system time
2647 * Schedule a BYE message for all sources marked as BYE in @sess.
2649 * Returns: a #GstFlowReturn.
2652 rtp_session_schedule_bye (RTPSession * sess, GstClockTime current_time)
2654 GstFlowReturn result = GST_FLOW_OK;
2656 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2658 RTP_SESSION_LOCK (sess);
2659 result = rtp_session_schedule_bye_locked (sess, current_time);
2660 RTP_SESSION_UNLOCK (sess);
2666 * rtp_session_next_timeout:
2667 * @sess: an #RTPSession
2668 * @current_time: the current system time
2670 * Get the next time we should perform session maintenance tasks.
2672 * Returns: a time when rtp_session_on_timeout() should be called with the
2673 * current system time.
2676 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2678 GstClockTime result, interval = 0;
2680 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
2682 RTP_SESSION_LOCK (sess);
2684 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
2685 result = sess->next_early_rtcp_time;
2689 result = sess->next_rtcp_check_time;
2691 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2692 ", next time: %" GST_TIME_FORMAT,
2693 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2695 if (result == GST_CLOCK_TIME_NONE || result < current_time) {
2696 GST_DEBUG ("take current time as base");
2697 /* our previous check time expired, start counting from the current time
2699 result = current_time;
2702 if (sess->scheduled_bye) {
2703 if (sess->stats.active_sources >= 50) {
2704 GST_DEBUG ("reconsider BYE, more than 50 sources");
2705 /* reconsider BYE if members >= 50 */
2706 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2709 if (sess->first_rtcp) {
2710 GST_DEBUG ("first RTCP packet");
2711 /* we are called for the first time */
2712 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2713 } else if (sess->next_rtcp_check_time < current_time) {
2714 GST_DEBUG ("old check time expired, getting new timeout");
2715 /* get a new timeout when we need to */
2716 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
2720 if (interval != GST_CLOCK_TIME_NONE)
2723 result = GST_CLOCK_TIME_NONE;
2725 sess->next_rtcp_check_time = result;
2729 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2730 ", next time: %" GST_TIME_FORMAT,
2731 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2732 RTP_SESSION_UNLOCK (sess);
2746 GstRTCPBuffer rtcpbuf;
2749 guint num_to_report;
2751 GstClockTime current_time;
2753 GstClockTime running_time;
2754 GstClockTime interval;
2755 GstRTCPPacket packet;
2758 gboolean may_suppress;
2763 session_start_rtcp (RTPSession * sess, ReportData * data)
2765 GstRTCPPacket *packet = &data->packet;
2766 RTPSource *own = data->source;
2767 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2769 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2770 data->has_sdes = FALSE;
2772 gst_rtcp_buffer_map (data->rtcp, GST_MAP_READWRITE, rtcp);
2774 if (RTP_SOURCE_IS_SENDER (own)) {
2777 guint32 packet_count, octet_count;
2779 /* we are a sender, create SR */
2780 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2781 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SR, packet);
2783 /* get latest stats */
2784 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
2785 &ntptime, &rtptime, &packet_count, &octet_count);
2787 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2788 packet_count, octet_count);
2790 /* fill in sender report info */
2791 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2792 ntptime, rtptime, packet_count, octet_count);
2794 /* we are only receiver, create RR */
2795 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2796 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RR, packet);
2797 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2801 /* construct a Sender or Receiver Report */
2803 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2805 RTPSession *sess = data->sess;
2806 GstRTCPPacket *packet = &data->packet;
2807 guint8 fractionlost;
2809 guint32 exthighestseq, jitter;
2812 /* don't report for sources in future generations */
2813 if (((gint16) (source->generation - sess->generation)) > 0) {
2814 GST_DEBUG ("source %08x generation %u > %u", source->ssrc,
2815 source->generation, sess->generation);
2819 /* only report about other sender */
2820 if (source == data->source)
2823 if (gst_rtcp_packet_get_rb_count (packet) == GST_RTCP_MAX_RB_COUNT) {
2824 GST_DEBUG ("max RB count reached");
2828 if (!RTP_SOURCE_IS_SENDER (source)) {
2829 GST_DEBUG ("source %08x not sender", source->ssrc);
2833 GST_DEBUG ("create RB for SSRC %08x", source->ssrc);
2836 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2837 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2839 /* store last generated RR packet */
2840 source->last_rr.is_valid = TRUE;
2841 source->last_rr.fractionlost = fractionlost;
2842 source->last_rr.packetslost = packetslost;
2843 source->last_rr.exthighestseq = exthighestseq;
2844 source->last_rr.jitter = jitter;
2845 source->last_rr.lsr = lsr;
2846 source->last_rr.dlsr = dlsr;
2848 /* packet is not yet filled, add report block for this source. */
2849 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2850 exthighestseq, jitter, lsr, dlsr);
2853 /* source is reported, move to next generation */
2854 source->generation = sess->generation + 1;
2856 /* if we reported all sources in this generation, move to next */
2857 if (--data->num_to_report == 0) {
2859 GST_DEBUG ("all reported, generation now %u", sess->generation);
2863 /* perform cleanup of sources that timed out */
2865 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2867 gboolean remove = FALSE;
2868 gboolean byetimeout = FALSE;
2869 gboolean sendertimeout = FALSE;
2870 gboolean is_sender, is_active;
2871 RTPSession *sess = data->sess;
2872 GstClockTime interval, binterval;
2875 GST_DEBUG ("look at %08x, generation %u", source->ssrc, source->generation);
2877 /* check for outdated collisions */
2878 if (source->internal) {
2879 GST_DEBUG ("Timing out collisions for %x", source->ssrc);
2880 rtp_source_timeout (source, data->current_time,
2881 /* "a relatively long time" -- RFC 3550 section 8.2 */
2882 RTP_STATS_MIN_INTERVAL * GST_SECOND * 10,
2883 data->running_time - sess->rtcp_feedback_retention_window);
2886 /* nothing else to do when without RTCP */
2887 if (data->interval == GST_CLOCK_TIME_NONE)
2890 is_sender = RTP_SOURCE_IS_SENDER (source);
2891 is_active = RTP_SOURCE_IS_ACTIVE (source);
2893 /* our own rtcp interval may have been forced low by secondary configuration,
2894 * while sender side may still operate with higher interval,
2895 * so do not just take our interval to decide on timing out sender,
2896 * but take (if data->interval <= 5 * GST_SECOND):
2897 * interval = CLAMP (sender_interval, data->interval, 5 * GST_SECOND)
2898 * where sender_interval is difference between last 2 received RTCP reports
2900 if (data->interval >= 5 * GST_SECOND || source->internal) {
2901 binterval = data->interval;
2903 GST_LOG ("prev_rtcp %" GST_TIME_FORMAT ", last_rtcp %" GST_TIME_FORMAT,
2904 GST_TIME_ARGS (source->stats.prev_rtcptime),
2905 GST_TIME_ARGS (source->stats.last_rtcptime));
2906 /* if not received enough yet, fallback to larger default */
2907 if (source->stats.last_rtcptime > source->stats.prev_rtcptime)
2908 binterval = source->stats.last_rtcptime - source->stats.prev_rtcptime;
2910 binterval = 5 * GST_SECOND;
2911 binterval = CLAMP (binterval, data->interval, 5 * GST_SECOND);
2913 GST_LOG ("timeout base interval %" GST_TIME_FORMAT,
2914 GST_TIME_ARGS (binterval));
2916 if (!source->internal) {
2917 if (source->marked_bye) {
2918 /* if we received a BYE from the source, remove the source after some
2920 if (data->current_time > source->bye_time &&
2921 data->current_time - source->bye_time > sess->stats.bye_timeout) {
2922 GST_DEBUG ("removing BYE source %08x", source->ssrc);
2927 /* sources that were inactive for more than 5 times the deterministic reporting
2928 * interval get timed out. the min timeout is 5 seconds. */
2929 /* mind old time that might pre-date last time going to PLAYING */
2930 btime = MAX (source->last_activity, sess->start_time);
2931 if (data->current_time > btime) {
2932 interval = MAX (binterval * 5, 5 * GST_SECOND);
2933 if (data->current_time - btime > interval) {
2934 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2935 source->ssrc, GST_TIME_ARGS (btime));
2941 /* senders that did not send for a long time become a receiver, this also
2942 * holds for our own sources. */
2944 /* mind old time that might pre-date last time going to PLAYING */
2945 btime = MAX (source->last_rtp_activity, sess->start_time);
2946 if (data->current_time > btime) {
2947 interval = MAX (binterval * 2, 5 * GST_SECOND);
2948 if (data->current_time - btime > interval) {
2949 if (source->internal && source->sent_bye) {
2950 /* an internal source is BYE and stopped sending RTP, remove */
2951 GST_DEBUG ("internal BYE source %08x timed out, last %"
2952 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
2955 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
2956 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
2957 sendertimeout = TRUE;
2964 sess->total_sources--;
2966 sess->stats.sender_sources--;
2967 if (source->internal)
2968 sess->stats.internal_sender_sources--;
2971 sess->stats.active_sources--;
2973 if (source->internal)
2974 sess->stats.internal_sources--;
2977 on_bye_timeout (sess, source);
2979 on_timeout (sess, source);
2981 if (sendertimeout) {
2982 source->is_sender = FALSE;
2983 sess->stats.sender_sources--;
2984 if (source->internal)
2985 sess->stats.internal_sender_sources--;
2987 on_sender_timeout (sess, source);
2989 /* count how many source to report in this generation */
2990 if (((gint16) (source->generation - sess->generation)) <= 0)
2991 data->num_to_report++;
2993 source->closing = remove;
2997 session_sdes (RTPSession * sess, ReportData * data)
2999 GstRTCPPacket *packet = &data->packet;
3000 const GstStructure *sdes;
3002 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3004 /* add SDES packet */
3005 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SDES, packet);
3007 gst_rtcp_packet_sdes_add_item (packet, data->source->ssrc);
3009 sdes = rtp_source_get_sdes_struct (data->source);
3011 /* add all fields in the structure, the order is not important. */
3012 n_fields = gst_structure_n_fields (sdes);
3013 for (i = 0; i < n_fields; ++i) {
3016 GstRTCPSDESType type;
3018 field = gst_structure_nth_field_name (sdes, i);
3021 value = gst_structure_get_string (sdes, field);
3024 type = gst_rtcp_sdes_name_to_type (field);
3026 /* Early packets are minimal and only include the CNAME */
3027 if (data->is_early && type != GST_RTCP_SDES_CNAME)
3030 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
3031 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
3032 (const guint8 *) value);
3033 } else if (type == GST_RTCP_SDES_PRIV) {
3039 /* don't accept entries that are too big */
3040 prefix_len = strlen (field);
3041 if (prefix_len > 255)
3043 value_len = strlen (value);
3044 if (value_len > 255)
3046 data_len = 1 + prefix_len + value_len;
3050 data[0] = prefix_len;
3051 memcpy (&data[1], field, prefix_len);
3052 memcpy (&data[1 + prefix_len], value, value_len);
3054 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
3058 data->has_sdes = TRUE;
3061 /* schedule a BYE packet */
3063 make_source_bye (RTPSession * sess, RTPSource * source, ReportData * data)
3065 GstRTCPPacket *packet = &data->packet;
3066 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3069 session_sdes (sess, data);
3070 /* add a BYE packet */
3071 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_BYE, packet);
3072 gst_rtcp_packet_bye_add_ssrc (packet, source->ssrc);
3073 if (source->bye_reason)
3074 gst_rtcp_packet_bye_set_reason (packet, source->bye_reason);
3076 /* we have a BYE packet now */
3077 source->sent_bye = TRUE;
3081 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
3083 GstClockTime new_send_time, elapsed;
3085 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3086 data->is_early = TRUE;
3088 data->is_early = FALSE;
3090 if (data->is_early && sess->next_early_rtcp_time < current_time)
3093 /* no need to check yet */
3094 if (sess->next_rtcp_check_time == GST_CLOCK_TIME_NONE ||
3095 sess->next_rtcp_check_time > current_time) {
3096 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
3097 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
3098 GST_TIME_ARGS (current_time));
3102 /* get elapsed time since we last reported */
3103 elapsed = current_time - sess->last_rtcp_send_time;
3105 new_send_time = data->interval;
3106 /* perform forward reconsideration */
3107 if (new_send_time != GST_CLOCK_TIME_NONE) {
3108 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, new_send_time);
3110 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
3111 GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time),
3112 GST_TIME_ARGS (elapsed));
3114 new_send_time += sess->last_rtcp_send_time;
3117 /* check if reconsideration */
3118 if (new_send_time == GST_CLOCK_TIME_NONE || current_time < new_send_time) {
3119 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
3120 GST_TIME_ARGS (new_send_time));
3121 /* store new check time */
3122 sess->next_rtcp_check_time = new_send_time;
3128 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
3130 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
3131 GST_TIME_ARGS (new_send_time));
3133 sess->next_rtcp_check_time = new_send_time;
3134 if (new_send_time != GST_CLOCK_TIME_NONE) {
3135 sess->next_rtcp_check_time += current_time;
3137 /* Apply the rules from RFC 4585 section 3.5.3 */
3138 if (sess->stats.min_interval != 0 && !sess->first_rtcp) {
3139 GstClockTimeDiff T_rr_current_interval =
3140 g_random_double_range (0.5, 1.5) * sess->stats.min_interval;
3142 /* This will caused the RTCP to be suppressed if no FB packets are added */
3143 if (sess->last_rtcp_send_time + T_rr_current_interval >
3144 sess->next_rtcp_check_time) {
3145 GST_DEBUG ("RTCP packet could be suppressed min: %" GST_TIME_FORMAT
3146 " last: %" GST_TIME_FORMAT
3147 " + T_rr_current_interval: %" GST_TIME_FORMAT
3148 " > sess->next_rtcp_check_time: %" GST_TIME_FORMAT,
3149 GST_TIME_ARGS (sess->stats.min_interval),
3150 GST_TIME_ARGS (sess->last_rtcp_send_time),
3151 GST_TIME_ARGS (T_rr_current_interval),
3152 GST_TIME_ARGS (sess->next_rtcp_check_time));
3153 data->may_suppress = TRUE;
3162 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
3164 g_hash_table_insert (hash_table, key, g_object_ref (source));
3168 remove_closing_sources (const gchar * key, RTPSource * source, gpointer * data)
3170 return source->closing;
3174 generate_rtcp (const gchar * key, RTPSource * source, ReportData * data)
3176 RTPSession *sess = data->sess;
3177 gboolean is_bye = FALSE;
3178 ReportOutput *output;
3180 /* only generate RTCP for active internal sources */
3181 if (!source->internal || source->sent_bye)
3184 data->source = source;
3187 session_start_rtcp (sess, data);
3189 if (source->marked_bye) {
3191 make_source_bye (sess, source, data);
3193 } else if (!data->is_early) {
3194 /* loop over all known sources and add report blocks. If we are early, we
3195 * just make a minimal RTCP packet and skip this step */
3196 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3197 (GHFunc) session_report_blocks, data);
3199 if (!data->has_sdes)
3200 session_sdes (sess, data);
3202 gst_rtcp_buffer_unmap (&data->rtcpbuf);
3204 output = g_slice_new (ReportOutput);
3205 output->source = g_object_ref (source);
3206 output->is_bye = is_bye;
3207 output->buffer = data->rtcp;
3208 /* queue the RTCP packet to push later */
3209 g_queue_push_tail (&data->output, output);
3213 * rtp_session_on_timeout:
3214 * @sess: an #RTPSession
3215 * @current_time: the current system time
3216 * @ntpnstime: the current NTP time in nanoseconds
3217 * @running_time: the current running_time of the pipeline
3219 * Perform maintenance actions after the timeout obtained with
3220 * rtp_session_next_timeout() expired.
3222 * This function will perform timeouts of receivers and senders, send a BYE
3223 * packet or generate RTCP packets with current session stats.
3225 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
3226 * times, for each packet that should be processed.
3228 * Returns: a #GstFlowReturn.
3231 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
3232 guint64 ntpnstime, GstClockTime running_time)
3234 GstFlowReturn result = GST_FLOW_OK;
3235 ReportData data = { GST_RTCP_BUFFER_INIT };
3236 GHashTable *table_copy;
3237 ReportOutput *output;
3239 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3241 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT
3242 ", running-time %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
3243 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (running_time));
3246 data.current_time = current_time;
3247 data.ntpnstime = ntpnstime;
3248 data.running_time = running_time;
3249 data.num_to_report = 0;
3250 data.may_suppress = FALSE;
3251 g_queue_init (&data.output);
3253 RTP_SESSION_LOCK (sess);
3254 /* get a new interval, we need this for various cleanups etc */
3255 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
3257 /* we need an internal source now */
3258 if (sess->stats.internal_sources == 0) {
3262 source = obtain_internal_source (sess, sess->suggested_ssrc, &created);
3263 g_object_unref (source);
3266 /* Make a local copy of the hashtable. We need to do this because the
3267 * cleanup stage below releases the session lock. */
3268 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
3269 (GDestroyNotify) g_object_unref);
3270 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3271 (GHFunc) clone_ssrcs_hashtable, table_copy);
3273 /* Clean up the session, mark the source for removing, this might release the
3275 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
3276 g_hash_table_destroy (table_copy);
3278 /* Now remove the marked sources */
3279 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
3280 (GHRFunc) remove_closing_sources, NULL);
3282 /* see if we need to generate SR or RR packets */
3283 if (!is_rtcp_time (sess, current_time, &data))
3286 GST_DEBUG ("doing RTCP generation %u for %u sources", sess->generation,
3287 data.num_to_report);
3289 /* generate RTCP for all internal sources */
3290 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3291 (GHFunc) generate_rtcp, &data);
3293 /* we keep track of the last report time in order to timeout inactive
3294 * receivers or senders */
3295 if (!data.is_early && !data.may_suppress)
3296 sess->last_rtcp_send_time = data.current_time;
3297 sess->first_rtcp = FALSE;
3298 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
3301 RTP_SESSION_UNLOCK (sess);
3303 /* push out the RTCP packets */
3304 while ((output = g_queue_pop_head (&data.output))) {
3305 gboolean do_not_suppress;
3306 GstBuffer *buffer = output->buffer;
3307 RTPSource *source = output->source;
3309 /* Give the user a change to add its own packet */
3310 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
3311 buffer, data.is_early, &do_not_suppress);
3313 if (sess->callbacks.send_rtcp && (do_not_suppress || !data.may_suppress)) {
3316 packet_size = gst_buffer_get_size (buffer) + sess->header_len;
3318 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
3319 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
3320 sess->stats.avg_rtcp_packet_size, packet_size);
3322 sess->callbacks.send_rtcp (sess, source, buffer, output->is_bye,
3323 sess->send_rtcp_user_data);
3325 GST_DEBUG ("freeing packet callback: %p"
3326 " do_not_suppress: %d may_suppress: %d",
3327 sess->callbacks.send_rtcp, do_not_suppress, data.may_suppress);
3328 gst_buffer_unref (buffer);
3330 g_object_unref (source);
3331 g_slice_free (ReportOutput, output);
3337 * rtp_session_request_early_rtcp:
3338 * @sess: an #RTPSession
3339 * @current_time: the current system time
3340 * @max_delay: maximum delay
3342 * Request transmission of early RTCP
3345 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
3346 GstClockTimeDiff max_delay)
3348 GstClockTime T_dither_max;
3350 /* Implements the algorithm described in RFC 4585 section 3.5.2 */
3352 RTP_SESSION_LOCK (sess);
3354 /* Check if already requested */
3355 /* RFC 4585 section 3.5.2 step 2 */
3356 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3359 if (!GST_CLOCK_TIME_IS_VALID (sess->next_rtcp_check_time))
3362 /* Ignore the request a scheduled packet will be in time anyway */
3363 if (current_time + max_delay > sess->next_rtcp_check_time)
3366 /* RFC 4585 section 3.5.2 step 2b */
3367 /* If the total sources is <=2, then there is only us and one peer */
3368 if (sess->total_sources <= 2) {
3371 /* Divide by 2 because l = 0.5 */
3372 T_dither_max = sess->next_rtcp_check_time - sess->last_rtcp_send_time;
3376 /* RFC 4585 section 3.5.2 step 3 */
3377 if (current_time + T_dither_max > sess->next_rtcp_check_time)
3380 /* RFC 4585 section 3.5.2 step 4
3381 * Don't send if allow_early is FALSE, but not if we are in
3382 * immediate mode, meaning we are part of a group of at most the
3383 * application-specific threshold.
3385 if (sess->total_sources > sess->rtcp_immediate_feedback_threshold &&
3386 sess->allow_early == FALSE)
3390 /* Schedule an early transmission later */
3391 sess->next_early_rtcp_time = g_random_double () * T_dither_max +
3394 /* If no dithering, schedule it for NOW */
3395 sess->next_early_rtcp_time = current_time;
3398 RTP_SESSION_UNLOCK (sess);
3400 /* notify app of need to send packet early
3401 * and therefore of timeout change */
3402 if (sess->callbacks.reconsider)
3403 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
3409 RTP_SESSION_UNLOCK (sess);
3413 rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc, GstClockTime now,
3414 gboolean fir, gint count)
3416 RTPSource *src = find_source (sess, ssrc);
3422 src->send_pli = FALSE;
3423 src->send_fir = TRUE;
3425 if (count == -1 || count != src->last_fir_count)
3426 src->current_send_fir_seqnum++;
3427 src->last_fir_count = count;
3428 } else if (!src->send_fir) {
3429 src->send_pli = TRUE;
3432 rtp_session_request_early_rtcp (sess, now, 200 * GST_MSECOND);
3438 has_pli_compare_func (gconstpointer a, gconstpointer ignored)
3440 GstRTCPPacket packet;
3441 GstRTCPBuffer rtcp = { NULL, };
3442 gboolean ret = FALSE;
3444 gst_rtcp_buffer_map ((GstBuffer *) a, GST_MAP_READ, &rtcp);
3446 if (gst_rtcp_buffer_get_first_packet (&rtcp, &packet)) {
3447 if (gst_rtcp_packet_get_type (&packet) == GST_RTCP_TYPE_PSFB &&
3448 gst_rtcp_packet_fb_get_type (&packet) == GST_RTCP_PSFB_TYPE_PLI)
3452 gst_rtcp_buffer_unmap (&rtcp);
3458 rtp_session_on_sending_rtcp (RTPSession * sess, GstBuffer * buffer,
3461 gboolean ret = FALSE;
3462 GHashTableIter iter;
3463 gpointer key, value;
3464 gboolean started_fir = FALSE;
3465 GstRTCPPacket fir_rtcppacket;
3466 GstRTCPPacket packet;
3467 GstRTCPBuffer rtcp = { NULL, };
3470 gst_rtcp_buffer_map (buffer, GST_MAP_READWRITE, &rtcp);
3472 gst_rtcp_buffer_get_first_packet (&rtcp, &packet);
3473 switch (gst_rtcp_packet_get_type (&packet)) {
3474 case GST_RTCP_TYPE_SR:
3475 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc,
3476 NULL, NULL, NULL, NULL);
3478 case GST_RTCP_TYPE_RR:
3479 ssrc = gst_rtcp_packet_rr_get_ssrc (&packet);
3485 RTP_SESSION_LOCK (sess);
3486 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
3487 while (g_hash_table_iter_next (&iter, &key, &value)) {
3488 guint media_ssrc = GPOINTER_TO_UINT (key);
3489 RTPSource *media_src = value;
3492 if (media_src->send_fir) {
3494 if (!gst_rtcp_buffer_add_packet (&rtcp, GST_RTCP_TYPE_PSFB,
3497 gst_rtcp_packet_fb_set_type (&fir_rtcppacket, GST_RTCP_PSFB_TYPE_FIR);
3498 gst_rtcp_packet_fb_set_sender_ssrc (&fir_rtcppacket, ssrc);
3499 gst_rtcp_packet_fb_set_media_ssrc (&fir_rtcppacket, 0);
3501 if (!gst_rtcp_packet_fb_set_fci_length (&fir_rtcppacket, 2)) {
3502 gst_rtcp_packet_remove (&fir_rtcppacket);
3508 if (!gst_rtcp_packet_fb_set_fci_length (&fir_rtcppacket,
3509 !gst_rtcp_packet_fb_get_fci_length (&fir_rtcppacket) + 2))
3513 fci_data = gst_rtcp_packet_fb_get_fci (&fir_rtcppacket) -
3514 ((gst_rtcp_packet_fb_get_fci_length (&fir_rtcppacket) - 2) * 4);
3516 GST_WRITE_UINT32_BE (fci_data, media_ssrc);
3518 fci_data[0] = media_src->current_send_fir_seqnum;
3519 fci_data[1] = fci_data[2] = fci_data[3] = 0;
3520 media_src->send_fir = FALSE;
3524 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
3525 while (g_hash_table_iter_next (&iter, &key, &value)) {
3526 guint media_ssrc = GPOINTER_TO_UINT (key);
3527 RTPSource *media_src = value;
3528 GstRTCPPacket pli_rtcppacket;
3530 if (media_src->send_pli && !rtp_source_has_retained (media_src,
3531 has_pli_compare_func, NULL)) {
3532 if (!gst_rtcp_buffer_add_packet (&rtcp, GST_RTCP_TYPE_PSFB,
3534 /* Break because the packet is full, will put next request in a
3537 gst_rtcp_packet_fb_set_type (&pli_rtcppacket, GST_RTCP_PSFB_TYPE_PLI);
3538 gst_rtcp_packet_fb_set_sender_ssrc (&pli_rtcppacket, ssrc);
3539 gst_rtcp_packet_fb_set_media_ssrc (&pli_rtcppacket, media_ssrc);
3542 media_src->send_pli = FALSE;
3544 RTP_SESSION_UNLOCK (sess);
3547 gst_rtcp_buffer_unmap (&rtcp);
3553 rtp_session_send_rtcp (RTPSession * sess, GstClockTimeDiff max_delay)
3557 if (!sess->callbacks.send_rtcp)
3560 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
3562 rtp_session_request_early_rtcp (sess, now, max_delay);