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.
22 #include <gst/rtp/gstrtpbuffer.h>
23 #include <gst/rtp/gstrtcpbuffer.h>
24 #include <gst/netbuffer/gstnetbuffer.h>
26 #include "gstrtpbin-marshal.h"
27 #include "rtpsession.h"
29 GST_DEBUG_CATEGORY_STATIC (rtp_session_debug);
30 #define GST_CAT_DEFAULT rtp_session_debug
32 /* signals and args */
35 SIGNAL_GET_SOURCE_BY_SSRC,
37 SIGNAL_ON_SSRC_COLLISION,
38 SIGNAL_ON_SSRC_VALIDATED,
39 SIGNAL_ON_SSRC_ACTIVE,
42 SIGNAL_ON_BYE_TIMEOUT,
44 SIGNAL_ON_SENDER_TIMEOUT,
48 #define DEFAULT_INTERNAL_SOURCE NULL
49 #define DEFAULT_BANDWIDTH RTP_STATS_BANDWIDTH
50 #define DEFAULT_RTCP_FRACTION RTP_STATS_RTCP_BANDWIDTH
51 #define DEFAULT_SDES_CNAME NULL
52 #define DEFAULT_SDES_NAME NULL
53 #define DEFAULT_SDES_EMAIL NULL
54 #define DEFAULT_SDES_PHONE NULL
55 #define DEFAULT_SDES_LOCATION NULL
56 #define DEFAULT_SDES_TOOL NULL
57 #define DEFAULT_SDES_NOTE NULL
58 #define DEFAULT_NUM_SOURCES 0
59 #define DEFAULT_NUM_ACTIVE_SOURCES 0
60 #define DEFAULT_SOURCES NULL
76 PROP_NUM_ACTIVE_SOURCES,
81 /* update average packet size, we keep this scaled by 16 to keep enough
83 #define UPDATE_AVG(avg, val) \
87 (avg) = ((val) + (15 * (avg))) >> 4;
89 /* The number RTCP intervals after which to timeout entries in the
92 #define RTCP_INTERVAL_COLLISION_TIMEOUT 10
94 /* GObject vmethods */
95 static void rtp_session_finalize (GObject * object);
96 static void rtp_session_set_property (GObject * object, guint prop_id,
97 const GValue * value, GParamSpec * pspec);
98 static void rtp_session_get_property (GObject * object, guint prop_id,
99 GValue * value, GParamSpec * pspec);
101 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
103 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
105 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
106 gboolean * created, RTPArrivalStats * arrival, gboolean rtp);
107 static GstFlowReturn rtp_session_send_bye_locked (RTPSession * sess,
108 const gchar * reason, GstClockTime current_time);
109 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
110 gboolean deterministic, gboolean first);
113 rtp_session_class_init (RTPSessionClass * klass)
115 GObjectClass *gobject_class;
117 gobject_class = (GObjectClass *) klass;
119 gobject_class->finalize = rtp_session_finalize;
120 gobject_class->set_property = rtp_session_set_property;
121 gobject_class->get_property = rtp_session_get_property;
124 * RTPSession::get-source-by-ssrc:
125 * @session: the object which received the signal
126 * @ssrc: the SSRC of the RTPSource
128 * Request the #RTPSource object with SSRC @ssrc in @session.
130 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
131 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
132 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
133 get_source_by_ssrc), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
134 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
137 * RTPSession::on-new-ssrc:
138 * @session: the object which received the signal
139 * @src: the new RTPSource
141 * Notify of a new SSRC that entered @session.
143 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
144 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
145 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
146 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
149 * RTPSession::on-ssrc-collision:
150 * @session: the object which received the signal
151 * @src: the #RTPSource that caused a collision
153 * Notify when we have an SSRC collision
155 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
156 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
157 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
158 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
161 * RTPSession::on-ssrc-validated:
162 * @session: the object which received the signal
163 * @src: the new validated RTPSource
165 * Notify of a new SSRC that became validated.
167 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
168 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
169 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
170 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
173 * RTPSession::on-ssrc-active:
174 * @session: the object which received the signal
175 * @src: the active RTPSource
177 * Notify of a SSRC that is active, i.e., sending RTCP.
179 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
180 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
181 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
182 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
185 * RTPSession::on-ssrc-sdes:
186 * @session: the object which received the signal
187 * @src: the RTPSource
189 * Notify that a new SDES was received for SSRC.
191 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
192 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
193 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
194 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
197 * RTPSession::on-bye-ssrc:
198 * @session: the object which received the signal
199 * @src: the RTPSource that went away
201 * Notify of an SSRC that became inactive because of a BYE packet.
203 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
204 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
205 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
206 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
209 * RTPSession::on-bye-timeout:
210 * @session: the object which received the signal
211 * @src: the RTPSource that timed out
213 * Notify of an SSRC that has timed out because of BYE
215 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
216 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
217 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
218 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
221 * RTPSession::on-timeout:
222 * @session: the object which received the signal
223 * @src: the RTPSource that timed out
225 * Notify of an SSRC that has timed out
227 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
228 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
229 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
230 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
233 * RTPSession::on-sender-timeout:
234 * @session: the object which received the signal
235 * @src: the RTPSource that timed out
237 * Notify of an SSRC that was a sender but timed out and became a receiver.
239 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
240 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
241 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
242 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
245 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
246 g_param_spec_object ("internal-source", "Internal Source",
247 "The internal source element of the session",
248 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
250 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
251 g_param_spec_double ("bandwidth", "Bandwidth",
252 "The bandwidth of the session",
253 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
254 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
257 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
258 "The fraction of the bandwidth used for RTCP",
259 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
260 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
262 g_object_class_install_property (gobject_class, PROP_SDES_CNAME,
263 g_param_spec_string ("sdes-cname", "SDES CNAME",
264 "The CNAME to put in SDES messages of this session",
265 DEFAULT_SDES_CNAME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
267 g_object_class_install_property (gobject_class, PROP_SDES_NAME,
268 g_param_spec_string ("sdes-name", "SDES NAME",
269 "The NAME to put in SDES messages of this session",
270 DEFAULT_SDES_NAME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
272 g_object_class_install_property (gobject_class, PROP_SDES_EMAIL,
273 g_param_spec_string ("sdes-email", "SDES EMAIL",
274 "The EMAIL to put in SDES messages of this session",
275 DEFAULT_SDES_EMAIL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
277 g_object_class_install_property (gobject_class, PROP_SDES_PHONE,
278 g_param_spec_string ("sdes-phone", "SDES PHONE",
279 "The PHONE to put in SDES messages of this session",
280 DEFAULT_SDES_PHONE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
282 g_object_class_install_property (gobject_class, PROP_SDES_LOCATION,
283 g_param_spec_string ("sdes-location", "SDES LOCATION",
284 "The LOCATION to put in SDES messages of this session",
285 DEFAULT_SDES_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
287 g_object_class_install_property (gobject_class, PROP_SDES_TOOL,
288 g_param_spec_string ("sdes-tool", "SDES TOOL",
289 "The TOOL to put in SDES messages of this session",
290 DEFAULT_SDES_TOOL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
292 g_object_class_install_property (gobject_class, PROP_SDES_NOTE,
293 g_param_spec_string ("sdes-note", "SDES NOTE",
294 "The NOTE to put in SDES messages of this session",
295 DEFAULT_SDES_NOTE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
297 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
298 g_param_spec_uint ("num-sources", "Num Sources",
299 "The number of sources in the session", 0, G_MAXUINT,
300 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
302 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
303 g_param_spec_uint ("num-active-sources", "Num Active Sources",
304 "The number of active sources in the session", 0, G_MAXUINT,
305 DEFAULT_NUM_ACTIVE_SOURCES,
306 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
310 * Get a GValue Array of all sources in the session.
313 * <title>Getting the #RTPSources of a session
320 * g_object_get (sess, "sources", &arr, NULL);
322 * for (i = 0; i < arr->n_values; i++) {
325 * val = g_value_array_get_nth (arr, i);
326 * source = g_value_get_object (val);
328 * g_value_array_free (arr);
333 g_object_class_install_property (gobject_class, PROP_SOURCES,
334 g_param_spec_boxed ("sources", "Sources",
335 "An array of all known sources in the session",
336 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
338 klass->get_source_by_ssrc =
339 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
341 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
345 rtp_session_init (RTPSession * sess)
350 sess->lock = g_mutex_new ();
351 sess->key = g_random_int ();
355 for (i = 0; i < 32; i++) {
357 g_hash_table_new_full (NULL, NULL, NULL,
358 (GDestroyNotify) g_object_unref);
360 sess->cnames = g_hash_table_new_full (NULL, NULL, g_free, NULL);
362 rtp_stats_init_defaults (&sess->stats);
364 /* create an active SSRC for this session manager */
365 sess->source = rtp_session_create_source (sess);
366 sess->source->validated = TRUE;
367 sess->source->internal = TRUE;
368 sess->stats.active_sources++;
370 /* default UDP header length */
371 sess->header_len = 28;
374 /* some default SDES entries */
375 str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
376 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_CNAME, str);
379 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_NAME,
381 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_TOOL, "GStreamer");
383 sess->first_rtcp = TRUE;
385 GST_DEBUG ("%p: session using SSRC: %08x", sess, sess->source->ssrc);
389 rtp_session_finalize (GObject * object)
394 sess = RTP_SESSION_CAST (object);
396 g_mutex_free (sess->lock);
397 for (i = 0; i < 32; i++)
398 g_hash_table_destroy (sess->ssrcs[i]);
400 g_free (sess->bye_reason);
402 g_hash_table_destroy (sess->cnames);
403 g_object_unref (sess->source);
405 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
409 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
411 GValue value = { 0 };
413 g_value_init (&value, RTP_TYPE_SOURCE);
414 g_value_take_object (&value, source);
415 g_value_array_append (arr, &value);
419 rtp_session_create_sources (RTPSession * sess)
424 RTP_SESSION_LOCK (sess);
425 /* get number of elements in the table */
426 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
427 /* create the result value array */
428 res = g_value_array_new (size);
430 /* and copy all values into the array */
431 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
432 RTP_SESSION_UNLOCK (sess);
438 rtp_session_set_property (GObject * object, guint prop_id,
439 const GValue * value, GParamSpec * pspec)
443 sess = RTP_SESSION (object);
447 rtp_session_set_bandwidth (sess, g_value_get_double (value));
449 case PROP_RTCP_FRACTION:
450 rtp_session_set_rtcp_fraction (sess, g_value_get_double (value));
452 case PROP_SDES_CNAME:
453 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_CNAME,
454 g_value_get_string (value));
457 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_NAME,
458 g_value_get_string (value));
460 case PROP_SDES_EMAIL:
461 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_EMAIL,
462 g_value_get_string (value));
464 case PROP_SDES_PHONE:
465 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_PHONE,
466 g_value_get_string (value));
468 case PROP_SDES_LOCATION:
469 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_LOC,
470 g_value_get_string (value));
473 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_TOOL,
474 g_value_get_string (value));
477 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_NOTE,
478 g_value_get_string (value));
481 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
487 rtp_session_get_property (GObject * object, guint prop_id,
488 GValue * value, GParamSpec * pspec)
492 sess = RTP_SESSION (object);
495 case PROP_INTERNAL_SOURCE:
496 g_value_take_object (value, rtp_session_get_internal_source (sess));
499 g_value_set_double (value, rtp_session_get_bandwidth (sess));
501 case PROP_RTCP_FRACTION:
502 g_value_set_double (value, rtp_session_get_rtcp_fraction (sess));
504 case PROP_SDES_CNAME:
505 g_value_take_string (value, rtp_session_get_sdes_string (sess,
506 GST_RTCP_SDES_CNAME));
509 g_value_take_string (value, rtp_session_get_sdes_string (sess,
510 GST_RTCP_SDES_NAME));
512 case PROP_SDES_EMAIL:
513 g_value_take_string (value, rtp_session_get_sdes_string (sess,
514 GST_RTCP_SDES_EMAIL));
516 case PROP_SDES_PHONE:
517 g_value_take_string (value, rtp_session_get_sdes_string (sess,
518 GST_RTCP_SDES_PHONE));
520 case PROP_SDES_LOCATION:
521 g_value_take_string (value, rtp_session_get_sdes_string (sess,
525 g_value_take_string (value, rtp_session_get_sdes_string (sess,
526 GST_RTCP_SDES_TOOL));
529 g_value_take_string (value, rtp_session_get_sdes_string (sess,
530 GST_RTCP_SDES_NOTE));
532 case PROP_NUM_SOURCES:
533 g_value_set_uint (value, rtp_session_get_num_sources (sess));
535 case PROP_NUM_ACTIVE_SOURCES:
536 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
539 g_value_take_boxed (value, rtp_session_create_sources (sess));
542 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
548 on_new_ssrc (RTPSession * sess, RTPSource * source)
550 g_object_ref (source);
551 RTP_SESSION_UNLOCK (sess);
552 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
553 RTP_SESSION_LOCK (sess);
554 g_object_unref (source);
558 on_ssrc_collision (RTPSession * sess, RTPSource * source)
560 g_object_ref (source);
561 RTP_SESSION_UNLOCK (sess);
562 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
564 RTP_SESSION_LOCK (sess);
565 g_object_unref (source);
569 on_ssrc_validated (RTPSession * sess, RTPSource * source)
571 g_object_ref (source);
572 RTP_SESSION_UNLOCK (sess);
573 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
575 RTP_SESSION_LOCK (sess);
576 g_object_unref (source);
580 on_ssrc_active (RTPSession * sess, RTPSource * source)
582 g_object_ref (source);
583 RTP_SESSION_UNLOCK (sess);
584 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
585 RTP_SESSION_LOCK (sess);
586 g_object_unref (source);
590 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
592 g_object_ref (source);
593 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
594 RTP_SESSION_UNLOCK (sess);
595 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
596 RTP_SESSION_LOCK (sess);
597 g_object_unref (source);
601 on_bye_ssrc (RTPSession * sess, RTPSource * source)
603 g_object_ref (source);
604 RTP_SESSION_UNLOCK (sess);
605 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
606 RTP_SESSION_LOCK (sess);
607 g_object_unref (source);
611 on_bye_timeout (RTPSession * sess, RTPSource * source)
613 g_object_ref (source);
614 RTP_SESSION_UNLOCK (sess);
615 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
616 RTP_SESSION_LOCK (sess);
617 g_object_unref (source);
621 on_timeout (RTPSession * sess, RTPSource * source)
623 g_object_ref (source);
624 RTP_SESSION_UNLOCK (sess);
625 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
626 RTP_SESSION_LOCK (sess);
627 g_object_unref (source);
631 on_sender_timeout (RTPSession * sess, RTPSource * source)
633 g_object_ref (source);
634 RTP_SESSION_UNLOCK (sess);
635 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
637 RTP_SESSION_LOCK (sess);
638 g_object_unref (source);
644 * Create a new session object.
646 * Returns: a new #RTPSession. g_object_unref() after usage.
649 rtp_session_new (void)
653 sess = g_object_new (RTP_TYPE_SESSION, NULL);
659 * rtp_session_set_callbacks:
660 * @sess: an #RTPSession
661 * @callbacks: callbacks to configure
662 * @user_data: user data passed in the callbacks
664 * Configure a set of callbacks to be notified of actions.
667 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
670 g_return_if_fail (RTP_IS_SESSION (sess));
672 if (callbacks->process_rtp) {
673 sess->callbacks.process_rtp = callbacks->process_rtp;
674 sess->process_rtp_user_data = user_data;
676 if (callbacks->send_rtp) {
677 sess->callbacks.send_rtp = callbacks->send_rtp;
678 sess->send_rtp_user_data = user_data;
680 if (callbacks->send_rtcp) {
681 sess->callbacks.send_rtcp = callbacks->send_rtcp;
682 sess->send_rtcp_user_data = user_data;
684 if (callbacks->sync_rtcp) {
685 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
686 sess->sync_rtcp_user_data = user_data;
688 if (callbacks->clock_rate) {
689 sess->callbacks.clock_rate = callbacks->clock_rate;
690 sess->clock_rate_user_data = user_data;
692 if (callbacks->reconsider) {
693 sess->callbacks.reconsider = callbacks->reconsider;
694 sess->reconsider_user_data = user_data;
699 * rtp_session_set_process_rtp_callback:
700 * @sess: an #RTPSession
701 * @callback: callback to set
702 * @user_data: user data passed in the callback
704 * Configure only the process_rtp callback to be notified of the process_rtp action.
707 rtp_session_set_process_rtp_callback (RTPSession * sess,
708 RTPSessionProcessRTP callback, gpointer user_data)
710 g_return_if_fail (RTP_IS_SESSION (sess));
712 sess->callbacks.process_rtp = callback;
713 sess->process_rtp_user_data = user_data;
717 * rtp_session_set_send_rtp_callback:
718 * @sess: an #RTPSession
719 * @callback: callback to set
720 * @user_data: user data passed in the callback
722 * Configure only the send_rtp callback to be notified of the send_rtp action.
725 rtp_session_set_send_rtp_callback (RTPSession * sess,
726 RTPSessionSendRTP callback, gpointer user_data)
728 g_return_if_fail (RTP_IS_SESSION (sess));
730 sess->callbacks.send_rtp = callback;
731 sess->send_rtp_user_data = user_data;
735 * rtp_session_set_send_rtcp_callback:
736 * @sess: an #RTPSession
737 * @callback: callback to set
738 * @user_data: user data passed in the callback
740 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
743 rtp_session_set_send_rtcp_callback (RTPSession * sess,
744 RTPSessionSendRTCP callback, gpointer user_data)
746 g_return_if_fail (RTP_IS_SESSION (sess));
748 sess->callbacks.send_rtcp = callback;
749 sess->send_rtcp_user_data = user_data;
753 * rtp_session_set_sync_rtcp_callback:
754 * @sess: an #RTPSession
755 * @callback: callback to set
756 * @user_data: user data passed in the callback
758 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
761 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
762 RTPSessionSyncRTCP callback, gpointer user_data)
764 g_return_if_fail (RTP_IS_SESSION (sess));
766 sess->callbacks.sync_rtcp = callback;
767 sess->sync_rtcp_user_data = user_data;
771 * rtp_session_set_clock_rate_callback:
772 * @sess: an #RTPSession
773 * @callback: callback to set
774 * @user_data: user data passed in the callback
776 * Configure only the clock_rate callback to be notified of the clock_rate action.
779 rtp_session_set_clock_rate_callback (RTPSession * sess,
780 RTPSessionClockRate callback, gpointer user_data)
782 g_return_if_fail (RTP_IS_SESSION (sess));
784 sess->callbacks.clock_rate = callback;
785 sess->clock_rate_user_data = user_data;
789 * rtp_session_set_reconsider_callback:
790 * @sess: an #RTPSession
791 * @callback: callback to set
792 * @user_data: user data passed in the callback
794 * Configure only the reconsider callback to be notified of the reconsider action.
797 rtp_session_set_reconsider_callback (RTPSession * sess,
798 RTPSessionReconsider callback, gpointer user_data)
800 g_return_if_fail (RTP_IS_SESSION (sess));
802 sess->callbacks.reconsider = callback;
803 sess->reconsider_user_data = user_data;
807 * rtp_session_set_bandwidth:
808 * @sess: an #RTPSession
809 * @bandwidth: the bandwidth allocated
811 * Set the session bandwidth in bytes per second.
814 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
816 g_return_if_fail (RTP_IS_SESSION (sess));
818 RTP_SESSION_LOCK (sess);
819 sess->stats.bandwidth = bandwidth;
820 RTP_SESSION_UNLOCK (sess);
824 * rtp_session_get_bandwidth:
825 * @sess: an #RTPSession
827 * Get the session bandwidth.
829 * Returns: the session bandwidth.
832 rtp_session_get_bandwidth (RTPSession * sess)
836 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
838 RTP_SESSION_LOCK (sess);
839 result = sess->stats.bandwidth;
840 RTP_SESSION_UNLOCK (sess);
846 * rtp_session_set_rtcp_fraction:
847 * @sess: an #RTPSession
848 * @bandwidth: the RTCP bandwidth
850 * Set the bandwidth that should be used for RTCP
854 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
856 g_return_if_fail (RTP_IS_SESSION (sess));
858 RTP_SESSION_LOCK (sess);
859 sess->stats.rtcp_bandwidth = bandwidth;
860 RTP_SESSION_UNLOCK (sess);
864 * rtp_session_get_rtcp_fraction:
865 * @sess: an #RTPSession
867 * Get the session bandwidth used for RTCP.
869 * Returns: The bandwidth used for RTCP messages.
872 rtp_session_get_rtcp_fraction (RTPSession * sess)
876 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
878 RTP_SESSION_LOCK (sess);
879 result = sess->stats.rtcp_bandwidth;
880 RTP_SESSION_UNLOCK (sess);
886 * rtp_session_set_sdes_string:
887 * @sess: an #RTPSession
888 * @type: the type of the SDES item
889 * @item: a null-terminated string to set.
891 * Store an SDES item of @type in @sess.
893 * Returns: %FALSE if the data was unchanged @type is invalid.
896 rtp_session_set_sdes_string (RTPSession * sess, GstRTCPSDESType type,
901 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
903 RTP_SESSION_LOCK (sess);
904 result = rtp_source_set_sdes_string (sess->source, type, item);
905 RTP_SESSION_UNLOCK (sess);
911 * rtp_session_get_sdes_string:
912 * @sess: an #RTPSession
913 * @type: the type of the SDES item
915 * Get the SDES item of @type from @sess.
917 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
918 * valid. g_free() after usage.
921 rtp_session_get_sdes_string (RTPSession * sess, GstRTCPSDESType type)
925 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
927 RTP_SESSION_LOCK (sess);
928 result = rtp_source_get_sdes_string (sess->source, type);
929 RTP_SESSION_UNLOCK (sess);
935 source_push_rtp (RTPSource * source, GstBuffer * buffer, RTPSession * session)
937 GstFlowReturn result = GST_FLOW_OK;
939 if (source == session->source) {
940 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
942 RTP_SESSION_UNLOCK (session);
944 if (session->callbacks.send_rtp)
946 session->callbacks.send_rtp (session, source, buffer,
947 session->send_rtp_user_data);
949 gst_buffer_unref (buffer);
952 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
953 RTP_SESSION_UNLOCK (session);
955 if (session->callbacks.process_rtp)
957 session->callbacks.process_rtp (session, source, buffer,
958 session->process_rtp_user_data);
960 gst_buffer_unref (buffer);
962 RTP_SESSION_LOCK (session);
968 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
972 RTP_SESSION_UNLOCK (session);
974 if (session->callbacks.clock_rate)
976 session->callbacks.clock_rate (session, pt,
977 session->clock_rate_user_data);
981 RTP_SESSION_LOCK (session);
983 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
988 static RTPSourceCallbacks callbacks = {
989 (RTPSourcePushRTP) source_push_rtp,
990 (RTPSourceClockRate) source_clock_rate,
994 * find_add_conflicting_addresses:
995 * @sess: The session to check in
996 * @arrival: The arrival stats for the buffer
998 * Checks if an address which has a conflict is already known,
999 * otherwise remembers it to prevent loops.
1001 * Returns: TRUE if it was a known conflict, FALSE otherwise
1005 find_add_conflicting_addresses (RTPSession * sess, RTPArrivalStats * arrival)
1008 RTPConflictingAddress *new_conflict;
1010 for (item = g_list_first (sess->conflicting_addresses);
1011 item; item = g_list_next (item)) {
1012 RTPConflictingAddress *known_conflict = item->data;
1014 if (gst_netaddress_equal (&arrival->address, &known_conflict->address)) {
1015 known_conflict->time = arrival->time;
1020 new_conflict = g_new0 (RTPConflictingAddress, 1);
1022 memcpy (&new_conflict->address, &arrival->address, sizeof (GstNetAddress));
1023 new_conflict->time = arrival->time;
1025 sess->conflicting_addresses = g_list_prepend (sess->conflicting_addresses,
1032 check_collision (RTPSession * sess, RTPSource * source,
1033 RTPArrivalStats * arrival, gboolean rtp)
1035 /* If we have no arrival address, we can't do collision checking */
1036 if (!arrival->have_address)
1039 if (sess->source != source) {
1040 /* This is not our local source, but lets check if two remote
1044 if (source->have_rtp_from) {
1045 if (gst_netaddress_equal (&source->rtp_from, &arrival->address))
1046 /* Address is the same */
1049 /* We don't already have a from address for RTP, just set it */
1050 rtp_source_set_rtp_from (source, &arrival->address);
1054 if (source->have_rtcp_from) {
1055 if (gst_netaddress_equal (&source->rtcp_from, &arrival->address))
1056 /* Address is the same */
1059 /* We don't already have a from address for RTCP, just set it */
1060 rtp_source_set_rtcp_from (source, &arrival->address);
1064 /* We received RTP or RTCP from this source before but the network address
1065 * changed. In this case, we have third-party collision or loop */
1066 GST_DEBUG ("we have a third-party collision or loop");
1068 /* FIXME: Log 3rd party collision somehow
1069 * Maybe should be done in upper layer, only the SDES can tell us
1070 * if its a collision or a loop
1073 /* This is sending with our ssrc, is it an address we already know */
1075 if (find_add_conflicting_addresses (sess, arrival)) {
1076 /* Its a known conflict, its probably a loop, not a collision
1077 * lets just drop the incoming packet
1079 GST_DEBUG ("Our packets are being looped back to us, dropping");
1081 /* Its a new collision, lets change our SSRC */
1083 GST_DEBUG ("Collision for SSRC %x", rtp_source_get_ssrc (source));
1084 on_ssrc_collision (sess, source);
1086 rtp_session_send_bye_locked (sess, "SSRC Collision", arrival->time);
1088 sess->change_ssrc = TRUE;
1096 /* must be called with the session lock */
1098 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1099 RTPArrivalStats * arrival, gboolean rtp)
1104 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1105 if (source == NULL) {
1106 /* make new Source in probation and insert */
1107 source = rtp_source_new (ssrc);
1109 /* for RTP packets we need to set the source in probation. Receiving RTCP
1110 * packets of an SSRC, on the other hand, is a strong indication that we
1111 * are dealing with a valid source. */
1113 source->probation = RTP_DEFAULT_PROBATION;
1115 source->probation = 0;
1117 /* store from address, if any */
1118 if (arrival->have_address) {
1120 rtp_source_set_rtp_from (source, &arrival->address);
1122 rtp_source_set_rtcp_from (source, &arrival->address);
1125 /* configure a callback on the source */
1126 rtp_source_set_callbacks (source, &callbacks, sess);
1128 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1131 /* we have one more source now */
1132 sess->total_sources++;
1136 /* check for collision, this updates the address when not previously set */
1137 if (check_collision (sess, source, arrival, rtp)) {
1141 /* update last activity */
1142 source->last_activity = arrival->time;
1144 source->last_rtp_activity = arrival->time;
1150 * rtp_session_get_internal_source:
1151 * @sess: a #RTPSession
1153 * Get the internal #RTPSource of @sess.
1155 * Returns: The internal #RTPSource. g_object_unref() after usage.
1158 rtp_session_get_internal_source (RTPSession * sess)
1162 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1164 result = g_object_ref (sess->source);
1170 * rtp_session_set_internal_ssrc:
1171 * @sess: a #RTPSession
1174 * Set the SSRC of @sess to @ssrc.
1177 rtp_session_set_internal_ssrc (RTPSession * sess, guint32 ssrc)
1179 RTP_SESSION_LOCK (sess);
1180 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
1181 GINT_TO_POINTER (sess->source->ssrc));
1183 sess->source->ssrc = ssrc;
1184 rtp_source_reset (sess->source);
1186 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1187 GINT_TO_POINTER (sess->source->ssrc), sess->source);
1188 RTP_SESSION_UNLOCK (sess);
1192 * rtp_session_get_internal_ssrc:
1193 * @sess: a #RTPSession
1195 * Get the internal SSRC of @sess.
1197 * Returns: The SSRC of the session.
1200 rtp_session_get_internal_ssrc (RTPSession * sess)
1204 RTP_SESSION_LOCK (sess);
1205 ssrc = sess->source->ssrc;
1206 RTP_SESSION_UNLOCK (sess);
1212 * rtp_session_add_source:
1213 * @sess: a #RTPSession
1214 * @src: #RTPSource to add
1216 * Add @src to @session.
1218 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1219 * existed in the session.
1222 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1224 gboolean result = FALSE;
1227 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1228 g_return_val_if_fail (src != NULL, FALSE);
1230 RTP_SESSION_LOCK (sess);
1232 g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1233 GINT_TO_POINTER (src->ssrc));
1235 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1236 GINT_TO_POINTER (src->ssrc), src);
1237 /* we have one more source now */
1238 sess->total_sources++;
1241 RTP_SESSION_UNLOCK (sess);
1247 * rtp_session_get_num_sources:
1248 * @sess: an #RTPSession
1250 * Get the number of sources in @sess.
1252 * Returns: The number of sources in @sess.
1255 rtp_session_get_num_sources (RTPSession * sess)
1259 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1261 RTP_SESSION_LOCK (sess);
1262 result = sess->total_sources;
1263 RTP_SESSION_UNLOCK (sess);
1269 * rtp_session_get_num_active_sources:
1270 * @sess: an #RTPSession
1272 * Get the number of active sources in @sess. A source is considered active when
1273 * it has been validated and has not yet received a BYE RTCP message.
1275 * Returns: The number of active sources in @sess.
1278 rtp_session_get_num_active_sources (RTPSession * sess)
1282 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1284 RTP_SESSION_LOCK (sess);
1285 result = sess->stats.active_sources;
1286 RTP_SESSION_UNLOCK (sess);
1292 * rtp_session_get_source_by_ssrc:
1293 * @sess: an #RTPSession
1296 * Find the source with @ssrc in @sess.
1298 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1299 * g_object_unref() after usage.
1302 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1306 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1308 RTP_SESSION_LOCK (sess);
1310 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1312 g_object_ref (result);
1313 RTP_SESSION_UNLOCK (sess);
1319 * rtp_session_get_source_by_cname:
1320 * @sess: a #RTPSession
1323 * Find the source with @cname in @sess.
1325 * Returns: a #RTPSource with CNAME @cname or NULL if the source was not found.
1326 * g_object_unref() after usage.
1329 rtp_session_get_source_by_cname (RTPSession * sess, const gchar * cname)
1333 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1334 g_return_val_if_fail (cname != NULL, NULL);
1336 RTP_SESSION_LOCK (sess);
1337 result = g_hash_table_lookup (sess->cnames, cname);
1339 g_object_ref (result);
1340 RTP_SESSION_UNLOCK (sess);
1346 rtp_session_create_new_ssrc (RTPSession * sess)
1351 ssrc = g_random_int ();
1353 /* see if it exists in the session, we're done if it doesn't */
1354 if (g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1355 GINT_TO_POINTER (ssrc)) == NULL)
1364 * rtp_session_create_source:
1365 * @sess: an #RTPSession
1367 * Create an #RTPSource for use in @sess. This function will create a source
1368 * with an ssrc that is currently not used by any participants in the session.
1370 * Returns: an #RTPSource.
1373 rtp_session_create_source (RTPSession * sess)
1378 RTP_SESSION_LOCK (sess);
1379 ssrc = rtp_session_create_new_ssrc (sess);
1380 source = rtp_source_new (ssrc);
1381 g_object_ref (source);
1382 rtp_source_set_callbacks (source, &callbacks, sess);
1383 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1385 /* we have one more source now */
1386 sess->total_sources++;
1387 RTP_SESSION_UNLOCK (sess);
1392 /* update the RTPArrivalStats structure with the current time and other bits
1393 * about the current buffer we are handling.
1394 * This function is typically called when a validated packet is received.
1395 * This function should be called with the SESSION_LOCK
1398 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
1399 gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
1400 GstClockTime running_time, guint64 ntpnstime)
1402 /* get time of arrival */
1403 arrival->time = current_time;
1404 arrival->running_time = running_time;
1405 arrival->ntpnstime = ntpnstime;
1407 /* get packet size including header overhead */
1408 arrival->bytes = GST_BUFFER_SIZE (buffer) + sess->header_len;
1411 arrival->payload_len = gst_rtp_buffer_get_payload_len (buffer);
1413 arrival->payload_len = 0;
1416 /* for netbuffer we can store the IP address to check for collisions */
1417 arrival->have_address = GST_IS_NETBUFFER (buffer);
1418 if (arrival->have_address) {
1419 GstNetBuffer *netbuf = (GstNetBuffer *) buffer;
1421 memcpy (&arrival->address, &netbuf->from, sizeof (GstNetAddress));
1426 * rtp_session_process_rtp:
1427 * @sess: and #RTPSession
1428 * @buffer: an RTP buffer
1429 * @current_time: the current system time
1430 * @ntpnstime: the NTP arrival time in nanoseconds
1432 * Process an RTP buffer in the session manager. This function takes ownership
1435 * Returns: a #GstFlowReturn.
1438 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1439 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
1441 GstFlowReturn result;
1445 gboolean prevsender, prevactive;
1446 RTPArrivalStats arrival;
1448 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1449 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1451 if (!gst_rtp_buffer_validate (buffer))
1452 goto invalid_packet;
1454 RTP_SESSION_LOCK (sess);
1455 /* update arrival stats */
1456 update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1457 running_time, ntpnstime);
1459 /* ignore more RTP packets when we left the session */
1460 if (sess->source->received_bye)
1463 /* get SSRC and look up in session database */
1464 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1465 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1470 prevsender = RTP_SOURCE_IS_SENDER (source);
1471 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1473 /* we need to ref so that we can process the CSRCs later */
1474 gst_buffer_ref (buffer);
1476 /* let source process the packet */
1477 result = rtp_source_process_rtp (source, buffer, &arrival);
1479 /* source became active */
1480 if (prevactive != RTP_SOURCE_IS_ACTIVE (source)) {
1481 sess->stats.active_sources++;
1482 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1483 sess->stats.active_sources);
1484 on_ssrc_validated (sess, source);
1486 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1487 sess->stats.sender_sources++;
1488 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1489 sess->stats.sender_sources);
1493 on_new_ssrc (sess, source);
1495 if (source->validated) {
1499 /* for validated sources, we add the CSRCs as well */
1500 count = gst_rtp_buffer_get_csrc_count (buffer);
1502 for (i = 0; i < count; i++) {
1504 RTPSource *csrc_src;
1506 csrc = gst_rtp_buffer_get_csrc (buffer, i);
1509 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1512 GST_DEBUG ("created new CSRC: %08x", csrc);
1513 rtp_source_set_as_csrc (csrc_src);
1514 if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1515 sess->stats.active_sources++;
1516 on_new_ssrc (sess, source);
1520 gst_buffer_unref (buffer);
1522 RTP_SESSION_UNLOCK (sess);
1529 gst_buffer_unref (buffer);
1530 GST_DEBUG ("invalid RTP packet received");
1535 gst_buffer_unref (buffer);
1536 RTP_SESSION_UNLOCK (sess);
1537 GST_DEBUG ("ignoring RTP packet because we are leaving");
1542 gst_buffer_unref (buffer);
1543 RTP_SESSION_UNLOCK (sess);
1544 GST_DEBUG ("ignoring packet because its collisioning");
1550 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1551 GstRTCPPacket * packet, RTPArrivalStats * arrival)
1555 count = gst_rtcp_packet_get_rb_count (packet);
1556 for (i = 0; i < count; i++) {
1557 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1558 guint8 fractionlost;
1561 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1562 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1564 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1566 if (ssrc == sess->source->ssrc) {
1567 /* only deal with report blocks for our session, we update the stats of
1568 * the sender of the RTCP message. We could also compare our stats against
1569 * the other sender to see if we are better or worse. */
1570 rtp_source_process_rb (source, arrival->time, fractionlost, packetslost,
1571 exthighestseq, jitter, lsr, dlsr);
1573 on_ssrc_active (sess, source);
1578 /* A Sender report contains statistics about how the sender is doing. This
1579 * includes timing informataion such as the relation between RTP and NTP
1580 * timestamps and the number of packets/bytes it sent to us.
1582 * In this report is also included a set of report blocks related to how this
1583 * sender is receiving data (in case we (or somebody else) is also sending stuff
1584 * to it). This info includes the packet loss, jitter and seqnum. It also
1585 * contains information to calculate the round trip time (LSR/DLSR).
1588 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1589 RTPArrivalStats * arrival)
1591 guint32 senderssrc, rtptime, packet_count, octet_count;
1594 gboolean created, prevsender;
1596 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1597 &packet_count, &octet_count);
1599 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1600 senderssrc, GST_TIME_ARGS (arrival->time));
1602 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1607 prevsender = RTP_SOURCE_IS_SENDER (source);
1609 /* first update the source */
1610 rtp_source_process_sr (source, arrival->time, ntptime, rtptime, packet_count,
1613 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1614 sess->stats.sender_sources++;
1615 GST_DEBUG ("source: %08x became sender, %d sender sources", senderssrc,
1616 sess->stats.sender_sources);
1620 on_new_ssrc (sess, source);
1622 rtp_session_process_rb (sess, source, packet, arrival);
1625 /* A receiver report contains statistics about how a receiver is doing. It
1626 * includes stuff like packet loss, jitter and the seqnum it received last. It
1627 * also contains info to calculate the round trip time.
1629 * We are only interested in how the sender of this report is doing wrt to us.
1632 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1633 RTPArrivalStats * arrival)
1639 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1641 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1643 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1649 on_new_ssrc (sess, source);
1651 rtp_session_process_rb (sess, source, packet, arrival);
1654 /* Get SDES items and store them in the SSRC */
1656 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1657 RTPArrivalStats * arrival)
1660 gboolean more_items, more_entries;
1662 items = gst_rtcp_packet_sdes_get_item_count (packet);
1663 GST_DEBUG ("got SDES packet with %d items", items);
1665 more_items = gst_rtcp_packet_sdes_first_item (packet);
1667 while (more_items) {
1669 gboolean changed, created;
1672 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1674 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1676 /* find src, no probation when dealing with RTCP */
1677 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1683 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1685 while (more_entries) {
1686 GstRTCPSDESType type;
1690 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1692 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1695 changed |= rtp_source_set_sdes (source, type, data, len);
1697 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1701 source->validated = TRUE;
1704 on_new_ssrc (sess, source);
1706 on_ssrc_sdes (sess, source);
1708 more_items = gst_rtcp_packet_sdes_next_item (packet);
1713 /* BYE is sent when a client leaves the session
1716 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
1717 RTPArrivalStats * arrival)
1722 reason = gst_rtcp_packet_bye_get_reason (packet);
1723 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
1725 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
1726 for (i = 0; i < count; i++) {
1729 gboolean created, prevactive, prevsender;
1730 guint pmembers, members;
1732 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
1733 GST_DEBUG ("SSRC: %08x", ssrc);
1735 /* find src and mark bye, no probation when dealing with RTCP */
1736 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1741 /* store time for when we need to time out this source */
1742 source->bye_time = arrival->time;
1744 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1745 prevsender = RTP_SOURCE_IS_SENDER (source);
1747 /* let the source handle the rest */
1748 rtp_source_process_bye (source, reason);
1750 pmembers = sess->stats.active_sources;
1752 if (prevactive && !RTP_SOURCE_IS_ACTIVE (source)) {
1753 sess->stats.active_sources--;
1754 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
1755 sess->stats.active_sources);
1757 if (prevsender && !RTP_SOURCE_IS_SENDER (source)) {
1758 sess->stats.sender_sources--;
1759 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
1760 sess->stats.sender_sources);
1762 members = sess->stats.active_sources;
1764 if (!sess->source->received_bye && members < pmembers) {
1765 /* some members went away since the previous timeout estimate.
1766 * Perform reverse reconsideration but only when we are not scheduling a
1768 if (arrival->time < sess->next_rtcp_check_time) {
1769 GstClockTime time_remaining;
1771 time_remaining = sess->next_rtcp_check_time - arrival->time;
1772 sess->next_rtcp_check_time =
1773 gst_util_uint64_scale (time_remaining, members, pmembers);
1775 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
1776 GST_TIME_ARGS (sess->next_rtcp_check_time));
1778 sess->next_rtcp_check_time += arrival->time;
1780 RTP_SESSION_UNLOCK (sess);
1781 /* notify app of reconsideration */
1782 if (sess->callbacks.reconsider)
1783 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
1784 RTP_SESSION_LOCK (sess);
1789 on_new_ssrc (sess, source);
1791 on_bye_ssrc (sess, source);
1797 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
1798 RTPArrivalStats * arrival)
1800 GST_DEBUG ("received APP");
1804 * rtp_session_process_rtcp:
1805 * @sess: and #RTPSession
1806 * @buffer: an RTCP buffer
1807 * @current_time: the current system time
1809 * Process an RTCP buffer in the session manager. This function takes ownership
1812 * Returns: a #GstFlowReturn.
1815 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
1816 GstClockTime current_time)
1818 GstRTCPPacket packet;
1819 gboolean more, is_bye = FALSE, is_sr = FALSE;
1820 RTPArrivalStats arrival;
1821 GstFlowReturn result = GST_FLOW_OK;
1823 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1824 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1826 if (!gst_rtcp_buffer_validate (buffer))
1827 goto invalid_packet;
1829 GST_DEBUG ("received RTCP packet");
1831 RTP_SESSION_LOCK (sess);
1832 /* update arrival stats */
1833 update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1, -1);
1838 /* make writable, we might want to change the buffer */
1839 buffer = gst_buffer_make_metadata_writable (buffer);
1841 /* start processing the compound packet */
1842 more = gst_rtcp_buffer_get_first_packet (buffer, &packet);
1846 type = gst_rtcp_packet_get_type (&packet);
1848 /* when we are leaving the session, we should ignore all non-BYE messages */
1849 if (sess->source->received_bye && type != GST_RTCP_TYPE_BYE) {
1850 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
1855 case GST_RTCP_TYPE_SR:
1856 rtp_session_process_sr (sess, &packet, &arrival);
1859 case GST_RTCP_TYPE_RR:
1860 rtp_session_process_rr (sess, &packet, &arrival);
1862 case GST_RTCP_TYPE_SDES:
1863 rtp_session_process_sdes (sess, &packet, &arrival);
1865 case GST_RTCP_TYPE_BYE:
1867 rtp_session_process_bye (sess, &packet, &arrival);
1869 case GST_RTCP_TYPE_APP:
1870 rtp_session_process_app (sess, &packet, &arrival);
1873 GST_WARNING ("got unknown RTCP packet");
1877 more = gst_rtcp_packet_move_to_next (&packet);
1880 /* if we are scheduling a BYE, we only want to count bye packets, else we
1881 * count everything */
1882 if (sess->source->received_bye) {
1884 sess->stats.bye_members++;
1885 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
1888 /* keep track of average packet size */
1889 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
1891 RTP_SESSION_UNLOCK (sess);
1893 /* notify caller of sr packets in the callback */
1894 if (is_sr && sess->callbacks.sync_rtcp)
1895 result = sess->callbacks.sync_rtcp (sess, sess->source, buffer,
1896 sess->sync_rtcp_user_data);
1898 gst_buffer_unref (buffer);
1905 GST_DEBUG ("invalid RTCP packet received");
1906 gst_buffer_unref (buffer);
1911 gst_buffer_unref (buffer);
1912 RTP_SESSION_UNLOCK (sess);
1913 GST_DEBUG ("ignoring RTP packet because we left");
1919 * rtp_session_send_rtp:
1920 * @sess: an #RTPSession
1921 * @buffer: an RTP buffer
1922 * @current_time: the current system time
1923 * @ntpnstime: the NTP time in nanoseconds of when this buffer was captured.
1924 * This is the buffer timestamp converted to NTP time.
1926 * Send the RTP buffer in the session manager. This function takes ownership of
1929 * Returns: a #GstFlowReturn.
1932 rtp_session_send_rtp (RTPSession * sess, GstBuffer * buffer,
1933 GstClockTime current_time, guint64 ntpnstime)
1935 GstFlowReturn result;
1937 gboolean prevsender;
1939 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1940 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1942 if (!gst_rtp_buffer_validate (buffer))
1943 goto invalid_packet;
1945 GST_LOG ("received RTP packet for sending");
1947 RTP_SESSION_LOCK (sess);
1948 source = sess->source;
1950 /* update last activity */
1951 source->last_rtp_activity = current_time;
1953 prevsender = RTP_SOURCE_IS_SENDER (source);
1955 /* we use our own source to send */
1956 result = rtp_source_send_rtp (source, buffer, ntpnstime);
1958 if (RTP_SOURCE_IS_SENDER (source) && !prevsender)
1959 sess->stats.sender_sources++;
1960 RTP_SESSION_UNLOCK (sess);
1967 gst_buffer_unref (buffer);
1968 GST_DEBUG ("invalid RTP packet received");
1974 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
1977 GstClockTime result;
1979 if (sess->source->received_bye) {
1980 result = rtp_stats_calculate_bye_interval (&sess->stats);
1982 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
1983 RTP_SOURCE_IS_SENDER (sess->source), first);
1986 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
1987 GST_TIME_ARGS (result), first);
1990 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
1992 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
1998 * rtp_session_send_bye_locked:
1999 * @sess: an #RTPSession
2000 * @reason: a reason or NULL
2002 * Stop the current @sess and schedule a BYE message for the other members.
2004 * One must have the session lock to call this function
2006 * Returns: a #GstFlowReturn.
2008 static GstFlowReturn
2009 rtp_session_send_bye_locked (RTPSession * sess, const gchar * reason,
2010 GstClockTime current_time)
2012 GstFlowReturn result = GST_FLOW_OK;
2014 GstClockTime interval;
2016 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2018 source = sess->source;
2020 /* ignore more BYEs */
2021 if (source->received_bye)
2024 /* we have BYE now */
2025 source->received_bye = TRUE;
2026 /* at least one member wants to send a BYE */
2027 g_free (sess->bye_reason);
2028 sess->bye_reason = g_strdup (reason);
2029 sess->stats.avg_rtcp_packet_size = 100;
2030 sess->stats.bye_members = 1;
2031 sess->first_rtcp = TRUE;
2032 sess->sent_bye = FALSE;
2034 /* reschedule transmission */
2035 sess->last_rtcp_send_time = current_time;
2036 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2037 sess->next_rtcp_check_time = current_time + interval;
2039 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2040 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2042 RTP_SESSION_UNLOCK (sess);
2043 /* notify app of reconsideration */
2044 if (sess->callbacks.reconsider)
2045 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2046 RTP_SESSION_LOCK (sess);
2053 * rtp_session_send_bye:
2054 * @sess: an #RTPSession
2055 * @reason: a reason or NULL
2056 * @current_time: the current system time
2058 * Stop the current @sess and schedule a BYE message for the other members.
2060 * One must have the session lock to call this function
2062 * Returns: a #GstFlowReturn.
2065 rtp_session_send_bye (RTPSession * sess, const gchar * reason,
2066 GstClockTime current_time)
2068 GstFlowReturn result = GST_FLOW_OK;
2070 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2072 RTP_SESSION_LOCK (sess);
2073 result = rtp_session_send_bye_locked (sess, reason, current_time);
2074 RTP_SESSION_UNLOCK (sess);
2080 * rtp_session_next_timeout:
2081 * @sess: an #RTPSession
2082 * @current_time: the current system time
2084 * Get the next time we should perform session maintenance tasks.
2086 * Returns: a time when rtp_session_on_timeout() should be called with the
2087 * current system time.
2090 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2092 GstClockTime result;
2094 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2096 RTP_SESSION_LOCK (sess);
2098 result = sess->next_rtcp_check_time;
2100 GST_DEBUG ("current time: %" GST_TIME_FORMAT ", next :%" GST_TIME_FORMAT,
2101 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2103 if (result < current_time) {
2104 GST_DEBUG ("take current time as base");
2105 /* our previous check time expired, start counting from the current time
2107 result = current_time;
2110 if (sess->source->received_bye) {
2111 if (sess->sent_bye) {
2112 GST_DEBUG ("we sent BYE already");
2113 result = GST_CLOCK_TIME_NONE;
2114 } else if (sess->stats.active_sources >= 50) {
2115 GST_DEBUG ("reconsider BYE, more than 50 sources");
2116 /* reconsider BYE if members >= 50 */
2117 result += calculate_rtcp_interval (sess, FALSE, TRUE);
2120 if (sess->first_rtcp) {
2121 GST_DEBUG ("first RTCP packet");
2122 /* we are called for the first time */
2123 result += calculate_rtcp_interval (sess, FALSE, TRUE);
2124 } else if (sess->next_rtcp_check_time < current_time) {
2125 GST_DEBUG ("old check time expired, getting new timeout");
2126 /* get a new timeout when we need to */
2127 result += calculate_rtcp_interval (sess, FALSE, FALSE);
2130 sess->next_rtcp_check_time = result;
2132 GST_DEBUG ("next timeout: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2133 RTP_SESSION_UNLOCK (sess);
2142 GstClockTime current_time;
2144 GstClockTime interval;
2145 GstRTCPPacket packet;
2151 session_start_rtcp (RTPSession * sess, ReportData * data)
2153 GstRTCPPacket *packet = &data->packet;
2154 RTPSource *own = sess->source;
2156 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2158 if (RTP_SOURCE_IS_SENDER (own)) {
2161 guint32 packet_count, octet_count;
2163 /* we are a sender, create SR */
2164 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2165 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SR, packet);
2167 /* get latest stats */
2168 rtp_source_get_new_sr (own, data->ntpnstime, &ntptime, &rtptime,
2169 &packet_count, &octet_count);
2171 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2172 packet_count, octet_count);
2174 /* fill in sender report info */
2175 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2176 ntptime, rtptime, packet_count, octet_count);
2178 /* we are only receiver, create RR */
2179 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2180 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_RR, packet);
2181 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2185 /* construct a Sender or Receiver Report */
2187 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2189 RTPSession *sess = data->sess;
2190 GstRTCPPacket *packet = &data->packet;
2192 /* create a new buffer if needed */
2193 if (data->rtcp == NULL) {
2194 session_start_rtcp (sess, data);
2196 if (gst_rtcp_packet_get_rb_count (packet) < GST_RTCP_MAX_RB_COUNT) {
2197 /* only report about other sender sources */
2198 if (source != sess->source && RTP_SOURCE_IS_SENDER (source)) {
2199 guint8 fractionlost;
2201 guint32 exthighestseq, jitter;
2205 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2206 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2208 /* packet is not yet filled, add report block for this source. */
2209 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2210 exthighestseq, jitter, lsr, dlsr);
2215 /* perform cleanup of sources that timed out */
2217 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2219 gboolean remove = FALSE;
2220 gboolean byetimeout = FALSE;
2221 gboolean sendertimeout = FALSE;
2222 gboolean is_sender, is_active;
2223 RTPSession *sess = data->sess;
2224 GstClockTime interval;
2226 is_sender = RTP_SOURCE_IS_SENDER (source);
2227 is_active = RTP_SOURCE_IS_ACTIVE (source);
2229 /* check for our own source, we don't want to delete our own source. */
2230 if (!(source == sess->source)) {
2231 if (source->received_bye) {
2232 /* if we received a BYE from the source, remove the source after some
2234 if (data->current_time > source->bye_time &&
2235 data->current_time - source->bye_time > sess->stats.bye_timeout) {
2236 GST_DEBUG ("removing BYE source %08x", source->ssrc);
2241 /* sources that were inactive for more than 5 times the deterministic reporting
2242 * interval get timed out. the min timeout is 5 seconds. */
2243 if (data->current_time > source->last_activity) {
2244 interval = MAX (data->interval * 5, 5 * GST_SECOND);
2245 if (data->current_time - source->last_activity > interval) {
2246 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2247 source->ssrc, GST_TIME_ARGS (source->last_activity));
2253 /* senders that did not send for a long time become a receiver, this also
2254 * holds for our own source. */
2256 if (data->current_time > source->last_rtp_activity) {
2257 interval = MAX (data->interval * 2, 5 * GST_SECOND);
2258 if (data->current_time - source->last_rtp_activity > interval) {
2259 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
2260 GST_TIME_FORMAT, source->ssrc,
2261 GST_TIME_ARGS (source->last_rtp_activity));
2262 source->is_sender = FALSE;
2263 sess->stats.sender_sources--;
2264 sendertimeout = TRUE;
2270 sess->total_sources--;
2272 sess->stats.sender_sources--;
2274 sess->stats.active_sources--;
2277 on_bye_timeout (sess, source);
2279 on_timeout (sess, source);
2282 on_sender_timeout (sess, source);
2288 session_sdes (RTPSession * sess, ReportData * data)
2290 GstRTCPPacket *packet = &data->packet;
2294 /* add SDES packet */
2295 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SDES, packet);
2297 gst_rtcp_packet_sdes_add_item (packet, sess->source->ssrc);
2299 rtp_source_get_sdes (sess->source, GST_RTCP_SDES_CNAME, &sdes_data,
2301 gst_rtcp_packet_sdes_add_entry (packet, GST_RTCP_SDES_CNAME, sdes_len,
2304 /* other SDES items must only be added at regular intervals and only when the
2305 * user requests to since it might be a privacy problem */
2307 gst_rtcp_packet_sdes_add_entry (&packet, GST_RTCP_SDES_NAME,
2308 strlen (sess->name), (guint8 *) sess->name);
2309 gst_rtcp_packet_sdes_add_entry (&packet, GST_RTCP_SDES_TOOL,
2310 strlen (sess->tool), (guint8 *) sess->tool);
2313 data->has_sdes = TRUE;
2316 /* schedule a BYE packet */
2318 session_bye (RTPSession * sess, ReportData * data)
2320 GstRTCPPacket *packet = &data->packet;
2323 session_start_rtcp (sess, data);
2326 session_sdes (sess, data);
2328 /* add a BYE packet */
2329 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_BYE, packet);
2330 gst_rtcp_packet_bye_add_ssrc (packet, sess->source->ssrc);
2331 if (sess->bye_reason)
2332 gst_rtcp_packet_bye_set_reason (packet, sess->bye_reason);
2334 /* we have a BYE packet now */
2335 data->is_bye = TRUE;
2339 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
2341 GstClockTime new_send_time, elapsed;
2344 /* no need to check yet */
2345 if (sess->next_rtcp_check_time > current_time) {
2346 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
2347 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
2348 GST_TIME_ARGS (current_time));
2352 /* get elapsed time since we last reported */
2353 elapsed = current_time - sess->last_rtcp_send_time;
2355 /* perform forward reconsideration */
2356 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, data->interval);
2358 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
2359 GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time), GST_TIME_ARGS (elapsed));
2361 new_send_time += sess->last_rtcp_send_time;
2363 /* check if reconsideration */
2364 if (current_time < new_send_time) {
2365 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
2366 GST_TIME_ARGS (new_send_time));
2368 /* store new check time */
2369 sess->next_rtcp_check_time = new_send_time;
2372 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
2374 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
2375 GST_TIME_ARGS (new_send_time));
2376 sess->next_rtcp_check_time = current_time + new_send_time;
2382 * rtp_session_on_timeout:
2383 * @sess: an #RTPSession
2384 * @current_time: the current system time
2385 * @ntpnstime: the current NTP time in nanoseconds
2387 * Perform maintenance actions after the timeout obtained with
2388 * rtp_session_next_timeout() expired.
2390 * This function will perform timeouts of receivers and senders, send a BYE
2391 * packet or generate RTCP packets with current session stats.
2393 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
2394 * times, for each packet that should be processed.
2396 * Returns: a #GstFlowReturn.
2399 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
2402 GstFlowReturn result = GST_FLOW_OK;
2407 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2409 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT,
2410 GST_TIME_ARGS (current_time), GST_TIME_ARGS (ntpnstime));
2414 data.current_time = current_time;
2415 data.ntpnstime = ntpnstime;
2416 data.is_bye = FALSE;
2417 data.has_sdes = FALSE;
2421 RTP_SESSION_LOCK (sess);
2422 /* get a new interval, we need this for various cleanups etc */
2423 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
2425 /* first perform cleanups */
2426 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
2427 (GHRFunc) session_cleanup, &data);
2429 /* see if we need to generate SR or RR packets */
2430 if (is_rtcp_time (sess, current_time, &data)) {
2431 if (own->received_bye) {
2432 /* generate BYE instead */
2433 GST_DEBUG ("generating BYE message");
2434 session_bye (sess, &data);
2435 sess->sent_bye = TRUE;
2437 /* loop over all known sources and do something */
2438 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2439 (GHFunc) session_report_blocks, &data);
2446 /* we keep track of the last report time in order to timeout inactive
2447 * receivers or senders */
2448 sess->last_rtcp_send_time = data.current_time;
2449 sess->first_rtcp = FALSE;
2451 /* add SDES for this source when not already added */
2453 session_sdes (sess, &data);
2455 /* update average RTCP size before sending */
2456 size = GST_BUFFER_SIZE (data.rtcp) + sess->header_len;
2457 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, size);
2460 /* check for outdated collisions */
2461 GST_DEBUG ("checking collision list");
2462 item = g_list_first (sess->conflicting_addresses);
2464 RTPConflictingAddress *known_conflict = item->data;
2465 GList *next_item = g_list_next (item);
2467 if (known_conflict->time < current_time - (data.interval *
2468 RTCP_INTERVAL_COLLISION_TIMEOUT)) {
2469 sess->conflicting_addresses =
2470 g_list_delete_link (sess->conflicting_addresses, item);
2471 GST_DEBUG ("collision %p timed out", known_conflict);
2472 g_free (known_conflict);
2477 if (sess->change_ssrc) {
2478 GST_DEBUG ("need to change our SSRC (%08x)", own->ssrc);
2479 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
2480 GINT_TO_POINTER (own->ssrc));
2482 own->ssrc = rtp_session_create_new_ssrc (sess);
2483 rtp_source_reset (own);
2485 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
2486 GINT_TO_POINTER (own->ssrc), own);
2488 g_free (sess->bye_reason);
2489 sess->bye_reason = NULL;
2490 sess->sent_bye = FALSE;
2491 sess->change_ssrc = FALSE;
2492 GST_DEBUG ("changed our SSRC to %08x", own->ssrc);
2494 RTP_SESSION_UNLOCK (sess);
2496 /* push out the RTCP packet */
2498 /* close the RTCP packet */
2499 gst_rtcp_buffer_end (data.rtcp);
2501 GST_DEBUG ("sending packet");
2502 if (sess->callbacks.send_rtcp)
2503 result = sess->callbacks.send_rtcp (sess, own, data.rtcp,
2504 sess->sent_bye, sess->send_rtcp_user_data);
2506 GST_DEBUG ("freeing packet");
2507 gst_buffer_unref (data.rtcp);