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_RTCP_MTU 1400
52 #define DEFAULT_SDES_CNAME NULL
53 #define DEFAULT_SDES_NAME NULL
54 #define DEFAULT_SDES_EMAIL NULL
55 #define DEFAULT_SDES_PHONE NULL
56 #define DEFAULT_SDES_LOCATION NULL
57 #define DEFAULT_SDES_TOOL NULL
58 #define DEFAULT_SDES_NOTE NULL
59 #define DEFAULT_NUM_SOURCES 0
60 #define DEFAULT_NUM_ACTIVE_SOURCES 0
61 #define DEFAULT_SOURCES NULL
78 PROP_NUM_ACTIVE_SOURCES,
83 /* update average packet size, we keep this scaled by 16 to keep enough
85 #define UPDATE_AVG(avg, val) \
89 (avg) = ((val) + (15 * (avg))) >> 4;
91 /* The number RTCP intervals after which to timeout entries in the
94 #define RTCP_INTERVAL_COLLISION_TIMEOUT 10
96 /* GObject vmethods */
97 static void rtp_session_finalize (GObject * object);
98 static void rtp_session_set_property (GObject * object, guint prop_id,
99 const GValue * value, GParamSpec * pspec);
100 static void rtp_session_get_property (GObject * object, guint prop_id,
101 GValue * value, GParamSpec * pspec);
103 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
105 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
107 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
108 gboolean * created, RTPArrivalStats * arrival, gboolean rtp);
109 static GstFlowReturn rtp_session_send_bye_locked (RTPSession * sess,
110 const gchar * reason, GstClockTime current_time);
111 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
112 gboolean deterministic, gboolean first);
115 rtp_session_class_init (RTPSessionClass * klass)
117 GObjectClass *gobject_class;
119 gobject_class = (GObjectClass *) klass;
121 gobject_class->finalize = rtp_session_finalize;
122 gobject_class->set_property = rtp_session_set_property;
123 gobject_class->get_property = rtp_session_get_property;
126 * RTPSession::get-source-by-ssrc:
127 * @session: the object which received the signal
128 * @ssrc: the SSRC of the RTPSource
130 * Request the #RTPSource object with SSRC @ssrc in @session.
132 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
133 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
134 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
135 get_source_by_ssrc), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
136 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
139 * RTPSession::on-new-ssrc:
140 * @session: the object which received the signal
141 * @src: the new RTPSource
143 * Notify of a new SSRC that entered @session.
145 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
146 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
147 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
148 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
151 * RTPSession::on-ssrc-collision:
152 * @session: the object which received the signal
153 * @src: the #RTPSource that caused a collision
155 * Notify when we have an SSRC collision
157 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
158 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
159 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
160 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
163 * RTPSession::on-ssrc-validated:
164 * @session: the object which received the signal
165 * @src: the new validated RTPSource
167 * Notify of a new SSRC that became validated.
169 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
170 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
171 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
172 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
175 * RTPSession::on-ssrc-active:
176 * @session: the object which received the signal
177 * @src: the active RTPSource
179 * Notify of a SSRC that is active, i.e., sending RTCP.
181 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
182 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
183 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
184 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
187 * RTPSession::on-ssrc-sdes:
188 * @session: the object which received the signal
189 * @src: the RTPSource
191 * Notify that a new SDES was received for SSRC.
193 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
194 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
195 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
196 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
199 * RTPSession::on-bye-ssrc:
200 * @session: the object which received the signal
201 * @src: the RTPSource that went away
203 * Notify of an SSRC that became inactive because of a BYE packet.
205 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
206 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
207 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
208 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
211 * RTPSession::on-bye-timeout:
212 * @session: the object which received the signal
213 * @src: the RTPSource that timed out
215 * Notify of an SSRC that has timed out because of BYE
217 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
218 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
219 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
220 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
223 * RTPSession::on-timeout:
224 * @session: the object which received the signal
225 * @src: the RTPSource that timed out
227 * Notify of an SSRC that has timed out
229 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
230 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
231 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
232 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
235 * RTPSession::on-sender-timeout:
236 * @session: the object which received the signal
237 * @src: the RTPSource that timed out
239 * Notify of an SSRC that was a sender but timed out and became a receiver.
241 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
242 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
243 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
244 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
247 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
248 g_param_spec_object ("internal-source", "Internal Source",
249 "The internal source element of the session",
250 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
252 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
253 g_param_spec_double ("bandwidth", "Bandwidth",
254 "The bandwidth of the session",
255 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
256 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
258 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
259 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
260 "The fraction of the bandwidth used for RTCP",
261 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
262 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
264 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
265 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
266 "The maximum size of the RTCP packets",
267 16, G_MAXINT16, DEFAULT_RTCP_MTU,
268 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
270 g_object_class_install_property (gobject_class, PROP_SDES_CNAME,
271 g_param_spec_string ("sdes-cname", "SDES CNAME",
272 "The CNAME to put in SDES messages of this session",
273 DEFAULT_SDES_CNAME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
275 g_object_class_install_property (gobject_class, PROP_SDES_NAME,
276 g_param_spec_string ("sdes-name", "SDES NAME",
277 "The NAME to put in SDES messages of this session",
278 DEFAULT_SDES_NAME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
280 g_object_class_install_property (gobject_class, PROP_SDES_EMAIL,
281 g_param_spec_string ("sdes-email", "SDES EMAIL",
282 "The EMAIL to put in SDES messages of this session",
283 DEFAULT_SDES_EMAIL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
285 g_object_class_install_property (gobject_class, PROP_SDES_PHONE,
286 g_param_spec_string ("sdes-phone", "SDES PHONE",
287 "The PHONE to put in SDES messages of this session",
288 DEFAULT_SDES_PHONE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
290 g_object_class_install_property (gobject_class, PROP_SDES_LOCATION,
291 g_param_spec_string ("sdes-location", "SDES LOCATION",
292 "The LOCATION to put in SDES messages of this session",
293 DEFAULT_SDES_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
295 g_object_class_install_property (gobject_class, PROP_SDES_TOOL,
296 g_param_spec_string ("sdes-tool", "SDES TOOL",
297 "The TOOL to put in SDES messages of this session",
298 DEFAULT_SDES_TOOL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
300 g_object_class_install_property (gobject_class, PROP_SDES_NOTE,
301 g_param_spec_string ("sdes-note", "SDES NOTE",
302 "The NOTE to put in SDES messages of this session",
303 DEFAULT_SDES_NOTE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
305 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
306 g_param_spec_uint ("num-sources", "Num Sources",
307 "The number of sources in the session", 0, G_MAXUINT,
308 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
310 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
311 g_param_spec_uint ("num-active-sources", "Num Active Sources",
312 "The number of active sources in the session", 0, G_MAXUINT,
313 DEFAULT_NUM_ACTIVE_SOURCES,
314 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
318 * Get a GValue Array of all sources in the session.
321 * <title>Getting the #RTPSources of a session
328 * g_object_get (sess, "sources", &arr, NULL);
330 * for (i = 0; i < arr->n_values; i++) {
333 * val = g_value_array_get_nth (arr, i);
334 * source = g_value_get_object (val);
336 * g_value_array_free (arr);
341 g_object_class_install_property (gobject_class, PROP_SOURCES,
342 g_param_spec_boxed ("sources", "Sources",
343 "An array of all known sources in the session",
344 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
346 klass->get_source_by_ssrc =
347 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
349 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
353 rtp_session_init (RTPSession * sess)
358 sess->lock = g_mutex_new ();
359 sess->key = g_random_int ();
363 for (i = 0; i < 32; i++) {
365 g_hash_table_new_full (NULL, NULL, NULL,
366 (GDestroyNotify) g_object_unref);
368 sess->cnames = g_hash_table_new_full (NULL, NULL, g_free, NULL);
370 rtp_stats_init_defaults (&sess->stats);
372 /* create an active SSRC for this session manager */
373 sess->source = rtp_session_create_source (sess);
374 sess->source->validated = TRUE;
375 sess->source->internal = TRUE;
376 sess->stats.active_sources++;
378 /* default UDP header length */
379 sess->header_len = 28;
380 sess->mtu = DEFAULT_RTCP_MTU;
382 /* some default SDES entries */
383 str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
384 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_CNAME, str);
387 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_NAME,
389 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_TOOL, "GStreamer");
391 sess->first_rtcp = TRUE;
393 GST_DEBUG ("%p: session using SSRC: %08x", sess, sess->source->ssrc);
397 rtp_session_finalize (GObject * object)
402 sess = RTP_SESSION_CAST (object);
404 g_mutex_free (sess->lock);
405 for (i = 0; i < 32; i++)
406 g_hash_table_destroy (sess->ssrcs[i]);
408 g_free (sess->bye_reason);
410 g_hash_table_destroy (sess->cnames);
411 g_object_unref (sess->source);
413 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
417 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
419 GValue value = { 0 };
421 g_value_init (&value, RTP_TYPE_SOURCE);
422 g_value_take_object (&value, source);
423 g_value_array_append (arr, &value);
427 rtp_session_create_sources (RTPSession * sess)
432 RTP_SESSION_LOCK (sess);
433 /* get number of elements in the table */
434 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
435 /* create the result value array */
436 res = g_value_array_new (size);
438 /* and copy all values into the array */
439 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
440 RTP_SESSION_UNLOCK (sess);
446 rtp_session_set_property (GObject * object, guint prop_id,
447 const GValue * value, GParamSpec * pspec)
451 sess = RTP_SESSION (object);
455 rtp_session_set_bandwidth (sess, g_value_get_double (value));
457 case PROP_RTCP_FRACTION:
458 rtp_session_set_rtcp_fraction (sess, g_value_get_double (value));
461 sess->mtu = g_value_get_uint (value);
463 case PROP_SDES_CNAME:
464 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_CNAME,
465 g_value_get_string (value));
468 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_NAME,
469 g_value_get_string (value));
471 case PROP_SDES_EMAIL:
472 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_EMAIL,
473 g_value_get_string (value));
475 case PROP_SDES_PHONE:
476 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_PHONE,
477 g_value_get_string (value));
479 case PROP_SDES_LOCATION:
480 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_LOC,
481 g_value_get_string (value));
484 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_TOOL,
485 g_value_get_string (value));
488 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_NOTE,
489 g_value_get_string (value));
492 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
498 rtp_session_get_property (GObject * object, guint prop_id,
499 GValue * value, GParamSpec * pspec)
503 sess = RTP_SESSION (object);
506 case PROP_INTERNAL_SOURCE:
507 g_value_take_object (value, rtp_session_get_internal_source (sess));
510 g_value_set_double (value, rtp_session_get_bandwidth (sess));
512 case PROP_RTCP_FRACTION:
513 g_value_set_double (value, rtp_session_get_rtcp_fraction (sess));
516 g_value_set_uint (value, sess->mtu);
518 case PROP_SDES_CNAME:
519 g_value_take_string (value, rtp_session_get_sdes_string (sess,
520 GST_RTCP_SDES_CNAME));
523 g_value_take_string (value, rtp_session_get_sdes_string (sess,
524 GST_RTCP_SDES_NAME));
526 case PROP_SDES_EMAIL:
527 g_value_take_string (value, rtp_session_get_sdes_string (sess,
528 GST_RTCP_SDES_EMAIL));
530 case PROP_SDES_PHONE:
531 g_value_take_string (value, rtp_session_get_sdes_string (sess,
532 GST_RTCP_SDES_PHONE));
534 case PROP_SDES_LOCATION:
535 g_value_take_string (value, rtp_session_get_sdes_string (sess,
539 g_value_take_string (value, rtp_session_get_sdes_string (sess,
540 GST_RTCP_SDES_TOOL));
543 g_value_take_string (value, rtp_session_get_sdes_string (sess,
544 GST_RTCP_SDES_NOTE));
546 case PROP_NUM_SOURCES:
547 g_value_set_uint (value, rtp_session_get_num_sources (sess));
549 case PROP_NUM_ACTIVE_SOURCES:
550 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
553 g_value_take_boxed (value, rtp_session_create_sources (sess));
556 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
562 on_new_ssrc (RTPSession * sess, RTPSource * source)
564 g_object_ref (source);
565 RTP_SESSION_UNLOCK (sess);
566 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
567 RTP_SESSION_LOCK (sess);
568 g_object_unref (source);
572 on_ssrc_collision (RTPSession * sess, RTPSource * source)
574 g_object_ref (source);
575 RTP_SESSION_UNLOCK (sess);
576 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
578 RTP_SESSION_LOCK (sess);
579 g_object_unref (source);
583 on_ssrc_validated (RTPSession * sess, RTPSource * source)
585 g_object_ref (source);
586 RTP_SESSION_UNLOCK (sess);
587 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
589 RTP_SESSION_LOCK (sess);
590 g_object_unref (source);
594 on_ssrc_active (RTPSession * sess, RTPSource * source)
596 g_object_ref (source);
597 RTP_SESSION_UNLOCK (sess);
598 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
599 RTP_SESSION_LOCK (sess);
600 g_object_unref (source);
604 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
606 g_object_ref (source);
607 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
608 RTP_SESSION_UNLOCK (sess);
609 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
610 RTP_SESSION_LOCK (sess);
611 g_object_unref (source);
615 on_bye_ssrc (RTPSession * sess, RTPSource * source)
617 g_object_ref (source);
618 RTP_SESSION_UNLOCK (sess);
619 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
620 RTP_SESSION_LOCK (sess);
621 g_object_unref (source);
625 on_bye_timeout (RTPSession * sess, RTPSource * source)
627 g_object_ref (source);
628 RTP_SESSION_UNLOCK (sess);
629 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
630 RTP_SESSION_LOCK (sess);
631 g_object_unref (source);
635 on_timeout (RTPSession * sess, RTPSource * source)
637 g_object_ref (source);
638 RTP_SESSION_UNLOCK (sess);
639 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
640 RTP_SESSION_LOCK (sess);
641 g_object_unref (source);
645 on_sender_timeout (RTPSession * sess, RTPSource * source)
647 g_object_ref (source);
648 RTP_SESSION_UNLOCK (sess);
649 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
651 RTP_SESSION_LOCK (sess);
652 g_object_unref (source);
658 * Create a new session object.
660 * Returns: a new #RTPSession. g_object_unref() after usage.
663 rtp_session_new (void)
667 sess = g_object_new (RTP_TYPE_SESSION, NULL);
673 * rtp_session_set_callbacks:
674 * @sess: an #RTPSession
675 * @callbacks: callbacks to configure
676 * @user_data: user data passed in the callbacks
678 * Configure a set of callbacks to be notified of actions.
681 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
684 g_return_if_fail (RTP_IS_SESSION (sess));
686 if (callbacks->process_rtp) {
687 sess->callbacks.process_rtp = callbacks->process_rtp;
688 sess->process_rtp_user_data = user_data;
690 if (callbacks->send_rtp) {
691 sess->callbacks.send_rtp = callbacks->send_rtp;
692 sess->send_rtp_user_data = user_data;
694 if (callbacks->send_rtcp) {
695 sess->callbacks.send_rtcp = callbacks->send_rtcp;
696 sess->send_rtcp_user_data = user_data;
698 if (callbacks->sync_rtcp) {
699 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
700 sess->sync_rtcp_user_data = user_data;
702 if (callbacks->clock_rate) {
703 sess->callbacks.clock_rate = callbacks->clock_rate;
704 sess->clock_rate_user_data = user_data;
706 if (callbacks->reconsider) {
707 sess->callbacks.reconsider = callbacks->reconsider;
708 sess->reconsider_user_data = user_data;
713 * rtp_session_set_process_rtp_callback:
714 * @sess: an #RTPSession
715 * @callback: callback to set
716 * @user_data: user data passed in the callback
718 * Configure only the process_rtp callback to be notified of the process_rtp action.
721 rtp_session_set_process_rtp_callback (RTPSession * sess,
722 RTPSessionProcessRTP callback, gpointer user_data)
724 g_return_if_fail (RTP_IS_SESSION (sess));
726 sess->callbacks.process_rtp = callback;
727 sess->process_rtp_user_data = user_data;
731 * rtp_session_set_send_rtp_callback:
732 * @sess: an #RTPSession
733 * @callback: callback to set
734 * @user_data: user data passed in the callback
736 * Configure only the send_rtp callback to be notified of the send_rtp action.
739 rtp_session_set_send_rtp_callback (RTPSession * sess,
740 RTPSessionSendRTP callback, gpointer user_data)
742 g_return_if_fail (RTP_IS_SESSION (sess));
744 sess->callbacks.send_rtp = callback;
745 sess->send_rtp_user_data = user_data;
749 * rtp_session_set_send_rtcp_callback:
750 * @sess: an #RTPSession
751 * @callback: callback to set
752 * @user_data: user data passed in the callback
754 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
757 rtp_session_set_send_rtcp_callback (RTPSession * sess,
758 RTPSessionSendRTCP callback, gpointer user_data)
760 g_return_if_fail (RTP_IS_SESSION (sess));
762 sess->callbacks.send_rtcp = callback;
763 sess->send_rtcp_user_data = user_data;
767 * rtp_session_set_sync_rtcp_callback:
768 * @sess: an #RTPSession
769 * @callback: callback to set
770 * @user_data: user data passed in the callback
772 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
775 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
776 RTPSessionSyncRTCP callback, gpointer user_data)
778 g_return_if_fail (RTP_IS_SESSION (sess));
780 sess->callbacks.sync_rtcp = callback;
781 sess->sync_rtcp_user_data = user_data;
785 * rtp_session_set_clock_rate_callback:
786 * @sess: an #RTPSession
787 * @callback: callback to set
788 * @user_data: user data passed in the callback
790 * Configure only the clock_rate callback to be notified of the clock_rate action.
793 rtp_session_set_clock_rate_callback (RTPSession * sess,
794 RTPSessionClockRate callback, gpointer user_data)
796 g_return_if_fail (RTP_IS_SESSION (sess));
798 sess->callbacks.clock_rate = callback;
799 sess->clock_rate_user_data = user_data;
803 * rtp_session_set_reconsider_callback:
804 * @sess: an #RTPSession
805 * @callback: callback to set
806 * @user_data: user data passed in the callback
808 * Configure only the reconsider callback to be notified of the reconsider action.
811 rtp_session_set_reconsider_callback (RTPSession * sess,
812 RTPSessionReconsider callback, gpointer user_data)
814 g_return_if_fail (RTP_IS_SESSION (sess));
816 sess->callbacks.reconsider = callback;
817 sess->reconsider_user_data = user_data;
821 * rtp_session_set_bandwidth:
822 * @sess: an #RTPSession
823 * @bandwidth: the bandwidth allocated
825 * Set the session bandwidth in bytes per second.
828 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
830 g_return_if_fail (RTP_IS_SESSION (sess));
832 RTP_SESSION_LOCK (sess);
833 sess->stats.bandwidth = bandwidth;
834 RTP_SESSION_UNLOCK (sess);
838 * rtp_session_get_bandwidth:
839 * @sess: an #RTPSession
841 * Get the session bandwidth.
843 * Returns: the session bandwidth.
846 rtp_session_get_bandwidth (RTPSession * sess)
850 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
852 RTP_SESSION_LOCK (sess);
853 result = sess->stats.bandwidth;
854 RTP_SESSION_UNLOCK (sess);
860 * rtp_session_set_rtcp_fraction:
861 * @sess: an #RTPSession
862 * @bandwidth: the RTCP bandwidth
864 * Set the bandwidth that should be used for RTCP
868 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
870 g_return_if_fail (RTP_IS_SESSION (sess));
872 RTP_SESSION_LOCK (sess);
873 sess->stats.rtcp_bandwidth = bandwidth;
874 RTP_SESSION_UNLOCK (sess);
878 * rtp_session_get_rtcp_fraction:
879 * @sess: an #RTPSession
881 * Get the session bandwidth used for RTCP.
883 * Returns: The bandwidth used for RTCP messages.
886 rtp_session_get_rtcp_fraction (RTPSession * sess)
890 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
892 RTP_SESSION_LOCK (sess);
893 result = sess->stats.rtcp_bandwidth;
894 RTP_SESSION_UNLOCK (sess);
900 * rtp_session_set_sdes_string:
901 * @sess: an #RTPSession
902 * @type: the type of the SDES item
903 * @item: a null-terminated string to set.
905 * Store an SDES item of @type in @sess.
907 * Returns: %FALSE if the data was unchanged @type is invalid.
910 rtp_session_set_sdes_string (RTPSession * sess, GstRTCPSDESType type,
915 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
917 RTP_SESSION_LOCK (sess);
918 result = rtp_source_set_sdes_string (sess->source, type, item);
919 RTP_SESSION_UNLOCK (sess);
925 * rtp_session_get_sdes_string:
926 * @sess: an #RTPSession
927 * @type: the type of the SDES item
929 * Get the SDES item of @type from @sess.
931 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
932 * valid. g_free() after usage.
935 rtp_session_get_sdes_string (RTPSession * sess, GstRTCPSDESType type)
939 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
941 RTP_SESSION_LOCK (sess);
942 result = rtp_source_get_sdes_string (sess->source, type);
943 RTP_SESSION_UNLOCK (sess);
949 source_push_rtp (RTPSource * source, GstBuffer * buffer, RTPSession * session)
951 GstFlowReturn result = GST_FLOW_OK;
953 if (source == session->source) {
954 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
956 RTP_SESSION_UNLOCK (session);
958 if (session->callbacks.send_rtp)
960 session->callbacks.send_rtp (session, source, buffer,
961 session->send_rtp_user_data);
963 gst_buffer_unref (buffer);
966 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
967 RTP_SESSION_UNLOCK (session);
969 if (session->callbacks.process_rtp)
971 session->callbacks.process_rtp (session, source, buffer,
972 session->process_rtp_user_data);
974 gst_buffer_unref (buffer);
976 RTP_SESSION_LOCK (session);
982 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
986 RTP_SESSION_UNLOCK (session);
988 if (session->callbacks.clock_rate)
990 session->callbacks.clock_rate (session, pt,
991 session->clock_rate_user_data);
995 RTP_SESSION_LOCK (session);
997 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1002 static RTPSourceCallbacks callbacks = {
1003 (RTPSourcePushRTP) source_push_rtp,
1004 (RTPSourceClockRate) source_clock_rate,
1008 * find_add_conflicting_addresses:
1009 * @sess: The session to check in
1010 * @arrival: The arrival stats for the buffer
1012 * Checks if an address which has a conflict is already known,
1013 * otherwise remembers it to prevent loops.
1015 * Returns: TRUE if it was a known conflict, FALSE otherwise
1019 find_add_conflicting_addresses (RTPSession * sess, RTPArrivalStats * arrival)
1022 RTPConflictingAddress *new_conflict;
1024 for (item = g_list_first (sess->conflicting_addresses);
1025 item; item = g_list_next (item)) {
1026 RTPConflictingAddress *known_conflict = item->data;
1028 if (gst_netaddress_equal (&arrival->address, &known_conflict->address)) {
1029 known_conflict->time = arrival->time;
1034 new_conflict = g_new0 (RTPConflictingAddress, 1);
1036 memcpy (&new_conflict->address, &arrival->address, sizeof (GstNetAddress));
1037 new_conflict->time = arrival->time;
1039 sess->conflicting_addresses = g_list_prepend (sess->conflicting_addresses,
1046 check_collision (RTPSession * sess, RTPSource * source,
1047 RTPArrivalStats * arrival, gboolean rtp)
1049 /* If we have no arrival address, we can't do collision checking */
1050 if (!arrival->have_address)
1053 if (sess->source != source) {
1054 /* This is not our local source, but lets check if two remote
1058 if (source->have_rtp_from) {
1059 if (gst_netaddress_equal (&source->rtp_from, &arrival->address))
1060 /* Address is the same */
1063 /* We don't already have a from address for RTP, just set it */
1064 rtp_source_set_rtp_from (source, &arrival->address);
1068 if (source->have_rtcp_from) {
1069 if (gst_netaddress_equal (&source->rtcp_from, &arrival->address))
1070 /* Address is the same */
1073 /* We don't already have a from address for RTCP, just set it */
1074 rtp_source_set_rtcp_from (source, &arrival->address);
1078 /* We received RTP or RTCP from this source before but the network address
1079 * changed. In this case, we have third-party collision or loop */
1080 GST_DEBUG ("we have a third-party collision or loop");
1082 /* FIXME: Log 3rd party collision somehow
1083 * Maybe should be done in upper layer, only the SDES can tell us
1084 * if its a collision or a loop
1087 /* This is sending with our ssrc, is it an address we already know */
1089 if (find_add_conflicting_addresses (sess, arrival)) {
1090 /* Its a known conflict, its probably a loop, not a collision
1091 * lets just drop the incoming packet
1093 GST_DEBUG ("Our packets are being looped back to us, dropping");
1095 /* Its a new collision, lets change our SSRC */
1097 GST_DEBUG ("Collision for SSRC %x", rtp_source_get_ssrc (source));
1098 on_ssrc_collision (sess, source);
1100 rtp_session_send_bye_locked (sess, "SSRC Collision", arrival->time);
1102 sess->change_ssrc = TRUE;
1110 /* must be called with the session lock, the returned source needs to be
1111 * unreffed after usage. */
1113 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1114 RTPArrivalStats * arrival, gboolean rtp)
1119 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1120 if (source == NULL) {
1121 /* make new Source in probation and insert */
1122 source = rtp_source_new (ssrc);
1124 /* for RTP packets we need to set the source in probation. Receiving RTCP
1125 * packets of an SSRC, on the other hand, is a strong indication that we
1126 * are dealing with a valid source. */
1128 source->probation = RTP_DEFAULT_PROBATION;
1130 source->probation = 0;
1132 /* store from address, if any */
1133 if (arrival->have_address) {
1135 rtp_source_set_rtp_from (source, &arrival->address);
1137 rtp_source_set_rtcp_from (source, &arrival->address);
1140 /* configure a callback on the source */
1141 rtp_source_set_callbacks (source, &callbacks, sess);
1143 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1146 /* we have one more source now */
1147 sess->total_sources++;
1151 /* check for collision, this updates the address when not previously set */
1152 if (check_collision (sess, source, arrival, rtp)) {
1156 /* update last activity */
1157 source->last_activity = arrival->time;
1159 source->last_rtp_activity = arrival->time;
1160 g_object_ref (source);
1166 * rtp_session_get_internal_source:
1167 * @sess: a #RTPSession
1169 * Get the internal #RTPSource of @sess.
1171 * Returns: The internal #RTPSource. g_object_unref() after usage.
1174 rtp_session_get_internal_source (RTPSession * sess)
1178 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1180 result = g_object_ref (sess->source);
1186 * rtp_session_set_internal_ssrc:
1187 * @sess: a #RTPSession
1190 * Set the SSRC of @sess to @ssrc.
1193 rtp_session_set_internal_ssrc (RTPSession * sess, guint32 ssrc)
1195 RTP_SESSION_LOCK (sess);
1196 if (ssrc != sess->source->ssrc) {
1197 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
1198 GINT_TO_POINTER (sess->source->ssrc));
1200 sess->source->ssrc = ssrc;
1201 rtp_source_reset (sess->source);
1203 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1204 GINT_TO_POINTER (sess->source->ssrc), sess->source);
1206 RTP_SESSION_UNLOCK (sess);
1210 * rtp_session_get_internal_ssrc:
1211 * @sess: a #RTPSession
1213 * Get the internal SSRC of @sess.
1215 * Returns: The SSRC of the session.
1218 rtp_session_get_internal_ssrc (RTPSession * sess)
1222 RTP_SESSION_LOCK (sess);
1223 ssrc = sess->source->ssrc;
1224 RTP_SESSION_UNLOCK (sess);
1230 * rtp_session_add_source:
1231 * @sess: a #RTPSession
1232 * @src: #RTPSource to add
1234 * Add @src to @session.
1236 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1237 * existed in the session.
1240 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1242 gboolean result = FALSE;
1245 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1246 g_return_val_if_fail (src != NULL, FALSE);
1248 RTP_SESSION_LOCK (sess);
1250 g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1251 GINT_TO_POINTER (src->ssrc));
1253 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1254 GINT_TO_POINTER (src->ssrc), src);
1255 /* we have one more source now */
1256 sess->total_sources++;
1259 RTP_SESSION_UNLOCK (sess);
1265 * rtp_session_get_num_sources:
1266 * @sess: an #RTPSession
1268 * Get the number of sources in @sess.
1270 * Returns: The number of sources in @sess.
1273 rtp_session_get_num_sources (RTPSession * sess)
1277 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1279 RTP_SESSION_LOCK (sess);
1280 result = sess->total_sources;
1281 RTP_SESSION_UNLOCK (sess);
1287 * rtp_session_get_num_active_sources:
1288 * @sess: an #RTPSession
1290 * Get the number of active sources in @sess. A source is considered active when
1291 * it has been validated and has not yet received a BYE RTCP message.
1293 * Returns: The number of active sources in @sess.
1296 rtp_session_get_num_active_sources (RTPSession * sess)
1300 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1302 RTP_SESSION_LOCK (sess);
1303 result = sess->stats.active_sources;
1304 RTP_SESSION_UNLOCK (sess);
1310 * rtp_session_get_source_by_ssrc:
1311 * @sess: an #RTPSession
1314 * Find the source with @ssrc in @sess.
1316 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1317 * g_object_unref() after usage.
1320 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1324 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1326 RTP_SESSION_LOCK (sess);
1328 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1330 g_object_ref (result);
1331 RTP_SESSION_UNLOCK (sess);
1337 * rtp_session_get_source_by_cname:
1338 * @sess: a #RTPSession
1341 * Find the source with @cname in @sess.
1343 * Returns: a #RTPSource with CNAME @cname or NULL if the source was not found.
1344 * g_object_unref() after usage.
1347 rtp_session_get_source_by_cname (RTPSession * sess, const gchar * cname)
1351 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1352 g_return_val_if_fail (cname != NULL, NULL);
1354 RTP_SESSION_LOCK (sess);
1355 result = g_hash_table_lookup (sess->cnames, cname);
1357 g_object_ref (result);
1358 RTP_SESSION_UNLOCK (sess);
1364 rtp_session_create_new_ssrc (RTPSession * sess)
1369 ssrc = g_random_int ();
1371 /* see if it exists in the session, we're done if it doesn't */
1372 if (g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1373 GINT_TO_POINTER (ssrc)) == NULL)
1382 * rtp_session_create_source:
1383 * @sess: an #RTPSession
1385 * Create an #RTPSource for use in @sess. This function will create a source
1386 * with an ssrc that is currently not used by any participants in the session.
1388 * Returns: an #RTPSource.
1391 rtp_session_create_source (RTPSession * sess)
1396 RTP_SESSION_LOCK (sess);
1397 ssrc = rtp_session_create_new_ssrc (sess);
1398 source = rtp_source_new (ssrc);
1399 rtp_source_set_callbacks (source, &callbacks, sess);
1400 /* we need an additional ref for the source in the hashtable */
1401 g_object_ref (source);
1402 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1404 /* we have one more source now */
1405 sess->total_sources++;
1406 RTP_SESSION_UNLOCK (sess);
1411 /* update the RTPArrivalStats structure with the current time and other bits
1412 * about the current buffer we are handling.
1413 * This function is typically called when a validated packet is received.
1414 * This function should be called with the SESSION_LOCK
1417 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
1418 gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
1419 GstClockTime running_time, guint64 ntpnstime)
1421 /* get time of arrival */
1422 arrival->time = current_time;
1423 arrival->running_time = running_time;
1424 arrival->ntpnstime = ntpnstime;
1426 /* get packet size including header overhead */
1427 arrival->bytes = GST_BUFFER_SIZE (buffer) + sess->header_len;
1430 arrival->payload_len = gst_rtp_buffer_get_payload_len (buffer);
1432 arrival->payload_len = 0;
1435 /* for netbuffer we can store the IP address to check for collisions */
1436 arrival->have_address = GST_IS_NETBUFFER (buffer);
1437 if (arrival->have_address) {
1438 GstNetBuffer *netbuf = (GstNetBuffer *) buffer;
1440 memcpy (&arrival->address, &netbuf->from, sizeof (GstNetAddress));
1445 * rtp_session_process_rtp:
1446 * @sess: and #RTPSession
1447 * @buffer: an RTP buffer
1448 * @current_time: the current system time
1449 * @ntpnstime: the NTP arrival time in nanoseconds
1451 * Process an RTP buffer in the session manager. This function takes ownership
1454 * Returns: a #GstFlowReturn.
1457 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1458 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
1460 GstFlowReturn result;
1464 gboolean prevsender, prevactive;
1465 RTPArrivalStats arrival;
1467 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1468 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1470 if (!gst_rtp_buffer_validate (buffer))
1471 goto invalid_packet;
1473 RTP_SESSION_LOCK (sess);
1474 /* update arrival stats */
1475 update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1476 running_time, ntpnstime);
1478 /* ignore more RTP packets when we left the session */
1479 if (sess->source->received_bye)
1482 /* get SSRC and look up in session database */
1483 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1484 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1488 prevsender = RTP_SOURCE_IS_SENDER (source);
1489 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1491 /* we need to ref so that we can process the CSRCs later */
1492 gst_buffer_ref (buffer);
1494 /* let source process the packet */
1495 result = rtp_source_process_rtp (source, buffer, &arrival);
1497 /* source became active */
1498 if (prevactive != RTP_SOURCE_IS_ACTIVE (source)) {
1499 sess->stats.active_sources++;
1500 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1501 sess->stats.active_sources);
1502 on_ssrc_validated (sess, source);
1504 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1505 sess->stats.sender_sources++;
1506 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1507 sess->stats.sender_sources);
1511 on_new_ssrc (sess, source);
1513 if (source->validated) {
1517 /* for validated sources, we add the CSRCs as well */
1518 count = gst_rtp_buffer_get_csrc_count (buffer);
1520 for (i = 0; i < count; i++) {
1522 RTPSource *csrc_src;
1524 csrc = gst_rtp_buffer_get_csrc (buffer, i);
1527 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1532 GST_DEBUG ("created new CSRC: %08x", csrc);
1533 rtp_source_set_as_csrc (csrc_src);
1534 if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1535 sess->stats.active_sources++;
1536 on_new_ssrc (sess, csrc_src);
1538 g_object_unref (csrc_src);
1541 g_object_unref (source);
1542 gst_buffer_unref (buffer);
1544 RTP_SESSION_UNLOCK (sess);
1551 gst_buffer_unref (buffer);
1552 GST_DEBUG ("invalid RTP packet received");
1557 gst_buffer_unref (buffer);
1558 RTP_SESSION_UNLOCK (sess);
1559 GST_DEBUG ("ignoring RTP packet because we are leaving");
1564 gst_buffer_unref (buffer);
1565 RTP_SESSION_UNLOCK (sess);
1566 GST_DEBUG ("ignoring packet because its collisioning");
1572 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1573 GstRTCPPacket * packet, RTPArrivalStats * arrival)
1577 count = gst_rtcp_packet_get_rb_count (packet);
1578 for (i = 0; i < count; i++) {
1579 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1580 guint8 fractionlost;
1583 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1584 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1586 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1588 if (ssrc == sess->source->ssrc) {
1589 /* only deal with report blocks for our session, we update the stats of
1590 * the sender of the RTCP message. We could also compare our stats against
1591 * the other sender to see if we are better or worse. */
1592 rtp_source_process_rb (source, arrival->time, fractionlost, packetslost,
1593 exthighestseq, jitter, lsr, dlsr);
1595 on_ssrc_active (sess, source);
1600 /* A Sender report contains statistics about how the sender is doing. This
1601 * includes timing informataion such as the relation between RTP and NTP
1602 * timestamps and the number of packets/bytes it sent to us.
1604 * In this report is also included a set of report blocks related to how this
1605 * sender is receiving data (in case we (or somebody else) is also sending stuff
1606 * to it). This info includes the packet loss, jitter and seqnum. It also
1607 * contains information to calculate the round trip time (LSR/DLSR).
1610 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1611 RTPArrivalStats * arrival)
1613 guint32 senderssrc, rtptime, packet_count, octet_count;
1616 gboolean created, prevsender;
1618 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1619 &packet_count, &octet_count);
1621 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1622 senderssrc, GST_TIME_ARGS (arrival->time));
1624 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1628 prevsender = RTP_SOURCE_IS_SENDER (source);
1630 /* first update the source */
1631 rtp_source_process_sr (source, arrival->time, ntptime, rtptime, packet_count,
1634 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1635 sess->stats.sender_sources++;
1636 GST_DEBUG ("source: %08x became sender, %d sender sources", senderssrc,
1637 sess->stats.sender_sources);
1641 on_new_ssrc (sess, source);
1643 rtp_session_process_rb (sess, source, packet, arrival);
1644 g_object_unref (source);
1647 /* A receiver report contains statistics about how a receiver is doing. It
1648 * includes stuff like packet loss, jitter and the seqnum it received last. It
1649 * also contains info to calculate the round trip time.
1651 * We are only interested in how the sender of this report is doing wrt to us.
1654 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1655 RTPArrivalStats * arrival)
1661 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1663 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1665 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1670 on_new_ssrc (sess, source);
1672 rtp_session_process_rb (sess, source, packet, arrival);
1673 g_object_unref (source);
1676 /* Get SDES items and store them in the SSRC */
1678 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1679 RTPArrivalStats * arrival)
1682 gboolean more_items, more_entries;
1684 items = gst_rtcp_packet_sdes_get_item_count (packet);
1685 GST_DEBUG ("got SDES packet with %d items", items);
1687 more_items = gst_rtcp_packet_sdes_first_item (packet);
1689 while (more_items) {
1691 gboolean changed, created;
1694 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1696 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1700 /* find src, no probation when dealing with RTCP */
1701 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1705 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1707 while (more_entries) {
1708 GstRTCPSDESType type;
1712 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1714 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1717 changed |= rtp_source_set_sdes (source, type, data, len);
1719 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1723 source->validated = TRUE;
1726 on_new_ssrc (sess, source);
1728 on_ssrc_sdes (sess, source);
1730 g_object_unref (source);
1732 more_items = gst_rtcp_packet_sdes_next_item (packet);
1737 /* BYE is sent when a client leaves the session
1740 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
1741 RTPArrivalStats * arrival)
1746 reason = gst_rtcp_packet_bye_get_reason (packet);
1747 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
1749 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
1750 for (i = 0; i < count; i++) {
1753 gboolean created, prevactive, prevsender;
1754 guint pmembers, members;
1756 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
1757 GST_DEBUG ("SSRC: %08x", ssrc);
1759 /* find src and mark bye, no probation when dealing with RTCP */
1760 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1764 /* store time for when we need to time out this source */
1765 source->bye_time = arrival->time;
1767 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1768 prevsender = RTP_SOURCE_IS_SENDER (source);
1770 /* let the source handle the rest */
1771 rtp_source_process_bye (source, reason);
1773 pmembers = sess->stats.active_sources;
1775 if (prevactive && !RTP_SOURCE_IS_ACTIVE (source)) {
1776 sess->stats.active_sources--;
1777 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
1778 sess->stats.active_sources);
1780 if (prevsender && !RTP_SOURCE_IS_SENDER (source)) {
1781 sess->stats.sender_sources--;
1782 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
1783 sess->stats.sender_sources);
1785 members = sess->stats.active_sources;
1787 if (!sess->source->received_bye && members < pmembers) {
1788 /* some members went away since the previous timeout estimate.
1789 * Perform reverse reconsideration but only when we are not scheduling a
1791 if (arrival->time < sess->next_rtcp_check_time) {
1792 GstClockTime time_remaining;
1794 time_remaining = sess->next_rtcp_check_time - arrival->time;
1795 sess->next_rtcp_check_time =
1796 gst_util_uint64_scale (time_remaining, members, pmembers);
1798 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
1799 GST_TIME_ARGS (sess->next_rtcp_check_time));
1801 sess->next_rtcp_check_time += arrival->time;
1803 RTP_SESSION_UNLOCK (sess);
1804 /* notify app of reconsideration */
1805 if (sess->callbacks.reconsider)
1806 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
1807 RTP_SESSION_LOCK (sess);
1812 on_new_ssrc (sess, source);
1814 on_bye_ssrc (sess, source);
1816 g_object_unref (source);
1822 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
1823 RTPArrivalStats * arrival)
1825 GST_DEBUG ("received APP");
1829 * rtp_session_process_rtcp:
1830 * @sess: and #RTPSession
1831 * @buffer: an RTCP buffer
1832 * @current_time: the current system time
1834 * Process an RTCP buffer in the session manager. This function takes ownership
1837 * Returns: a #GstFlowReturn.
1840 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
1841 GstClockTime current_time)
1843 GstRTCPPacket packet;
1844 gboolean more, is_bye = FALSE, is_sr = FALSE;
1845 RTPArrivalStats arrival;
1846 GstFlowReturn result = GST_FLOW_OK;
1848 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1849 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1851 if (!gst_rtcp_buffer_validate (buffer))
1852 goto invalid_packet;
1854 GST_DEBUG ("received RTCP packet");
1856 RTP_SESSION_LOCK (sess);
1857 /* update arrival stats */
1858 update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1, -1);
1863 /* make writable, we might want to change the buffer */
1864 buffer = gst_buffer_make_metadata_writable (buffer);
1866 /* start processing the compound packet */
1867 more = gst_rtcp_buffer_get_first_packet (buffer, &packet);
1871 type = gst_rtcp_packet_get_type (&packet);
1873 /* when we are leaving the session, we should ignore all non-BYE messages */
1874 if (sess->source->received_bye && type != GST_RTCP_TYPE_BYE) {
1875 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
1880 case GST_RTCP_TYPE_SR:
1881 rtp_session_process_sr (sess, &packet, &arrival);
1884 case GST_RTCP_TYPE_RR:
1885 rtp_session_process_rr (sess, &packet, &arrival);
1887 case GST_RTCP_TYPE_SDES:
1888 rtp_session_process_sdes (sess, &packet, &arrival);
1890 case GST_RTCP_TYPE_BYE:
1892 rtp_session_process_bye (sess, &packet, &arrival);
1894 case GST_RTCP_TYPE_APP:
1895 rtp_session_process_app (sess, &packet, &arrival);
1898 GST_WARNING ("got unknown RTCP packet");
1902 more = gst_rtcp_packet_move_to_next (&packet);
1905 /* if we are scheduling a BYE, we only want to count bye packets, else we
1906 * count everything */
1907 if (sess->source->received_bye) {
1909 sess->stats.bye_members++;
1910 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
1913 /* keep track of average packet size */
1914 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
1916 RTP_SESSION_UNLOCK (sess);
1918 /* notify caller of sr packets in the callback */
1919 if (is_sr && sess->callbacks.sync_rtcp)
1920 result = sess->callbacks.sync_rtcp (sess, sess->source, buffer,
1921 sess->sync_rtcp_user_data);
1923 gst_buffer_unref (buffer);
1930 GST_DEBUG ("invalid RTCP packet received");
1931 gst_buffer_unref (buffer);
1936 gst_buffer_unref (buffer);
1937 RTP_SESSION_UNLOCK (sess);
1938 GST_DEBUG ("ignoring RTP packet because we left");
1944 * rtp_session_send_rtp:
1945 * @sess: an #RTPSession
1946 * @buffer: an RTP buffer
1947 * @current_time: the current system time
1948 * @ntpnstime: the NTP time in nanoseconds of when this buffer was captured.
1949 * This is the buffer timestamp converted to NTP time.
1951 * Send the RTP buffer in the session manager. This function takes ownership of
1954 * Returns: a #GstFlowReturn.
1957 rtp_session_send_rtp (RTPSession * sess, GstBuffer * buffer,
1958 GstClockTime current_time, guint64 ntpnstime)
1960 GstFlowReturn result;
1962 gboolean prevsender;
1964 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1965 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1967 if (!gst_rtp_buffer_validate (buffer))
1968 goto invalid_packet;
1970 GST_LOG ("received RTP packet for sending");
1972 RTP_SESSION_LOCK (sess);
1973 source = sess->source;
1975 /* update last activity */
1976 source->last_rtp_activity = current_time;
1978 prevsender = RTP_SOURCE_IS_SENDER (source);
1980 /* we use our own source to send */
1981 result = rtp_source_send_rtp (source, buffer, ntpnstime);
1983 if (RTP_SOURCE_IS_SENDER (source) && !prevsender)
1984 sess->stats.sender_sources++;
1985 RTP_SESSION_UNLOCK (sess);
1992 gst_buffer_unref (buffer);
1993 GST_DEBUG ("invalid RTP packet received");
1999 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2002 GstClockTime result;
2004 if (sess->source->received_bye) {
2005 result = rtp_stats_calculate_bye_interval (&sess->stats);
2007 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2008 RTP_SOURCE_IS_SENDER (sess->source), first);
2011 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2012 GST_TIME_ARGS (result), first);
2015 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2017 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2023 * rtp_session_send_bye_locked:
2024 * @sess: an #RTPSession
2025 * @reason: a reason or NULL
2027 * Stop the current @sess and schedule a BYE message for the other members.
2029 * One must have the session lock to call this function
2031 * Returns: a #GstFlowReturn.
2033 static GstFlowReturn
2034 rtp_session_send_bye_locked (RTPSession * sess, const gchar * reason,
2035 GstClockTime current_time)
2037 GstFlowReturn result = GST_FLOW_OK;
2039 GstClockTime interval;
2041 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2043 source = sess->source;
2045 /* ignore more BYEs */
2046 if (source->received_bye)
2049 /* we have BYE now */
2050 source->received_bye = TRUE;
2051 /* at least one member wants to send a BYE */
2052 g_free (sess->bye_reason);
2053 sess->bye_reason = g_strdup (reason);
2054 sess->stats.avg_rtcp_packet_size = 100;
2055 sess->stats.bye_members = 1;
2056 sess->first_rtcp = TRUE;
2057 sess->sent_bye = FALSE;
2059 /* reschedule transmission */
2060 sess->last_rtcp_send_time = current_time;
2061 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2062 sess->next_rtcp_check_time = current_time + interval;
2064 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2065 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2067 RTP_SESSION_UNLOCK (sess);
2068 /* notify app of reconsideration */
2069 if (sess->callbacks.reconsider)
2070 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2071 RTP_SESSION_LOCK (sess);
2078 * rtp_session_send_bye:
2079 * @sess: an #RTPSession
2080 * @reason: a reason or NULL
2081 * @current_time: the current system time
2083 * Stop the current @sess and schedule a BYE message for the other members.
2085 * One must have the session lock to call this function
2087 * Returns: a #GstFlowReturn.
2090 rtp_session_send_bye (RTPSession * sess, const gchar * reason,
2091 GstClockTime current_time)
2093 GstFlowReturn result = GST_FLOW_OK;
2095 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2097 RTP_SESSION_LOCK (sess);
2098 result = rtp_session_send_bye_locked (sess, reason, current_time);
2099 RTP_SESSION_UNLOCK (sess);
2105 * rtp_session_next_timeout:
2106 * @sess: an #RTPSession
2107 * @current_time: the current system time
2109 * Get the next time we should perform session maintenance tasks.
2111 * Returns: a time when rtp_session_on_timeout() should be called with the
2112 * current system time.
2115 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2117 GstClockTime result;
2119 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2121 RTP_SESSION_LOCK (sess);
2123 result = sess->next_rtcp_check_time;
2125 GST_DEBUG ("current time: %" GST_TIME_FORMAT ", next :%" GST_TIME_FORMAT,
2126 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2128 if (result < current_time) {
2129 GST_DEBUG ("take current time as base");
2130 /* our previous check time expired, start counting from the current time
2132 result = current_time;
2135 if (sess->source->received_bye) {
2136 if (sess->sent_bye) {
2137 GST_DEBUG ("we sent BYE already");
2138 result = GST_CLOCK_TIME_NONE;
2139 } else if (sess->stats.active_sources >= 50) {
2140 GST_DEBUG ("reconsider BYE, more than 50 sources");
2141 /* reconsider BYE if members >= 50 */
2142 result += calculate_rtcp_interval (sess, FALSE, TRUE);
2145 if (sess->first_rtcp) {
2146 GST_DEBUG ("first RTCP packet");
2147 /* we are called for the first time */
2148 result += calculate_rtcp_interval (sess, FALSE, TRUE);
2149 } else if (sess->next_rtcp_check_time < current_time) {
2150 GST_DEBUG ("old check time expired, getting new timeout");
2151 /* get a new timeout when we need to */
2152 result += calculate_rtcp_interval (sess, FALSE, FALSE);
2155 sess->next_rtcp_check_time = result;
2157 GST_DEBUG ("next timeout: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2158 RTP_SESSION_UNLOCK (sess);
2167 GstClockTime current_time;
2169 GstClockTime interval;
2170 GstRTCPPacket packet;
2176 session_start_rtcp (RTPSession * sess, ReportData * data)
2178 GstRTCPPacket *packet = &data->packet;
2179 RTPSource *own = sess->source;
2181 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2183 if (RTP_SOURCE_IS_SENDER (own)) {
2186 guint32 packet_count, octet_count;
2188 /* we are a sender, create SR */
2189 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2190 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SR, packet);
2192 /* get latest stats */
2193 rtp_source_get_new_sr (own, data->ntpnstime, &ntptime, &rtptime,
2194 &packet_count, &octet_count);
2196 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2197 packet_count, octet_count);
2199 /* fill in sender report info */
2200 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2201 ntptime, rtptime, packet_count, octet_count);
2203 /* we are only receiver, create RR */
2204 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2205 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_RR, packet);
2206 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2210 /* construct a Sender or Receiver Report */
2212 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2214 RTPSession *sess = data->sess;
2215 GstRTCPPacket *packet = &data->packet;
2217 /* create a new buffer if needed */
2218 if (data->rtcp == NULL) {
2219 session_start_rtcp (sess, data);
2221 if (gst_rtcp_packet_get_rb_count (packet) < GST_RTCP_MAX_RB_COUNT) {
2222 /* only report about other sender sources */
2223 if (source != sess->source && RTP_SOURCE_IS_SENDER (source)) {
2224 guint8 fractionlost;
2226 guint32 exthighestseq, jitter;
2230 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2231 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2233 /* packet is not yet filled, add report block for this source. */
2234 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2235 exthighestseq, jitter, lsr, dlsr);
2240 /* perform cleanup of sources that timed out */
2242 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2244 gboolean remove = FALSE;
2245 gboolean byetimeout = FALSE;
2246 gboolean sendertimeout = FALSE;
2247 gboolean is_sender, is_active;
2248 RTPSession *sess = data->sess;
2249 GstClockTime interval;
2251 is_sender = RTP_SOURCE_IS_SENDER (source);
2252 is_active = RTP_SOURCE_IS_ACTIVE (source);
2254 /* check for our own source, we don't want to delete our own source. */
2255 if (!(source == sess->source)) {
2256 if (source->received_bye) {
2257 /* if we received a BYE from the source, remove the source after some
2259 if (data->current_time > source->bye_time &&
2260 data->current_time - source->bye_time > sess->stats.bye_timeout) {
2261 GST_DEBUG ("removing BYE source %08x", source->ssrc);
2266 /* sources that were inactive for more than 5 times the deterministic reporting
2267 * interval get timed out. the min timeout is 5 seconds. */
2268 if (data->current_time > source->last_activity) {
2269 interval = MAX (data->interval * 5, 5 * GST_SECOND);
2270 if (data->current_time - source->last_activity > interval) {
2271 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2272 source->ssrc, GST_TIME_ARGS (source->last_activity));
2278 /* senders that did not send for a long time become a receiver, this also
2279 * holds for our own source. */
2281 if (data->current_time > source->last_rtp_activity) {
2282 interval = MAX (data->interval * 2, 5 * GST_SECOND);
2283 if (data->current_time - source->last_rtp_activity > interval) {
2284 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
2285 GST_TIME_FORMAT, source->ssrc,
2286 GST_TIME_ARGS (source->last_rtp_activity));
2287 source->is_sender = FALSE;
2288 sess->stats.sender_sources--;
2289 sendertimeout = TRUE;
2295 sess->total_sources--;
2297 sess->stats.sender_sources--;
2299 sess->stats.active_sources--;
2302 on_bye_timeout (sess, source);
2304 on_timeout (sess, source);
2307 on_sender_timeout (sess, source);
2313 session_sdes (RTPSession * sess, ReportData * data)
2315 GstRTCPPacket *packet = &data->packet;
2319 /* add SDES packet */
2320 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SDES, packet);
2322 gst_rtcp_packet_sdes_add_item (packet, sess->source->ssrc);
2324 rtp_source_get_sdes (sess->source, GST_RTCP_SDES_CNAME, &sdes_data,
2326 gst_rtcp_packet_sdes_add_entry (packet, GST_RTCP_SDES_CNAME, sdes_len,
2329 /* other SDES items must only be added at regular intervals and only when the
2330 * user requests to since it might be a privacy problem */
2332 gst_rtcp_packet_sdes_add_entry (&packet, GST_RTCP_SDES_NAME,
2333 strlen (sess->name), (guint8 *) sess->name);
2334 gst_rtcp_packet_sdes_add_entry (&packet, GST_RTCP_SDES_TOOL,
2335 strlen (sess->tool), (guint8 *) sess->tool);
2338 data->has_sdes = TRUE;
2341 /* schedule a BYE packet */
2343 session_bye (RTPSession * sess, ReportData * data)
2345 GstRTCPPacket *packet = &data->packet;
2348 session_start_rtcp (sess, data);
2351 session_sdes (sess, data);
2353 /* add a BYE packet */
2354 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_BYE, packet);
2355 gst_rtcp_packet_bye_add_ssrc (packet, sess->source->ssrc);
2356 if (sess->bye_reason)
2357 gst_rtcp_packet_bye_set_reason (packet, sess->bye_reason);
2359 /* we have a BYE packet now */
2360 data->is_bye = TRUE;
2364 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
2366 GstClockTime new_send_time, elapsed;
2369 /* no need to check yet */
2370 if (sess->next_rtcp_check_time > current_time) {
2371 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
2372 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
2373 GST_TIME_ARGS (current_time));
2377 /* get elapsed time since we last reported */
2378 elapsed = current_time - sess->last_rtcp_send_time;
2380 /* perform forward reconsideration */
2381 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, data->interval);
2383 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
2384 GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time), GST_TIME_ARGS (elapsed));
2386 new_send_time += sess->last_rtcp_send_time;
2388 /* check if reconsideration */
2389 if (current_time < new_send_time) {
2390 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
2391 GST_TIME_ARGS (new_send_time));
2393 /* store new check time */
2394 sess->next_rtcp_check_time = new_send_time;
2397 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
2399 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
2400 GST_TIME_ARGS (new_send_time));
2401 sess->next_rtcp_check_time = current_time + new_send_time;
2407 * rtp_session_on_timeout:
2408 * @sess: an #RTPSession
2409 * @current_time: the current system time
2410 * @ntpnstime: the current NTP time in nanoseconds
2412 * Perform maintenance actions after the timeout obtained with
2413 * rtp_session_next_timeout() expired.
2415 * This function will perform timeouts of receivers and senders, send a BYE
2416 * packet or generate RTCP packets with current session stats.
2418 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
2419 * times, for each packet that should be processed.
2421 * Returns: a #GstFlowReturn.
2424 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
2427 GstFlowReturn result = GST_FLOW_OK;
2432 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2434 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT,
2435 GST_TIME_ARGS (current_time), GST_TIME_ARGS (ntpnstime));
2439 data.current_time = current_time;
2440 data.ntpnstime = ntpnstime;
2441 data.is_bye = FALSE;
2442 data.has_sdes = FALSE;
2446 RTP_SESSION_LOCK (sess);
2447 /* get a new interval, we need this for various cleanups etc */
2448 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
2450 /* first perform cleanups */
2451 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
2452 (GHRFunc) session_cleanup, &data);
2454 /* see if we need to generate SR or RR packets */
2455 if (is_rtcp_time (sess, current_time, &data)) {
2456 if (own->received_bye) {
2457 /* generate BYE instead */
2458 GST_DEBUG ("generating BYE message");
2459 session_bye (sess, &data);
2460 sess->sent_bye = TRUE;
2462 /* loop over all known sources and do something */
2463 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2464 (GHFunc) session_report_blocks, &data);
2471 /* we keep track of the last report time in order to timeout inactive
2472 * receivers or senders */
2473 sess->last_rtcp_send_time = data.current_time;
2474 sess->first_rtcp = FALSE;
2476 /* add SDES for this source when not already added */
2478 session_sdes (sess, &data);
2480 /* update average RTCP size before sending */
2481 size = GST_BUFFER_SIZE (data.rtcp) + sess->header_len;
2482 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, size);
2485 /* check for outdated collisions */
2486 GST_DEBUG ("checking collision list");
2487 item = g_list_first (sess->conflicting_addresses);
2489 RTPConflictingAddress *known_conflict = item->data;
2490 GList *next_item = g_list_next (item);
2492 if (known_conflict->time < current_time - (data.interval *
2493 RTCP_INTERVAL_COLLISION_TIMEOUT)) {
2494 sess->conflicting_addresses =
2495 g_list_delete_link (sess->conflicting_addresses, item);
2496 GST_DEBUG ("collision %p timed out", known_conflict);
2497 g_free (known_conflict);
2502 if (sess->change_ssrc) {
2503 GST_DEBUG ("need to change our SSRC (%08x)", own->ssrc);
2504 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
2505 GINT_TO_POINTER (own->ssrc));
2507 own->ssrc = rtp_session_create_new_ssrc (sess);
2508 rtp_source_reset (own);
2510 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
2511 GINT_TO_POINTER (own->ssrc), own);
2513 g_free (sess->bye_reason);
2514 sess->bye_reason = NULL;
2515 sess->sent_bye = FALSE;
2516 sess->change_ssrc = FALSE;
2517 GST_DEBUG ("changed our SSRC to %08x", own->ssrc);
2519 RTP_SESSION_UNLOCK (sess);
2521 /* push out the RTCP packet */
2523 /* close the RTCP packet */
2524 gst_rtcp_buffer_end (data.rtcp);
2526 GST_DEBUG ("sending packet");
2527 if (sess->callbacks.send_rtcp)
2528 result = sess->callbacks.send_rtcp (sess, own, data.rtcp,
2529 sess->sent_bye, sess->send_rtcp_user_data);
2531 GST_DEBUG ("freeing packet");
2532 gst_buffer_unref (data.rtcp);