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 NULL
53 #define DEFAULT_NUM_SOURCES 0
54 #define DEFAULT_NUM_ACTIVE_SOURCES 0
55 #define DEFAULT_SOURCES NULL
67 PROP_NUM_ACTIVE_SOURCES,
72 /* update average packet size, we keep this scaled by 16 to keep enough
74 #define UPDATE_AVG(avg, val) \
78 (avg) = ((val) + (15 * (avg))) >> 4;
80 /* The number RTCP intervals after which to timeout entries in the
83 #define RTCP_INTERVAL_COLLISION_TIMEOUT 10
85 /* GObject vmethods */
86 static void rtp_session_finalize (GObject * object);
87 static void rtp_session_set_property (GObject * object, guint prop_id,
88 const GValue * value, GParamSpec * pspec);
89 static void rtp_session_get_property (GObject * object, guint prop_id,
90 GValue * value, GParamSpec * pspec);
92 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
94 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
96 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
97 gboolean * created, RTPArrivalStats * arrival, gboolean rtp);
98 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
99 const gchar * reason, GstClockTime current_time);
100 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
101 gboolean deterministic, gboolean first);
104 rtp_session_class_init (RTPSessionClass * klass)
106 GObjectClass *gobject_class;
108 gobject_class = (GObjectClass *) klass;
110 gobject_class->finalize = rtp_session_finalize;
111 gobject_class->set_property = rtp_session_set_property;
112 gobject_class->get_property = rtp_session_get_property;
115 * RTPSession::get-source-by-ssrc:
116 * @session: the object which received the signal
117 * @ssrc: the SSRC of the RTPSource
119 * Request the #RTPSource object with SSRC @ssrc in @session.
121 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
122 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
123 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
124 get_source_by_ssrc), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
125 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
128 * RTPSession::on-new-ssrc:
129 * @session: the object which received the signal
130 * @src: the new RTPSource
132 * Notify of a new SSRC that entered @session.
134 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
135 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
136 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
137 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
140 * RTPSession::on-ssrc-collision:
141 * @session: the object which received the signal
142 * @src: the #RTPSource that caused a collision
144 * Notify when we have an SSRC collision
146 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
147 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
148 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
149 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
152 * RTPSession::on-ssrc-validated:
153 * @session: the object which received the signal
154 * @src: the new validated RTPSource
156 * Notify of a new SSRC that became validated.
158 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
159 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
160 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
161 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
164 * RTPSession::on-ssrc-active:
165 * @session: the object which received the signal
166 * @src: the active RTPSource
168 * Notify of a SSRC that is active, i.e., sending RTCP.
170 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
171 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
172 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
173 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
176 * RTPSession::on-ssrc-sdes:
177 * @session: the object which received the signal
178 * @src: the RTPSource
180 * Notify that a new SDES was received for SSRC.
182 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
183 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
184 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
185 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
188 * RTPSession::on-bye-ssrc:
189 * @session: the object which received the signal
190 * @src: the RTPSource that went away
192 * Notify of an SSRC that became inactive because of a BYE packet.
194 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
195 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
196 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
197 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
200 * RTPSession::on-bye-timeout:
201 * @session: the object which received the signal
202 * @src: the RTPSource that timed out
204 * Notify of an SSRC that has timed out because of BYE
206 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
207 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
208 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
209 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
212 * RTPSession::on-timeout:
213 * @session: the object which received the signal
214 * @src: the RTPSource that timed out
216 * Notify of an SSRC that has timed out
218 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
219 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
220 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
221 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
224 * RTPSession::on-sender-timeout:
225 * @session: the object which received the signal
226 * @src: the RTPSource that timed out
228 * Notify of an SSRC that was a sender but timed out and became a receiver.
230 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
231 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
232 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
233 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
236 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
237 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
238 "The internal SSRC used for the session",
239 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
242 g_param_spec_object ("internal-source", "Internal Source",
243 "The internal source element of the session",
244 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
246 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
247 g_param_spec_double ("bandwidth", "Bandwidth",
248 "The bandwidth of the session",
249 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
250 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
253 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
254 "The fraction of the bandwidth used for RTCP",
255 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
256 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
258 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
259 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
260 "The maximum size of the RTCP packets",
261 16, G_MAXINT16, DEFAULT_RTCP_MTU,
262 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
264 g_object_class_install_property (gobject_class, PROP_SDES,
265 g_param_spec_boxed ("sdes", "SDES",
266 "The SDES items of this session",
267 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
269 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
270 g_param_spec_uint ("num-sources", "Num Sources",
271 "The number of sources in the session", 0, G_MAXUINT,
272 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
274 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
275 g_param_spec_uint ("num-active-sources", "Num Active Sources",
276 "The number of active sources in the session", 0, G_MAXUINT,
277 DEFAULT_NUM_ACTIVE_SOURCES,
278 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
282 * Get a GValue Array of all sources in the session.
285 * <title>Getting the #RTPSources of a session
292 * g_object_get (sess, "sources", &arr, NULL);
294 * for (i = 0; i < arr->n_values; i++) {
297 * val = g_value_array_get_nth (arr, i);
298 * source = g_value_get_object (val);
300 * g_value_array_free (arr);
305 g_object_class_install_property (gobject_class, PROP_SOURCES,
306 g_param_spec_boxed ("sources", "Sources",
307 "An array of all known sources in the session",
308 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
310 klass->get_source_by_ssrc =
311 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
313 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
317 rtp_session_init (RTPSession * sess)
322 sess->lock = g_mutex_new ();
323 sess->key = g_random_int ();
327 for (i = 0; i < 32; i++) {
329 g_hash_table_new_full (NULL, NULL, NULL,
330 (GDestroyNotify) g_object_unref);
332 sess->cnames = g_hash_table_new_full (NULL, NULL, g_free, NULL);
334 rtp_stats_init_defaults (&sess->stats);
336 /* create an active SSRC for this session manager */
337 sess->source = rtp_session_create_source (sess);
338 sess->source->validated = TRUE;
339 sess->source->internal = TRUE;
340 sess->stats.active_sources++;
342 /* default UDP header length */
343 sess->header_len = 28;
344 sess->mtu = DEFAULT_RTCP_MTU;
346 /* some default SDES entries */
347 str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
348 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_CNAME, str);
351 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_NAME,
353 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_TOOL, "GStreamer");
355 sess->first_rtcp = TRUE;
357 GST_DEBUG ("%p: session using SSRC: %08x", sess, sess->source->ssrc);
361 rtp_session_finalize (GObject * object)
366 sess = RTP_SESSION_CAST (object);
368 g_mutex_free (sess->lock);
369 for (i = 0; i < 32; i++)
370 g_hash_table_destroy (sess->ssrcs[i]);
372 g_list_foreach (sess->conflicting_addresses, (GFunc) g_free, NULL);
373 g_list_free (sess->conflicting_addresses);
375 g_free (sess->bye_reason);
377 g_hash_table_destroy (sess->cnames);
378 g_object_unref (sess->source);
380 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
384 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
386 GValue value = { 0 };
388 g_value_init (&value, RTP_TYPE_SOURCE);
389 g_value_take_object (&value, source);
390 /* copies the value */
391 g_value_array_append (arr, &value);
395 rtp_session_create_sources (RTPSession * sess)
400 RTP_SESSION_LOCK (sess);
401 /* get number of elements in the table */
402 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
403 /* create the result value array */
404 res = g_value_array_new (size);
406 /* and copy all values into the array */
407 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
408 RTP_SESSION_UNLOCK (sess);
414 rtp_session_set_property (GObject * object, guint prop_id,
415 const GValue * value, GParamSpec * pspec)
419 sess = RTP_SESSION (object);
422 case PROP_INTERNAL_SSRC:
423 rtp_session_set_internal_ssrc (sess, g_value_get_uint (value));
426 rtp_session_set_bandwidth (sess, g_value_get_double (value));
428 case PROP_RTCP_FRACTION:
429 rtp_session_set_rtcp_fraction (sess, g_value_get_double (value));
432 sess->mtu = g_value_get_uint (value);
435 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
438 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
444 rtp_session_get_property (GObject * object, guint prop_id,
445 GValue * value, GParamSpec * pspec)
449 sess = RTP_SESSION (object);
452 case PROP_INTERNAL_SSRC:
453 g_value_set_uint (value, rtp_session_get_internal_ssrc (sess));
455 case PROP_INTERNAL_SOURCE:
456 g_value_take_object (value, rtp_session_get_internal_source (sess));
459 g_value_set_double (value, rtp_session_get_bandwidth (sess));
461 case PROP_RTCP_FRACTION:
462 g_value_set_double (value, rtp_session_get_rtcp_fraction (sess));
465 g_value_set_uint (value, sess->mtu);
468 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
470 case PROP_NUM_SOURCES:
471 g_value_set_uint (value, rtp_session_get_num_sources (sess));
473 case PROP_NUM_ACTIVE_SOURCES:
474 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
477 g_value_take_boxed (value, rtp_session_create_sources (sess));
480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
486 on_new_ssrc (RTPSession * sess, RTPSource * source)
488 g_object_ref (source);
489 RTP_SESSION_UNLOCK (sess);
490 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
491 RTP_SESSION_LOCK (sess);
492 g_object_unref (source);
496 on_ssrc_collision (RTPSession * sess, RTPSource * source)
498 g_object_ref (source);
499 RTP_SESSION_UNLOCK (sess);
500 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
502 RTP_SESSION_LOCK (sess);
503 g_object_unref (source);
507 on_ssrc_validated (RTPSession * sess, RTPSource * source)
509 g_object_ref (source);
510 RTP_SESSION_UNLOCK (sess);
511 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
513 RTP_SESSION_LOCK (sess);
514 g_object_unref (source);
518 on_ssrc_active (RTPSession * sess, RTPSource * source)
520 g_object_ref (source);
521 RTP_SESSION_UNLOCK (sess);
522 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
523 RTP_SESSION_LOCK (sess);
524 g_object_unref (source);
528 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
530 g_object_ref (source);
531 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
532 RTP_SESSION_UNLOCK (sess);
533 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
534 RTP_SESSION_LOCK (sess);
535 g_object_unref (source);
539 on_bye_ssrc (RTPSession * sess, RTPSource * source)
541 g_object_ref (source);
542 RTP_SESSION_UNLOCK (sess);
543 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
544 RTP_SESSION_LOCK (sess);
545 g_object_unref (source);
549 on_bye_timeout (RTPSession * sess, RTPSource * source)
551 g_object_ref (source);
552 RTP_SESSION_UNLOCK (sess);
553 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
554 RTP_SESSION_LOCK (sess);
555 g_object_unref (source);
559 on_timeout (RTPSession * sess, RTPSource * source)
561 g_object_ref (source);
562 RTP_SESSION_UNLOCK (sess);
563 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
564 RTP_SESSION_LOCK (sess);
565 g_object_unref (source);
569 on_sender_timeout (RTPSession * sess, RTPSource * source)
571 g_object_ref (source);
572 RTP_SESSION_UNLOCK (sess);
573 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
575 RTP_SESSION_LOCK (sess);
576 g_object_unref (source);
582 * Create a new session object.
584 * Returns: a new #RTPSession. g_object_unref() after usage.
587 rtp_session_new (void)
591 sess = g_object_new (RTP_TYPE_SESSION, NULL);
597 * rtp_session_set_callbacks:
598 * @sess: an #RTPSession
599 * @callbacks: callbacks to configure
600 * @user_data: user data passed in the callbacks
602 * Configure a set of callbacks to be notified of actions.
605 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
608 g_return_if_fail (RTP_IS_SESSION (sess));
610 if (callbacks->process_rtp) {
611 sess->callbacks.process_rtp = callbacks->process_rtp;
612 sess->process_rtp_user_data = user_data;
614 if (callbacks->send_rtp) {
615 sess->callbacks.send_rtp = callbacks->send_rtp;
616 sess->send_rtp_user_data = user_data;
618 if (callbacks->send_rtcp) {
619 sess->callbacks.send_rtcp = callbacks->send_rtcp;
620 sess->send_rtcp_user_data = user_data;
622 if (callbacks->sync_rtcp) {
623 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
624 sess->sync_rtcp_user_data = user_data;
626 if (callbacks->clock_rate) {
627 sess->callbacks.clock_rate = callbacks->clock_rate;
628 sess->clock_rate_user_data = user_data;
630 if (callbacks->reconsider) {
631 sess->callbacks.reconsider = callbacks->reconsider;
632 sess->reconsider_user_data = user_data;
637 * rtp_session_set_process_rtp_callback:
638 * @sess: an #RTPSession
639 * @callback: callback to set
640 * @user_data: user data passed in the callback
642 * Configure only the process_rtp callback to be notified of the process_rtp action.
645 rtp_session_set_process_rtp_callback (RTPSession * sess,
646 RTPSessionProcessRTP callback, gpointer user_data)
648 g_return_if_fail (RTP_IS_SESSION (sess));
650 sess->callbacks.process_rtp = callback;
651 sess->process_rtp_user_data = user_data;
655 * rtp_session_set_send_rtp_callback:
656 * @sess: an #RTPSession
657 * @callback: callback to set
658 * @user_data: user data passed in the callback
660 * Configure only the send_rtp callback to be notified of the send_rtp action.
663 rtp_session_set_send_rtp_callback (RTPSession * sess,
664 RTPSessionSendRTP callback, gpointer user_data)
666 g_return_if_fail (RTP_IS_SESSION (sess));
668 sess->callbacks.send_rtp = callback;
669 sess->send_rtp_user_data = user_data;
673 * rtp_session_set_send_rtcp_callback:
674 * @sess: an #RTPSession
675 * @callback: callback to set
676 * @user_data: user data passed in the callback
678 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
681 rtp_session_set_send_rtcp_callback (RTPSession * sess,
682 RTPSessionSendRTCP callback, gpointer user_data)
684 g_return_if_fail (RTP_IS_SESSION (sess));
686 sess->callbacks.send_rtcp = callback;
687 sess->send_rtcp_user_data = user_data;
691 * rtp_session_set_sync_rtcp_callback:
692 * @sess: an #RTPSession
693 * @callback: callback to set
694 * @user_data: user data passed in the callback
696 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
699 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
700 RTPSessionSyncRTCP callback, gpointer user_data)
702 g_return_if_fail (RTP_IS_SESSION (sess));
704 sess->callbacks.sync_rtcp = callback;
705 sess->sync_rtcp_user_data = user_data;
709 * rtp_session_set_clock_rate_callback:
710 * @sess: an #RTPSession
711 * @callback: callback to set
712 * @user_data: user data passed in the callback
714 * Configure only the clock_rate callback to be notified of the clock_rate action.
717 rtp_session_set_clock_rate_callback (RTPSession * sess,
718 RTPSessionClockRate callback, gpointer user_data)
720 g_return_if_fail (RTP_IS_SESSION (sess));
722 sess->callbacks.clock_rate = callback;
723 sess->clock_rate_user_data = user_data;
727 * rtp_session_set_reconsider_callback:
728 * @sess: an #RTPSession
729 * @callback: callback to set
730 * @user_data: user data passed in the callback
732 * Configure only the reconsider callback to be notified of the reconsider action.
735 rtp_session_set_reconsider_callback (RTPSession * sess,
736 RTPSessionReconsider callback, gpointer user_data)
738 g_return_if_fail (RTP_IS_SESSION (sess));
740 sess->callbacks.reconsider = callback;
741 sess->reconsider_user_data = user_data;
745 * rtp_session_set_bandwidth:
746 * @sess: an #RTPSession
747 * @bandwidth: the bandwidth allocated
749 * Set the session bandwidth in bytes per second.
752 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
754 g_return_if_fail (RTP_IS_SESSION (sess));
756 RTP_SESSION_LOCK (sess);
757 sess->stats.bandwidth = bandwidth;
758 RTP_SESSION_UNLOCK (sess);
762 * rtp_session_get_bandwidth:
763 * @sess: an #RTPSession
765 * Get the session bandwidth.
767 * Returns: the session bandwidth.
770 rtp_session_get_bandwidth (RTPSession * sess)
774 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
776 RTP_SESSION_LOCK (sess);
777 result = sess->stats.bandwidth;
778 RTP_SESSION_UNLOCK (sess);
784 * rtp_session_set_rtcp_fraction:
785 * @sess: an #RTPSession
786 * @bandwidth: the RTCP bandwidth
788 * Set the bandwidth that should be used for RTCP
792 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
794 g_return_if_fail (RTP_IS_SESSION (sess));
796 RTP_SESSION_LOCK (sess);
797 sess->stats.rtcp_bandwidth = bandwidth;
798 RTP_SESSION_UNLOCK (sess);
802 * rtp_session_get_rtcp_fraction:
803 * @sess: an #RTPSession
805 * Get the session bandwidth used for RTCP.
807 * Returns: The bandwidth used for RTCP messages.
810 rtp_session_get_rtcp_fraction (RTPSession * sess)
814 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
816 RTP_SESSION_LOCK (sess);
817 result = sess->stats.rtcp_bandwidth;
818 RTP_SESSION_UNLOCK (sess);
824 * rtp_session_set_sdes_string:
825 * @sess: an #RTPSession
826 * @type: the type of the SDES item
827 * @item: a null-terminated string to set.
829 * Store an SDES item of @type in @sess.
831 * Returns: %FALSE if the data was unchanged @type is invalid.
834 rtp_session_set_sdes_string (RTPSession * sess, GstRTCPSDESType type,
839 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
841 RTP_SESSION_LOCK (sess);
842 result = rtp_source_set_sdes_string (sess->source, type, item);
843 RTP_SESSION_UNLOCK (sess);
849 * rtp_session_get_sdes_string:
850 * @sess: an #RTPSession
851 * @type: the type of the SDES item
853 * Get the SDES item of @type from @sess.
855 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
856 * valid. g_free() after usage.
859 rtp_session_get_sdes_string (RTPSession * sess, GstRTCPSDESType type)
863 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
865 RTP_SESSION_LOCK (sess);
866 result = rtp_source_get_sdes_string (sess->source, type);
867 RTP_SESSION_UNLOCK (sess);
873 * rtp_session_get_sdes_struct:
874 * @sess: an #RTSPSession
876 * Get the SDES data as a #GstStructure
878 * Returns: a GstStructure with SDES items for @sess.
881 rtp_session_get_sdes_struct (RTPSession * sess)
883 GstStructure *result;
885 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
887 RTP_SESSION_LOCK (sess);
888 result = rtp_source_get_sdes_struct (sess->source);
889 RTP_SESSION_UNLOCK (sess);
895 * rtp_session_set_sdes_struct:
896 * @sess: an #RTSPSession
897 * @sdes: a #GstStructure
899 * Set the SDES data as a #GstStructure.
902 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
904 g_return_if_fail (RTP_IS_SESSION (sess));
906 RTP_SESSION_LOCK (sess);
907 rtp_source_set_sdes_struct (sess->source, sdes);
908 RTP_SESSION_UNLOCK (sess);
912 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
914 GstFlowReturn result = GST_FLOW_OK;
916 if (source == session->source) {
917 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
919 RTP_SESSION_UNLOCK (session);
921 if (session->callbacks.send_rtp)
923 session->callbacks.send_rtp (session, source, data,
924 session->send_rtp_user_data);
926 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
929 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
930 RTP_SESSION_UNLOCK (session);
932 if (session->callbacks.process_rtp)
934 session->callbacks.process_rtp (session, source,
935 GST_BUFFER_CAST (data), session->process_rtp_user_data);
937 gst_buffer_unref (GST_BUFFER_CAST (data));
939 RTP_SESSION_LOCK (session);
945 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
949 RTP_SESSION_UNLOCK (session);
951 if (session->callbacks.clock_rate)
953 session->callbacks.clock_rate (session, pt,
954 session->clock_rate_user_data);
958 RTP_SESSION_LOCK (session);
960 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
965 static RTPSourceCallbacks callbacks = {
966 (RTPSourcePushRTP) source_push_rtp,
967 (RTPSourceClockRate) source_clock_rate,
971 * find_add_conflicting_addresses:
972 * @sess: The session to check in
973 * @arrival: The arrival stats for the buffer
975 * Checks if an address which has a conflict is already known,
976 * otherwise remembers it to prevent loops.
978 * Returns: TRUE if it was a known conflict, FALSE otherwise
982 find_add_conflicting_addresses (RTPSession * sess, RTPArrivalStats * arrival)
985 RTPConflictingAddress *new_conflict;
987 for (item = g_list_first (sess->conflicting_addresses);
988 item; item = g_list_next (item)) {
989 RTPConflictingAddress *known_conflict = item->data;
991 if (gst_netaddress_equal (&arrival->address, &known_conflict->address)) {
992 known_conflict->time = arrival->time;
997 new_conflict = g_new0 (RTPConflictingAddress, 1);
999 memcpy (&new_conflict->address, &arrival->address, sizeof (GstNetAddress));
1000 new_conflict->time = arrival->time;
1002 sess->conflicting_addresses = g_list_prepend (sess->conflicting_addresses,
1009 check_collision (RTPSession * sess, RTPSource * source,
1010 RTPArrivalStats * arrival, gboolean rtp)
1012 /* If we have no arrival address, we can't do collision checking */
1013 if (!arrival->have_address)
1016 if (sess->source != source) {
1017 /* This is not our local source, but lets check if two remote
1021 if (source->have_rtp_from) {
1022 if (gst_netaddress_equal (&source->rtp_from, &arrival->address))
1023 /* Address is the same */
1026 /* We don't already have a from address for RTP, just set it */
1027 rtp_source_set_rtp_from (source, &arrival->address);
1031 if (source->have_rtcp_from) {
1032 if (gst_netaddress_equal (&source->rtcp_from, &arrival->address))
1033 /* Address is the same */
1036 /* We don't already have a from address for RTCP, just set it */
1037 rtp_source_set_rtcp_from (source, &arrival->address);
1041 /* We received RTP or RTCP from this source before but the network address
1042 * changed. In this case, we have third-party collision or loop */
1043 GST_DEBUG ("we have a third-party collision or loop");
1045 /* FIXME: Log 3rd party collision somehow
1046 * Maybe should be done in upper layer, only the SDES can tell us
1047 * if its a collision or a loop
1050 /* This is sending with our ssrc, is it an address we already know */
1052 if (find_add_conflicting_addresses (sess, arrival)) {
1053 /* Its a known conflict, its probably a loop, not a collision
1054 * lets just drop the incoming packet
1056 GST_DEBUG ("Our packets are being looped back to us, dropping");
1058 /* Its a new collision, lets change our SSRC */
1060 GST_DEBUG ("Collision for SSRC %x", rtp_source_get_ssrc (source));
1061 on_ssrc_collision (sess, source);
1063 rtp_session_schedule_bye_locked (sess, "SSRC Collision", arrival->time);
1065 sess->change_ssrc = TRUE;
1073 /* must be called with the session lock, the returned source needs to be
1074 * unreffed after usage. */
1076 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1077 RTPArrivalStats * arrival, gboolean rtp)
1082 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1083 if (source == NULL) {
1084 /* make new Source in probation and insert */
1085 source = rtp_source_new (ssrc);
1087 /* for RTP packets we need to set the source in probation. Receiving RTCP
1088 * packets of an SSRC, on the other hand, is a strong indication that we
1089 * are dealing with a valid source. */
1091 source->probation = RTP_DEFAULT_PROBATION;
1093 source->probation = 0;
1095 /* store from address, if any */
1096 if (arrival->have_address) {
1098 rtp_source_set_rtp_from (source, &arrival->address);
1100 rtp_source_set_rtcp_from (source, &arrival->address);
1103 /* configure a callback on the source */
1104 rtp_source_set_callbacks (source, &callbacks, sess);
1106 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1109 /* we have one more source now */
1110 sess->total_sources++;
1114 /* check for collision, this updates the address when not previously set */
1115 if (check_collision (sess, source, arrival, rtp)) {
1119 /* update last activity */
1120 source->last_activity = arrival->time;
1122 source->last_rtp_activity = arrival->time;
1123 g_object_ref (source);
1129 * rtp_session_get_internal_source:
1130 * @sess: a #RTPSession
1132 * Get the internal #RTPSource of @sess.
1134 * Returns: The internal #RTPSource. g_object_unref() after usage.
1137 rtp_session_get_internal_source (RTPSession * sess)
1141 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1143 result = g_object_ref (sess->source);
1149 * rtp_session_set_internal_ssrc:
1150 * @sess: a #RTPSession
1153 * Set the SSRC of @sess to @ssrc.
1156 rtp_session_set_internal_ssrc (RTPSession * sess, guint32 ssrc)
1158 RTP_SESSION_LOCK (sess);
1159 if (ssrc != sess->source->ssrc) {
1160 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
1161 GINT_TO_POINTER (sess->source->ssrc));
1163 GST_DEBUG ("setting internal SSRC to %08x", ssrc);
1164 /* After this call, any receiver of the old SSRC either in RTP or RTCP
1165 * packets will timeout on the old SSRC, we could potentially schedule a
1166 * BYE RTCP for the old SSRC... */
1167 sess->source->ssrc = ssrc;
1168 rtp_source_reset (sess->source);
1170 /* rehash with the new SSRC */
1171 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1172 GINT_TO_POINTER (sess->source->ssrc), sess->source);
1174 RTP_SESSION_UNLOCK (sess);
1176 g_object_notify (G_OBJECT (sess), "internal-ssrc");
1180 * rtp_session_get_internal_ssrc:
1181 * @sess: a #RTPSession
1183 * Get the internal SSRC of @sess.
1185 * Returns: The SSRC of the session.
1188 rtp_session_get_internal_ssrc (RTPSession * sess)
1192 RTP_SESSION_LOCK (sess);
1193 ssrc = sess->source->ssrc;
1194 RTP_SESSION_UNLOCK (sess);
1200 * rtp_session_add_source:
1201 * @sess: a #RTPSession
1202 * @src: #RTPSource to add
1204 * Add @src to @session.
1206 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1207 * existed in the session.
1210 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1212 gboolean result = FALSE;
1215 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1216 g_return_val_if_fail (src != NULL, FALSE);
1218 RTP_SESSION_LOCK (sess);
1220 g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1221 GINT_TO_POINTER (src->ssrc));
1223 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1224 GINT_TO_POINTER (src->ssrc), src);
1225 /* we have one more source now */
1226 sess->total_sources++;
1229 RTP_SESSION_UNLOCK (sess);
1235 * rtp_session_get_num_sources:
1236 * @sess: an #RTPSession
1238 * Get the number of sources in @sess.
1240 * Returns: The number of sources in @sess.
1243 rtp_session_get_num_sources (RTPSession * sess)
1247 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1249 RTP_SESSION_LOCK (sess);
1250 result = sess->total_sources;
1251 RTP_SESSION_UNLOCK (sess);
1257 * rtp_session_get_num_active_sources:
1258 * @sess: an #RTPSession
1260 * Get the number of active sources in @sess. A source is considered active when
1261 * it has been validated and has not yet received a BYE RTCP message.
1263 * Returns: The number of active sources in @sess.
1266 rtp_session_get_num_active_sources (RTPSession * sess)
1270 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1272 RTP_SESSION_LOCK (sess);
1273 result = sess->stats.active_sources;
1274 RTP_SESSION_UNLOCK (sess);
1280 * rtp_session_get_source_by_ssrc:
1281 * @sess: an #RTPSession
1284 * Find the source with @ssrc in @sess.
1286 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1287 * g_object_unref() after usage.
1290 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1294 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1296 RTP_SESSION_LOCK (sess);
1298 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1300 g_object_ref (result);
1301 RTP_SESSION_UNLOCK (sess);
1307 * rtp_session_get_source_by_cname:
1308 * @sess: a #RTPSession
1311 * Find the source with @cname in @sess.
1313 * Returns: a #RTPSource with CNAME @cname or NULL if the source was not found.
1314 * g_object_unref() after usage.
1317 rtp_session_get_source_by_cname (RTPSession * sess, const gchar * cname)
1321 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1322 g_return_val_if_fail (cname != NULL, NULL);
1324 RTP_SESSION_LOCK (sess);
1325 result = g_hash_table_lookup (sess->cnames, cname);
1327 g_object_ref (result);
1328 RTP_SESSION_UNLOCK (sess);
1334 rtp_session_create_new_ssrc (RTPSession * sess)
1339 ssrc = g_random_int ();
1341 /* see if it exists in the session, we're done if it doesn't */
1342 if (g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1343 GINT_TO_POINTER (ssrc)) == NULL)
1351 * rtp_session_create_source:
1352 * @sess: an #RTPSession
1354 * Create an #RTPSource for use in @sess. This function will create a source
1355 * with an ssrc that is currently not used by any participants in the session.
1357 * Returns: an #RTPSource.
1360 rtp_session_create_source (RTPSession * sess)
1365 RTP_SESSION_LOCK (sess);
1366 ssrc = rtp_session_create_new_ssrc (sess);
1367 source = rtp_source_new (ssrc);
1368 rtp_source_set_callbacks (source, &callbacks, sess);
1369 /* we need an additional ref for the source in the hashtable */
1370 g_object_ref (source);
1371 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1373 /* we have one more source now */
1374 sess->total_sources++;
1375 RTP_SESSION_UNLOCK (sess);
1380 /* update the RTPArrivalStats structure with the current time and other bits
1381 * about the current buffer we are handling.
1382 * This function is typically called when a validated packet is received.
1383 * This function should be called with the SESSION_LOCK
1386 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
1387 gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
1388 GstClockTime running_time, guint64 ntpnstime)
1390 /* get time of arrival */
1391 arrival->time = current_time;
1392 arrival->running_time = running_time;
1393 arrival->ntpnstime = ntpnstime;
1395 /* get packet size including header overhead */
1396 arrival->bytes = GST_BUFFER_SIZE (buffer) + sess->header_len;
1399 arrival->payload_len = gst_rtp_buffer_get_payload_len (buffer);
1401 arrival->payload_len = 0;
1404 /* for netbuffer we can store the IP address to check for collisions */
1405 arrival->have_address = GST_IS_NETBUFFER (buffer);
1406 if (arrival->have_address) {
1407 GstNetBuffer *netbuf = (GstNetBuffer *) buffer;
1409 memcpy (&arrival->address, &netbuf->from, sizeof (GstNetAddress));
1414 * rtp_session_process_rtp:
1415 * @sess: and #RTPSession
1416 * @buffer: an RTP buffer
1417 * @current_time: the current system time
1418 * @ntpnstime: the NTP arrival time in nanoseconds
1420 * Process an RTP buffer in the session manager. This function takes ownership
1423 * Returns: a #GstFlowReturn.
1426 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1427 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
1429 GstFlowReturn result;
1433 gboolean prevsender, prevactive;
1434 RTPArrivalStats arrival;
1436 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1437 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1439 if (!gst_rtp_buffer_validate (buffer))
1440 goto invalid_packet;
1442 RTP_SESSION_LOCK (sess);
1443 /* update arrival stats */
1444 update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1445 running_time, ntpnstime);
1447 /* ignore more RTP packets when we left the session */
1448 if (sess->source->received_bye)
1451 /* get SSRC and look up in session database */
1452 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1453 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1457 prevsender = RTP_SOURCE_IS_SENDER (source);
1458 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1460 /* we need to ref so that we can process the CSRCs later */
1461 gst_buffer_ref (buffer);
1463 /* let source process the packet */
1464 result = rtp_source_process_rtp (source, buffer, &arrival);
1466 /* source became active */
1467 if (prevactive != RTP_SOURCE_IS_ACTIVE (source)) {
1468 sess->stats.active_sources++;
1469 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1470 sess->stats.active_sources);
1471 on_ssrc_validated (sess, source);
1473 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1474 sess->stats.sender_sources++;
1475 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1476 sess->stats.sender_sources);
1480 on_new_ssrc (sess, source);
1482 if (source->validated) {
1486 /* for validated sources, we add the CSRCs as well */
1487 count = gst_rtp_buffer_get_csrc_count (buffer);
1489 for (i = 0; i < count; i++) {
1491 RTPSource *csrc_src;
1493 csrc = gst_rtp_buffer_get_csrc (buffer, i);
1496 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1501 GST_DEBUG ("created new CSRC: %08x", csrc);
1502 rtp_source_set_as_csrc (csrc_src);
1503 if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1504 sess->stats.active_sources++;
1505 on_new_ssrc (sess, csrc_src);
1507 g_object_unref (csrc_src);
1510 g_object_unref (source);
1511 gst_buffer_unref (buffer);
1513 RTP_SESSION_UNLOCK (sess);
1520 gst_buffer_unref (buffer);
1521 GST_DEBUG ("invalid RTP packet received");
1526 gst_buffer_unref (buffer);
1527 RTP_SESSION_UNLOCK (sess);
1528 GST_DEBUG ("ignoring RTP packet because we are leaving");
1533 gst_buffer_unref (buffer);
1534 RTP_SESSION_UNLOCK (sess);
1535 GST_DEBUG ("ignoring packet because its collisioning");
1541 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1542 GstRTCPPacket * packet, RTPArrivalStats * arrival)
1546 count = gst_rtcp_packet_get_rb_count (packet);
1547 for (i = 0; i < count; i++) {
1548 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1549 guint8 fractionlost;
1552 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1553 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1555 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1557 if (ssrc == sess->source->ssrc) {
1558 /* only deal with report blocks for our session, we update the stats of
1559 * the sender of the RTCP message. We could also compare our stats against
1560 * the other sender to see if we are better or worse. */
1561 rtp_source_process_rb (source, arrival->time, fractionlost, packetslost,
1562 exthighestseq, jitter, lsr, dlsr);
1564 on_ssrc_active (sess, source);
1569 /* A Sender report contains statistics about how the sender is doing. This
1570 * includes timing informataion such as the relation between RTP and NTP
1571 * timestamps and the number of packets/bytes it sent to us.
1573 * In this report is also included a set of report blocks related to how this
1574 * sender is receiving data (in case we (or somebody else) is also sending stuff
1575 * to it). This info includes the packet loss, jitter and seqnum. It also
1576 * contains information to calculate the round trip time (LSR/DLSR).
1579 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1580 RTPArrivalStats * arrival, gboolean * do_sync)
1582 guint32 senderssrc, rtptime, packet_count, octet_count;
1585 gboolean created, prevsender;
1587 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1588 &packet_count, &octet_count);
1590 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1591 senderssrc, GST_TIME_ARGS (arrival->time));
1593 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1597 /* don't try to do lip-sync for sources that sent a BYE */
1598 if (rtp_source_received_bye (source))
1603 prevsender = RTP_SOURCE_IS_SENDER (source);
1605 /* first update the source */
1606 rtp_source_process_sr (source, arrival->time, ntptime, rtptime, packet_count,
1609 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1610 sess->stats.sender_sources++;
1611 GST_DEBUG ("source: %08x became sender, %d sender sources", senderssrc,
1612 sess->stats.sender_sources);
1616 on_new_ssrc (sess, source);
1618 rtp_session_process_rb (sess, source, packet, arrival);
1619 g_object_unref (source);
1622 /* A receiver report contains statistics about how a receiver is doing. It
1623 * includes stuff like packet loss, jitter and the seqnum it received last. It
1624 * also contains info to calculate the round trip time.
1626 * We are only interested in how the sender of this report is doing wrt to us.
1629 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1630 RTPArrivalStats * arrival)
1636 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1638 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1640 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1645 on_new_ssrc (sess, source);
1647 rtp_session_process_rb (sess, source, packet, arrival);
1648 g_object_unref (source);
1651 /* Get SDES items and store them in the SSRC */
1653 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1654 RTPArrivalStats * arrival)
1657 gboolean more_items, more_entries;
1659 items = gst_rtcp_packet_sdes_get_item_count (packet);
1660 GST_DEBUG ("got SDES packet with %d items", items);
1662 more_items = gst_rtcp_packet_sdes_first_item (packet);
1664 while (more_items) {
1666 gboolean changed, created;
1669 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1671 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1675 /* find src, no probation when dealing with RTCP */
1676 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1680 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1682 while (more_entries) {
1683 GstRTCPSDESType type;
1687 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1689 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1692 changed |= rtp_source_set_sdes (source, type, data, len);
1694 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1698 source->validated = TRUE;
1701 on_new_ssrc (sess, source);
1703 on_ssrc_sdes (sess, source);
1705 g_object_unref (source);
1707 more_items = gst_rtcp_packet_sdes_next_item (packet);
1712 /* BYE is sent when a client leaves the session
1715 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
1716 RTPArrivalStats * arrival)
1720 gboolean reconsider = FALSE;
1722 reason = gst_rtcp_packet_bye_get_reason (packet);
1723 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
1725 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
1726 for (i = 0; i < count; i++) {
1729 gboolean created, prevactive, prevsender;
1730 guint pmembers, members;
1732 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
1733 GST_DEBUG ("SSRC: %08x", ssrc);
1735 /* find src and mark bye, no probation when dealing with RTCP */
1736 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1740 /* store time for when we need to time out this source */
1741 source->bye_time = arrival->time;
1743 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1744 prevsender = RTP_SOURCE_IS_SENDER (source);
1746 /* let the source handle the rest */
1747 rtp_source_process_bye (source, reason);
1749 pmembers = sess->stats.active_sources;
1751 if (prevactive && !RTP_SOURCE_IS_ACTIVE (source)) {
1752 sess->stats.active_sources--;
1753 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
1754 sess->stats.active_sources);
1756 if (prevsender && !RTP_SOURCE_IS_SENDER (source)) {
1757 sess->stats.sender_sources--;
1758 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
1759 sess->stats.sender_sources);
1761 members = sess->stats.active_sources;
1763 if (!sess->source->received_bye && members < pmembers) {
1764 /* some members went away since the previous timeout estimate.
1765 * Perform reverse reconsideration but only when we are not scheduling a
1767 if (arrival->time < sess->next_rtcp_check_time) {
1768 GstClockTime time_remaining;
1770 time_remaining = sess->next_rtcp_check_time - arrival->time;
1771 sess->next_rtcp_check_time =
1772 gst_util_uint64_scale (time_remaining, members, pmembers);
1774 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
1775 GST_TIME_ARGS (sess->next_rtcp_check_time));
1777 sess->next_rtcp_check_time += arrival->time;
1779 /* mark pending reconsider. We only want to signal the reconsideration
1780 * once after we handled all the source in the bye packet */
1786 on_new_ssrc (sess, source);
1788 on_bye_ssrc (sess, source);
1790 g_object_unref (source);
1793 RTP_SESSION_UNLOCK (sess);
1794 /* notify app of reconsideration */
1795 if (sess->callbacks.reconsider)
1796 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
1797 RTP_SESSION_LOCK (sess);
1803 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
1804 RTPArrivalStats * arrival)
1806 GST_DEBUG ("received APP");
1810 * rtp_session_process_rtcp:
1811 * @sess: and #RTPSession
1812 * @buffer: an RTCP buffer
1813 * @current_time: the current system time
1815 * Process an RTCP buffer in the session manager. This function takes ownership
1818 * Returns: a #GstFlowReturn.
1821 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
1822 GstClockTime current_time)
1824 GstRTCPPacket packet;
1825 gboolean more, is_bye = FALSE, do_sync = FALSE;
1826 RTPArrivalStats arrival;
1827 GstFlowReturn result = GST_FLOW_OK;
1829 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1830 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1832 if (!gst_rtcp_buffer_validate (buffer))
1833 goto invalid_packet;
1835 GST_DEBUG ("received RTCP packet");
1837 RTP_SESSION_LOCK (sess);
1838 /* update arrival stats */
1839 update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1, -1);
1844 /* make writable, we might want to change the buffer */
1845 buffer = gst_buffer_make_metadata_writable (buffer);
1847 /* start processing the compound packet */
1848 more = gst_rtcp_buffer_get_first_packet (buffer, &packet);
1852 type = gst_rtcp_packet_get_type (&packet);
1854 /* when we are leaving the session, we should ignore all non-BYE messages */
1855 if (sess->source->received_bye && type != GST_RTCP_TYPE_BYE) {
1856 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
1861 case GST_RTCP_TYPE_SR:
1862 rtp_session_process_sr (sess, &packet, &arrival, &do_sync);
1864 case GST_RTCP_TYPE_RR:
1865 rtp_session_process_rr (sess, &packet, &arrival);
1867 case GST_RTCP_TYPE_SDES:
1868 rtp_session_process_sdes (sess, &packet, &arrival);
1870 case GST_RTCP_TYPE_BYE:
1872 /* don't try to attempt lip-sync anymore for streams with a BYE */
1874 rtp_session_process_bye (sess, &packet, &arrival);
1876 case GST_RTCP_TYPE_APP:
1877 rtp_session_process_app (sess, &packet, &arrival);
1880 GST_WARNING ("got unknown RTCP packet");
1884 more = gst_rtcp_packet_move_to_next (&packet);
1887 /* if we are scheduling a BYE, we only want to count bye packets, else we
1888 * count everything */
1889 if (sess->source->received_bye) {
1891 sess->stats.bye_members++;
1892 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
1895 /* keep track of average packet size */
1896 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
1898 RTP_SESSION_UNLOCK (sess);
1900 /* notify caller of sr packets in the callback */
1901 if (do_sync && sess->callbacks.sync_rtcp)
1902 result = sess->callbacks.sync_rtcp (sess, sess->source, buffer,
1903 sess->sync_rtcp_user_data);
1905 gst_buffer_unref (buffer);
1912 GST_DEBUG ("invalid RTCP packet received");
1913 gst_buffer_unref (buffer);
1918 gst_buffer_unref (buffer);
1919 RTP_SESSION_UNLOCK (sess);
1920 GST_DEBUG ("ignoring RTP packet because we left");
1926 * rtp_session_send_rtp:
1927 * @sess: an #RTPSession
1928 * @data: pointer to either an RTP buffer or a list of RTP buffers
1929 * @current_time: the current system time
1930 * @ntpnstime: the NTP time in nanoseconds of when this buffer was captured.
1931 * This is the buffer timestamp converted to NTP time.
1933 * Send the RTP buffer in the session manager. This function takes ownership of
1936 * Returns: a #GstFlowReturn.
1939 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
1940 GstClockTime current_time, guint64 ntpnstime)
1942 GstFlowReturn result;
1944 gboolean prevsender;
1945 gboolean valid_packet;
1947 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1948 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
1951 valid_packet = gst_rtp_buffer_list_validate (GST_BUFFER_LIST_CAST (data));
1953 valid_packet = gst_rtp_buffer_validate (GST_BUFFER_CAST (data));
1957 goto invalid_packet;
1959 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
1961 RTP_SESSION_LOCK (sess);
1962 source = sess->source;
1964 /* update last activity */
1965 source->last_rtp_activity = current_time;
1967 prevsender = RTP_SOURCE_IS_SENDER (source);
1969 /* we use our own source to send */
1970 result = rtp_source_send_rtp (source, data, is_list, ntpnstime);
1972 if (RTP_SOURCE_IS_SENDER (source) && !prevsender)
1973 sess->stats.sender_sources++;
1974 RTP_SESSION_UNLOCK (sess);
1981 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1982 GST_DEBUG ("invalid RTP packet received");
1988 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
1991 GstClockTime result;
1993 if (sess->source->received_bye) {
1994 result = rtp_stats_calculate_bye_interval (&sess->stats);
1996 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
1997 RTP_SOURCE_IS_SENDER (sess->source), first);
2000 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2001 GST_TIME_ARGS (result), first);
2004 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2006 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2011 /* Stop the current @sess and schedule a BYE message for the other members.
2012 * One must have the session lock to call this function
2014 static GstFlowReturn
2015 rtp_session_schedule_bye_locked (RTPSession * sess, const gchar * reason,
2016 GstClockTime current_time)
2018 GstFlowReturn result = GST_FLOW_OK;
2020 GstClockTime interval;
2022 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2024 source = sess->source;
2026 /* ignore more BYEs */
2027 if (source->received_bye)
2030 /* we have BYE now */
2031 source->received_bye = TRUE;
2032 /* at least one member wants to send a BYE */
2033 g_free (sess->bye_reason);
2034 sess->bye_reason = g_strdup (reason);
2035 sess->stats.avg_rtcp_packet_size = 100;
2036 sess->stats.bye_members = 1;
2037 sess->first_rtcp = TRUE;
2038 sess->sent_bye = FALSE;
2040 /* reschedule transmission */
2041 sess->last_rtcp_send_time = current_time;
2042 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2043 sess->next_rtcp_check_time = current_time + interval;
2045 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2046 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2048 RTP_SESSION_UNLOCK (sess);
2049 /* notify app of reconsideration */
2050 if (sess->callbacks.reconsider)
2051 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2052 RTP_SESSION_LOCK (sess);
2059 * rtp_session_schedule_bye:
2060 * @sess: an #RTPSession
2061 * @reason: a reason or NULL
2062 * @current_time: the current system time
2064 * Stop the current @sess and schedule a BYE message for the other members.
2066 * Returns: a #GstFlowReturn.
2069 rtp_session_schedule_bye (RTPSession * sess, const gchar * reason,
2070 GstClockTime current_time)
2072 GstFlowReturn result = GST_FLOW_OK;
2074 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2076 RTP_SESSION_LOCK (sess);
2077 result = rtp_session_schedule_bye_locked (sess, reason, current_time);
2078 RTP_SESSION_UNLOCK (sess);
2084 * rtp_session_next_timeout:
2085 * @sess: an #RTPSession
2086 * @current_time: the current system time
2088 * Get the next time we should perform session maintenance tasks.
2090 * Returns: a time when rtp_session_on_timeout() should be called with the
2091 * current system time.
2094 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2096 GstClockTime result;
2098 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2100 RTP_SESSION_LOCK (sess);
2102 result = sess->next_rtcp_check_time;
2104 GST_DEBUG ("current time: %" GST_TIME_FORMAT ", next :%" GST_TIME_FORMAT,
2105 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2107 if (result < current_time) {
2108 GST_DEBUG ("take current time as base");
2109 /* our previous check time expired, start counting from the current time
2111 result = current_time;
2114 if (sess->source->received_bye) {
2115 if (sess->sent_bye) {
2116 GST_DEBUG ("we sent BYE already");
2117 result = GST_CLOCK_TIME_NONE;
2118 } else if (sess->stats.active_sources >= 50) {
2119 GST_DEBUG ("reconsider BYE, more than 50 sources");
2120 /* reconsider BYE if members >= 50 */
2121 result += calculate_rtcp_interval (sess, FALSE, TRUE);
2124 if (sess->first_rtcp) {
2125 GST_DEBUG ("first RTCP packet");
2126 /* we are called for the first time */
2127 result += calculate_rtcp_interval (sess, FALSE, TRUE);
2128 } else if (sess->next_rtcp_check_time < current_time) {
2129 GST_DEBUG ("old check time expired, getting new timeout");
2130 /* get a new timeout when we need to */
2131 result += calculate_rtcp_interval (sess, FALSE, FALSE);
2134 sess->next_rtcp_check_time = result;
2136 GST_DEBUG ("next timeout: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2137 RTP_SESSION_UNLOCK (sess);
2146 GstClockTime current_time;
2148 GstClockTime interval;
2149 GstRTCPPacket packet;
2155 session_start_rtcp (RTPSession * sess, ReportData * data)
2157 GstRTCPPacket *packet = &data->packet;
2158 RTPSource *own = sess->source;
2160 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2162 if (RTP_SOURCE_IS_SENDER (own)) {
2165 guint32 packet_count, octet_count;
2167 /* we are a sender, create SR */
2168 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2169 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SR, packet);
2171 /* get latest stats */
2172 rtp_source_get_new_sr (own, data->ntpnstime, &ntptime, &rtptime,
2173 &packet_count, &octet_count);
2175 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2176 packet_count, octet_count);
2178 /* fill in sender report info */
2179 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2180 ntptime, rtptime, packet_count, octet_count);
2182 /* we are only receiver, create RR */
2183 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2184 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_RR, packet);
2185 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2189 /* construct a Sender or Receiver Report */
2191 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2193 RTPSession *sess = data->sess;
2194 GstRTCPPacket *packet = &data->packet;
2196 /* create a new buffer if needed */
2197 if (data->rtcp == NULL) {
2198 session_start_rtcp (sess, data);
2200 if (gst_rtcp_packet_get_rb_count (packet) < GST_RTCP_MAX_RB_COUNT) {
2201 /* only report about other sender sources */
2202 if (source != sess->source && RTP_SOURCE_IS_SENDER (source)) {
2203 guint8 fractionlost;
2205 guint32 exthighestseq, jitter;
2209 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2210 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2212 /* packet is not yet filled, add report block for this source. */
2213 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2214 exthighestseq, jitter, lsr, dlsr);
2219 /* perform cleanup of sources that timed out */
2221 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2223 gboolean remove = FALSE;
2224 gboolean byetimeout = FALSE;
2225 gboolean sendertimeout = FALSE;
2226 gboolean is_sender, is_active;
2227 RTPSession *sess = data->sess;
2228 GstClockTime interval;
2230 is_sender = RTP_SOURCE_IS_SENDER (source);
2231 is_active = RTP_SOURCE_IS_ACTIVE (source);
2233 /* check for our own source, we don't want to delete our own source. */
2234 if (!(source == sess->source)) {
2235 if (source->received_bye) {
2236 /* if we received a BYE from the source, remove the source after some
2238 if (data->current_time > source->bye_time &&
2239 data->current_time - source->bye_time > sess->stats.bye_timeout) {
2240 GST_DEBUG ("removing BYE source %08x", source->ssrc);
2245 /* sources that were inactive for more than 5 times the deterministic reporting
2246 * interval get timed out. the min timeout is 5 seconds. */
2247 if (data->current_time > source->last_activity) {
2248 interval = MAX (data->interval * 5, 5 * GST_SECOND);
2249 if (data->current_time - source->last_activity > interval) {
2250 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2251 source->ssrc, GST_TIME_ARGS (source->last_activity));
2257 /* senders that did not send for a long time become a receiver, this also
2258 * holds for our own source. */
2260 if (data->current_time > source->last_rtp_activity) {
2261 interval = MAX (data->interval * 2, 5 * GST_SECOND);
2262 if (data->current_time - source->last_rtp_activity > interval) {
2263 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
2264 GST_TIME_FORMAT, source->ssrc,
2265 GST_TIME_ARGS (source->last_rtp_activity));
2266 source->is_sender = FALSE;
2267 sess->stats.sender_sources--;
2268 sendertimeout = TRUE;
2274 sess->total_sources--;
2276 sess->stats.sender_sources--;
2278 sess->stats.active_sources--;
2281 on_bye_timeout (sess, source);
2283 on_timeout (sess, source);
2286 on_sender_timeout (sess, source);
2292 session_sdes (RTPSession * sess, ReportData * data)
2294 GstRTCPPacket *packet = &data->packet;
2298 /* add SDES packet */
2299 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SDES, packet);
2301 gst_rtcp_packet_sdes_add_item (packet, sess->source->ssrc);
2303 rtp_source_get_sdes (sess->source, GST_RTCP_SDES_CNAME, &sdes_data,
2305 gst_rtcp_packet_sdes_add_entry (packet, GST_RTCP_SDES_CNAME, sdes_len,
2308 /* other SDES items must only be added at regular intervals and only when the
2309 * user requests to since it might be a privacy problem */
2311 gst_rtcp_packet_sdes_add_entry (&packet, GST_RTCP_SDES_NAME,
2312 strlen (sess->name), (guint8 *) sess->name);
2313 gst_rtcp_packet_sdes_add_entry (&packet, GST_RTCP_SDES_TOOL,
2314 strlen (sess->tool), (guint8 *) sess->tool);
2317 data->has_sdes = TRUE;
2320 /* schedule a BYE packet */
2322 session_bye (RTPSession * sess, ReportData * data)
2324 GstRTCPPacket *packet = &data->packet;
2327 session_start_rtcp (sess, data);
2330 session_sdes (sess, data);
2332 /* add a BYE packet */
2333 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_BYE, packet);
2334 gst_rtcp_packet_bye_add_ssrc (packet, sess->source->ssrc);
2335 if (sess->bye_reason)
2336 gst_rtcp_packet_bye_set_reason (packet, sess->bye_reason);
2338 /* we have a BYE packet now */
2339 data->is_bye = TRUE;
2343 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
2345 GstClockTime new_send_time, elapsed;
2348 /* no need to check yet */
2349 if (sess->next_rtcp_check_time > current_time) {
2350 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
2351 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
2352 GST_TIME_ARGS (current_time));
2356 /* get elapsed time since we last reported */
2357 elapsed = current_time - sess->last_rtcp_send_time;
2359 /* perform forward reconsideration */
2360 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, data->interval);
2362 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
2363 GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time), GST_TIME_ARGS (elapsed));
2365 new_send_time += sess->last_rtcp_send_time;
2367 /* check if reconsideration */
2368 if (current_time < new_send_time) {
2369 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
2370 GST_TIME_ARGS (new_send_time));
2372 /* store new check time */
2373 sess->next_rtcp_check_time = new_send_time;
2376 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
2378 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
2379 GST_TIME_ARGS (new_send_time));
2380 sess->next_rtcp_check_time = current_time + new_send_time;
2386 * rtp_session_on_timeout:
2387 * @sess: an #RTPSession
2388 * @current_time: the current system time
2389 * @ntpnstime: the current NTP time in nanoseconds
2391 * Perform maintenance actions after the timeout obtained with
2392 * rtp_session_next_timeout() expired.
2394 * This function will perform timeouts of receivers and senders, send a BYE
2395 * packet or generate RTCP packets with current session stats.
2397 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
2398 * times, for each packet that should be processed.
2400 * Returns: a #GstFlowReturn.
2403 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
2406 GstFlowReturn result = GST_FLOW_OK;
2410 gboolean notify = FALSE;
2412 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2414 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT,
2415 GST_TIME_ARGS (current_time), GST_TIME_ARGS (ntpnstime));
2419 data.current_time = current_time;
2420 data.ntpnstime = ntpnstime;
2421 data.is_bye = FALSE;
2422 data.has_sdes = FALSE;
2426 RTP_SESSION_LOCK (sess);
2427 /* get a new interval, we need this for various cleanups etc */
2428 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
2430 /* first perform cleanups */
2431 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
2432 (GHRFunc) session_cleanup, &data);
2434 /* see if we need to generate SR or RR packets */
2435 if (is_rtcp_time (sess, current_time, &data)) {
2436 if (own->received_bye) {
2437 /* generate BYE instead */
2438 GST_DEBUG ("generating BYE message");
2439 session_bye (sess, &data);
2440 sess->sent_bye = TRUE;
2442 /* loop over all known sources and do something */
2443 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2444 (GHFunc) session_report_blocks, &data);
2451 /* we keep track of the last report time in order to timeout inactive
2452 * receivers or senders */
2453 sess->last_rtcp_send_time = data.current_time;
2454 sess->first_rtcp = FALSE;
2456 /* add SDES for this source when not already added */
2458 session_sdes (sess, &data);
2460 /* update average RTCP size before sending */
2461 size = GST_BUFFER_SIZE (data.rtcp) + sess->header_len;
2462 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, size);
2465 /* check for outdated collisions */
2466 GST_DEBUG ("checking collision list");
2467 item = g_list_first (sess->conflicting_addresses);
2469 RTPConflictingAddress *known_conflict = item->data;
2470 GList *next_item = g_list_next (item);
2472 if (known_conflict->time < current_time - (data.interval *
2473 RTCP_INTERVAL_COLLISION_TIMEOUT)) {
2474 sess->conflicting_addresses =
2475 g_list_delete_link (sess->conflicting_addresses, item);
2476 GST_DEBUG ("collision %p timed out", known_conflict);
2477 g_free (known_conflict);
2482 if (sess->change_ssrc) {
2483 GST_DEBUG ("need to change our SSRC (%08x)", own->ssrc);
2484 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
2485 GINT_TO_POINTER (own->ssrc));
2487 own->ssrc = rtp_session_create_new_ssrc (sess);
2488 rtp_source_reset (own);
2490 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
2491 GINT_TO_POINTER (own->ssrc), own);
2493 g_free (sess->bye_reason);
2494 sess->bye_reason = NULL;
2495 sess->sent_bye = FALSE;
2496 sess->change_ssrc = FALSE;
2498 GST_DEBUG ("changed our SSRC to %08x", own->ssrc);
2500 RTP_SESSION_UNLOCK (sess);
2503 g_object_notify (G_OBJECT (sess), "internal-ssrc");
2505 /* push out the RTCP packet */
2507 /* close the RTCP packet */
2508 gst_rtcp_buffer_end (data.rtcp);
2510 GST_DEBUG ("sending packet");
2511 if (sess->callbacks.send_rtcp)
2512 result = sess->callbacks.send_rtcp (sess, own, data.rtcp,
2513 sess->sent_bye, sess->send_rtcp_user_data);
2515 GST_DEBUG ("freeing packet");
2516 gst_buffer_unref (data.rtcp);