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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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>
28 #include <gst/netbuffer/gstnetbuffer.h>
30 #include <gst/glib-compat-private.h>
32 #include "gstrtpbin-marshal.h"
33 #include "rtpsession.h"
35 GST_DEBUG_CATEGORY_STATIC (rtp_session_debug);
36 #define GST_CAT_DEFAULT rtp_session_debug
38 /* signals and args */
41 SIGNAL_GET_SOURCE_BY_SSRC,
43 SIGNAL_ON_SSRC_COLLISION,
44 SIGNAL_ON_SSRC_VALIDATED,
45 SIGNAL_ON_SSRC_ACTIVE,
48 SIGNAL_ON_BYE_TIMEOUT,
50 SIGNAL_ON_SENDER_TIMEOUT,
51 SIGNAL_ON_SENDING_RTCP,
52 SIGNAL_ON_FEEDBACK_RTCP,
57 #define DEFAULT_INTERNAL_SOURCE NULL
58 #define DEFAULT_BANDWIDTH RTP_STATS_BANDWIDTH
59 #define DEFAULT_RTCP_FRACTION (RTP_STATS_RTCP_FRACTION * RTP_STATS_BANDWIDTH)
60 #define DEFAULT_RTCP_RR_BANDWIDTH -1
61 #define DEFAULT_RTCP_RS_BANDWIDTH -1
62 #define DEFAULT_RTCP_MTU 1400
63 #define DEFAULT_SDES NULL
64 #define DEFAULT_NUM_SOURCES 0
65 #define DEFAULT_NUM_ACTIVE_SOURCES 0
66 #define DEFAULT_SOURCES NULL
67 #define DEFAULT_RTCP_MIN_INTERVAL (RTP_STATS_MIN_INTERVAL * GST_SECOND)
68 #define DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW (2 * GST_SECOND)
69 #define DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD (3)
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,
92 /* update average packet size */
93 #define INIT_AVG(avg, val) \
95 #define UPDATE_AVG(avg, val) \
99 (avg) = ((val) + (15 * (avg))) >> 4;
102 /* The number RTCP intervals after which to timeout entries in the
105 #define RTCP_INTERVAL_COLLISION_TIMEOUT 10
107 /* GObject vmethods */
108 static void rtp_session_finalize (GObject * object);
109 static void rtp_session_set_property (GObject * object, guint prop_id,
110 const GValue * value, GParamSpec * pspec);
111 static void rtp_session_get_property (GObject * object, guint prop_id,
112 GValue * value, GParamSpec * pspec);
114 static gboolean rtp_session_on_sending_rtcp (RTPSession * sess,
115 GstBuffer * buffer, gboolean early);
116 static void rtp_session_send_rtcp (RTPSession * sess,
117 GstClockTimeDiff max_delay);
120 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
122 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
124 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
125 gboolean * created, RTPArrivalStats * arrival, gboolean rtp);
126 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
127 const gchar * reason, GstClockTime current_time);
128 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
129 gboolean deterministic, gboolean first);
132 accumulate_trues (GSignalInvocationHint * ihint, GValue * return_accu,
133 const GValue * handler_return, gpointer data)
135 if (g_value_get_boolean (handler_return))
136 g_value_set_boolean (return_accu, TRUE);
142 gst_rtp_bin_marshal_BOOLEAN__MINIOBJECT_BOOLEAN (GClosure * closure,
143 GValue * return_value G_GNUC_UNUSED, guint n_param_values,
144 const GValue * param_values, gpointer invocation_hint G_GNUC_UNUSED,
145 gpointer marshal_data)
147 typedef gboolean (*GMarshalFunc_BOOLEAN__MINIOBJECT_BOOLEAN) (gpointer data1,
148 gpointer arg_1, gboolean arg_2, gpointer data2);
149 register GMarshalFunc_BOOLEAN__MINIOBJECT_BOOLEAN callback;
150 register GCClosure *cc = (GCClosure *) closure;
151 register gpointer data1, data2;
154 g_return_if_fail (return_value != NULL);
155 g_return_if_fail (n_param_values == 3);
157 if (G_CCLOSURE_SWAP_DATA (closure)) {
158 data1 = closure->data;
159 data2 = g_value_peek_pointer (param_values + 0);
161 data1 = g_value_peek_pointer (param_values + 0);
162 data2 = closure->data;
165 (GMarshalFunc_BOOLEAN__MINIOBJECT_BOOLEAN) (marshal_data ? marshal_data :
168 v_return = callback (data1,
169 gst_value_get_mini_object (param_values + 1),
170 g_value_get_boolean (param_values + 2), data2);
172 g_value_set_boolean (return_value, v_return);
176 gst_rtp_bin_marshal_VOID__UINT_UINT_UINT_UINT_MINIOBJECT (GClosure * closure,
177 GValue * return_value G_GNUC_UNUSED, guint n_param_values,
178 const GValue * param_values, gpointer invocation_hint G_GNUC_UNUSED,
179 gpointer marshal_data)
181 typedef void (*GMarshalFunc_VOID__UINT_UINT_UINT_UINT_MINIOBJECT) (gpointer
182 data1, guint arg_1, guint arg_2, guint arg_3, guint arg_4, gpointer arg_5,
184 register GMarshalFunc_VOID__UINT_UINT_UINT_UINT_MINIOBJECT callback;
185 register GCClosure *cc = (GCClosure *) closure;
186 register gpointer data1, data2;
188 g_return_if_fail (n_param_values == 6);
190 if (G_CCLOSURE_SWAP_DATA (closure)) {
191 data1 = closure->data;
192 data2 = g_value_peek_pointer (param_values + 0);
194 data1 = g_value_peek_pointer (param_values + 0);
195 data2 = closure->data;
198 (GMarshalFunc_VOID__UINT_UINT_UINT_UINT_MINIOBJECT) (marshal_data ?
199 marshal_data : cc->callback);
202 g_value_get_uint (param_values + 1),
203 g_value_get_uint (param_values + 2),
204 g_value_get_uint (param_values + 3),
205 g_value_get_uint (param_values + 4),
206 gst_value_get_mini_object (param_values + 5), data2);
211 rtp_session_class_init (RTPSessionClass * klass)
213 GObjectClass *gobject_class;
215 gobject_class = (GObjectClass *) klass;
217 gobject_class->finalize = rtp_session_finalize;
218 gobject_class->set_property = rtp_session_set_property;
219 gobject_class->get_property = rtp_session_get_property;
222 * RTPSession::get-source-by-ssrc:
223 * @session: the object which received the signal
224 * @ssrc: the SSRC of the RTPSource
226 * Request the #RTPSource object with SSRC @ssrc in @session.
228 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
229 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
230 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
231 get_source_by_ssrc), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
232 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
235 * RTPSession::on-new-ssrc:
236 * @session: the object which received the signal
237 * @src: the new RTPSource
239 * Notify of a new SSRC that entered @session.
241 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
242 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
243 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
244 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
247 * RTPSession::on-ssrc-collision:
248 * @session: the object which received the signal
249 * @src: the #RTPSource that caused a collision
251 * Notify when we have an SSRC collision
253 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
254 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
255 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
256 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
259 * RTPSession::on-ssrc-validated:
260 * @session: the object which received the signal
261 * @src: the new validated RTPSource
263 * Notify of a new SSRC that became validated.
265 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
266 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
267 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
268 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
271 * RTPSession::on-ssrc-active:
272 * @session: the object which received the signal
273 * @src: the active RTPSource
275 * Notify of a SSRC that is active, i.e., sending RTCP.
277 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
278 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
279 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
280 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
283 * RTPSession::on-ssrc-sdes:
284 * @session: the object which received the signal
285 * @src: the RTPSource
287 * Notify that a new SDES was received for SSRC.
289 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
290 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
291 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
292 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
295 * RTPSession::on-bye-ssrc:
296 * @session: the object which received the signal
297 * @src: the RTPSource that went away
299 * Notify of an SSRC that became inactive because of a BYE packet.
301 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
302 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
303 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
304 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
307 * RTPSession::on-bye-timeout:
308 * @session: the object which received the signal
309 * @src: the RTPSource that timed out
311 * Notify of an SSRC that has timed out because of BYE
313 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
314 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
315 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
316 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
319 * RTPSession::on-timeout:
320 * @session: the object which received the signal
321 * @src: the RTPSource that timed out
323 * Notify of an SSRC that has timed out
325 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
326 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
327 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
328 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
331 * RTPSession::on-sender-timeout:
332 * @session: the object which received the signal
333 * @src: the RTPSource that timed out
335 * Notify of an SSRC that was a sender but timed out and became a receiver.
337 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
338 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
339 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
340 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
344 * RTPSession::on-sending-rtcp
345 * @session: the object which received the signal
346 * @buffer: the #GstBuffer containing the RTCP packet about to be sent
347 * @early: %TRUE if the packet is early, %FALSE if it is regular
349 * This signal is emitted before sending an RTCP packet, it can be used
350 * to add extra RTCP Packets.
352 * Returns: %TRUE if the RTCP buffer should NOT be suppressed, %FALSE
353 * if suppressing it is acceptable
355 rtp_session_signals[SIGNAL_ON_SENDING_RTCP] =
356 g_signal_new ("on-sending-rtcp", G_TYPE_FROM_CLASS (klass),
357 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_rtcp),
358 accumulate_trues, NULL, gst_rtp_bin_marshal_BOOLEAN__MINIOBJECT_BOOLEAN,
359 G_TYPE_BOOLEAN, 2, GST_TYPE_BUFFER, G_TYPE_BOOLEAN);
362 * RTPSession::on-feedback-rtcp:
363 * @session: the object which received the signal
364 * @type: Type of RTCP packet, will be %GST_RTCP_TYPE_RTPFB or
365 * %GST_RTCP_TYPE_RTPFB
366 * @fbtype: The type of RTCP FB packet, probably part of #GstRTCPFBType
367 * @sender_ssrc: The SSRC of the sender
368 * @media_ssrc: The SSRC of the media this refers to
369 * @fci: a #GstBuffer with the FCI data from the FB packet or %NULL if
372 * Notify that a RTCP feedback packet has been received
375 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP] =
376 g_signal_new ("on-feedback-rtcp", G_TYPE_FROM_CLASS (klass),
377 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_feedback_rtcp),
378 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT_UINT_UINT_MINIOBJECT,
379 G_TYPE_NONE, 5, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT,
383 * RTPSession::send-rtcp:
384 * @session: the object which received the signal
385 * @max_delay: The maximum delay after which the feedback will not be useful
388 * Requests that the #RTPSession initiate a new RTCP packet as soon as
389 * possible within the requested delay.
392 rtp_session_signals[SIGNAL_SEND_RTCP] =
393 g_signal_new ("send-rtcp", G_TYPE_FROM_CLASS (klass),
394 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
395 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
396 gst_rtp_bin_marshal_VOID__UINT64, G_TYPE_NONE, 1, G_TYPE_UINT64);
398 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
399 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
400 "The internal SSRC used for the session",
401 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
403 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
404 g_param_spec_object ("internal-source", "Internal Source",
405 "The internal source element of the session",
406 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
408 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
409 g_param_spec_double ("bandwidth", "Bandwidth",
410 "The bandwidth of the session (0 for auto-discover)",
411 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
412 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
414 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
415 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
416 "The fraction of the bandwidth used for RTCP (or as a real fraction of the RTP bandwidth if < 1)",
417 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
418 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
420 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
421 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
422 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
423 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
424 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
426 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
427 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
428 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
429 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
430 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
432 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
433 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
434 "The maximum size of the RTCP packets",
435 16, G_MAXINT16, DEFAULT_RTCP_MTU,
436 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
438 g_object_class_install_property (gobject_class, PROP_SDES,
439 g_param_spec_boxed ("sdes", "SDES",
440 "The SDES items of this session",
441 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
443 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
444 g_param_spec_uint ("num-sources", "Num Sources",
445 "The number of sources in the session", 0, G_MAXUINT,
446 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
448 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
449 g_param_spec_uint ("num-active-sources", "Num Active Sources",
450 "The number of active sources in the session", 0, G_MAXUINT,
451 DEFAULT_NUM_ACTIVE_SOURCES,
452 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
456 * Get a GValue Array of all sources in the session.
459 * <title>Getting the #RTPSources of a session
466 * g_object_get (sess, "sources", &arr, NULL);
468 * for (i = 0; i < arr->n_values; i++) {
471 * val = g_value_array_get_nth (arr, i);
472 * source = g_value_get_object (val);
474 * g_value_array_free (arr);
479 g_object_class_install_property (gobject_class, PROP_SOURCES,
480 g_param_spec_boxed ("sources", "Sources",
481 "An array of all known sources in the session",
482 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
484 g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
485 g_param_spec_boolean ("favor-new", "Favor new sources",
486 "Resolve SSRC conflict in favor of new sources", FALSE,
487 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
489 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
490 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
491 "Minimum interval between Regular RTCP packet (in ns)",
492 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
493 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
495 g_object_class_install_property (gobject_class,
496 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
497 g_param_spec_uint64 ("rtcp-feedback-retention-window",
498 "RTCP Feedback retention window",
499 "Duration during which RTCP Feedback packets are retained (in ns)",
500 0, G_MAXUINT64, DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW,
501 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
503 g_object_class_install_property (gobject_class,
504 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
505 g_param_spec_uint ("rtcp-immediate-feedback-threshold",
506 "RTCP Immediate Feedback threshold",
507 "The maximum number of members of a RTP session for which immediate"
509 0, G_MAXUINT, DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
510 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
512 klass->get_source_by_ssrc =
513 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
514 klass->on_sending_rtcp = GST_DEBUG_FUNCPTR (rtp_session_on_sending_rtcp);
515 klass->send_rtcp = GST_DEBUG_FUNCPTR (rtp_session_send_rtcp);
517 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
521 rtp_session_init (RTPSession * sess)
526 sess->lock = g_mutex_new ();
527 sess->key = g_random_int ();
531 for (i = 0; i < 32; i++) {
533 g_hash_table_new_full (NULL, NULL, NULL,
534 (GDestroyNotify) g_object_unref);
536 sess->cnames = g_hash_table_new_full (NULL, NULL, g_free, NULL);
538 rtp_stats_init_defaults (&sess->stats);
540 sess->recalc_bandwidth = TRUE;
541 sess->bandwidth = DEFAULT_BANDWIDTH;
542 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
543 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
544 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
546 /* create an active SSRC for this session manager */
547 sess->source = rtp_session_create_source (sess);
548 sess->source->validated = TRUE;
549 sess->source->internal = TRUE;
550 sess->stats.active_sources++;
551 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
552 sess->source->stats.prev_rtcptime = 0;
553 sess->source->stats.last_rtcptime = 1;
555 rtp_stats_set_min_interval (&sess->stats,
556 (gdouble) DEFAULT_RTCP_MIN_INTERVAL / GST_SECOND);
558 /* default UDP header length */
559 sess->header_len = 28;
560 sess->mtu = DEFAULT_RTCP_MTU;
562 /* some default SDES entries */
564 /* we do not want to leak details like the username or hostname here */
565 str = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
566 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_CNAME, str);
570 /* we do not want to leak the user's real name here */
571 str = g_strdup_printf ("Anon%u", g_random_int ());
572 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_NAME, str);
576 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_TOOL, "GStreamer");
578 sess->first_rtcp = TRUE;
579 sess->allow_early = TRUE;
580 sess->rtcp_feedback_retention_window = DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW;
581 sess->rtcp_immediate_feedback_threshold =
582 DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD;
584 sess->last_keyframe_request = GST_CLOCK_TIME_NONE;
586 GST_DEBUG ("%p: session using SSRC: %08x", sess, sess->source->ssrc);
590 rtp_session_finalize (GObject * object)
595 sess = RTP_SESSION_CAST (object);
597 g_mutex_free (sess->lock);
598 for (i = 0; i < 32; i++)
599 g_hash_table_destroy (sess->ssrcs[i]);
601 g_free (sess->bye_reason);
603 g_hash_table_destroy (sess->cnames);
604 g_object_unref (sess->source);
606 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
610 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
612 GValue value = { 0 };
614 g_value_init (&value, RTP_TYPE_SOURCE);
615 g_value_take_object (&value, source);
616 /* copies the value */
617 g_value_array_append (arr, &value);
621 rtp_session_create_sources (RTPSession * sess)
626 RTP_SESSION_LOCK (sess);
627 /* get number of elements in the table */
628 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
629 /* create the result value array */
630 res = g_value_array_new (size);
632 /* and copy all values into the array */
633 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
634 RTP_SESSION_UNLOCK (sess);
640 rtp_session_set_property (GObject * object, guint prop_id,
641 const GValue * value, GParamSpec * pspec)
645 sess = RTP_SESSION (object);
648 case PROP_INTERNAL_SSRC:
649 rtp_session_set_internal_ssrc (sess, g_value_get_uint (value));
652 sess->bandwidth = g_value_get_double (value);
653 sess->recalc_bandwidth = TRUE;
655 case PROP_RTCP_FRACTION:
656 sess->rtcp_bandwidth = g_value_get_double (value);
657 sess->recalc_bandwidth = TRUE;
659 case PROP_RTCP_RR_BANDWIDTH:
660 sess->rtcp_rr_bandwidth = g_value_get_int (value);
661 sess->recalc_bandwidth = TRUE;
663 case PROP_RTCP_RS_BANDWIDTH:
664 sess->rtcp_rs_bandwidth = g_value_get_int (value);
665 sess->recalc_bandwidth = TRUE;
668 sess->mtu = g_value_get_uint (value);
671 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
674 sess->favor_new = g_value_get_boolean (value);
676 case PROP_RTCP_MIN_INTERVAL:
677 rtp_stats_set_min_interval (&sess->stats,
678 (gdouble) g_value_get_uint64 (value) / GST_SECOND);
679 /* trigger reconsideration */
680 RTP_SESSION_LOCK (sess);
681 sess->next_rtcp_check_time = 0;
682 RTP_SESSION_UNLOCK (sess);
683 if (sess->callbacks.reconsider)
684 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
686 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
687 sess->rtcp_immediate_feedback_threshold = g_value_get_uint (value);
690 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
696 rtp_session_get_property (GObject * object, guint prop_id,
697 GValue * value, GParamSpec * pspec)
701 sess = RTP_SESSION (object);
704 case PROP_INTERNAL_SSRC:
705 g_value_set_uint (value, rtp_session_get_internal_ssrc (sess));
707 case PROP_INTERNAL_SOURCE:
708 g_value_take_object (value, rtp_session_get_internal_source (sess));
711 g_value_set_double (value, sess->bandwidth);
713 case PROP_RTCP_FRACTION:
714 g_value_set_double (value, sess->rtcp_bandwidth);
716 case PROP_RTCP_RR_BANDWIDTH:
717 g_value_set_int (value, sess->rtcp_rr_bandwidth);
719 case PROP_RTCP_RS_BANDWIDTH:
720 g_value_set_int (value, sess->rtcp_rs_bandwidth);
723 g_value_set_uint (value, sess->mtu);
726 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
728 case PROP_NUM_SOURCES:
729 g_value_set_uint (value, rtp_session_get_num_sources (sess));
731 case PROP_NUM_ACTIVE_SOURCES:
732 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
735 g_value_take_boxed (value, rtp_session_create_sources (sess));
738 g_value_set_boolean (value, sess->favor_new);
740 case PROP_RTCP_MIN_INTERVAL:
741 g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
743 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
744 g_value_set_uint (value, sess->rtcp_immediate_feedback_threshold);
747 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
753 on_new_ssrc (RTPSession * sess, RTPSource * source)
755 g_object_ref (source);
756 RTP_SESSION_UNLOCK (sess);
757 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
758 RTP_SESSION_LOCK (sess);
759 g_object_unref (source);
763 on_ssrc_collision (RTPSession * sess, RTPSource * source)
765 g_object_ref (source);
766 RTP_SESSION_UNLOCK (sess);
767 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
769 RTP_SESSION_LOCK (sess);
770 g_object_unref (source);
774 on_ssrc_validated (RTPSession * sess, RTPSource * source)
776 g_object_ref (source);
777 RTP_SESSION_UNLOCK (sess);
778 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
780 RTP_SESSION_LOCK (sess);
781 g_object_unref (source);
785 on_ssrc_active (RTPSession * sess, RTPSource * source)
787 g_object_ref (source);
788 RTP_SESSION_UNLOCK (sess);
789 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
790 RTP_SESSION_LOCK (sess);
791 g_object_unref (source);
795 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
797 g_object_ref (source);
798 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
799 RTP_SESSION_UNLOCK (sess);
800 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
801 RTP_SESSION_LOCK (sess);
802 g_object_unref (source);
806 on_bye_ssrc (RTPSession * sess, RTPSource * source)
808 g_object_ref (source);
809 RTP_SESSION_UNLOCK (sess);
810 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
811 RTP_SESSION_LOCK (sess);
812 g_object_unref (source);
816 on_bye_timeout (RTPSession * sess, RTPSource * source)
818 g_object_ref (source);
819 RTP_SESSION_UNLOCK (sess);
820 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
821 RTP_SESSION_LOCK (sess);
822 g_object_unref (source);
826 on_timeout (RTPSession * sess, RTPSource * source)
828 g_object_ref (source);
829 RTP_SESSION_UNLOCK (sess);
830 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
831 RTP_SESSION_LOCK (sess);
832 g_object_unref (source);
836 on_sender_timeout (RTPSession * sess, RTPSource * source)
838 g_object_ref (source);
839 RTP_SESSION_UNLOCK (sess);
840 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
842 RTP_SESSION_LOCK (sess);
843 g_object_unref (source);
849 * Create a new session object.
851 * Returns: a new #RTPSession. g_object_unref() after usage.
854 rtp_session_new (void)
858 sess = g_object_new (RTP_TYPE_SESSION, NULL);
864 * rtp_session_set_callbacks:
865 * @sess: an #RTPSession
866 * @callbacks: callbacks to configure
867 * @user_data: user data passed in the callbacks
869 * Configure a set of callbacks to be notified of actions.
872 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
875 g_return_if_fail (RTP_IS_SESSION (sess));
877 if (callbacks->process_rtp) {
878 sess->callbacks.process_rtp = callbacks->process_rtp;
879 sess->process_rtp_user_data = user_data;
881 if (callbacks->send_rtp) {
882 sess->callbacks.send_rtp = callbacks->send_rtp;
883 sess->send_rtp_user_data = user_data;
885 if (callbacks->send_rtcp) {
886 sess->callbacks.send_rtcp = callbacks->send_rtcp;
887 sess->send_rtcp_user_data = user_data;
889 if (callbacks->sync_rtcp) {
890 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
891 sess->sync_rtcp_user_data = user_data;
893 if (callbacks->clock_rate) {
894 sess->callbacks.clock_rate = callbacks->clock_rate;
895 sess->clock_rate_user_data = user_data;
897 if (callbacks->reconsider) {
898 sess->callbacks.reconsider = callbacks->reconsider;
899 sess->reconsider_user_data = user_data;
901 if (callbacks->request_key_unit) {
902 sess->callbacks.request_key_unit = callbacks->request_key_unit;
903 sess->request_key_unit_user_data = user_data;
905 if (callbacks->request_time) {
906 sess->callbacks.request_time = callbacks->request_time;
907 sess->request_time_user_data = user_data;
912 * rtp_session_set_process_rtp_callback:
913 * @sess: an #RTPSession
914 * @callback: callback to set
915 * @user_data: user data passed in the callback
917 * Configure only the process_rtp callback to be notified of the process_rtp action.
920 rtp_session_set_process_rtp_callback (RTPSession * sess,
921 RTPSessionProcessRTP callback, gpointer user_data)
923 g_return_if_fail (RTP_IS_SESSION (sess));
925 sess->callbacks.process_rtp = callback;
926 sess->process_rtp_user_data = user_data;
930 * rtp_session_set_send_rtp_callback:
931 * @sess: an #RTPSession
932 * @callback: callback to set
933 * @user_data: user data passed in the callback
935 * Configure only the send_rtp callback to be notified of the send_rtp action.
938 rtp_session_set_send_rtp_callback (RTPSession * sess,
939 RTPSessionSendRTP callback, gpointer user_data)
941 g_return_if_fail (RTP_IS_SESSION (sess));
943 sess->callbacks.send_rtp = callback;
944 sess->send_rtp_user_data = user_data;
948 * rtp_session_set_send_rtcp_callback:
949 * @sess: an #RTPSession
950 * @callback: callback to set
951 * @user_data: user data passed in the callback
953 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
956 rtp_session_set_send_rtcp_callback (RTPSession * sess,
957 RTPSessionSendRTCP callback, gpointer user_data)
959 g_return_if_fail (RTP_IS_SESSION (sess));
961 sess->callbacks.send_rtcp = callback;
962 sess->send_rtcp_user_data = user_data;
966 * rtp_session_set_sync_rtcp_callback:
967 * @sess: an #RTPSession
968 * @callback: callback to set
969 * @user_data: user data passed in the callback
971 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
974 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
975 RTPSessionSyncRTCP callback, gpointer user_data)
977 g_return_if_fail (RTP_IS_SESSION (sess));
979 sess->callbacks.sync_rtcp = callback;
980 sess->sync_rtcp_user_data = user_data;
984 * rtp_session_set_clock_rate_callback:
985 * @sess: an #RTPSession
986 * @callback: callback to set
987 * @user_data: user data passed in the callback
989 * Configure only the clock_rate callback to be notified of the clock_rate action.
992 rtp_session_set_clock_rate_callback (RTPSession * sess,
993 RTPSessionClockRate callback, gpointer user_data)
995 g_return_if_fail (RTP_IS_SESSION (sess));
997 sess->callbacks.clock_rate = callback;
998 sess->clock_rate_user_data = user_data;
1002 * rtp_session_set_reconsider_callback:
1003 * @sess: an #RTPSession
1004 * @callback: callback to set
1005 * @user_data: user data passed in the callback
1007 * Configure only the reconsider callback to be notified of the reconsider action.
1010 rtp_session_set_reconsider_callback (RTPSession * sess,
1011 RTPSessionReconsider callback, gpointer user_data)
1013 g_return_if_fail (RTP_IS_SESSION (sess));
1015 sess->callbacks.reconsider = callback;
1016 sess->reconsider_user_data = user_data;
1020 * rtp_session_set_request_time_callback:
1021 * @sess: an #RTPSession
1022 * @callback: callback to set
1023 * @user_data: user data passed in the callback
1025 * Configure only the request_time callback
1028 rtp_session_set_request_time_callback (RTPSession * sess,
1029 RTPSessionRequestTime callback, gpointer user_data)
1031 g_return_if_fail (RTP_IS_SESSION (sess));
1033 sess->callbacks.request_time = callback;
1034 sess->request_time_user_data = user_data;
1038 * rtp_session_set_bandwidth:
1039 * @sess: an #RTPSession
1040 * @bandwidth: the bandwidth allocated
1042 * Set the session bandwidth in bytes per second.
1045 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
1047 g_return_if_fail (RTP_IS_SESSION (sess));
1049 RTP_SESSION_LOCK (sess);
1050 sess->stats.bandwidth = bandwidth;
1051 RTP_SESSION_UNLOCK (sess);
1055 * rtp_session_get_bandwidth:
1056 * @sess: an #RTPSession
1058 * Get the session bandwidth.
1060 * Returns: the session bandwidth.
1063 rtp_session_get_bandwidth (RTPSession * sess)
1067 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1069 RTP_SESSION_LOCK (sess);
1070 result = sess->stats.bandwidth;
1071 RTP_SESSION_UNLOCK (sess);
1077 * rtp_session_set_rtcp_fraction:
1078 * @sess: an #RTPSession
1079 * @bandwidth: the RTCP bandwidth
1081 * Set the bandwidth in bytes per second that should be used for RTCP
1085 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
1087 g_return_if_fail (RTP_IS_SESSION (sess));
1089 RTP_SESSION_LOCK (sess);
1090 sess->stats.rtcp_bandwidth = bandwidth;
1091 RTP_SESSION_UNLOCK (sess);
1095 * rtp_session_get_rtcp_fraction:
1096 * @sess: an #RTPSession
1098 * Get the session bandwidth used for RTCP.
1100 * Returns: The bandwidth used for RTCP messages.
1103 rtp_session_get_rtcp_fraction (RTPSession * sess)
1107 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
1109 RTP_SESSION_LOCK (sess);
1110 result = sess->stats.rtcp_bandwidth;
1111 RTP_SESSION_UNLOCK (sess);
1117 * rtp_session_set_sdes_string:
1118 * @sess: an #RTPSession
1119 * @type: the type of the SDES item
1120 * @item: a null-terminated string to set.
1122 * Store an SDES item of @type in @sess.
1124 * Returns: %FALSE if the data was unchanged @type is invalid.
1127 rtp_session_set_sdes_string (RTPSession * sess, GstRTCPSDESType type,
1132 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1134 RTP_SESSION_LOCK (sess);
1135 result = rtp_source_set_sdes_string (sess->source, type, item);
1136 RTP_SESSION_UNLOCK (sess);
1142 * rtp_session_get_sdes_string:
1143 * @sess: an #RTPSession
1144 * @type: the type of the SDES item
1146 * Get the SDES item of @type from @sess.
1148 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
1149 * valid. g_free() after usage.
1152 rtp_session_get_sdes_string (RTPSession * sess, GstRTCPSDESType type)
1156 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1158 RTP_SESSION_LOCK (sess);
1159 result = rtp_source_get_sdes_string (sess->source, type);
1160 RTP_SESSION_UNLOCK (sess);
1166 * rtp_session_get_sdes_struct:
1167 * @sess: an #RTSPSession
1169 * Get the SDES data as a #GstStructure
1171 * Returns: a GstStructure with SDES items for @sess. This function returns a
1172 * copy of the SDES structure, use gst_structure_free() after usage.
1175 rtp_session_get_sdes_struct (RTPSession * sess)
1177 const GstStructure *sdes;
1178 GstStructure *result = NULL;
1180 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1182 RTP_SESSION_LOCK (sess);
1183 sdes = rtp_source_get_sdes_struct (sess->source);
1185 result = gst_structure_copy (sdes);
1186 RTP_SESSION_UNLOCK (sess);
1192 * rtp_session_set_sdes_struct:
1193 * @sess: an #RTSPSession
1194 * @sdes: a #GstStructure
1196 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1199 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
1201 g_return_if_fail (sdes);
1202 g_return_if_fail (RTP_IS_SESSION (sess));
1204 RTP_SESSION_LOCK (sess);
1205 rtp_source_set_sdes_struct (sess->source, gst_structure_copy (sdes));
1206 RTP_SESSION_UNLOCK (sess);
1209 static GstFlowReturn
1210 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1212 GstFlowReturn result = GST_FLOW_OK;
1214 if (source == session->source) {
1215 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1217 RTP_SESSION_UNLOCK (session);
1219 if (session->callbacks.send_rtp)
1221 session->callbacks.send_rtp (session, source, data,
1222 session->send_rtp_user_data);
1224 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1227 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1228 RTP_SESSION_UNLOCK (session);
1230 if (session->callbacks.process_rtp)
1232 session->callbacks.process_rtp (session, source,
1233 GST_BUFFER_CAST (data), session->process_rtp_user_data);
1235 gst_buffer_unref (GST_BUFFER_CAST (data));
1237 RTP_SESSION_LOCK (session);
1243 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1247 RTP_SESSION_UNLOCK (session);
1249 if (session->callbacks.clock_rate)
1251 session->callbacks.clock_rate (session, pt,
1252 session->clock_rate_user_data);
1256 RTP_SESSION_LOCK (session);
1258 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1263 static RTPSourceCallbacks callbacks = {
1264 (RTPSourcePushRTP) source_push_rtp,
1265 (RTPSourceClockRate) source_clock_rate,
1269 check_collision (RTPSession * sess, RTPSource * source,
1270 RTPArrivalStats * arrival, gboolean rtp)
1272 /* If we have no arrival address, we can't do collision checking */
1273 if (!arrival->have_address)
1276 if (sess->source != source) {
1277 GstNetAddress *from;
1280 /* This is not our local source, but lets check if two remote
1285 from = &source->rtp_from;
1286 have_from = source->have_rtp_from;
1288 from = &source->rtcp_from;
1289 have_from = source->have_rtcp_from;
1293 if (gst_netaddress_equal (from, &arrival->address)) {
1294 /* Address is the same */
1297 GST_LOG ("we have a third-party collision or loop ssrc:%x",
1298 rtp_source_get_ssrc (source));
1299 if (sess->favor_new) {
1300 if (rtp_source_find_conflicting_address (source,
1301 &arrival->address, arrival->current_time)) {
1303 gst_netaddress_to_string (&arrival->address, buf1, 40);
1304 GST_LOG ("Known conflict on %x for %s, dropping packet",
1305 rtp_source_get_ssrc (source), buf1);
1308 gchar buf1[40], buf2[40];
1310 /* Current address is not a known conflict, lets assume this is
1311 * a new source. Save old address in possible conflict list
1313 rtp_source_add_conflicting_address (source, from,
1314 arrival->current_time);
1316 gst_netaddress_to_string (from, buf1, 40);
1317 gst_netaddress_to_string (&arrival->address, buf2, 40);
1318 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1319 " saving old as known conflict",
1320 rtp_source_get_ssrc (source), buf1, buf2);
1323 rtp_source_set_rtp_from (source, &arrival->address);
1325 rtp_source_set_rtcp_from (source, &arrival->address);
1329 /* Don't need to save old addresses, we ignore new sources */
1334 /* We don't already have a from address for RTP, just set it */
1336 rtp_source_set_rtp_from (source, &arrival->address);
1338 rtp_source_set_rtcp_from (source, &arrival->address);
1342 /* FIXME: Log 3rd party collision somehow
1343 * Maybe should be done in upper layer, only the SDES can tell us
1344 * if its a collision or a loop
1347 /* If the source has been inactive for some time, we assume that it has
1348 * simply changed its transport source address. Hence, there is no true
1349 * third-party collision - only a simulated one. */
1350 if (arrival->current_time > source->last_activity) {
1351 GstClockTime inactivity_period =
1352 arrival->current_time - source->last_activity;
1353 if (inactivity_period > 1 * GST_SECOND) {
1354 /* Use new network address */
1356 g_assert (source->have_rtp_from);
1357 rtp_source_set_rtp_from (source, &arrival->address);
1359 g_assert (source->have_rtcp_from);
1360 rtp_source_set_rtcp_from (source, &arrival->address);
1366 /* This is sending with our ssrc, is it an address we already know */
1368 if (rtp_source_find_conflicting_address (source, &arrival->address,
1369 arrival->current_time)) {
1370 /* Its a known conflict, its probably a loop, not a collision
1371 * lets just drop the incoming packet
1373 GST_DEBUG ("Our packets are being looped back to us, dropping");
1375 /* Its a new collision, lets change our SSRC */
1377 rtp_source_add_conflicting_address (source, &arrival->address,
1378 arrival->current_time);
1380 GST_DEBUG ("Collision for SSRC %x", rtp_source_get_ssrc (source));
1381 on_ssrc_collision (sess, source);
1383 rtp_session_schedule_bye_locked (sess, "SSRC Collision",
1384 arrival->current_time);
1386 sess->change_ssrc = TRUE;
1394 /* must be called with the session lock, the returned source needs to be
1395 * unreffed after usage. */
1397 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1398 RTPArrivalStats * arrival, gboolean rtp)
1403 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1404 if (source == NULL) {
1405 /* make new Source in probation and insert */
1406 source = rtp_source_new (ssrc);
1408 /* for RTP packets we need to set the source in probation. Receiving RTCP
1409 * packets of an SSRC, on the other hand, is a strong indication that we
1410 * are dealing with a valid source. */
1412 source->probation = RTP_DEFAULT_PROBATION;
1414 source->probation = 0;
1416 /* store from address, if any */
1417 if (arrival->have_address) {
1419 rtp_source_set_rtp_from (source, &arrival->address);
1421 rtp_source_set_rtcp_from (source, &arrival->address);
1424 /* configure a callback on the source */
1425 rtp_source_set_callbacks (source, &callbacks, sess);
1427 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1430 /* we have one more source now */
1431 sess->total_sources++;
1435 /* check for collision, this updates the address when not previously set */
1436 if (check_collision (sess, source, arrival, rtp)) {
1440 /* update last activity */
1441 source->last_activity = arrival->current_time;
1443 source->last_rtp_activity = arrival->current_time;
1444 g_object_ref (source);
1450 * rtp_session_get_internal_source:
1451 * @sess: a #RTPSession
1453 * Get the internal #RTPSource of @sess.
1455 * Returns: The internal #RTPSource. g_object_unref() after usage.
1458 rtp_session_get_internal_source (RTPSession * sess)
1462 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1464 result = g_object_ref (sess->source);
1470 * rtp_session_set_internal_ssrc:
1471 * @sess: a #RTPSession
1474 * Set the SSRC of @sess to @ssrc.
1477 rtp_session_set_internal_ssrc (RTPSession * sess, guint32 ssrc)
1479 RTP_SESSION_LOCK (sess);
1480 if (ssrc != sess->source->ssrc) {
1481 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
1482 GINT_TO_POINTER (sess->source->ssrc));
1484 GST_DEBUG ("setting internal SSRC to %08x", ssrc);
1485 /* After this call, any receiver of the old SSRC either in RTP or RTCP
1486 * packets will timeout on the old SSRC, we could potentially schedule a
1487 * BYE RTCP for the old SSRC... */
1488 sess->source->ssrc = ssrc;
1489 rtp_source_reset (sess->source);
1491 /* rehash with the new SSRC */
1492 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1493 GINT_TO_POINTER (sess->source->ssrc), sess->source);
1495 RTP_SESSION_UNLOCK (sess);
1497 g_object_notify (G_OBJECT (sess), "internal-ssrc");
1501 * rtp_session_get_internal_ssrc:
1502 * @sess: a #RTPSession
1504 * Get the internal SSRC of @sess.
1506 * Returns: The SSRC of the session.
1509 rtp_session_get_internal_ssrc (RTPSession * sess)
1513 RTP_SESSION_LOCK (sess);
1514 ssrc = sess->source->ssrc;
1515 RTP_SESSION_UNLOCK (sess);
1521 * rtp_session_add_source:
1522 * @sess: a #RTPSession
1523 * @src: #RTPSource to add
1525 * Add @src to @session.
1527 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1528 * existed in the session.
1531 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1533 gboolean result = FALSE;
1536 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1537 g_return_val_if_fail (src != NULL, FALSE);
1539 RTP_SESSION_LOCK (sess);
1541 g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1542 GINT_TO_POINTER (src->ssrc));
1544 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1545 GINT_TO_POINTER (src->ssrc), src);
1546 /* we have one more source now */
1547 sess->total_sources++;
1550 RTP_SESSION_UNLOCK (sess);
1556 * rtp_session_get_num_sources:
1557 * @sess: an #RTPSession
1559 * Get the number of sources in @sess.
1561 * Returns: The number of sources in @sess.
1564 rtp_session_get_num_sources (RTPSession * sess)
1568 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1570 RTP_SESSION_LOCK (sess);
1571 result = sess->total_sources;
1572 RTP_SESSION_UNLOCK (sess);
1578 * rtp_session_get_num_active_sources:
1579 * @sess: an #RTPSession
1581 * Get the number of active sources in @sess. A source is considered active when
1582 * it has been validated and has not yet received a BYE RTCP message.
1584 * Returns: The number of active sources in @sess.
1587 rtp_session_get_num_active_sources (RTPSession * sess)
1591 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1593 RTP_SESSION_LOCK (sess);
1594 result = sess->stats.active_sources;
1595 RTP_SESSION_UNLOCK (sess);
1601 * rtp_session_get_source_by_ssrc:
1602 * @sess: an #RTPSession
1605 * Find the source with @ssrc in @sess.
1607 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1608 * g_object_unref() after usage.
1611 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1615 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1617 RTP_SESSION_LOCK (sess);
1619 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1621 g_object_ref (result);
1622 RTP_SESSION_UNLOCK (sess);
1628 * rtp_session_get_source_by_cname:
1629 * @sess: a #RTPSession
1632 * Find the source with @cname in @sess.
1634 * Returns: a #RTPSource with CNAME @cname or NULL if the source was not found.
1635 * g_object_unref() after usage.
1638 rtp_session_get_source_by_cname (RTPSession * sess, const gchar * cname)
1642 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1643 g_return_val_if_fail (cname != NULL, NULL);
1645 RTP_SESSION_LOCK (sess);
1646 result = g_hash_table_lookup (sess->cnames, cname);
1648 g_object_ref (result);
1649 RTP_SESSION_UNLOCK (sess);
1654 /* should be called with the SESSION lock */
1656 rtp_session_create_new_ssrc (RTPSession * sess)
1661 ssrc = g_random_int ();
1663 /* see if it exists in the session, we're done if it doesn't */
1664 if (g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1665 GINT_TO_POINTER (ssrc)) == NULL)
1673 * rtp_session_create_source:
1674 * @sess: an #RTPSession
1676 * Create an #RTPSource for use in @sess. This function will create a source
1677 * with an ssrc that is currently not used by any participants in the session.
1679 * Returns: an #RTPSource.
1682 rtp_session_create_source (RTPSession * sess)
1687 RTP_SESSION_LOCK (sess);
1688 ssrc = rtp_session_create_new_ssrc (sess);
1689 source = rtp_source_new (ssrc);
1690 rtp_source_set_callbacks (source, &callbacks, sess);
1691 /* we need an additional ref for the source in the hashtable */
1692 g_object_ref (source);
1693 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1695 /* we have one more source now */
1696 sess->total_sources++;
1697 RTP_SESSION_UNLOCK (sess);
1702 /* update the RTPArrivalStats structure with the current time and other bits
1703 * about the current buffer we are handling.
1704 * This function is typically called when a validated packet is received.
1705 * This function should be called with the SESSION_LOCK
1708 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
1709 gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
1710 GstClockTime running_time, guint64 ntpnstime)
1712 /* get time of arrival */
1713 arrival->current_time = current_time;
1714 arrival->running_time = running_time;
1715 arrival->ntpnstime = ntpnstime;
1717 /* get packet size including header overhead */
1718 arrival->bytes = GST_BUFFER_SIZE (buffer) + sess->header_len;
1721 arrival->payload_len = gst_rtp_buffer_get_payload_len (buffer);
1723 arrival->payload_len = 0;
1726 /* for netbuffer we can store the IP address to check for collisions */
1727 arrival->have_address = GST_IS_NETBUFFER (buffer);
1728 if (arrival->have_address) {
1729 GstNetBuffer *netbuf = (GstNetBuffer *) buffer;
1731 memcpy (&arrival->address, &netbuf->from, sizeof (GstNetAddress));
1736 * rtp_session_process_rtp:
1737 * @sess: and #RTPSession
1738 * @buffer: an RTP buffer
1739 * @current_time: the current system time
1740 * @running_time: the running_time of @buffer
1742 * Process an RTP buffer in the session manager. This function takes ownership
1745 * Returns: a #GstFlowReturn.
1748 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1749 GstClockTime current_time, GstClockTime running_time)
1751 GstFlowReturn result;
1755 gboolean prevsender, prevactive;
1756 RTPArrivalStats arrival;
1761 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1762 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1764 if (!gst_rtp_buffer_validate (buffer))
1765 goto invalid_packet;
1767 RTP_SESSION_LOCK (sess);
1768 /* update arrival stats */
1769 update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1772 /* ignore more RTP packets when we left the session */
1773 if (sess->source->received_bye)
1776 /* get SSRC and look up in session database */
1777 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1778 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1782 prevsender = RTP_SOURCE_IS_SENDER (source);
1783 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1784 oldrate = source->bitrate;
1786 /* copy available csrc for later */
1787 count = gst_rtp_buffer_get_csrc_count (buffer);
1788 /* make sure to not overflow our array. An RTP buffer can maximally contain
1790 count = MIN (count, 16);
1792 for (i = 0; i < count; i++)
1793 csrcs[i] = gst_rtp_buffer_get_csrc (buffer, i);
1795 /* let source process the packet */
1796 result = rtp_source_process_rtp (source, buffer, &arrival);
1798 /* source became active */
1799 if (prevactive != RTP_SOURCE_IS_ACTIVE (source)) {
1800 sess->stats.active_sources++;
1801 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1802 sess->stats.active_sources);
1803 on_ssrc_validated (sess, source);
1805 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1806 sess->stats.sender_sources++;
1807 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1808 sess->stats.sender_sources);
1810 if (oldrate != source->bitrate)
1811 sess->recalc_bandwidth = TRUE;
1814 on_new_ssrc (sess, source);
1816 if (source->validated) {
1819 /* for validated sources, we add the CSRCs as well */
1820 for (i = 0; i < count; i++) {
1822 RTPSource *csrc_src;
1827 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1832 GST_DEBUG ("created new CSRC: %08x", csrc);
1833 rtp_source_set_as_csrc (csrc_src);
1834 if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1835 sess->stats.active_sources++;
1836 on_new_ssrc (sess, csrc_src);
1838 g_object_unref (csrc_src);
1841 g_object_unref (source);
1843 RTP_SESSION_UNLOCK (sess);
1850 gst_buffer_unref (buffer);
1851 GST_DEBUG ("invalid RTP packet received");
1856 gst_buffer_unref (buffer);
1857 RTP_SESSION_UNLOCK (sess);
1858 GST_DEBUG ("ignoring RTP packet because we are leaving");
1863 gst_buffer_unref (buffer);
1864 RTP_SESSION_UNLOCK (sess);
1865 GST_DEBUG ("ignoring packet because its collisioning");
1871 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1872 GstRTCPPacket * packet, RTPArrivalStats * arrival)
1876 count = gst_rtcp_packet_get_rb_count (packet);
1877 for (i = 0; i < count; i++) {
1878 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1879 guint8 fractionlost;
1882 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1883 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1885 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1887 if (ssrc == sess->source->ssrc) {
1888 /* only deal with report blocks for our session, we update the stats of
1889 * the sender of the RTCP message. We could also compare our stats against
1890 * the other sender to see if we are better or worse. */
1891 rtp_source_process_rb (source, arrival->ntpnstime, fractionlost,
1892 packetslost, exthighestseq, jitter, lsr, dlsr);
1895 on_ssrc_active (sess, source);
1898 /* A Sender report contains statistics about how the sender is doing. This
1899 * includes timing informataion such as the relation between RTP and NTP
1900 * timestamps and the number of packets/bytes it sent to us.
1902 * In this report is also included a set of report blocks related to how this
1903 * sender is receiving data (in case we (or somebody else) is also sending stuff
1904 * to it). This info includes the packet loss, jitter and seqnum. It also
1905 * contains information to calculate the round trip time (LSR/DLSR).
1908 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1909 RTPArrivalStats * arrival, gboolean * do_sync)
1911 guint32 senderssrc, rtptime, packet_count, octet_count;
1914 gboolean created, prevsender;
1916 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1917 &packet_count, &octet_count);
1919 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1920 senderssrc, GST_TIME_ARGS (arrival->current_time));
1922 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1926 /* don't try to do lip-sync for sources that sent a BYE */
1927 if (rtp_source_received_bye (source))
1932 prevsender = RTP_SOURCE_IS_SENDER (source);
1934 /* first update the source */
1935 rtp_source_process_sr (source, arrival->current_time, ntptime, rtptime,
1936 packet_count, octet_count);
1938 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1939 sess->stats.sender_sources++;
1940 GST_DEBUG ("source: %08x became sender, %d sender sources", senderssrc,
1941 sess->stats.sender_sources);
1945 on_new_ssrc (sess, source);
1947 rtp_session_process_rb (sess, source, packet, arrival);
1948 g_object_unref (source);
1951 /* A receiver report contains statistics about how a receiver is doing. It
1952 * includes stuff like packet loss, jitter and the seqnum it received last. It
1953 * also contains info to calculate the round trip time.
1955 * We are only interested in how the sender of this report is doing wrt to us.
1958 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1959 RTPArrivalStats * arrival)
1965 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1967 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1969 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1974 on_new_ssrc (sess, source);
1976 rtp_session_process_rb (sess, source, packet, arrival);
1977 g_object_unref (source);
1980 /* Get SDES items and store them in the SSRC */
1982 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1983 RTPArrivalStats * arrival)
1986 gboolean more_items, more_entries;
1988 items = gst_rtcp_packet_sdes_get_item_count (packet);
1989 GST_DEBUG ("got SDES packet with %d items", items);
1991 more_items = gst_rtcp_packet_sdes_first_item (packet);
1993 while (more_items) {
1995 gboolean changed, created, validated;
1999 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
2001 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
2005 /* find src, no probation when dealing with RTCP */
2006 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
2010 sdes = gst_structure_new ("application/x-rtp-source-sdes", NULL);
2012 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
2014 while (more_entries) {
2015 GstRTCPSDESType type;
2021 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
2023 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
2026 if (type == GST_RTCP_SDES_PRIV) {
2027 name = g_strndup ((const gchar *) &data[1], data[0]);
2029 data += data[0] + 1;
2031 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
2034 value = g_strndup ((const gchar *) data, len);
2036 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
2041 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
2045 /* takes ownership of sdes */
2046 changed = rtp_source_set_sdes_struct (source, sdes);
2048 validated = !RTP_SOURCE_IS_ACTIVE (source);
2049 source->validated = TRUE;
2051 /* source became active */
2053 sess->stats.active_sources++;
2054 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
2055 sess->stats.active_sources);
2056 on_ssrc_validated (sess, source);
2060 on_new_ssrc (sess, source);
2062 on_ssrc_sdes (sess, source);
2064 g_object_unref (source);
2066 more_items = gst_rtcp_packet_sdes_next_item (packet);
2071 /* BYE is sent when a client leaves the session
2074 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
2075 RTPArrivalStats * arrival)
2079 gboolean reconsider = FALSE;
2081 reason = gst_rtcp_packet_bye_get_reason (packet);
2082 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
2084 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
2085 for (i = 0; i < count; i++) {
2088 gboolean created, prevactive, prevsender;
2089 guint pmembers, members;
2091 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
2092 GST_DEBUG ("SSRC: %08x", ssrc);
2094 if (ssrc == sess->source->ssrc)
2097 /* find src and mark bye, no probation when dealing with RTCP */
2098 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
2102 /* store time for when we need to time out this source */
2103 source->bye_time = arrival->current_time;
2105 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2106 prevsender = RTP_SOURCE_IS_SENDER (source);
2108 /* let the source handle the rest */
2109 rtp_source_process_bye (source, reason);
2111 pmembers = sess->stats.active_sources;
2113 if (prevactive && !RTP_SOURCE_IS_ACTIVE (source)) {
2114 sess->stats.active_sources--;
2115 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
2116 sess->stats.active_sources);
2118 if (prevsender && !RTP_SOURCE_IS_SENDER (source)) {
2119 sess->stats.sender_sources--;
2120 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
2121 sess->stats.sender_sources);
2123 members = sess->stats.active_sources;
2125 if (!sess->source->received_bye && members < pmembers) {
2126 /* some members went away since the previous timeout estimate.
2127 * Perform reverse reconsideration but only when we are not scheduling a
2129 if (arrival->current_time < sess->next_rtcp_check_time) {
2130 GstClockTime time_remaining;
2132 time_remaining = sess->next_rtcp_check_time - arrival->current_time;
2133 sess->next_rtcp_check_time =
2134 gst_util_uint64_scale (time_remaining, members, pmembers);
2136 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
2137 GST_TIME_ARGS (sess->next_rtcp_check_time));
2139 sess->next_rtcp_check_time += arrival->current_time;
2141 /* mark pending reconsider. We only want to signal the reconsideration
2142 * once after we handled all the source in the bye packet */
2148 on_new_ssrc (sess, source);
2150 on_bye_ssrc (sess, source);
2152 g_object_unref (source);
2155 RTP_SESSION_UNLOCK (sess);
2156 /* notify app of reconsideration */
2157 if (sess->callbacks.reconsider)
2158 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2159 RTP_SESSION_LOCK (sess);
2165 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
2166 RTPArrivalStats * arrival)
2168 GST_DEBUG ("received APP");
2172 rtp_session_request_local_key_unit (RTPSession * sess, RTPSource * src,
2173 gboolean fir, GstClockTime current_time)
2175 guint32 round_trip = 0;
2177 rtp_source_get_last_rb (src, NULL, NULL, NULL, NULL, NULL, NULL, &round_trip);
2179 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE && round_trip) {
2180 GstClockTime round_trip_in_ns = gst_util_uint64_scale (round_trip,
2183 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE &&
2184 current_time - sess->last_keyframe_request < 2 * round_trip_in_ns) {
2185 GST_DEBUG ("Ignoring %s request because one was send without one "
2186 "RTT (%" GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")",
2187 fir ? "FIR" : "PLI",
2188 GST_TIME_ARGS (current_time - sess->last_keyframe_request),
2189 GST_TIME_ARGS (round_trip_in_ns));;
2194 sess->last_keyframe_request = current_time;
2196 GST_LOG ("received %s request from %X %p(%p)", fir ? "FIR" : "PLI",
2197 rtp_source_get_ssrc (src), sess->callbacks.process_rtp,
2198 sess->callbacks.request_key_unit);
2200 RTP_SESSION_UNLOCK (sess);
2201 sess->callbacks.request_key_unit (sess, fir,
2202 sess->request_key_unit_user_data);
2203 RTP_SESSION_LOCK (sess);
2209 rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
2210 guint32 media_ssrc, GstClockTime current_time)
2214 if (!sess->callbacks.request_key_unit)
2217 src = g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
2218 GINT_TO_POINTER (sender_ssrc));
2222 rtp_session_request_local_key_unit (sess, src, FALSE, current_time);
2226 rtp_session_process_fir (RTPSession * sess, guint32 sender_ssrc,
2227 guint8 * fci_data, guint fci_length, GstClockTime current_time)
2232 gboolean our_request = FALSE;
2234 if (!sess->callbacks.request_key_unit)
2240 src = g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
2241 GINT_TO_POINTER (sender_ssrc));
2243 /* Hack because Google fails to set the sender_ssrc correctly */
2244 if (!src && sender_ssrc == 1) {
2245 GHashTableIter iter;
2247 if (sess->stats.sender_sources >
2248 RTP_SOURCE_IS_SENDER (sess->source) ? 2 : 1)
2251 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
2253 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
2254 if (src != sess->source && rtp_source_is_sender (src))
2263 for (position = 0; position < fci_length; position += 8) {
2264 guint8 *data = fci_data + position;
2266 ssrc = GST_READ_UINT32_BE (data);
2268 if (ssrc == rtp_source_get_ssrc (sess->source)) {
2276 rtp_session_request_local_key_unit (sess, src, TRUE, current_time);
2280 rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
2281 RTPArrivalStats * arrival, GstClockTime current_time)
2283 GstRTCPType type = gst_rtcp_packet_get_type (packet);
2284 GstRTCPFBType fbtype = gst_rtcp_packet_fb_get_type (packet);
2285 guint32 sender_ssrc = gst_rtcp_packet_fb_get_sender_ssrc (packet);
2286 guint32 media_ssrc = gst_rtcp_packet_fb_get_media_ssrc (packet);
2287 guint8 *fci_data = gst_rtcp_packet_fb_get_fci (packet);
2288 guint fci_length = 4 * gst_rtcp_packet_fb_get_fci_length (packet);
2290 GST_DEBUG ("received feedback %d:%d from %08X about %08X with FCI of "
2291 "length %d", type, fbtype, sender_ssrc, media_ssrc, fci_length);
2293 if (g_signal_has_handler_pending (sess,
2294 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0, TRUE)) {
2295 GstBuffer *fci_buffer = NULL;
2297 if (fci_length > 0) {
2298 fci_buffer = gst_buffer_create_sub (packet->buffer,
2299 fci_data - GST_BUFFER_DATA (packet->buffer), fci_length);
2300 GST_BUFFER_TIMESTAMP (fci_buffer) = arrival->running_time;
2303 RTP_SESSION_UNLOCK (sess);
2304 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0,
2305 type, fbtype, sender_ssrc, media_ssrc, fci_buffer);
2306 RTP_SESSION_LOCK (sess);
2309 gst_buffer_unref (fci_buffer);
2312 if (sess->rtcp_feedback_retention_window) {
2313 RTPSource *src = g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
2314 GINT_TO_POINTER (media_ssrc));
2317 rtp_source_retain_rtcp_packet (src, packet, arrival->running_time);
2320 if (rtp_source_get_ssrc (sess->source) == media_ssrc ||
2321 /* PSFB FIR puts the media ssrc inside the FCI */
2322 (type == GST_RTCP_TYPE_PSFB && fbtype == GST_RTCP_PSFB_TYPE_FIR)) {
2324 case GST_RTCP_TYPE_PSFB:
2326 case GST_RTCP_PSFB_TYPE_PLI:
2327 rtp_session_process_pli (sess, sender_ssrc, media_ssrc,
2330 case GST_RTCP_PSFB_TYPE_FIR:
2331 rtp_session_process_fir (sess, sender_ssrc, fci_data, fci_length,
2338 case GST_RTCP_TYPE_RTPFB:
2346 * rtp_session_process_rtcp:
2347 * @sess: and #RTPSession
2348 * @buffer: an RTCP buffer
2349 * @current_time: the current system time
2350 * @ntpnstime: the current NTP time in nanoseconds
2352 * Process an RTCP buffer in the session manager. This function takes ownership
2355 * Returns: a #GstFlowReturn.
2358 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
2359 GstClockTime current_time, guint64 ntpnstime)
2361 GstRTCPPacket packet;
2362 gboolean more, is_bye = FALSE, do_sync = FALSE;
2363 RTPArrivalStats arrival;
2364 GstFlowReturn result = GST_FLOW_OK;
2366 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2367 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2369 if (!gst_rtcp_buffer_validate (buffer))
2370 goto invalid_packet;
2372 GST_DEBUG ("received RTCP packet");
2374 RTP_SESSION_LOCK (sess);
2375 /* update arrival stats */
2376 update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1,
2382 /* start processing the compound packet */
2383 more = gst_rtcp_buffer_get_first_packet (buffer, &packet);
2387 type = gst_rtcp_packet_get_type (&packet);
2389 /* when we are leaving the session, we should ignore all non-BYE messages */
2390 if (sess->source->received_bye && type != GST_RTCP_TYPE_BYE) {
2391 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
2396 case GST_RTCP_TYPE_SR:
2397 rtp_session_process_sr (sess, &packet, &arrival, &do_sync);
2399 case GST_RTCP_TYPE_RR:
2400 rtp_session_process_rr (sess, &packet, &arrival);
2402 case GST_RTCP_TYPE_SDES:
2403 rtp_session_process_sdes (sess, &packet, &arrival);
2405 case GST_RTCP_TYPE_BYE:
2407 /* don't try to attempt lip-sync anymore for streams with a BYE */
2409 rtp_session_process_bye (sess, &packet, &arrival);
2411 case GST_RTCP_TYPE_APP:
2412 rtp_session_process_app (sess, &packet, &arrival);
2414 case GST_RTCP_TYPE_RTPFB:
2415 case GST_RTCP_TYPE_PSFB:
2416 rtp_session_process_feedback (sess, &packet, &arrival, current_time);
2419 GST_WARNING ("got unknown RTCP packet");
2423 more = gst_rtcp_packet_move_to_next (&packet);
2426 /* if we are scheduling a BYE, we only want to count bye packets, else we
2427 * count everything */
2428 if (sess->source->received_bye) {
2430 sess->stats.bye_members++;
2431 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2434 /* keep track of average packet size */
2435 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2437 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
2438 sess->stats.avg_rtcp_packet_size, arrival.bytes);
2439 RTP_SESSION_UNLOCK (sess);
2441 /* notify caller of sr packets in the callback */
2442 if (do_sync && sess->callbacks.sync_rtcp) {
2443 /* make writable, we might want to change the buffer */
2444 buffer = gst_buffer_make_metadata_writable (buffer);
2446 result = sess->callbacks.sync_rtcp (sess, sess->source, buffer,
2447 sess->sync_rtcp_user_data);
2449 gst_buffer_unref (buffer);
2456 GST_DEBUG ("invalid RTCP packet received");
2457 gst_buffer_unref (buffer);
2462 gst_buffer_unref (buffer);
2463 RTP_SESSION_UNLOCK (sess);
2464 GST_DEBUG ("ignoring RTP packet because we left");
2470 * rtp_session_send_rtp:
2471 * @sess: an #RTPSession
2472 * @data: pointer to either an RTP buffer or a list of RTP buffers
2473 * @is_list: TRUE when @data is a buffer list
2474 * @current_time: the current system time
2475 * @running_time: the running time of @data
2477 * Send the RTP buffer in the session manager. This function takes ownership of
2480 * Returns: a #GstFlowReturn.
2483 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
2484 GstClockTime current_time, GstClockTime running_time)
2486 GstFlowReturn result;
2488 gboolean prevsender;
2489 gboolean valid_packet;
2492 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2493 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
2496 valid_packet = gst_rtp_buffer_list_validate (GST_BUFFER_LIST_CAST (data));
2498 valid_packet = gst_rtp_buffer_validate (GST_BUFFER_CAST (data));
2502 goto invalid_packet;
2504 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
2506 RTP_SESSION_LOCK (sess);
2507 source = sess->source;
2509 /* update last activity */
2510 source->last_rtp_activity = current_time;
2512 prevsender = RTP_SOURCE_IS_SENDER (source);
2513 oldrate = source->bitrate;
2515 /* we use our own source to send */
2516 result = rtp_source_send_rtp (source, data, is_list, running_time);
2518 if (RTP_SOURCE_IS_SENDER (source) && !prevsender)
2519 sess->stats.sender_sources++;
2520 if (oldrate != source->bitrate)
2521 sess->recalc_bandwidth = TRUE;
2522 RTP_SESSION_UNLOCK (sess);
2529 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
2530 GST_DEBUG ("invalid RTP packet received");
2536 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
2538 *bandwidth += source->bitrate;
2542 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2545 GstClockTime result;
2547 /* recalculate bandwidth when it changed */
2548 if (sess->recalc_bandwidth) {
2551 if (sess->bandwidth > 0)
2552 bandwidth = sess->bandwidth;
2554 /* If it is <= 0, then try to estimate the actual bandwidth */
2555 bandwidth = sess->source->bitrate;
2557 g_hash_table_foreach (sess->cnames, (GHFunc) add_bitrates, &bandwidth);
2560 if (bandwidth < 8000)
2561 bandwidth = RTP_STATS_BANDWIDTH;
2563 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
2564 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
2566 sess->recalc_bandwidth = FALSE;
2569 if (sess->source->received_bye) {
2570 result = rtp_stats_calculate_bye_interval (&sess->stats);
2572 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2573 RTP_SOURCE_IS_SENDER (sess->source), first);
2576 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2577 GST_TIME_ARGS (result), first);
2579 if (!deterministic && result != GST_CLOCK_TIME_NONE)
2580 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2582 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2587 /* Stop the current @sess and schedule a BYE message for the other members.
2588 * One must have the session lock to call this function
2590 static GstFlowReturn
2591 rtp_session_schedule_bye_locked (RTPSession * sess, const gchar * reason,
2592 GstClockTime current_time)
2594 GstFlowReturn result = GST_FLOW_OK;
2596 GstClockTime interval;
2598 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2600 source = sess->source;
2602 /* ignore more BYEs */
2603 if (source->received_bye)
2606 /* we have BYE now */
2607 source->received_bye = TRUE;
2608 /* at least one member wants to send a BYE */
2609 g_free (sess->bye_reason);
2610 sess->bye_reason = g_strdup (reason);
2611 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
2612 sess->stats.bye_members = 1;
2613 sess->first_rtcp = TRUE;
2614 sess->sent_bye = FALSE;
2615 sess->allow_early = TRUE;
2617 /* reschedule transmission */
2618 sess->last_rtcp_send_time = current_time;
2619 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2620 sess->next_rtcp_check_time = current_time + interval;
2622 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2623 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2625 RTP_SESSION_UNLOCK (sess);
2626 /* notify app of reconsideration */
2627 if (sess->callbacks.reconsider)
2628 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2629 RTP_SESSION_LOCK (sess);
2636 * rtp_session_schedule_bye:
2637 * @sess: an #RTPSession
2638 * @reason: a reason or NULL
2639 * @current_time: the current system time
2641 * Stop the current @sess and schedule a BYE message for the other members.
2643 * Returns: a #GstFlowReturn.
2646 rtp_session_schedule_bye (RTPSession * sess, const gchar * reason,
2647 GstClockTime current_time)
2649 GstFlowReturn result = GST_FLOW_OK;
2651 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2653 RTP_SESSION_LOCK (sess);
2654 result = rtp_session_schedule_bye_locked (sess, reason, current_time);
2655 RTP_SESSION_UNLOCK (sess);
2661 * rtp_session_next_timeout:
2662 * @sess: an #RTPSession
2663 * @current_time: the current system time
2665 * Get the next time we should perform session maintenance tasks.
2667 * Returns: a time when rtp_session_on_timeout() should be called with the
2668 * current system time.
2671 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2673 GstClockTime result, interval = 0;
2675 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
2677 RTP_SESSION_LOCK (sess);
2679 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
2680 result = sess->next_early_rtcp_time;
2684 result = sess->next_rtcp_check_time;
2686 GST_DEBUG ("current time: %" GST_TIME_FORMAT ", next :%" GST_TIME_FORMAT,
2687 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2689 if (result < current_time) {
2690 GST_DEBUG ("take current time as base");
2691 /* our previous check time expired, start counting from the current time
2693 result = current_time;
2696 if (sess->source->received_bye) {
2697 if (sess->sent_bye) {
2698 GST_DEBUG ("we sent BYE already");
2699 interval = GST_CLOCK_TIME_NONE;
2700 } else if (sess->stats.active_sources >= 50) {
2701 GST_DEBUG ("reconsider BYE, more than 50 sources");
2702 /* reconsider BYE if members >= 50 */
2703 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2706 if (sess->first_rtcp) {
2707 GST_DEBUG ("first RTCP packet");
2708 /* we are called for the first time */
2709 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2710 } else if (sess->next_rtcp_check_time < current_time) {
2711 GST_DEBUG ("old check time expired, getting new timeout");
2712 /* get a new timeout when we need to */
2713 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
2717 if (interval != GST_CLOCK_TIME_NONE)
2720 result = GST_CLOCK_TIME_NONE;
2722 sess->next_rtcp_check_time = result;
2726 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2727 ", next time: %" GST_TIME_FORMAT,
2728 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2729 RTP_SESSION_UNLOCK (sess);
2738 GstClockTime current_time;
2740 GstClockTime running_time;
2741 GstClockTime interval;
2742 GstRTCPPacket packet;
2746 gboolean may_suppress;
2750 session_start_rtcp (RTPSession * sess, ReportData * data)
2752 GstRTCPPacket *packet = &data->packet;
2753 RTPSource *own = sess->source;
2755 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2757 if (RTP_SOURCE_IS_SENDER (own)) {
2760 guint32 packet_count, octet_count;
2762 /* we are a sender, create SR */
2763 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2764 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SR, packet);
2766 /* get latest stats */
2767 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
2768 &ntptime, &rtptime, &packet_count, &octet_count);
2770 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2771 packet_count, octet_count);
2773 /* fill in sender report info */
2774 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2775 ntptime, rtptime, packet_count, octet_count);
2777 /* we are only receiver, create RR */
2778 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2779 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_RR, packet);
2780 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2784 /* construct a Sender or Receiver Report */
2786 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2788 RTPSession *sess = data->sess;
2789 GstRTCPPacket *packet = &data->packet;
2791 /* create a new buffer if needed */
2792 if (data->rtcp == NULL) {
2793 session_start_rtcp (sess, data);
2794 } else if (data->is_early) {
2795 /* Put a single RR or SR in minimal compound packets */
2798 if (gst_rtcp_packet_get_rb_count (packet) < GST_RTCP_MAX_RB_COUNT) {
2799 /* only report about other sender sources */
2800 if (source != sess->source && RTP_SOURCE_IS_SENDER (source)) {
2801 guint8 fractionlost;
2803 guint32 exthighestseq, jitter;
2807 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2808 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2810 /* store last generated RR packet */
2811 source->last_rr.is_valid = TRUE;
2812 source->last_rr.fractionlost = fractionlost;
2813 source->last_rr.packetslost = packetslost;
2814 source->last_rr.exthighestseq = exthighestseq;
2815 source->last_rr.jitter = jitter;
2816 source->last_rr.lsr = lsr;
2817 source->last_rr.dlsr = dlsr;
2819 /* packet is not yet filled, add report block for this source. */
2820 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2821 exthighestseq, jitter, lsr, dlsr);
2826 /* perform cleanup of sources that timed out */
2828 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2830 gboolean remove = FALSE;
2831 gboolean byetimeout = FALSE;
2832 gboolean sendertimeout = FALSE;
2833 gboolean is_sender, is_active;
2834 RTPSession *sess = data->sess;
2835 GstClockTime interval, binterval;
2838 is_sender = RTP_SOURCE_IS_SENDER (source);
2839 is_active = RTP_SOURCE_IS_ACTIVE (source);
2841 /* our own rtcp interval may have been forced low by secondary configuration,
2842 * while sender side may still operate with higher interval,
2843 * so do not just take our interval to decide on timing out sender,
2844 * but take (if data->interval <= 5 * GST_SECOND):
2845 * interval = CLAMP (sender_interval, data->interval, 5 * GST_SECOND)
2846 * where sender_interval is difference between last 2 received RTCP reports
2848 if (data->interval >= 5 * GST_SECOND || (source == sess->source)) {
2849 binterval = data->interval;
2851 GST_LOG ("prev_rtcp %" GST_TIME_FORMAT ", last_rtcp %" GST_TIME_FORMAT,
2852 GST_TIME_ARGS (source->stats.prev_rtcptime),
2853 GST_TIME_ARGS (source->stats.last_rtcptime));
2854 /* if not received enough yet, fallback to larger default */
2855 if (source->stats.last_rtcptime > source->stats.prev_rtcptime)
2856 binterval = source->stats.last_rtcptime - source->stats.prev_rtcptime;
2858 binterval = 5 * GST_SECOND;
2859 binterval = CLAMP (binterval, data->interval, 5 * GST_SECOND);
2861 GST_LOG ("timeout base interval %" GST_TIME_FORMAT,
2862 GST_TIME_ARGS (binterval));
2864 /* check for our own source, we don't want to delete our own source. */
2865 if (!(source == sess->source)) {
2866 if (source->received_bye) {
2867 /* if we received a BYE from the source, remove the source after some
2869 if (data->current_time > source->bye_time &&
2870 data->current_time - source->bye_time > sess->stats.bye_timeout) {
2871 GST_DEBUG ("removing BYE source %08x", source->ssrc);
2876 /* sources that were inactive for more than 5 times the deterministic reporting
2877 * interval get timed out. the min timeout is 5 seconds. */
2878 /* mind old time that might pre-date last time going to PLAYING */
2879 btime = MAX (source->last_activity, sess->start_time);
2880 if (data->current_time > btime) {
2881 interval = MAX (binterval * 5, 5 * GST_SECOND);
2882 if (data->current_time - btime > interval) {
2883 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2884 source->ssrc, GST_TIME_ARGS (btime));
2890 /* senders that did not send for a long time become a receiver, this also
2891 * holds for our own source. */
2893 /* mind old time that might pre-date last time going to PLAYING */
2894 btime = MAX (source->last_rtp_activity, sess->start_time);
2895 if (data->current_time > btime) {
2896 interval = MAX (binterval * 2, 5 * GST_SECOND);
2897 if (data->current_time - btime > interval) {
2898 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
2899 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
2900 source->is_sender = FALSE;
2901 sess->stats.sender_sources--;
2902 sendertimeout = TRUE;
2908 sess->total_sources--;
2910 sess->stats.sender_sources--;
2912 sess->stats.active_sources--;
2915 on_bye_timeout (sess, source);
2917 on_timeout (sess, source);
2920 on_sender_timeout (sess, source);
2923 source->closing = remove;
2927 session_sdes (RTPSession * sess, ReportData * data)
2929 GstRTCPPacket *packet = &data->packet;
2930 const GstStructure *sdes;
2933 /* add SDES packet */
2934 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SDES, packet);
2936 gst_rtcp_packet_sdes_add_item (packet, sess->source->ssrc);
2938 sdes = rtp_source_get_sdes_struct (sess->source);
2940 /* add all fields in the structure, the order is not important. */
2941 n_fields = gst_structure_n_fields (sdes);
2942 for (i = 0; i < n_fields; ++i) {
2945 GstRTCPSDESType type;
2947 field = gst_structure_nth_field_name (sdes, i);
2950 value = gst_structure_get_string (sdes, field);
2953 type = gst_rtcp_sdes_name_to_type (field);
2955 /* Early packets are minimal and only include the CNAME */
2956 if (data->is_early && type != GST_RTCP_SDES_CNAME)
2959 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
2960 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
2961 (const guint8 *) value);
2962 } else if (type == GST_RTCP_SDES_PRIV) {
2968 /* don't accept entries that are too big */
2969 prefix_len = strlen (field);
2970 if (prefix_len > 255)
2972 value_len = strlen (value);
2973 if (value_len > 255)
2975 data_len = 1 + prefix_len + value_len;
2979 data[0] = prefix_len;
2980 memcpy (&data[1], field, prefix_len);
2981 memcpy (&data[1 + prefix_len], value, value_len);
2983 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
2987 data->has_sdes = TRUE;
2990 /* schedule a BYE packet */
2992 session_bye (RTPSession * sess, ReportData * data)
2994 GstRTCPPacket *packet = &data->packet;
2997 session_start_rtcp (sess, data);
3000 session_sdes (sess, data);
3002 /* add a BYE packet */
3003 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_BYE, packet);
3004 gst_rtcp_packet_bye_add_ssrc (packet, sess->source->ssrc);
3005 if (sess->bye_reason)
3006 gst_rtcp_packet_bye_set_reason (packet, sess->bye_reason);
3008 /* we have a BYE packet now */
3009 data->is_bye = TRUE;
3013 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
3015 GstClockTime new_send_time, elapsed;
3017 if (data->is_early && sess->next_early_rtcp_time < current_time)
3020 /* no need to check yet */
3021 if (sess->next_rtcp_check_time > current_time) {
3022 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
3023 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
3024 GST_TIME_ARGS (current_time));
3028 /* get elapsed time since we last reported */
3029 elapsed = current_time - sess->last_rtcp_send_time;
3031 /* perform forward reconsideration */
3032 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, data->interval);
3034 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
3035 GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time), GST_TIME_ARGS (elapsed));
3037 new_send_time += sess->last_rtcp_send_time;
3039 /* check if reconsideration */
3040 if (current_time < new_send_time) {
3041 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
3042 GST_TIME_ARGS (new_send_time));
3043 /* store new check time */
3044 sess->next_rtcp_check_time = new_send_time;
3050 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
3052 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
3053 GST_TIME_ARGS (new_send_time));
3054 sess->next_rtcp_check_time = current_time + new_send_time;
3056 /* Apply the rules from RFC 4585 section 3.5.3 */
3057 if (sess->stats.min_interval != 0 && !sess->first_rtcp) {
3058 GstClockTimeDiff T_rr_current_interval = g_random_double_range (0.5, 1.5) *
3059 sess->stats.min_interval;
3061 /* This will caused the RTCP to be suppressed if no FB packets are added */
3062 if (sess->last_rtcp_send_time + T_rr_current_interval >
3063 sess->next_rtcp_check_time) {
3064 GST_DEBUG ("RTCP packet could be suppressed min: %" GST_TIME_FORMAT
3065 " last: %" GST_TIME_FORMAT
3066 " + T_rr_current_interval: %" GST_TIME_FORMAT
3067 " > sess->next_rtcp_check_time: %" GST_TIME_FORMAT,
3068 GST_TIME_ARGS (sess->stats.min_interval),
3069 GST_TIME_ARGS (sess->last_rtcp_send_time),
3070 GST_TIME_ARGS (T_rr_current_interval),
3071 GST_TIME_ARGS (sess->next_rtcp_check_time));
3072 data->may_suppress = TRUE;
3080 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
3082 g_hash_table_insert (hash_table, key, g_object_ref (source));
3086 remove_closing_sources (const gchar * key, RTPSource * source, gpointer * data)
3088 return source->closing;
3092 * rtp_session_on_timeout:
3093 * @sess: an #RTPSession
3094 * @current_time: the current system time
3095 * @ntpnstime: the current NTP time in nanoseconds
3096 * @running_time: the current running_time of the pipeline
3098 * Perform maintenance actions after the timeout obtained with
3099 * rtp_session_next_timeout() expired.
3101 * This function will perform timeouts of receivers and senders, send a BYE
3102 * packet or generate RTCP packets with current session stats.
3104 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
3105 * times, for each packet that should be processed.
3107 * Returns: a #GstFlowReturn.
3110 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
3111 guint64 ntpnstime, GstClockTime running_time)
3113 GstFlowReturn result = GST_FLOW_OK;
3116 GHashTable *table_copy;
3117 gboolean notify = FALSE;
3119 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3121 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT,
3122 GST_TIME_ARGS (current_time), GST_TIME_ARGS (ntpnstime));
3126 data.current_time = current_time;
3127 data.ntpnstime = ntpnstime;
3128 data.is_bye = FALSE;
3129 data.has_sdes = FALSE;
3130 data.may_suppress = FALSE;
3131 data.running_time = running_time;
3135 RTP_SESSION_LOCK (sess);
3136 /* get a new interval, we need this for various cleanups etc */
3137 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
3139 /* Make a local copy of the hashtable. We need to do this because the
3140 * cleanup stage below releases the session lock. */
3141 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
3142 (GDestroyNotify) g_object_unref);
3143 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3144 (GHFunc) clone_ssrcs_hashtable, table_copy);
3146 /* Clean up the session, mark the source for removing, this might release the
3148 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
3149 g_hash_table_destroy (table_copy);
3151 /* Now remove the marked sources */
3152 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
3153 (GHRFunc) remove_closing_sources, NULL);
3155 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3156 data.is_early = TRUE;
3158 data.is_early = FALSE;
3160 /* see if we need to generate SR or RR packets */
3161 if (is_rtcp_time (sess, current_time, &data)) {
3162 if (own->received_bye) {
3163 /* generate BYE instead */
3164 GST_DEBUG ("generating BYE message");
3165 session_bye (sess, &data);
3166 sess->sent_bye = TRUE;
3168 /* loop over all known sources and do something */
3169 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3170 (GHFunc) session_report_blocks, &data);
3175 /* we keep track of the last report time in order to timeout inactive
3176 * receivers or senders */
3177 if (!data.is_early && !data.may_suppress)
3178 sess->last_rtcp_send_time = data.current_time;
3179 sess->first_rtcp = FALSE;
3180 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
3182 /* add SDES for this source when not already added */
3184 session_sdes (sess, &data);
3187 /* check for outdated collisions */
3188 GST_DEBUG ("Timing out collisions");
3189 rtp_source_timeout (sess->source, current_time,
3190 /* "a relatively long time" -- RFC 3550 section 8.2 */
3191 RTP_STATS_MIN_INTERVAL * GST_SECOND * 10,
3192 running_time - sess->rtcp_feedback_retention_window);
3194 if (sess->change_ssrc) {
3195 GST_DEBUG ("need to change our SSRC (%08x)", own->ssrc);
3196 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
3197 GINT_TO_POINTER (own->ssrc));
3199 own->ssrc = rtp_session_create_new_ssrc (sess);
3200 rtp_source_reset (own);
3202 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
3203 GINT_TO_POINTER (own->ssrc), own);
3205 g_free (sess->bye_reason);
3206 sess->bye_reason = NULL;
3207 sess->sent_bye = FALSE;
3208 sess->change_ssrc = FALSE;
3210 GST_DEBUG ("changed our SSRC to %08x", own->ssrc);
3213 sess->allow_early = TRUE;
3215 RTP_SESSION_UNLOCK (sess);
3218 g_object_notify (G_OBJECT (sess), "internal-ssrc");
3220 /* push out the RTCP packet */
3222 gboolean do_not_suppress;
3224 /* Give the user a change to add its own packet */
3225 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
3226 data.rtcp, data.is_early, &do_not_suppress);
3228 if (sess->callbacks.send_rtcp && (do_not_suppress || !data.may_suppress)) {
3231 /* close the RTCP packet */
3232 gst_rtcp_buffer_end (data.rtcp);
3234 packet_size = GST_BUFFER_SIZE (data.rtcp) + sess->header_len;
3236 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
3237 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
3238 sess->stats.avg_rtcp_packet_size, packet_size);
3240 sess->callbacks.send_rtcp (sess, own, data.rtcp, sess->sent_bye,
3241 sess->send_rtcp_user_data);
3243 GST_DEBUG ("freeing packet callback: %p"
3244 " do_not_suppress: %d may_suppress: %d",
3245 sess->callbacks.send_rtcp, do_not_suppress, data.may_suppress);
3246 gst_buffer_unref (data.rtcp);
3254 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
3255 GstClockTimeDiff max_delay)
3257 GstClockTime T_dither_max;
3259 /* Implements the algorithm described in RFC 4585 section 3.5.2 */
3261 RTP_SESSION_LOCK (sess);
3263 /* Check if already requested */
3264 /* RFC 4585 section 3.5.2 step 2 */
3265 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3268 /* Ignore the request a scheduled packet will be in time anyway */
3269 if (current_time + max_delay > sess->next_rtcp_check_time)
3272 /* RFC 4585 section 3.5.2 step 2b */
3273 /* If the total sources is <=2, then there is only us and one peer */
3274 if (sess->total_sources <= 2) {
3277 /* Divide by 2 because l = 0.5 */
3278 T_dither_max = sess->next_rtcp_check_time - sess->last_rtcp_send_time;
3282 /* RFC 4585 section 3.5.2 step 3 */
3283 if (current_time + T_dither_max > sess->next_rtcp_check_time)
3286 /* RFC 4585 section 3.5.2 step 4
3287 * Don't send if allow_early is FALSE, but not if we are in
3288 * immediate mode, meaning we are part of a group of at most the
3289 * application-specific threshold.
3291 if (sess->total_sources > sess->rtcp_immediate_feedback_threshold &&
3292 sess->allow_early == FALSE)
3296 /* Schedule an early transmission later */
3297 sess->next_early_rtcp_time = g_random_double () * T_dither_max +
3300 /* If no dithering, schedule it for NOW */
3301 sess->next_early_rtcp_time = current_time;
3304 RTP_SESSION_UNLOCK (sess);
3306 /* notify app of need to send packet early
3307 * and therefore of timeout change */
3308 if (sess->callbacks.reconsider)
3309 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
3315 RTP_SESSION_UNLOCK (sess);
3319 rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc, GstClockTime now,
3320 gboolean fir, gint count)
3322 RTPSource *src = g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
3323 GUINT_TO_POINTER (ssrc));
3329 src->send_pli = FALSE;
3330 src->send_fir = TRUE;
3332 if (count == -1 || count != src->last_fir_count)
3333 src->current_send_fir_seqnum++;
3334 src->last_fir_count = count;
3335 } else if (!src->send_fir) {
3336 src->send_pli = TRUE;
3339 rtp_session_request_early_rtcp (sess, now, 200 * GST_MSECOND);
3345 has_pli_compare_func (gconstpointer a, gconstpointer ignored)
3347 GstRTCPPacket packet;
3349 packet.buffer = (GstBuffer *) a;
3352 if (gst_rtcp_packet_get_type (&packet) == GST_RTCP_TYPE_PSFB &&
3353 gst_rtcp_packet_fb_get_type (&packet) == GST_RTCP_PSFB_TYPE_PLI)
3360 rtp_session_on_sending_rtcp (RTPSession * sess, GstBuffer * buffer,
3363 gboolean ret = FALSE;
3364 GHashTableIter iter;
3365 gpointer key, value;
3366 gboolean started_fir = FALSE;
3367 GstRTCPPacket fir_rtcppacket;
3369 RTP_SESSION_LOCK (sess);
3371 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
3372 while (g_hash_table_iter_next (&iter, &key, &value)) {
3373 guint media_ssrc = GPOINTER_TO_UINT (key);
3374 RTPSource *media_src = value;
3377 if (media_src->send_fir) {
3379 if (!gst_rtcp_buffer_add_packet (buffer, GST_RTCP_TYPE_PSFB,
3382 gst_rtcp_packet_fb_set_type (&fir_rtcppacket, GST_RTCP_PSFB_TYPE_FIR);
3383 gst_rtcp_packet_fb_set_sender_ssrc (&fir_rtcppacket,
3384 rtp_source_get_ssrc (sess->source));
3385 gst_rtcp_packet_fb_set_media_ssrc (&fir_rtcppacket, 0);
3387 if (!gst_rtcp_packet_fb_set_fci_length (&fir_rtcppacket, 2)) {
3388 gst_rtcp_packet_remove (&fir_rtcppacket);
3394 if (!gst_rtcp_packet_fb_set_fci_length (&fir_rtcppacket,
3395 !gst_rtcp_packet_fb_get_fci_length (&fir_rtcppacket) + 2))
3399 fci_data = gst_rtcp_packet_fb_get_fci (&fir_rtcppacket) -
3400 ((gst_rtcp_packet_fb_get_fci_length (&fir_rtcppacket) - 2) * 4);
3402 GST_WRITE_UINT32_BE (fci_data, media_ssrc);
3404 fci_data[0] = media_src->current_send_fir_seqnum;
3405 fci_data[1] = fci_data[2] = fci_data[3] = 0;
3406 media_src->send_fir = FALSE;
3410 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
3411 while (g_hash_table_iter_next (&iter, &key, &value)) {
3412 guint media_ssrc = GPOINTER_TO_UINT (key);
3413 RTPSource *media_src = value;
3414 GstRTCPPacket pli_rtcppacket;
3416 if (media_src->send_pli && !rtp_source_has_retained (media_src,
3417 has_pli_compare_func, NULL)) {
3418 if (gst_rtcp_buffer_add_packet (buffer, GST_RTCP_TYPE_PSFB,
3420 gst_rtcp_packet_fb_set_type (&pli_rtcppacket, GST_RTCP_PSFB_TYPE_PLI);
3421 gst_rtcp_packet_fb_set_sender_ssrc (&pli_rtcppacket,
3422 rtp_source_get_ssrc (sess->source));
3423 gst_rtcp_packet_fb_set_media_ssrc (&pli_rtcppacket, media_ssrc);
3426 /* Break because the packet is full, will put next request in a
3432 media_src->send_pli = FALSE;
3435 RTP_SESSION_UNLOCK (sess);
3441 rtp_session_send_rtcp (RTPSession * sess, GstClockTimeDiff max_delay)
3445 if (!sess->callbacks.send_rtcp)
3448 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
3450 rtp_session_request_early_rtcp (sess, now, max_delay);