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
75 PROP_NUM_ACTIVE_SOURCES,
79 /* update average packet size, we keep this scaled by 16 to keep enough
81 #define UPDATE_AVG(avg, val) \
85 (avg) = ((val) + (15 * (avg))) >> 4;
87 /* The number RTCP intervals after which to timeout entries in the
90 #define RTCP_INTERVAL_COLLISION_TIMEOUT 10
92 /* GObject vmethods */
93 static void rtp_session_finalize (GObject * object);
94 static void rtp_session_set_property (GObject * object, guint prop_id,
95 const GValue * value, GParamSpec * pspec);
96 static void rtp_session_get_property (GObject * object, guint prop_id,
97 GValue * value, GParamSpec * pspec);
99 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
101 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
103 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
104 gboolean * created, RTPArrivalStats * arrival, gboolean rtp);
105 static GstFlowReturn rtp_session_send_bye_locked (RTPSession * sess,
106 const gchar * reason, GstClockTime current_time);
107 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
108 gboolean deterministic, gboolean first);
111 rtp_session_class_init (RTPSessionClass * klass)
113 GObjectClass *gobject_class;
115 gobject_class = (GObjectClass *) klass;
117 gobject_class->finalize = rtp_session_finalize;
118 gobject_class->set_property = rtp_session_set_property;
119 gobject_class->get_property = rtp_session_get_property;
122 * RTPSession::get-source-by-ssrc:
123 * @session: the object which received the signal
124 * @ssrc: the SSRC of the RTPSource
126 * Request the #RTPSource object with SSRC @ssrc in @session.
128 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
129 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
130 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
131 get_source_by_ssrc), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
132 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
135 * RTPSession::on-new-ssrc:
136 * @session: the object which received the signal
137 * @src: the new RTPSource
139 * Notify of a new SSRC that entered @session.
141 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
142 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
143 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
144 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
147 * RTPSession::on-ssrc-collision:
148 * @session: the object which received the signal
149 * @src: the #RTPSource that caused a collision
151 * Notify when we have an SSRC collision
153 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
154 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
155 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
156 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
159 * RTPSession::on-ssrc-validated:
160 * @session: the object which received the signal
161 * @src: the new validated RTPSource
163 * Notify of a new SSRC that became validated.
165 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
166 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
167 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
168 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
171 * RTPSession::on-ssrc-active:
172 * @session: the object which received the signal
173 * @src: the active RTPSource
175 * Notify of a SSRC that is active, i.e., sending RTCP.
177 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
178 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
179 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
180 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
183 * RTPSession::on-ssrc-sdes:
184 * @session: the object which received the signal
185 * @src: the RTPSource
187 * Notify that a new SDES was received for SSRC.
189 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
190 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
191 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
192 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
195 * RTPSession::on-bye-ssrc:
196 * @session: the object which received the signal
197 * @src: the RTPSource that went away
199 * Notify of an SSRC that became inactive because of a BYE packet.
201 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
202 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
203 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
204 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
207 * RTPSession::on-bye-timeout:
208 * @session: the object which received the signal
209 * @src: the RTPSource that timed out
211 * Notify of an SSRC that has timed out because of BYE
213 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
214 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
215 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
216 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
219 * RTPSession::on-timeout:
220 * @session: the object which received the signal
221 * @src: the RTPSource that timed out
223 * Notify of an SSRC that has timed out
225 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
226 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
227 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
228 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
231 * RTPSession::on-sender-timeout:
232 * @session: the object which received the signal
233 * @src: the RTPSource that timed out
235 * Notify of an SSRC that was a sender but timed out and became a receiver.
237 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
238 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
239 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
240 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
243 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
244 g_param_spec_object ("internal-source", "Internal Source",
245 "The internal source element of the session",
246 RTP_TYPE_SOURCE, G_PARAM_READABLE));
248 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
249 g_param_spec_double ("bandwidth", "Bandwidth",
250 "The bandwidth of the session",
251 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH, G_PARAM_READWRITE));
253 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
254 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
255 "The fraction of the bandwidth used for RTCP",
256 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION, G_PARAM_READWRITE));
258 g_object_class_install_property (gobject_class, PROP_SDES_CNAME,
259 g_param_spec_string ("sdes-cname", "SDES CNAME",
260 "The CNAME to put in SDES messages of this session",
261 DEFAULT_SDES_CNAME, G_PARAM_READWRITE));
263 g_object_class_install_property (gobject_class, PROP_SDES_NAME,
264 g_param_spec_string ("sdes-name", "SDES NAME",
265 "The NAME to put in SDES messages of this session",
266 DEFAULT_SDES_NAME, G_PARAM_READWRITE));
268 g_object_class_install_property (gobject_class, PROP_SDES_EMAIL,
269 g_param_spec_string ("sdes-email", "SDES EMAIL",
270 "The EMAIL to put in SDES messages of this session",
271 DEFAULT_SDES_EMAIL, G_PARAM_READWRITE));
273 g_object_class_install_property (gobject_class, PROP_SDES_PHONE,
274 g_param_spec_string ("sdes-phone", "SDES PHONE",
275 "The PHONE to put in SDES messages of this session",
276 DEFAULT_SDES_PHONE, G_PARAM_READWRITE));
278 g_object_class_install_property (gobject_class, PROP_SDES_LOCATION,
279 g_param_spec_string ("sdes-location", "SDES LOCATION",
280 "The LOCATION to put in SDES messages of this session",
281 DEFAULT_SDES_LOCATION, G_PARAM_READWRITE));
283 g_object_class_install_property (gobject_class, PROP_SDES_TOOL,
284 g_param_spec_string ("sdes-tool", "SDES TOOL",
285 "The TOOL to put in SDES messages of this session",
286 DEFAULT_SDES_TOOL, G_PARAM_READWRITE));
288 g_object_class_install_property (gobject_class, PROP_SDES_NOTE,
289 g_param_spec_string ("sdes-note", "SDES NOTE",
290 "The NOTE to put in SDES messages of this session",
291 DEFAULT_SDES_NOTE, G_PARAM_READWRITE));
293 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
294 g_param_spec_uint ("num-sources", "Num Sources",
295 "The number of sources in the session", 0, G_MAXUINT,
296 DEFAULT_NUM_SOURCES, G_PARAM_READABLE));
298 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
299 g_param_spec_uint ("num-active-sources", "Num Active Sources",
300 "The number of active sources in the session", 0, G_MAXUINT,
301 DEFAULT_NUM_ACTIVE_SOURCES, G_PARAM_READABLE));
303 klass->get_source_by_ssrc =
304 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
306 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
310 rtp_session_init (RTPSession * sess)
315 sess->lock = g_mutex_new ();
316 sess->key = g_random_int ();
320 for (i = 0; i < 32; i++) {
322 g_hash_table_new_full (NULL, NULL, NULL,
323 (GDestroyNotify) g_object_unref);
325 sess->cnames = g_hash_table_new_full (NULL, NULL, g_free, NULL);
327 rtp_stats_init_defaults (&sess->stats);
329 /* create an active SSRC for this session manager */
330 sess->source = rtp_session_create_source (sess);
331 sess->source->validated = TRUE;
332 sess->source->internal = TRUE;
333 sess->stats.active_sources++;
335 /* default UDP header length */
336 sess->header_len = 28;
339 /* some default SDES entries */
340 str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
341 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_CNAME, str);
344 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_NAME,
346 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_TOOL, "GStreamer");
348 sess->first_rtcp = TRUE;
350 GST_DEBUG ("%p: session using SSRC: %08x", sess, sess->source->ssrc);
354 rtp_session_finalize (GObject * object)
359 sess = RTP_SESSION_CAST (object);
361 g_mutex_free (sess->lock);
362 for (i = 0; i < 32; i++)
363 g_hash_table_destroy (sess->ssrcs[i]);
365 g_free (sess->bye_reason);
367 g_hash_table_destroy (sess->cnames);
368 g_object_unref (sess->source);
370 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
374 rtp_session_set_property (GObject * object, guint prop_id,
375 const GValue * value, GParamSpec * pspec)
379 sess = RTP_SESSION (object);
383 rtp_session_set_bandwidth (sess, g_value_get_double (value));
385 case PROP_RTCP_FRACTION:
386 rtp_session_set_rtcp_fraction (sess, g_value_get_double (value));
388 case PROP_SDES_CNAME:
389 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_CNAME,
390 g_value_get_string (value));
393 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_NAME,
394 g_value_get_string (value));
396 case PROP_SDES_EMAIL:
397 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_EMAIL,
398 g_value_get_string (value));
400 case PROP_SDES_PHONE:
401 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_PHONE,
402 g_value_get_string (value));
404 case PROP_SDES_LOCATION:
405 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_LOC,
406 g_value_get_string (value));
409 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_TOOL,
410 g_value_get_string (value));
413 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_NOTE,
414 g_value_get_string (value));
417 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
423 rtp_session_get_property (GObject * object, guint prop_id,
424 GValue * value, GParamSpec * pspec)
428 sess = RTP_SESSION (object);
431 case PROP_INTERNAL_SOURCE:
432 g_value_take_object (value, rtp_session_get_internal_source (sess));
435 g_value_set_double (value, rtp_session_get_bandwidth (sess));
437 case PROP_RTCP_FRACTION:
438 g_value_set_double (value, rtp_session_get_rtcp_fraction (sess));
440 case PROP_SDES_CNAME:
441 g_value_take_string (value, rtp_session_get_sdes_string (sess,
442 GST_RTCP_SDES_CNAME));
445 g_value_take_string (value, rtp_session_get_sdes_string (sess,
446 GST_RTCP_SDES_NAME));
448 case PROP_SDES_EMAIL:
449 g_value_take_string (value, rtp_session_get_sdes_string (sess,
450 GST_RTCP_SDES_EMAIL));
452 case PROP_SDES_PHONE:
453 g_value_take_string (value, rtp_session_get_sdes_string (sess,
454 GST_RTCP_SDES_PHONE));
456 case PROP_SDES_LOCATION:
457 g_value_take_string (value, rtp_session_get_sdes_string (sess,
461 g_value_take_string (value, rtp_session_get_sdes_string (sess,
462 GST_RTCP_SDES_TOOL));
465 g_value_take_string (value, rtp_session_get_sdes_string (sess,
466 GST_RTCP_SDES_NOTE));
468 case PROP_NUM_SOURCES:
469 g_value_set_uint (value, rtp_session_get_num_sources (sess));
471 case PROP_NUM_ACTIVE_SOURCES:
472 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
475 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
481 on_new_ssrc (RTPSession * sess, RTPSource * source)
483 g_object_ref (source);
484 RTP_SESSION_UNLOCK (sess);
485 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
486 RTP_SESSION_LOCK (sess);
487 g_object_unref (source);
491 on_ssrc_collision (RTPSession * sess, RTPSource * source)
493 g_object_ref (source);
494 RTP_SESSION_UNLOCK (sess);
495 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
497 RTP_SESSION_LOCK (sess);
498 g_object_unref (source);
502 on_ssrc_validated (RTPSession * sess, RTPSource * source)
504 g_object_ref (source);
505 RTP_SESSION_UNLOCK (sess);
506 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
508 RTP_SESSION_LOCK (sess);
509 g_object_unref (source);
513 on_ssrc_active (RTPSession * sess, RTPSource * source)
515 g_object_ref (source);
516 RTP_SESSION_UNLOCK (sess);
517 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
518 RTP_SESSION_LOCK (sess);
519 g_object_unref (source);
523 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
525 g_object_ref (source);
526 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
527 RTP_SESSION_UNLOCK (sess);
528 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
529 RTP_SESSION_LOCK (sess);
530 g_object_unref (source);
534 on_bye_ssrc (RTPSession * sess, RTPSource * source)
536 g_object_ref (source);
537 RTP_SESSION_UNLOCK (sess);
538 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
539 RTP_SESSION_LOCK (sess);
540 g_object_unref (source);
544 on_bye_timeout (RTPSession * sess, RTPSource * source)
546 g_object_ref (source);
547 RTP_SESSION_UNLOCK (sess);
548 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
549 RTP_SESSION_LOCK (sess);
550 g_object_unref (source);
554 on_timeout (RTPSession * sess, RTPSource * source)
556 g_object_ref (source);
557 RTP_SESSION_UNLOCK (sess);
558 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
559 RTP_SESSION_LOCK (sess);
560 g_object_unref (source);
564 on_sender_timeout (RTPSession * sess, RTPSource * source)
566 g_object_ref (source);
567 RTP_SESSION_UNLOCK (sess);
568 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
570 RTP_SESSION_LOCK (sess);
571 g_object_unref (source);
577 * Create a new session object.
579 * Returns: a new #RTPSession. g_object_unref() after usage.
582 rtp_session_new (void)
586 sess = g_object_new (RTP_TYPE_SESSION, NULL);
592 * rtp_session_set_callbacks:
593 * @sess: an #RTPSession
594 * @callbacks: callbacks to configure
595 * @user_data: user data passed in the callbacks
597 * Configure a set of callbacks to be notified of actions.
600 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
603 g_return_if_fail (RTP_IS_SESSION (sess));
605 if (callbacks->process_rtp) {
606 sess->callbacks.process_rtp = callbacks->process_rtp;
607 sess->process_rtp_user_data = user_data;
609 if (callbacks->send_rtp) {
610 sess->callbacks.send_rtp = callbacks->send_rtp;
611 sess->send_rtp_user_data = user_data;
613 if (callbacks->send_rtcp) {
614 sess->callbacks.send_rtcp = callbacks->send_rtcp;
615 sess->send_rtcp_user_data = user_data;
617 if (callbacks->sync_rtcp) {
618 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
619 sess->sync_rtcp_user_data = user_data;
621 if (callbacks->clock_rate) {
622 sess->callbacks.clock_rate = callbacks->clock_rate;
623 sess->clock_rate_user_data = user_data;
625 if (callbacks->reconsider) {
626 sess->callbacks.reconsider = callbacks->reconsider;
627 sess->reconsider_user_data = user_data;
632 * rtp_session_set_process_rtp_callback:
633 * @sess: an #RTPSession
634 * @callback: callback to set
635 * @user_data: user data passed in the callback
637 * Configure only the process_rtp callback to be notified of the process_rtp action.
640 rtp_session_set_process_rtp_callback (RTPSession * sess,
641 RTPSessionProcessRTP callback, gpointer user_data)
643 g_return_if_fail (RTP_IS_SESSION (sess));
645 sess->callbacks.process_rtp = callback;
646 sess->process_rtp_user_data = user_data;
650 * rtp_session_set_send_rtp_callback:
651 * @sess: an #RTPSession
652 * @callback: callback to set
653 * @user_data: user data passed in the callback
655 * Configure only the send_rtp callback to be notified of the send_rtp action.
658 rtp_session_set_send_rtp_callback (RTPSession * sess,
659 RTPSessionSendRTP callback, gpointer user_data)
661 g_return_if_fail (RTP_IS_SESSION (sess));
663 sess->callbacks.send_rtp = callback;
664 sess->send_rtp_user_data = user_data;
668 * rtp_session_set_send_rtcp_callback:
669 * @sess: an #RTPSession
670 * @callback: callback to set
671 * @user_data: user data passed in the callback
673 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
676 rtp_session_set_send_rtcp_callback (RTPSession * sess,
677 RTPSessionSendRTCP callback, gpointer user_data)
679 g_return_if_fail (RTP_IS_SESSION (sess));
681 sess->callbacks.send_rtcp = callback;
682 sess->send_rtcp_user_data = user_data;
686 * rtp_session_set_sync_rtcp_callback:
687 * @sess: an #RTPSession
688 * @callback: callback to set
689 * @user_data: user data passed in the callback
691 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
694 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
695 RTPSessionSyncRTCP callback, gpointer user_data)
697 g_return_if_fail (RTP_IS_SESSION (sess));
699 sess->callbacks.sync_rtcp = callback;
700 sess->sync_rtcp_user_data = user_data;
704 * rtp_session_set_clock_rate_callback:
705 * @sess: an #RTPSession
706 * @callback: callback to set
707 * @user_data: user data passed in the callback
709 * Configure only the clock_rate callback to be notified of the clock_rate action.
712 rtp_session_set_clock_rate_callback (RTPSession * sess,
713 RTPSessionClockRate callback, gpointer user_data)
715 g_return_if_fail (RTP_IS_SESSION (sess));
717 sess->callbacks.clock_rate = callback;
718 sess->clock_rate_user_data = user_data;
722 * rtp_session_set_reconsider_callback:
723 * @sess: an #RTPSession
724 * @callback: callback to set
725 * @user_data: user data passed in the callback
727 * Configure only the reconsider callback to be notified of the reconsider action.
730 rtp_session_set_reconsider_callback (RTPSession * sess,
731 RTPSessionReconsider callback, gpointer user_data)
733 g_return_if_fail (RTP_IS_SESSION (sess));
735 sess->callbacks.reconsider = callback;
736 sess->reconsider_user_data = user_data;
740 * rtp_session_set_bandwidth:
741 * @sess: an #RTPSession
742 * @bandwidth: the bandwidth allocated
744 * Set the session bandwidth in bytes per second.
747 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
749 g_return_if_fail (RTP_IS_SESSION (sess));
751 RTP_SESSION_LOCK (sess);
752 sess->stats.bandwidth = bandwidth;
753 RTP_SESSION_UNLOCK (sess);
757 * rtp_session_get_bandwidth:
758 * @sess: an #RTPSession
760 * Get the session bandwidth.
762 * Returns: the session bandwidth.
765 rtp_session_get_bandwidth (RTPSession * sess)
769 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
771 RTP_SESSION_LOCK (sess);
772 result = sess->stats.bandwidth;
773 RTP_SESSION_UNLOCK (sess);
779 * rtp_session_set_rtcp_fraction:
780 * @sess: an #RTPSession
781 * @bandwidth: the RTCP bandwidth
783 * Set the bandwidth that should be used for RTCP
787 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
789 g_return_if_fail (RTP_IS_SESSION (sess));
791 RTP_SESSION_LOCK (sess);
792 sess->stats.rtcp_bandwidth = bandwidth;
793 RTP_SESSION_UNLOCK (sess);
797 * rtp_session_get_rtcp_fraction:
798 * @sess: an #RTPSession
800 * Get the session bandwidth used for RTCP.
802 * Returns: The bandwidth used for RTCP messages.
805 rtp_session_get_rtcp_fraction (RTPSession * sess)
809 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
811 RTP_SESSION_LOCK (sess);
812 result = sess->stats.rtcp_bandwidth;
813 RTP_SESSION_UNLOCK (sess);
819 * rtp_session_set_sdes_string:
820 * @sess: an #RTPSession
821 * @type: the type of the SDES item
822 * @item: a null-terminated string to set.
824 * Store an SDES item of @type in @sess.
826 * Returns: %FALSE if the data was unchanged @type is invalid.
829 rtp_session_set_sdes_string (RTPSession * sess, GstRTCPSDESType type,
834 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
836 RTP_SESSION_LOCK (sess);
837 result = rtp_source_set_sdes_string (sess->source, type, item);
838 RTP_SESSION_UNLOCK (sess);
844 * rtp_session_get_sdes_string:
845 * @sess: an #RTPSession
846 * @type: the type of the SDES item
848 * Get the SDES item of @type from @sess.
850 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
851 * valid. g_free() after usage.
854 rtp_session_get_sdes_string (RTPSession * sess, GstRTCPSDESType type)
858 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
860 RTP_SESSION_LOCK (sess);
861 result = rtp_source_get_sdes_string (sess->source, type);
862 RTP_SESSION_UNLOCK (sess);
868 source_push_rtp (RTPSource * source, GstBuffer * buffer, RTPSession * session)
870 GstFlowReturn result = GST_FLOW_OK;
872 if (source == session->source) {
873 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
875 RTP_SESSION_UNLOCK (session);
877 if (session->callbacks.send_rtp)
879 session->callbacks.send_rtp (session, source, buffer,
880 session->send_rtp_user_data);
882 gst_buffer_unref (buffer);
885 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
886 RTP_SESSION_UNLOCK (session);
888 if (session->callbacks.process_rtp)
890 session->callbacks.process_rtp (session, source, buffer,
891 session->process_rtp_user_data);
893 gst_buffer_unref (buffer);
895 RTP_SESSION_LOCK (session);
901 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
905 RTP_SESSION_UNLOCK (session);
907 if (session->callbacks.clock_rate)
909 session->callbacks.clock_rate (session, pt,
910 session->clock_rate_user_data);
914 RTP_SESSION_LOCK (session);
916 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
921 static RTPSourceCallbacks callbacks = {
922 (RTPSourcePushRTP) source_push_rtp,
923 (RTPSourceClockRate) source_clock_rate,
927 * find_add_conflicting_addresses:
928 * @sess: The session to check in
929 * @arrival: The arrival stats for the buffer
931 * Checks if an address which has a conflict is already known,
932 * otherwise remembers it to prevent loops.
934 * Returns: TRUE if it was a known conflict, FALSE otherwise
938 find_add_conflicting_addresses (RTPSession * sess, RTPArrivalStats * arrival)
941 RTPConflictingAddress *new_conflict;
943 for (item = g_list_first (sess->conflicting_addresses);
944 item; item = g_list_next (item)) {
945 RTPConflictingAddress *known_conflict = item->data;
947 if (gst_netaddress_equal (&arrival->address, &known_conflict->address)) {
948 known_conflict->time = arrival->time;
953 new_conflict = g_new0 (RTPConflictingAddress, 1);
955 memcpy (&new_conflict->address, &arrival->address, sizeof (GstNetAddress));
956 new_conflict->time = arrival->time;
958 sess->conflicting_addresses = g_list_prepend (sess->conflicting_addresses,
965 check_collision (RTPSession * sess, RTPSource * source,
966 RTPArrivalStats * arrival, gboolean rtp)
968 /* If we have no arrival address, we can't do collision checking */
969 if (!arrival->have_address)
972 if (sess->source != source) {
973 /* This is not our local source, but lets check if two remote
977 if (source->have_rtp_from) {
978 if (gst_netaddress_equal (&source->rtp_from, &arrival->address))
979 /* Address is the same */
982 /* We don't already have a from address for RTP, just set it */
983 rtp_source_set_rtp_from (source, &arrival->address);
987 if (source->have_rtcp_from) {
988 if (gst_netaddress_equal (&source->rtcp_from, &arrival->address))
989 /* Address is the same */
992 /* We don't already have a from address for RTCP, just set it */
993 rtp_source_set_rtcp_from (source, &arrival->address);
997 /* We received RTP or RTCP from this source before but the network address
998 * changed. In this case, we have third-party collision or loop */
999 GST_DEBUG ("we have a third-party collision or loop");
1001 /* FIXME: Log 3rd party collision somehow
1002 * Maybe should be done in upper layer, only the SDES can tell us
1003 * if its a collision or a loop
1006 /* This is sending with our ssrc, is it an address we already know */
1008 if (find_add_conflicting_addresses (sess, arrival)) {
1009 /* Its a known conflict, its probably a loop, not a collision
1010 * lets just drop the incoming packet
1012 GST_DEBUG ("Our packets are being looped back to us, dropping");
1014 /* Its a new collision, lets change our SSRC */
1016 GST_DEBUG ("Collision for SSRC %x", rtp_source_get_ssrc (source));
1017 on_ssrc_collision (sess, source);
1019 rtp_session_send_bye_locked (sess, "SSRC Collision", arrival->time);
1021 sess->change_ssrc = TRUE;
1029 /* must be called with the session lock */
1031 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1032 RTPArrivalStats * arrival, gboolean rtp)
1037 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1038 if (source == NULL) {
1039 /* make new Source in probation and insert */
1040 source = rtp_source_new (ssrc);
1042 /* for RTP packets we need to set the source in probation. Receiving RTCP
1043 * packets of an SSRC, on the other hand, is a strong indication that we
1044 * are dealing with a valid source. */
1046 source->probation = RTP_DEFAULT_PROBATION;
1048 source->probation = 0;
1050 /* store from address, if any */
1051 if (arrival->have_address) {
1053 rtp_source_set_rtp_from (source, &arrival->address);
1055 rtp_source_set_rtcp_from (source, &arrival->address);
1058 /* configure a callback on the source */
1059 rtp_source_set_callbacks (source, &callbacks, sess);
1061 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1064 /* we have one more source now */
1065 sess->total_sources++;
1069 /* check for collision, this updates the address when not previously set */
1070 if (check_collision (sess, source, arrival, rtp)) {
1074 /* update last activity */
1075 source->last_activity = arrival->time;
1077 source->last_rtp_activity = arrival->time;
1083 * rtp_session_get_internal_source:
1084 * @sess: a #RTPSession
1086 * Get the internal #RTPSource of @sess.
1088 * Returns: The internal #RTPSource. g_object_unref() after usage.
1091 rtp_session_get_internal_source (RTPSession * sess)
1095 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1097 result = g_object_ref (sess->source);
1103 * rtp_session_set_internal_ssrc:
1104 * @sess: a #RTPSession
1107 * Set the SSRC of @sess to @ssrc.
1110 rtp_session_set_internal_ssrc (RTPSession * sess, guint32 ssrc)
1112 RTP_SESSION_LOCK (sess);
1113 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
1114 GINT_TO_POINTER (sess->source->ssrc));
1116 sess->source->ssrc = ssrc;
1117 rtp_source_reset (sess->source);
1119 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1120 GINT_TO_POINTER (sess->source->ssrc), sess->source);
1121 RTP_SESSION_UNLOCK (sess);
1125 * rtp_session_get_internal_ssrc:
1126 * @sess: a #RTPSession
1128 * Get the internal SSRC of @sess.
1130 * Returns: The SSRC of the session.
1133 rtp_session_get_internal_ssrc (RTPSession * sess)
1137 RTP_SESSION_LOCK (sess);
1138 ssrc = sess->source->ssrc;
1139 RTP_SESSION_UNLOCK (sess);
1145 * rtp_session_add_source:
1146 * @sess: a #RTPSession
1147 * @src: #RTPSource to add
1149 * Add @src to @session.
1151 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1152 * existed in the session.
1155 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1157 gboolean result = FALSE;
1160 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1161 g_return_val_if_fail (src != NULL, FALSE);
1163 RTP_SESSION_LOCK (sess);
1165 g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1166 GINT_TO_POINTER (src->ssrc));
1168 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1169 GINT_TO_POINTER (src->ssrc), src);
1170 /* we have one more source now */
1171 sess->total_sources++;
1174 RTP_SESSION_UNLOCK (sess);
1180 * rtp_session_get_num_sources:
1181 * @sess: an #RTPSession
1183 * Get the number of sources in @sess.
1185 * Returns: The number of sources in @sess.
1188 rtp_session_get_num_sources (RTPSession * sess)
1192 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1194 RTP_SESSION_LOCK (sess);
1195 result = sess->total_sources;
1196 RTP_SESSION_UNLOCK (sess);
1202 * rtp_session_get_num_active_sources:
1203 * @sess: an #RTPSession
1205 * Get the number of active sources in @sess. A source is considered active when
1206 * it has been validated and has not yet received a BYE RTCP message.
1208 * Returns: The number of active sources in @sess.
1211 rtp_session_get_num_active_sources (RTPSession * sess)
1215 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1217 RTP_SESSION_LOCK (sess);
1218 result = sess->stats.active_sources;
1219 RTP_SESSION_UNLOCK (sess);
1225 * rtp_session_get_source_by_ssrc:
1226 * @sess: an #RTPSession
1229 * Find the source with @ssrc in @sess.
1231 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1232 * g_object_unref() after usage.
1235 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1239 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1241 RTP_SESSION_LOCK (sess);
1243 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1245 g_object_ref (result);
1246 RTP_SESSION_UNLOCK (sess);
1252 * rtp_session_get_source_by_cname:
1253 * @sess: a #RTPSession
1256 * Find the source with @cname in @sess.
1258 * Returns: a #RTPSource with CNAME @cname or NULL if the source was not found.
1259 * g_object_unref() after usage.
1262 rtp_session_get_source_by_cname (RTPSession * sess, const gchar * cname)
1266 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1267 g_return_val_if_fail (cname != NULL, NULL);
1269 RTP_SESSION_LOCK (sess);
1270 result = g_hash_table_lookup (sess->cnames, cname);
1272 g_object_ref (result);
1273 RTP_SESSION_UNLOCK (sess);
1279 rtp_session_create_new_ssrc (RTPSession * sess)
1284 ssrc = g_random_int ();
1286 /* see if it exists in the session, we're done if it doesn't */
1287 if (g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1288 GINT_TO_POINTER (ssrc)) == NULL)
1297 * rtp_session_create_source:
1298 * @sess: an #RTPSession
1300 * Create an #RTPSource for use in @sess. This function will create a source
1301 * with an ssrc that is currently not used by any participants in the session.
1303 * Returns: an #RTPSource.
1306 rtp_session_create_source (RTPSession * sess)
1311 RTP_SESSION_LOCK (sess);
1312 ssrc = rtp_session_create_new_ssrc (sess);
1313 source = rtp_source_new (ssrc);
1314 g_object_ref (source);
1315 rtp_source_set_callbacks (source, &callbacks, sess);
1316 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1318 /* we have one more source now */
1319 sess->total_sources++;
1320 RTP_SESSION_UNLOCK (sess);
1325 /* update the RTPArrivalStats structure with the current time and other bits
1326 * about the current buffer we are handling.
1327 * This function is typically called when a validated packet is received.
1328 * This function should be called with the SESSION_LOCK
1331 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
1332 gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
1333 GstClockTime running_time, guint64 ntpnstime)
1335 /* get time of arrival */
1336 arrival->time = current_time;
1337 arrival->running_time = running_time;
1338 arrival->ntpnstime = ntpnstime;
1340 /* get packet size including header overhead */
1341 arrival->bytes = GST_BUFFER_SIZE (buffer) + sess->header_len;
1344 arrival->payload_len = gst_rtp_buffer_get_payload_len (buffer);
1346 arrival->payload_len = 0;
1349 /* for netbuffer we can store the IP address to check for collisions */
1350 arrival->have_address = GST_IS_NETBUFFER (buffer);
1351 if (arrival->have_address) {
1352 GstNetBuffer *netbuf = (GstNetBuffer *) buffer;
1354 memcpy (&arrival->address, &netbuf->from, sizeof (GstNetAddress));
1359 * rtp_session_process_rtp:
1360 * @sess: and #RTPSession
1361 * @buffer: an RTP buffer
1362 * @current_time: the current system time
1363 * @ntpnstime: the NTP arrival time in nanoseconds
1365 * Process an RTP buffer in the session manager. This function takes ownership
1368 * Returns: a #GstFlowReturn.
1371 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1372 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
1374 GstFlowReturn result;
1378 gboolean prevsender, prevactive;
1379 RTPArrivalStats arrival;
1381 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1382 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1384 if (!gst_rtp_buffer_validate (buffer))
1385 goto invalid_packet;
1387 RTP_SESSION_LOCK (sess);
1388 /* update arrival stats */
1389 update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1390 running_time, ntpnstime);
1392 /* ignore more RTP packets when we left the session */
1393 if (sess->source->received_bye)
1396 /* get SSRC and look up in session database */
1397 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1398 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1403 prevsender = RTP_SOURCE_IS_SENDER (source);
1404 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1406 /* we need to ref so that we can process the CSRCs later */
1407 gst_buffer_ref (buffer);
1409 /* let source process the packet */
1410 result = rtp_source_process_rtp (source, buffer, &arrival);
1412 /* source became active */
1413 if (prevactive != RTP_SOURCE_IS_ACTIVE (source)) {
1414 sess->stats.active_sources++;
1415 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1416 sess->stats.active_sources);
1417 on_ssrc_validated (sess, source);
1419 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1420 sess->stats.sender_sources++;
1421 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1422 sess->stats.sender_sources);
1426 on_new_ssrc (sess, source);
1428 if (source->validated) {
1432 /* for validated sources, we add the CSRCs as well */
1433 count = gst_rtp_buffer_get_csrc_count (buffer);
1435 for (i = 0; i < count; i++) {
1437 RTPSource *csrc_src;
1439 csrc = gst_rtp_buffer_get_csrc (buffer, i);
1442 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1445 GST_DEBUG ("created new CSRC: %08x", csrc);
1446 rtp_source_set_as_csrc (csrc_src);
1447 if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1448 sess->stats.active_sources++;
1449 on_new_ssrc (sess, source);
1453 gst_buffer_unref (buffer);
1455 RTP_SESSION_UNLOCK (sess);
1462 gst_buffer_unref (buffer);
1463 GST_DEBUG ("invalid RTP packet received");
1468 gst_buffer_unref (buffer);
1469 RTP_SESSION_UNLOCK (sess);
1470 GST_DEBUG ("ignoring RTP packet because we are leaving");
1475 gst_buffer_unref (buffer);
1476 RTP_SESSION_UNLOCK (sess);
1477 GST_DEBUG ("ignoring packet because its collisioning");
1483 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1484 GstRTCPPacket * packet, RTPArrivalStats * arrival)
1488 count = gst_rtcp_packet_get_rb_count (packet);
1489 for (i = 0; i < count; i++) {
1490 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1491 guint8 fractionlost;
1494 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1495 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1497 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1499 if (ssrc == sess->source->ssrc) {
1500 /* only deal with report blocks for our session, we update the stats of
1501 * the sender of the RTCP message. We could also compare our stats against
1502 * the other sender to see if we are better or worse. */
1503 rtp_source_process_rb (source, arrival->time, fractionlost, packetslost,
1504 exthighestseq, jitter, lsr, dlsr);
1506 on_ssrc_active (sess, source);
1511 /* A Sender report contains statistics about how the sender is doing. This
1512 * includes timing informataion such as the relation between RTP and NTP
1513 * timestamps and the number of packets/bytes it sent to us.
1515 * In this report is also included a set of report blocks related to how this
1516 * sender is receiving data (in case we (or somebody else) is also sending stuff
1517 * to it). This info includes the packet loss, jitter and seqnum. It also
1518 * contains information to calculate the round trip time (LSR/DLSR).
1521 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1522 RTPArrivalStats * arrival)
1524 guint32 senderssrc, rtptime, packet_count, octet_count;
1527 gboolean created, prevsender;
1529 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1530 &packet_count, &octet_count);
1532 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1533 senderssrc, GST_TIME_ARGS (arrival->time));
1535 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1540 prevsender = RTP_SOURCE_IS_SENDER (source);
1542 /* first update the source */
1543 rtp_source_process_sr (source, arrival->time, ntptime, rtptime, packet_count,
1546 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1547 sess->stats.sender_sources++;
1548 GST_DEBUG ("source: %08x became sender, %d sender sources", senderssrc,
1549 sess->stats.sender_sources);
1553 on_new_ssrc (sess, source);
1555 rtp_session_process_rb (sess, source, packet, arrival);
1558 /* A receiver report contains statistics about how a receiver is doing. It
1559 * includes stuff like packet loss, jitter and the seqnum it received last. It
1560 * also contains info to calculate the round trip time.
1562 * We are only interested in how the sender of this report is doing wrt to us.
1565 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1566 RTPArrivalStats * arrival)
1572 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1574 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1576 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1582 on_new_ssrc (sess, source);
1584 rtp_session_process_rb (sess, source, packet, arrival);
1587 /* Get SDES items and store them in the SSRC */
1589 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1590 RTPArrivalStats * arrival)
1593 gboolean more_items, more_entries;
1595 items = gst_rtcp_packet_sdes_get_item_count (packet);
1596 GST_DEBUG ("got SDES packet with %d items", items);
1598 more_items = gst_rtcp_packet_sdes_first_item (packet);
1600 while (more_items) {
1602 gboolean changed, created;
1605 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1607 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1609 /* find src, no probation when dealing with RTCP */
1610 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1616 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1618 while (more_entries) {
1619 GstRTCPSDESType type;
1623 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1625 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1628 changed |= rtp_source_set_sdes (source, type, data, len);
1630 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1634 source->validated = TRUE;
1637 on_new_ssrc (sess, source);
1639 on_ssrc_sdes (sess, source);
1641 more_items = gst_rtcp_packet_sdes_next_item (packet);
1646 /* BYE is sent when a client leaves the session
1649 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
1650 RTPArrivalStats * arrival)
1655 reason = gst_rtcp_packet_bye_get_reason (packet);
1656 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
1658 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
1659 for (i = 0; i < count; i++) {
1662 gboolean created, prevactive, prevsender;
1663 guint pmembers, members;
1665 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
1666 GST_DEBUG ("SSRC: %08x", ssrc);
1668 /* find src and mark bye, no probation when dealing with RTCP */
1669 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1674 /* store time for when we need to time out this source */
1675 source->bye_time = arrival->time;
1677 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1678 prevsender = RTP_SOURCE_IS_SENDER (source);
1680 /* let the source handle the rest */
1681 rtp_source_process_bye (source, reason);
1683 pmembers = sess->stats.active_sources;
1685 if (prevactive && !RTP_SOURCE_IS_ACTIVE (source)) {
1686 sess->stats.active_sources--;
1687 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
1688 sess->stats.active_sources);
1690 if (prevsender && !RTP_SOURCE_IS_SENDER (source)) {
1691 sess->stats.sender_sources--;
1692 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
1693 sess->stats.sender_sources);
1695 members = sess->stats.active_sources;
1697 if (!sess->source->received_bye && members < pmembers) {
1698 /* some members went away since the previous timeout estimate.
1699 * Perform reverse reconsideration but only when we are not scheduling a
1701 if (arrival->time < sess->next_rtcp_check_time) {
1702 GstClockTime time_remaining;
1704 time_remaining = sess->next_rtcp_check_time - arrival->time;
1705 sess->next_rtcp_check_time =
1706 gst_util_uint64_scale (time_remaining, members, pmembers);
1708 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
1709 GST_TIME_ARGS (sess->next_rtcp_check_time));
1711 sess->next_rtcp_check_time += arrival->time;
1713 RTP_SESSION_UNLOCK (sess);
1714 /* notify app of reconsideration */
1715 if (sess->callbacks.reconsider)
1716 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
1717 RTP_SESSION_LOCK (sess);
1722 on_new_ssrc (sess, source);
1724 on_bye_ssrc (sess, source);
1730 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
1731 RTPArrivalStats * arrival)
1733 GST_DEBUG ("received APP");
1737 * rtp_session_process_rtcp:
1738 * @sess: and #RTPSession
1739 * @buffer: an RTCP buffer
1740 * @current_time: the current system time
1742 * Process an RTCP buffer in the session manager. This function takes ownership
1745 * Returns: a #GstFlowReturn.
1748 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
1749 GstClockTime current_time)
1751 GstRTCPPacket packet;
1752 gboolean more, is_bye = FALSE, is_sr = FALSE;
1753 RTPArrivalStats arrival;
1754 GstFlowReturn result = GST_FLOW_OK;
1756 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1757 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1759 if (!gst_rtcp_buffer_validate (buffer))
1760 goto invalid_packet;
1762 GST_DEBUG ("received RTCP packet");
1764 RTP_SESSION_LOCK (sess);
1765 /* update arrival stats */
1766 update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1, -1);
1771 /* make writable, we might want to change the buffer */
1772 buffer = gst_buffer_make_metadata_writable (buffer);
1774 /* start processing the compound packet */
1775 more = gst_rtcp_buffer_get_first_packet (buffer, &packet);
1779 type = gst_rtcp_packet_get_type (&packet);
1781 /* when we are leaving the session, we should ignore all non-BYE messages */
1782 if (sess->source->received_bye && type != GST_RTCP_TYPE_BYE) {
1783 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
1788 case GST_RTCP_TYPE_SR:
1789 rtp_session_process_sr (sess, &packet, &arrival);
1792 case GST_RTCP_TYPE_RR:
1793 rtp_session_process_rr (sess, &packet, &arrival);
1795 case GST_RTCP_TYPE_SDES:
1796 rtp_session_process_sdes (sess, &packet, &arrival);
1798 case GST_RTCP_TYPE_BYE:
1800 rtp_session_process_bye (sess, &packet, &arrival);
1802 case GST_RTCP_TYPE_APP:
1803 rtp_session_process_app (sess, &packet, &arrival);
1806 GST_WARNING ("got unknown RTCP packet");
1810 more = gst_rtcp_packet_move_to_next (&packet);
1813 /* if we are scheduling a BYE, we only want to count bye packets, else we
1814 * count everything */
1815 if (sess->source->received_bye) {
1817 sess->stats.bye_members++;
1818 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
1821 /* keep track of average packet size */
1822 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
1824 RTP_SESSION_UNLOCK (sess);
1826 /* notify caller of sr packets in the callback */
1827 if (is_sr && sess->callbacks.sync_rtcp)
1828 result = sess->callbacks.sync_rtcp (sess, sess->source, buffer,
1829 sess->sync_rtcp_user_data);
1831 gst_buffer_unref (buffer);
1838 GST_DEBUG ("invalid RTCP packet received");
1839 gst_buffer_unref (buffer);
1844 gst_buffer_unref (buffer);
1845 RTP_SESSION_UNLOCK (sess);
1846 GST_DEBUG ("ignoring RTP packet because we left");
1852 * rtp_session_send_rtp:
1853 * @sess: an #RTPSession
1854 * @buffer: an RTP buffer
1855 * @current_time: the current system time
1856 * @ntpnstime: the NTP time in nanoseconds of when this buffer was captured.
1857 * This is the buffer timestamp converted to NTP time.
1859 * Send the RTP buffer in the session manager. This function takes ownership of
1862 * Returns: a #GstFlowReturn.
1865 rtp_session_send_rtp (RTPSession * sess, GstBuffer * buffer,
1866 GstClockTime current_time, guint64 ntpnstime)
1868 GstFlowReturn result;
1870 gboolean prevsender;
1872 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1873 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1875 if (!gst_rtp_buffer_validate (buffer))
1876 goto invalid_packet;
1878 GST_LOG ("received RTP packet for sending");
1880 RTP_SESSION_LOCK (sess);
1881 source = sess->source;
1883 /* update last activity */
1884 source->last_rtp_activity = current_time;
1886 prevsender = RTP_SOURCE_IS_SENDER (source);
1888 /* we use our own source to send */
1889 result = rtp_source_send_rtp (source, buffer, ntpnstime);
1891 if (RTP_SOURCE_IS_SENDER (source) && !prevsender)
1892 sess->stats.sender_sources++;
1893 RTP_SESSION_UNLOCK (sess);
1900 gst_buffer_unref (buffer);
1901 GST_DEBUG ("invalid RTP packet received");
1907 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
1910 GstClockTime result;
1912 if (sess->source->received_bye) {
1913 result = rtp_stats_calculate_bye_interval (&sess->stats);
1915 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
1916 RTP_SOURCE_IS_SENDER (sess->source), first);
1919 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
1920 GST_TIME_ARGS (result), first);
1923 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
1925 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
1931 * rtp_session_send_bye_locked:
1932 * @sess: an #RTPSession
1933 * @reason: a reason or NULL
1935 * Stop the current @sess and schedule a BYE message for the other members.
1937 * One must have the session lock to call this function
1939 * Returns: a #GstFlowReturn.
1941 static GstFlowReturn
1942 rtp_session_send_bye_locked (RTPSession * sess, const gchar * reason,
1943 GstClockTime current_time)
1945 GstFlowReturn result = GST_FLOW_OK;
1947 GstClockTime interval;
1949 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1951 source = sess->source;
1953 /* ignore more BYEs */
1954 if (source->received_bye)
1957 /* we have BYE now */
1958 source->received_bye = TRUE;
1959 /* at least one member wants to send a BYE */
1960 g_free (sess->bye_reason);
1961 sess->bye_reason = g_strdup (reason);
1962 sess->stats.avg_rtcp_packet_size = 100;
1963 sess->stats.bye_members = 1;
1964 sess->first_rtcp = TRUE;
1965 sess->sent_bye = FALSE;
1967 /* reschedule transmission */
1968 sess->last_rtcp_send_time = current_time;
1969 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
1970 sess->next_rtcp_check_time = current_time + interval;
1972 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
1973 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
1975 RTP_SESSION_UNLOCK (sess);
1976 /* notify app of reconsideration */
1977 if (sess->callbacks.reconsider)
1978 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
1979 RTP_SESSION_LOCK (sess);
1986 * rtp_session_send_bye:
1987 * @sess: an #RTPSession
1988 * @reason: a reason or NULL
1989 * @current_time: the current system time
1991 * Stop the current @sess and schedule a BYE message for the other members.
1993 * One must have the session lock to call this function
1995 * Returns: a #GstFlowReturn.
1998 rtp_session_send_bye (RTPSession * sess, const gchar * reason,
1999 GstClockTime current_time)
2001 GstFlowReturn result = GST_FLOW_OK;
2003 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2005 RTP_SESSION_LOCK (sess);
2006 result = rtp_session_send_bye_locked (sess, reason, current_time);
2007 RTP_SESSION_UNLOCK (sess);
2013 * rtp_session_next_timeout:
2014 * @sess: an #RTPSession
2015 * @current_time: the current system time
2017 * Get the next time we should perform session maintenance tasks.
2019 * Returns: a time when rtp_session_on_timeout() should be called with the
2020 * current system time.
2023 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2025 GstClockTime result;
2027 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2029 RTP_SESSION_LOCK (sess);
2031 result = sess->next_rtcp_check_time;
2033 GST_DEBUG ("current time: %" GST_TIME_FORMAT ", next :%" GST_TIME_FORMAT,
2034 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2036 if (result < current_time) {
2037 GST_DEBUG ("take current time as base");
2038 /* our previous check time expired, start counting from the current time
2040 result = current_time;
2043 if (sess->source->received_bye) {
2044 if (sess->sent_bye) {
2045 GST_DEBUG ("we sent BYE already");
2046 result = GST_CLOCK_TIME_NONE;
2047 } else if (sess->stats.active_sources >= 50) {
2048 GST_DEBUG ("reconsider BYE, more than 50 sources");
2049 /* reconsider BYE if members >= 50 */
2050 result += calculate_rtcp_interval (sess, FALSE, TRUE);
2053 if (sess->first_rtcp) {
2054 GST_DEBUG ("first RTCP packet");
2055 /* we are called for the first time */
2056 result += calculate_rtcp_interval (sess, FALSE, TRUE);
2057 } else if (sess->next_rtcp_check_time < current_time) {
2058 GST_DEBUG ("old check time expired, getting new timeout");
2059 /* get a new timeout when we need to */
2060 result += calculate_rtcp_interval (sess, FALSE, FALSE);
2063 sess->next_rtcp_check_time = result;
2065 GST_DEBUG ("next timeout: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2066 RTP_SESSION_UNLOCK (sess);
2075 GstClockTime current_time;
2077 GstClockTime interval;
2078 GstRTCPPacket packet;
2084 session_start_rtcp (RTPSession * sess, ReportData * data)
2086 GstRTCPPacket *packet = &data->packet;
2087 RTPSource *own = sess->source;
2089 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2091 if (RTP_SOURCE_IS_SENDER (own)) {
2094 guint32 packet_count, octet_count;
2096 /* we are a sender, create SR */
2097 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2098 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SR, packet);
2100 /* get latest stats */
2101 rtp_source_get_new_sr (own, data->ntpnstime, &ntptime, &rtptime,
2102 &packet_count, &octet_count);
2104 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2105 packet_count, octet_count);
2107 /* fill in sender report info */
2108 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2109 ntptime, rtptime, packet_count, octet_count);
2111 /* we are only receiver, create RR */
2112 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2113 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_RR, packet);
2114 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2118 /* construct a Sender or Receiver Report */
2120 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2122 RTPSession *sess = data->sess;
2123 GstRTCPPacket *packet = &data->packet;
2125 /* create a new buffer if needed */
2126 if (data->rtcp == NULL) {
2127 session_start_rtcp (sess, data);
2129 if (gst_rtcp_packet_get_rb_count (packet) < GST_RTCP_MAX_RB_COUNT) {
2130 /* only report about other sender sources */
2131 if (source != sess->source && RTP_SOURCE_IS_SENDER (source)) {
2132 guint8 fractionlost;
2134 guint32 exthighestseq, jitter;
2138 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2139 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2141 /* packet is not yet filled, add report block for this source. */
2142 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2143 exthighestseq, jitter, lsr, dlsr);
2148 /* perform cleanup of sources that timed out */
2150 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2152 gboolean remove = FALSE;
2153 gboolean byetimeout = FALSE;
2154 gboolean sendertimeout = FALSE;
2155 gboolean is_sender, is_active;
2156 RTPSession *sess = data->sess;
2157 GstClockTime interval;
2159 is_sender = RTP_SOURCE_IS_SENDER (source);
2160 is_active = RTP_SOURCE_IS_ACTIVE (source);
2162 /* check for our own source, we don't want to delete our own source. */
2163 if (!(source == sess->source)) {
2164 if (source->received_bye) {
2165 /* if we received a BYE from the source, remove the source after some
2167 if (data->current_time > source->bye_time &&
2168 data->current_time - source->bye_time > sess->stats.bye_timeout) {
2169 GST_DEBUG ("removing BYE source %08x", source->ssrc);
2174 /* sources that were inactive for more than 5 times the deterministic reporting
2175 * interval get timed out. the min timeout is 5 seconds. */
2176 if (data->current_time > source->last_activity) {
2177 interval = MAX (data->interval * 5, 5 * GST_SECOND);
2178 if (data->current_time - source->last_activity > interval) {
2179 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2180 source->ssrc, GST_TIME_ARGS (source->last_activity));
2186 /* senders that did not send for a long time become a receiver, this also
2187 * holds for our own source. */
2189 if (data->current_time > source->last_rtp_activity) {
2190 interval = MAX (data->interval * 2, 5 * GST_SECOND);
2191 if (data->current_time - source->last_rtp_activity > interval) {
2192 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
2193 GST_TIME_FORMAT, source->ssrc,
2194 GST_TIME_ARGS (source->last_rtp_activity));
2195 source->is_sender = FALSE;
2196 sess->stats.sender_sources--;
2197 sendertimeout = TRUE;
2203 sess->total_sources--;
2205 sess->stats.sender_sources--;
2207 sess->stats.active_sources--;
2210 on_bye_timeout (sess, source);
2212 on_timeout (sess, source);
2215 on_sender_timeout (sess, source);
2221 session_sdes (RTPSession * sess, ReportData * data)
2223 GstRTCPPacket *packet = &data->packet;
2227 /* add SDES packet */
2228 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SDES, packet);
2230 gst_rtcp_packet_sdes_add_item (packet, sess->source->ssrc);
2232 rtp_source_get_sdes (sess->source, GST_RTCP_SDES_CNAME, &sdes_data,
2234 gst_rtcp_packet_sdes_add_entry (packet, GST_RTCP_SDES_CNAME, sdes_len,
2237 /* other SDES items must only be added at regular intervals and only when the
2238 * user requests to since it might be a privacy problem */
2240 gst_rtcp_packet_sdes_add_entry (&packet, GST_RTCP_SDES_NAME,
2241 strlen (sess->name), (guint8 *) sess->name);
2242 gst_rtcp_packet_sdes_add_entry (&packet, GST_RTCP_SDES_TOOL,
2243 strlen (sess->tool), (guint8 *) sess->tool);
2246 data->has_sdes = TRUE;
2249 /* schedule a BYE packet */
2251 session_bye (RTPSession * sess, ReportData * data)
2253 GstRTCPPacket *packet = &data->packet;
2256 session_start_rtcp (sess, data);
2259 session_sdes (sess, data);
2261 /* add a BYE packet */
2262 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_BYE, packet);
2263 gst_rtcp_packet_bye_add_ssrc (packet, sess->source->ssrc);
2264 if (sess->bye_reason)
2265 gst_rtcp_packet_bye_set_reason (packet, sess->bye_reason);
2267 /* we have a BYE packet now */
2268 data->is_bye = TRUE;
2272 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
2274 GstClockTime new_send_time, elapsed;
2277 /* no need to check yet */
2278 if (sess->next_rtcp_check_time > current_time) {
2279 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
2280 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
2281 GST_TIME_ARGS (current_time));
2285 /* get elapsed time since we last reported */
2286 elapsed = current_time - sess->last_rtcp_send_time;
2288 /* perform forward reconsideration */
2289 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, data->interval);
2291 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
2292 GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time), GST_TIME_ARGS (elapsed));
2294 new_send_time += sess->last_rtcp_send_time;
2296 /* check if reconsideration */
2297 if (current_time < new_send_time) {
2298 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
2299 GST_TIME_ARGS (new_send_time));
2301 /* store new check time */
2302 sess->next_rtcp_check_time = new_send_time;
2305 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
2307 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
2308 GST_TIME_ARGS (new_send_time));
2309 sess->next_rtcp_check_time = current_time + new_send_time;
2315 * rtp_session_on_timeout:
2316 * @sess: an #RTPSession
2317 * @current_time: the current system time
2318 * @ntpnstime: the current NTP time in nanoseconds
2320 * Perform maintenance actions after the timeout obtained with
2321 * rtp_session_next_timeout() expired.
2323 * This function will perform timeouts of receivers and senders, send a BYE
2324 * packet or generate RTCP packets with current session stats.
2326 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
2327 * times, for each packet that should be processed.
2329 * Returns: a #GstFlowReturn.
2332 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
2335 GstFlowReturn result = GST_FLOW_OK;
2340 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2342 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT,
2343 GST_TIME_ARGS (current_time), GST_TIME_ARGS (ntpnstime));
2347 data.current_time = current_time;
2348 data.ntpnstime = ntpnstime;
2349 data.is_bye = FALSE;
2350 data.has_sdes = FALSE;
2354 RTP_SESSION_LOCK (sess);
2355 /* get a new interval, we need this for various cleanups etc */
2356 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
2358 /* first perform cleanups */
2359 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
2360 (GHRFunc) session_cleanup, &data);
2362 /* see if we need to generate SR or RR packets */
2363 if (is_rtcp_time (sess, current_time, &data)) {
2364 if (own->received_bye) {
2365 /* generate BYE instead */
2366 GST_DEBUG ("generating BYE message");
2367 session_bye (sess, &data);
2368 sess->sent_bye = TRUE;
2370 /* loop over all known sources and do something */
2371 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2372 (GHFunc) session_report_blocks, &data);
2379 /* we keep track of the last report time in order to timeout inactive
2380 * receivers or senders */
2381 sess->last_rtcp_send_time = data.current_time;
2382 sess->first_rtcp = FALSE;
2384 /* add SDES for this source when not already added */
2386 session_sdes (sess, &data);
2388 /* update average RTCP size before sending */
2389 size = GST_BUFFER_SIZE (data.rtcp) + sess->header_len;
2390 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, size);
2393 /* check for outdated collisions */
2394 GST_DEBUG ("checking collision list");
2395 item = g_list_first (sess->conflicting_addresses);
2397 RTPConflictingAddress *known_conflict = item->data;
2398 GList *next_item = g_list_next (item);
2400 if (known_conflict->time < current_time - (data.interval *
2401 RTCP_INTERVAL_COLLISION_TIMEOUT)) {
2402 sess->conflicting_addresses =
2403 g_list_delete_link (sess->conflicting_addresses, item);
2404 GST_DEBUG ("collision %p timed out", known_conflict);
2405 g_free (known_conflict);
2410 if (sess->change_ssrc) {
2411 GST_DEBUG ("need to change our SSRC (%08x)", own->ssrc);
2412 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
2413 GINT_TO_POINTER (own->ssrc));
2415 own->ssrc = rtp_session_create_new_ssrc (sess);
2416 rtp_source_reset (own);
2418 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
2419 GINT_TO_POINTER (own->ssrc), own);
2421 g_free (sess->bye_reason);
2422 sess->bye_reason = NULL;
2423 sess->sent_bye = FALSE;
2424 sess->change_ssrc = FALSE;
2425 GST_DEBUG ("changed our SSRC to %08x", own->ssrc);
2427 RTP_SESSION_UNLOCK (sess);
2429 /* push out the RTCP packet */
2431 /* close the RTCP packet */
2432 gst_rtcp_buffer_end (data.rtcp);
2434 GST_DEBUG ("sending packet");
2435 if (sess->callbacks.send_rtcp)
2436 result = sess->callbacks.send_rtcp (sess, own, data.rtcp,
2437 sess->sent_bye, sess->send_rtcp_user_data);
2439 GST_DEBUG ("freeing packet");
2440 gst_buffer_unref (data.rtcp);