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_FRACTION * RTP_STATS_BANDWIDTH)
51 #define DEFAULT_RTCP_RR_BANDWIDTH -1
52 #define DEFAULT_RTCP_RS_BANDWIDTH -1
53 #define DEFAULT_RTCP_MTU 1400
54 #define DEFAULT_SDES NULL
55 #define DEFAULT_NUM_SOURCES 0
56 #define DEFAULT_NUM_ACTIVE_SOURCES 0
57 #define DEFAULT_SOURCES NULL
66 PROP_RTCP_RR_BANDWIDTH,
67 PROP_RTCP_RS_BANDWIDTH,
71 PROP_NUM_ACTIVE_SOURCES,
77 /* update average packet size */
78 #define INIT_AVG(avg, val) \
80 #define UPDATE_AVG(avg, val) \
84 (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_schedule_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_SSRC,
244 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
245 "The internal SSRC used for the session",
246 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
249 g_param_spec_object ("internal-source", "Internal Source",
250 "The internal source element of the session",
251 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
253 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
254 g_param_spec_double ("bandwidth", "Bandwidth",
255 "The bandwidth of the session (0 for auto-discover)",
256 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
257 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
259 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
260 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
261 "The fraction of the bandwidth used for RTCP (or as a real fraction of the RTP bandwidth if < 1)",
262 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
263 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
265 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
266 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
267 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
268 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
269 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
271 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
272 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
273 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
274 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
275 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
277 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
278 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
279 "The maximum size of the RTCP packets",
280 16, G_MAXINT16, DEFAULT_RTCP_MTU,
281 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
283 g_object_class_install_property (gobject_class, PROP_SDES,
284 g_param_spec_boxed ("sdes", "SDES",
285 "The SDES items of this session",
286 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
288 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
289 g_param_spec_uint ("num-sources", "Num Sources",
290 "The number of sources in the session", 0, G_MAXUINT,
291 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
293 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
294 g_param_spec_uint ("num-active-sources", "Num Active Sources",
295 "The number of active sources in the session", 0, G_MAXUINT,
296 DEFAULT_NUM_ACTIVE_SOURCES,
297 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
301 * Get a GValue Array of all sources in the session.
304 * <title>Getting the #RTPSources of a session
311 * g_object_get (sess, "sources", &arr, NULL);
313 * for (i = 0; i < arr->n_values; i++) {
316 * val = g_value_array_get_nth (arr, i);
317 * source = g_value_get_object (val);
319 * g_value_array_free (arr);
324 g_object_class_install_property (gobject_class, PROP_SOURCES,
325 g_param_spec_boxed ("sources", "Sources",
326 "An array of all known sources in the session",
327 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
329 g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
330 g_param_spec_boolean ("favor-new", "Favor new sources",
331 "Resolve SSRC conflict in favor of new sources", FALSE,
332 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
335 klass->get_source_by_ssrc =
336 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
338 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
342 rtp_session_init (RTPSession * sess)
347 sess->lock = g_mutex_new ();
348 sess->key = g_random_int ();
352 for (i = 0; i < 32; i++) {
354 g_hash_table_new_full (NULL, NULL, NULL,
355 (GDestroyNotify) g_object_unref);
357 sess->cnames = g_hash_table_new_full (NULL, NULL, g_free, NULL);
359 rtp_stats_init_defaults (&sess->stats);
361 sess->recalc_bandwidth = TRUE;
362 sess->bandwidth = DEFAULT_BANDWIDTH;
363 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
364 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
365 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
367 /* create an active SSRC for this session manager */
368 sess->source = rtp_session_create_source (sess);
369 sess->source->validated = TRUE;
370 sess->source->internal = TRUE;
371 sess->stats.active_sources++;
372 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
374 /* default UDP header length */
375 sess->header_len = 28;
376 sess->mtu = DEFAULT_RTCP_MTU;
378 /* some default SDES entries */
379 str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
380 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_CNAME, str);
383 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_NAME,
385 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_TOOL, "GStreamer");
387 sess->first_rtcp = TRUE;
389 GST_DEBUG ("%p: session using SSRC: %08x", sess, sess->source->ssrc);
393 rtp_session_finalize (GObject * object)
398 sess = RTP_SESSION_CAST (object);
400 g_mutex_free (sess->lock);
401 for (i = 0; i < 32; i++)
402 g_hash_table_destroy (sess->ssrcs[i]);
404 g_free (sess->bye_reason);
406 g_hash_table_destroy (sess->cnames);
407 g_object_unref (sess->source);
409 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
413 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
415 GValue value = { 0 };
417 g_value_init (&value, RTP_TYPE_SOURCE);
418 g_value_take_object (&value, source);
419 /* copies the value */
420 g_value_array_append (arr, &value);
424 rtp_session_create_sources (RTPSession * sess)
429 RTP_SESSION_LOCK (sess);
430 /* get number of elements in the table */
431 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
432 /* create the result value array */
433 res = g_value_array_new (size);
435 /* and copy all values into the array */
436 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
437 RTP_SESSION_UNLOCK (sess);
443 rtp_session_set_property (GObject * object, guint prop_id,
444 const GValue * value, GParamSpec * pspec)
448 sess = RTP_SESSION (object);
451 case PROP_INTERNAL_SSRC:
452 rtp_session_set_internal_ssrc (sess, g_value_get_uint (value));
455 sess->bandwidth = g_value_get_double (value);
456 sess->recalc_bandwidth = TRUE;
458 case PROP_RTCP_FRACTION:
459 sess->rtcp_bandwidth = g_value_get_double (value);
460 sess->recalc_bandwidth = TRUE;
462 case PROP_RTCP_RR_BANDWIDTH:
463 sess->rtcp_rr_bandwidth = g_value_get_int (value);
464 sess->recalc_bandwidth = TRUE;
466 case PROP_RTCP_RS_BANDWIDTH:
467 sess->rtcp_rs_bandwidth = g_value_get_int (value);
468 sess->recalc_bandwidth = TRUE;
471 sess->mtu = g_value_get_uint (value);
474 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
477 sess->favor_new = g_value_get_boolean (value);
480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
486 rtp_session_get_property (GObject * object, guint prop_id,
487 GValue * value, GParamSpec * pspec)
491 sess = RTP_SESSION (object);
494 case PROP_INTERNAL_SSRC:
495 g_value_set_uint (value, rtp_session_get_internal_ssrc (sess));
497 case PROP_INTERNAL_SOURCE:
498 g_value_take_object (value, rtp_session_get_internal_source (sess));
501 g_value_set_double (value, sess->bandwidth);
503 case PROP_RTCP_FRACTION:
504 g_value_set_double (value, sess->rtcp_bandwidth);
506 case PROP_RTCP_RR_BANDWIDTH:
507 g_value_set_int (value, sess->rtcp_rr_bandwidth);
509 case PROP_RTCP_RS_BANDWIDTH:
510 g_value_set_int (value, sess->rtcp_rs_bandwidth);
513 g_value_set_uint (value, sess->mtu);
516 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
518 case PROP_NUM_SOURCES:
519 g_value_set_uint (value, rtp_session_get_num_sources (sess));
521 case PROP_NUM_ACTIVE_SOURCES:
522 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
525 g_value_take_boxed (value, rtp_session_create_sources (sess));
528 g_value_set_boolean (value, sess->favor_new);
531 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
537 on_new_ssrc (RTPSession * sess, RTPSource * source)
539 g_object_ref (source);
540 RTP_SESSION_UNLOCK (sess);
541 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
542 RTP_SESSION_LOCK (sess);
543 g_object_unref (source);
547 on_ssrc_collision (RTPSession * sess, RTPSource * source)
549 g_object_ref (source);
550 RTP_SESSION_UNLOCK (sess);
551 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
553 RTP_SESSION_LOCK (sess);
554 g_object_unref (source);
558 on_ssrc_validated (RTPSession * sess, RTPSource * source)
560 g_object_ref (source);
561 RTP_SESSION_UNLOCK (sess);
562 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
564 RTP_SESSION_LOCK (sess);
565 g_object_unref (source);
569 on_ssrc_active (RTPSession * sess, RTPSource * source)
571 g_object_ref (source);
572 RTP_SESSION_UNLOCK (sess);
573 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
574 RTP_SESSION_LOCK (sess);
575 g_object_unref (source);
579 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
581 g_object_ref (source);
582 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
583 RTP_SESSION_UNLOCK (sess);
584 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
585 RTP_SESSION_LOCK (sess);
586 g_object_unref (source);
590 on_bye_ssrc (RTPSession * sess, RTPSource * source)
592 g_object_ref (source);
593 RTP_SESSION_UNLOCK (sess);
594 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
595 RTP_SESSION_LOCK (sess);
596 g_object_unref (source);
600 on_bye_timeout (RTPSession * sess, RTPSource * source)
602 g_object_ref (source);
603 RTP_SESSION_UNLOCK (sess);
604 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
605 RTP_SESSION_LOCK (sess);
606 g_object_unref (source);
610 on_timeout (RTPSession * sess, RTPSource * source)
612 g_object_ref (source);
613 RTP_SESSION_UNLOCK (sess);
614 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
615 RTP_SESSION_LOCK (sess);
616 g_object_unref (source);
620 on_sender_timeout (RTPSession * sess, RTPSource * source)
622 g_object_ref (source);
623 RTP_SESSION_UNLOCK (sess);
624 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
626 RTP_SESSION_LOCK (sess);
627 g_object_unref (source);
633 * Create a new session object.
635 * Returns: a new #RTPSession. g_object_unref() after usage.
638 rtp_session_new (void)
642 sess = g_object_new (RTP_TYPE_SESSION, NULL);
648 * rtp_session_set_callbacks:
649 * @sess: an #RTPSession
650 * @callbacks: callbacks to configure
651 * @user_data: user data passed in the callbacks
653 * Configure a set of callbacks to be notified of actions.
656 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
659 g_return_if_fail (RTP_IS_SESSION (sess));
661 if (callbacks->process_rtp) {
662 sess->callbacks.process_rtp = callbacks->process_rtp;
663 sess->process_rtp_user_data = user_data;
665 if (callbacks->send_rtp) {
666 sess->callbacks.send_rtp = callbacks->send_rtp;
667 sess->send_rtp_user_data = user_data;
669 if (callbacks->send_rtcp) {
670 sess->callbacks.send_rtcp = callbacks->send_rtcp;
671 sess->send_rtcp_user_data = user_data;
673 if (callbacks->sync_rtcp) {
674 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
675 sess->sync_rtcp_user_data = user_data;
677 if (callbacks->clock_rate) {
678 sess->callbacks.clock_rate = callbacks->clock_rate;
679 sess->clock_rate_user_data = user_data;
681 if (callbacks->reconsider) {
682 sess->callbacks.reconsider = callbacks->reconsider;
683 sess->reconsider_user_data = user_data;
688 * rtp_session_set_process_rtp_callback:
689 * @sess: an #RTPSession
690 * @callback: callback to set
691 * @user_data: user data passed in the callback
693 * Configure only the process_rtp callback to be notified of the process_rtp action.
696 rtp_session_set_process_rtp_callback (RTPSession * sess,
697 RTPSessionProcessRTP callback, gpointer user_data)
699 g_return_if_fail (RTP_IS_SESSION (sess));
701 sess->callbacks.process_rtp = callback;
702 sess->process_rtp_user_data = user_data;
706 * rtp_session_set_send_rtp_callback:
707 * @sess: an #RTPSession
708 * @callback: callback to set
709 * @user_data: user data passed in the callback
711 * Configure only the send_rtp callback to be notified of the send_rtp action.
714 rtp_session_set_send_rtp_callback (RTPSession * sess,
715 RTPSessionSendRTP callback, gpointer user_data)
717 g_return_if_fail (RTP_IS_SESSION (sess));
719 sess->callbacks.send_rtp = callback;
720 sess->send_rtp_user_data = user_data;
724 * rtp_session_set_send_rtcp_callback:
725 * @sess: an #RTPSession
726 * @callback: callback to set
727 * @user_data: user data passed in the callback
729 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
732 rtp_session_set_send_rtcp_callback (RTPSession * sess,
733 RTPSessionSendRTCP callback, gpointer user_data)
735 g_return_if_fail (RTP_IS_SESSION (sess));
737 sess->callbacks.send_rtcp = callback;
738 sess->send_rtcp_user_data = user_data;
742 * rtp_session_set_sync_rtcp_callback:
743 * @sess: an #RTPSession
744 * @callback: callback to set
745 * @user_data: user data passed in the callback
747 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
750 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
751 RTPSessionSyncRTCP callback, gpointer user_data)
753 g_return_if_fail (RTP_IS_SESSION (sess));
755 sess->callbacks.sync_rtcp = callback;
756 sess->sync_rtcp_user_data = user_data;
760 * rtp_session_set_clock_rate_callback:
761 * @sess: an #RTPSession
762 * @callback: callback to set
763 * @user_data: user data passed in the callback
765 * Configure only the clock_rate callback to be notified of the clock_rate action.
768 rtp_session_set_clock_rate_callback (RTPSession * sess,
769 RTPSessionClockRate callback, gpointer user_data)
771 g_return_if_fail (RTP_IS_SESSION (sess));
773 sess->callbacks.clock_rate = callback;
774 sess->clock_rate_user_data = user_data;
778 * rtp_session_set_reconsider_callback:
779 * @sess: an #RTPSession
780 * @callback: callback to set
781 * @user_data: user data passed in the callback
783 * Configure only the reconsider callback to be notified of the reconsider action.
786 rtp_session_set_reconsider_callback (RTPSession * sess,
787 RTPSessionReconsider callback, gpointer user_data)
789 g_return_if_fail (RTP_IS_SESSION (sess));
791 sess->callbacks.reconsider = callback;
792 sess->reconsider_user_data = user_data;
796 * rtp_session_set_bandwidth:
797 * @sess: an #RTPSession
798 * @bandwidth: the bandwidth allocated
800 * Set the session bandwidth in bytes per second.
803 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
805 g_return_if_fail (RTP_IS_SESSION (sess));
807 RTP_SESSION_LOCK (sess);
808 sess->stats.bandwidth = bandwidth;
809 RTP_SESSION_UNLOCK (sess);
813 * rtp_session_get_bandwidth:
814 * @sess: an #RTPSession
816 * Get the session bandwidth.
818 * Returns: the session bandwidth.
821 rtp_session_get_bandwidth (RTPSession * sess)
825 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
827 RTP_SESSION_LOCK (sess);
828 result = sess->stats.bandwidth;
829 RTP_SESSION_UNLOCK (sess);
835 * rtp_session_set_rtcp_fraction:
836 * @sess: an #RTPSession
837 * @bandwidth: the RTCP bandwidth
839 * Set the bandwidth in bytes per second that should be used for RTCP
843 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
845 g_return_if_fail (RTP_IS_SESSION (sess));
847 RTP_SESSION_LOCK (sess);
848 sess->stats.rtcp_bandwidth = bandwidth;
849 RTP_SESSION_UNLOCK (sess);
853 * rtp_session_get_rtcp_fraction:
854 * @sess: an #RTPSession
856 * Get the session bandwidth used for RTCP.
858 * Returns: The bandwidth used for RTCP messages.
861 rtp_session_get_rtcp_fraction (RTPSession * sess)
865 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
867 RTP_SESSION_LOCK (sess);
868 result = sess->stats.rtcp_bandwidth;
869 RTP_SESSION_UNLOCK (sess);
875 * rtp_session_set_sdes_string:
876 * @sess: an #RTPSession
877 * @type: the type of the SDES item
878 * @item: a null-terminated string to set.
880 * Store an SDES item of @type in @sess.
882 * Returns: %FALSE if the data was unchanged @type is invalid.
885 rtp_session_set_sdes_string (RTPSession * sess, GstRTCPSDESType type,
890 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
892 RTP_SESSION_LOCK (sess);
893 result = rtp_source_set_sdes_string (sess->source, type, item);
894 RTP_SESSION_UNLOCK (sess);
900 * rtp_session_get_sdes_string:
901 * @sess: an #RTPSession
902 * @type: the type of the SDES item
904 * Get the SDES item of @type from @sess.
906 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
907 * valid. g_free() after usage.
910 rtp_session_get_sdes_string (RTPSession * sess, GstRTCPSDESType type)
914 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
916 RTP_SESSION_LOCK (sess);
917 result = rtp_source_get_sdes_string (sess->source, type);
918 RTP_SESSION_UNLOCK (sess);
924 * rtp_session_get_sdes_struct:
925 * @sess: an #RTSPSession
927 * Get the SDES data as a #GstStructure
929 * Returns: a GstStructure with SDES items for @sess. This function returns a
930 * copy of the SDES structure, use gst_structure_free() after usage.
933 rtp_session_get_sdes_struct (RTPSession * sess)
935 const GstStructure *sdes;
936 GstStructure *result = NULL;
938 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
940 RTP_SESSION_LOCK (sess);
941 sdes = rtp_source_get_sdes_struct (sess->source);
943 result = gst_structure_copy (sdes);
944 RTP_SESSION_UNLOCK (sess);
950 * rtp_session_set_sdes_struct:
951 * @sess: an #RTSPSession
952 * @sdes: a #GstStructure
954 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
957 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
959 g_return_if_fail (sdes);
960 g_return_if_fail (RTP_IS_SESSION (sess));
962 RTP_SESSION_LOCK (sess);
963 rtp_source_set_sdes_struct (sess->source, gst_structure_copy (sdes));
964 RTP_SESSION_UNLOCK (sess);
968 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
970 GstFlowReturn result = GST_FLOW_OK;
972 if (source == session->source) {
973 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
975 RTP_SESSION_UNLOCK (session);
977 if (session->callbacks.send_rtp)
979 session->callbacks.send_rtp (session, source, data,
980 session->send_rtp_user_data);
982 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
985 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
986 RTP_SESSION_UNLOCK (session);
988 if (session->callbacks.process_rtp)
990 session->callbacks.process_rtp (session, source,
991 GST_BUFFER_CAST (data), session->process_rtp_user_data);
993 gst_buffer_unref (GST_BUFFER_CAST (data));
995 RTP_SESSION_LOCK (session);
1001 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1005 RTP_SESSION_UNLOCK (session);
1007 if (session->callbacks.clock_rate)
1009 session->callbacks.clock_rate (session, pt,
1010 session->clock_rate_user_data);
1014 RTP_SESSION_LOCK (session);
1016 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1021 static RTPSourceCallbacks callbacks = {
1022 (RTPSourcePushRTP) source_push_rtp,
1023 (RTPSourceClockRate) source_clock_rate,
1027 check_collision (RTPSession * sess, RTPSource * source,
1028 RTPArrivalStats * arrival, gboolean rtp)
1030 /* If we have no arrival address, we can't do collision checking */
1031 if (!arrival->have_address)
1034 if (sess->source != source) {
1035 GstNetAddress *from;
1038 /* This is not our local source, but lets check if two remote
1043 from = &source->rtp_from;
1044 have_from = source->have_rtp_from;
1046 from = &source->rtcp_from;
1047 have_from = source->have_rtcp_from;
1051 if (gst_netaddress_equal (from, &arrival->address)) {
1052 /* Address is the same */
1055 GST_LOG ("we have a third-party collision or loop ssrc:%x",
1056 rtp_source_get_ssrc (source));
1057 if (sess->favor_new) {
1058 if (rtp_source_find_conflicting_address (source,
1059 &arrival->address, arrival->current_time)) {
1061 gst_netaddress_to_string (&arrival->address, buf1, 40);
1062 GST_LOG ("Known conflict on %x for %s, dropping packet",
1063 rtp_source_get_ssrc (source), buf1);
1066 gchar buf1[40], buf2[40];
1068 /* Current address is not a known conflict, lets assume this is
1069 * a new source. Save old address in possible conflict list
1071 rtp_source_add_conflicting_address (source, from,
1072 arrival->current_time);
1074 gst_netaddress_to_string (from, buf1, 40);
1075 gst_netaddress_to_string (&arrival->address, buf2, 40);
1076 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1077 " saving old as known conflict",
1078 rtp_source_get_ssrc (source), buf1, buf2);
1081 rtp_source_set_rtp_from (source, &arrival->address);
1083 rtp_source_set_rtcp_from (source, &arrival->address);
1087 /* Don't need to save old addresses, we ignore new sources */
1092 /* We don't already have a from address for RTP, just set it */
1094 rtp_source_set_rtp_from (source, &arrival->address);
1096 rtp_source_set_rtcp_from (source, &arrival->address);
1100 /* FIXME: Log 3rd party collision somehow
1101 * Maybe should be done in upper layer, only the SDES can tell us
1102 * if its a collision or a loop
1105 /* If the source has been inactive for some time, we assume that it has
1106 * simply changed its transport source address. Hence, there is no true
1107 * third-party collision - only a simulated one. */
1108 if (arrival->current_time > source->last_activity) {
1109 GstClockTime inactivity_period =
1110 arrival->current_time - source->last_activity;
1111 if (inactivity_period > 1 * GST_SECOND) {
1112 /* Use new network address */
1114 g_assert (source->have_rtp_from);
1115 rtp_source_set_rtp_from (source, &arrival->address);
1117 g_assert (source->have_rtcp_from);
1118 rtp_source_set_rtcp_from (source, &arrival->address);
1124 /* This is sending with our ssrc, is it an address we already know */
1126 if (rtp_source_find_conflicting_address (source, &arrival->address,
1127 arrival->current_time)) {
1128 /* Its a known conflict, its probably a loop, not a collision
1129 * lets just drop the incoming packet
1131 GST_DEBUG ("Our packets are being looped back to us, dropping");
1133 /* Its a new collision, lets change our SSRC */
1135 rtp_source_add_conflicting_address (source, &arrival->address,
1136 arrival->current_time);
1138 GST_DEBUG ("Collision for SSRC %x", rtp_source_get_ssrc (source));
1139 on_ssrc_collision (sess, source);
1141 rtp_session_schedule_bye_locked (sess, "SSRC Collision",
1142 arrival->current_time);
1144 sess->change_ssrc = TRUE;
1152 /* must be called with the session lock, the returned source needs to be
1153 * unreffed after usage. */
1155 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1156 RTPArrivalStats * arrival, gboolean rtp)
1161 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1162 if (source == NULL) {
1163 /* make new Source in probation and insert */
1164 source = rtp_source_new (ssrc);
1166 /* for RTP packets we need to set the source in probation. Receiving RTCP
1167 * packets of an SSRC, on the other hand, is a strong indication that we
1168 * are dealing with a valid source. */
1170 source->probation = RTP_DEFAULT_PROBATION;
1172 source->probation = 0;
1174 /* store from address, if any */
1175 if (arrival->have_address) {
1177 rtp_source_set_rtp_from (source, &arrival->address);
1179 rtp_source_set_rtcp_from (source, &arrival->address);
1182 /* configure a callback on the source */
1183 rtp_source_set_callbacks (source, &callbacks, sess);
1185 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1188 /* we have one more source now */
1189 sess->total_sources++;
1193 /* check for collision, this updates the address when not previously set */
1194 if (check_collision (sess, source, arrival, rtp)) {
1198 /* update last activity */
1199 source->last_activity = arrival->current_time;
1201 source->last_rtp_activity = arrival->current_time;
1202 g_object_ref (source);
1208 * rtp_session_get_internal_source:
1209 * @sess: a #RTPSession
1211 * Get the internal #RTPSource of @sess.
1213 * Returns: The internal #RTPSource. g_object_unref() after usage.
1216 rtp_session_get_internal_source (RTPSession * sess)
1220 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1222 result = g_object_ref (sess->source);
1228 * rtp_session_set_internal_ssrc:
1229 * @sess: a #RTPSession
1232 * Set the SSRC of @sess to @ssrc.
1235 rtp_session_set_internal_ssrc (RTPSession * sess, guint32 ssrc)
1237 RTP_SESSION_LOCK (sess);
1238 if (ssrc != sess->source->ssrc) {
1239 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
1240 GINT_TO_POINTER (sess->source->ssrc));
1242 GST_DEBUG ("setting internal SSRC to %08x", ssrc);
1243 /* After this call, any receiver of the old SSRC either in RTP or RTCP
1244 * packets will timeout on the old SSRC, we could potentially schedule a
1245 * BYE RTCP for the old SSRC... */
1246 sess->source->ssrc = ssrc;
1247 rtp_source_reset (sess->source);
1249 /* rehash with the new SSRC */
1250 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1251 GINT_TO_POINTER (sess->source->ssrc), sess->source);
1253 RTP_SESSION_UNLOCK (sess);
1255 g_object_notify (G_OBJECT (sess), "internal-ssrc");
1259 * rtp_session_get_internal_ssrc:
1260 * @sess: a #RTPSession
1262 * Get the internal SSRC of @sess.
1264 * Returns: The SSRC of the session.
1267 rtp_session_get_internal_ssrc (RTPSession * sess)
1271 RTP_SESSION_LOCK (sess);
1272 ssrc = sess->source->ssrc;
1273 RTP_SESSION_UNLOCK (sess);
1279 * rtp_session_add_source:
1280 * @sess: a #RTPSession
1281 * @src: #RTPSource to add
1283 * Add @src to @session.
1285 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1286 * existed in the session.
1289 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1291 gboolean result = FALSE;
1294 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1295 g_return_val_if_fail (src != NULL, FALSE);
1297 RTP_SESSION_LOCK (sess);
1299 g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1300 GINT_TO_POINTER (src->ssrc));
1302 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1303 GINT_TO_POINTER (src->ssrc), src);
1304 /* we have one more source now */
1305 sess->total_sources++;
1308 RTP_SESSION_UNLOCK (sess);
1314 * rtp_session_get_num_sources:
1315 * @sess: an #RTPSession
1317 * Get the number of sources in @sess.
1319 * Returns: The number of sources in @sess.
1322 rtp_session_get_num_sources (RTPSession * sess)
1326 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1328 RTP_SESSION_LOCK (sess);
1329 result = sess->total_sources;
1330 RTP_SESSION_UNLOCK (sess);
1336 * rtp_session_get_num_active_sources:
1337 * @sess: an #RTPSession
1339 * Get the number of active sources in @sess. A source is considered active when
1340 * it has been validated and has not yet received a BYE RTCP message.
1342 * Returns: The number of active sources in @sess.
1345 rtp_session_get_num_active_sources (RTPSession * sess)
1349 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1351 RTP_SESSION_LOCK (sess);
1352 result = sess->stats.active_sources;
1353 RTP_SESSION_UNLOCK (sess);
1359 * rtp_session_get_source_by_ssrc:
1360 * @sess: an #RTPSession
1363 * Find the source with @ssrc in @sess.
1365 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1366 * g_object_unref() after usage.
1369 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1373 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1375 RTP_SESSION_LOCK (sess);
1377 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1379 g_object_ref (result);
1380 RTP_SESSION_UNLOCK (sess);
1386 * rtp_session_get_source_by_cname:
1387 * @sess: a #RTPSession
1390 * Find the source with @cname in @sess.
1392 * Returns: a #RTPSource with CNAME @cname or NULL if the source was not found.
1393 * g_object_unref() after usage.
1396 rtp_session_get_source_by_cname (RTPSession * sess, const gchar * cname)
1400 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1401 g_return_val_if_fail (cname != NULL, NULL);
1403 RTP_SESSION_LOCK (sess);
1404 result = g_hash_table_lookup (sess->cnames, cname);
1406 g_object_ref (result);
1407 RTP_SESSION_UNLOCK (sess);
1412 /* should be called with the SESSION lock */
1414 rtp_session_create_new_ssrc (RTPSession * sess)
1419 ssrc = g_random_int ();
1421 /* see if it exists in the session, we're done if it doesn't */
1422 if (g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1423 GINT_TO_POINTER (ssrc)) == NULL)
1431 * rtp_session_create_source:
1432 * @sess: an #RTPSession
1434 * Create an #RTPSource for use in @sess. This function will create a source
1435 * with an ssrc that is currently not used by any participants in the session.
1437 * Returns: an #RTPSource.
1440 rtp_session_create_source (RTPSession * sess)
1445 RTP_SESSION_LOCK (sess);
1446 ssrc = rtp_session_create_new_ssrc (sess);
1447 source = rtp_source_new (ssrc);
1448 rtp_source_set_callbacks (source, &callbacks, sess);
1449 /* we need an additional ref for the source in the hashtable */
1450 g_object_ref (source);
1451 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1453 /* we have one more source now */
1454 sess->total_sources++;
1455 RTP_SESSION_UNLOCK (sess);
1460 /* update the RTPArrivalStats structure with the current time and other bits
1461 * about the current buffer we are handling.
1462 * This function is typically called when a validated packet is received.
1463 * This function should be called with the SESSION_LOCK
1466 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
1467 gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
1468 GstClockTime running_time)
1470 /* get time of arrival */
1471 arrival->current_time = current_time;
1472 arrival->running_time = running_time;
1474 /* get packet size including header overhead */
1475 arrival->bytes = GST_BUFFER_SIZE (buffer) + sess->header_len;
1478 arrival->payload_len = gst_rtp_buffer_get_payload_len (buffer);
1480 arrival->payload_len = 0;
1483 /* for netbuffer we can store the IP address to check for collisions */
1484 arrival->have_address = GST_IS_NETBUFFER (buffer);
1485 if (arrival->have_address) {
1486 GstNetBuffer *netbuf = (GstNetBuffer *) buffer;
1488 memcpy (&arrival->address, &netbuf->from, sizeof (GstNetAddress));
1493 * rtp_session_process_rtp:
1494 * @sess: and #RTPSession
1495 * @buffer: an RTP buffer
1496 * @current_time: the current system time
1497 * @running_time: the running_time of @buffer
1499 * Process an RTP buffer in the session manager. This function takes ownership
1502 * Returns: a #GstFlowReturn.
1505 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1506 GstClockTime current_time, GstClockTime running_time)
1508 GstFlowReturn result;
1512 gboolean prevsender, prevactive;
1513 RTPArrivalStats arrival;
1518 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1519 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1521 if (!gst_rtp_buffer_validate (buffer))
1522 goto invalid_packet;
1524 RTP_SESSION_LOCK (sess);
1525 /* update arrival stats */
1526 update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1529 /* ignore more RTP packets when we left the session */
1530 if (sess->source->received_bye)
1533 /* get SSRC and look up in session database */
1534 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1535 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1539 prevsender = RTP_SOURCE_IS_SENDER (source);
1540 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1541 oldrate = source->bitrate;
1543 /* copy available csrc for later */
1544 count = gst_rtp_buffer_get_csrc_count (buffer);
1545 /* make sure to not overflow our array. An RTP buffer can maximally contain
1547 count = MIN (count, 16);
1549 for (i = 0; i < count; i++)
1550 csrcs[i] = gst_rtp_buffer_get_csrc (buffer, i);
1552 /* let source process the packet */
1553 result = rtp_source_process_rtp (source, buffer, &arrival);
1555 /* source became active */
1556 if (prevactive != RTP_SOURCE_IS_ACTIVE (source)) {
1557 sess->stats.active_sources++;
1558 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1559 sess->stats.active_sources);
1560 on_ssrc_validated (sess, source);
1562 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1563 sess->stats.sender_sources++;
1564 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1565 sess->stats.sender_sources);
1567 if (oldrate != source->bitrate)
1568 sess->recalc_bandwidth = TRUE;
1571 on_new_ssrc (sess, source);
1573 if (source->validated) {
1576 /* for validated sources, we add the CSRCs as well */
1577 for (i = 0; i < count; i++) {
1579 RTPSource *csrc_src;
1584 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1589 GST_DEBUG ("created new CSRC: %08x", csrc);
1590 rtp_source_set_as_csrc (csrc_src);
1591 if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1592 sess->stats.active_sources++;
1593 on_new_ssrc (sess, csrc_src);
1595 g_object_unref (csrc_src);
1598 g_object_unref (source);
1600 RTP_SESSION_UNLOCK (sess);
1607 gst_buffer_unref (buffer);
1608 GST_DEBUG ("invalid RTP packet received");
1613 gst_buffer_unref (buffer);
1614 RTP_SESSION_UNLOCK (sess);
1615 GST_DEBUG ("ignoring RTP packet because we are leaving");
1620 gst_buffer_unref (buffer);
1621 RTP_SESSION_UNLOCK (sess);
1622 GST_DEBUG ("ignoring packet because its collisioning");
1628 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1629 GstRTCPPacket * packet, RTPArrivalStats * arrival)
1633 count = gst_rtcp_packet_get_rb_count (packet);
1634 for (i = 0; i < count; i++) {
1635 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1636 guint8 fractionlost;
1639 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1640 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1642 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1644 if (ssrc == sess->source->ssrc) {
1645 /* only deal with report blocks for our session, we update the stats of
1646 * the sender of the RTCP message. We could also compare our stats against
1647 * the other sender to see if we are better or worse. */
1648 rtp_source_process_rb (source, arrival->current_time, fractionlost,
1649 packetslost, exthighestseq, jitter, lsr, dlsr);
1652 on_ssrc_active (sess, source);
1655 /* A Sender report contains statistics about how the sender is doing. This
1656 * includes timing informataion such as the relation between RTP and NTP
1657 * timestamps and the number of packets/bytes it sent to us.
1659 * In this report is also included a set of report blocks related to how this
1660 * sender is receiving data (in case we (or somebody else) is also sending stuff
1661 * to it). This info includes the packet loss, jitter and seqnum. It also
1662 * contains information to calculate the round trip time (LSR/DLSR).
1665 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1666 RTPArrivalStats * arrival, gboolean * do_sync)
1668 guint32 senderssrc, rtptime, packet_count, octet_count;
1671 gboolean created, prevsender;
1673 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1674 &packet_count, &octet_count);
1676 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1677 senderssrc, GST_TIME_ARGS (arrival->current_time));
1679 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1683 /* don't try to do lip-sync for sources that sent a BYE */
1684 if (rtp_source_received_bye (source))
1689 prevsender = RTP_SOURCE_IS_SENDER (source);
1691 /* first update the source */
1692 rtp_source_process_sr (source, arrival->current_time, ntptime, rtptime,
1693 packet_count, octet_count);
1695 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1696 sess->stats.sender_sources++;
1697 GST_DEBUG ("source: %08x became sender, %d sender sources", senderssrc,
1698 sess->stats.sender_sources);
1702 on_new_ssrc (sess, source);
1704 rtp_session_process_rb (sess, source, packet, arrival);
1705 g_object_unref (source);
1708 /* A receiver report contains statistics about how a receiver is doing. It
1709 * includes stuff like packet loss, jitter and the seqnum it received last. It
1710 * also contains info to calculate the round trip time.
1712 * We are only interested in how the sender of this report is doing wrt to us.
1715 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1716 RTPArrivalStats * arrival)
1722 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1724 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1726 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1731 on_new_ssrc (sess, source);
1733 rtp_session_process_rb (sess, source, packet, arrival);
1734 g_object_unref (source);
1737 /* Get SDES items and store them in the SSRC */
1739 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1740 RTPArrivalStats * arrival)
1743 gboolean more_items, more_entries;
1745 items = gst_rtcp_packet_sdes_get_item_count (packet);
1746 GST_DEBUG ("got SDES packet with %d items", items);
1748 more_items = gst_rtcp_packet_sdes_first_item (packet);
1750 while (more_items) {
1752 gboolean changed, created;
1756 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1758 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1762 /* find src, no probation when dealing with RTCP */
1763 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1767 sdes = gst_structure_new ("application/x-rtp-source-sdes", NULL);
1769 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1771 while (more_entries) {
1772 GstRTCPSDESType type;
1778 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1780 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1783 if (type == GST_RTCP_SDES_PRIV) {
1784 name = g_strndup ((const gchar *) &data[1], data[0]);
1786 data += data[0] + 1;
1788 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
1791 value = g_strndup ((const gchar *) data, len);
1793 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
1798 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1802 /* takes ownership of sdes */
1803 changed = rtp_source_set_sdes_struct (source, sdes);
1805 source->validated = TRUE;
1808 on_new_ssrc (sess, source);
1810 on_ssrc_sdes (sess, source);
1812 g_object_unref (source);
1814 more_items = gst_rtcp_packet_sdes_next_item (packet);
1819 /* BYE is sent when a client leaves the session
1822 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
1823 RTPArrivalStats * arrival)
1827 gboolean reconsider = FALSE;
1829 reason = gst_rtcp_packet_bye_get_reason (packet);
1830 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
1832 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
1833 for (i = 0; i < count; i++) {
1836 gboolean created, prevactive, prevsender;
1837 guint pmembers, members;
1839 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
1840 GST_DEBUG ("SSRC: %08x", ssrc);
1842 /* find src and mark bye, no probation when dealing with RTCP */
1843 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1847 /* store time for when we need to time out this source */
1848 source->bye_time = arrival->current_time;
1850 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1851 prevsender = RTP_SOURCE_IS_SENDER (source);
1853 /* let the source handle the rest */
1854 rtp_source_process_bye (source, reason);
1856 pmembers = sess->stats.active_sources;
1858 if (prevactive && !RTP_SOURCE_IS_ACTIVE (source)) {
1859 sess->stats.active_sources--;
1860 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
1861 sess->stats.active_sources);
1863 if (prevsender && !RTP_SOURCE_IS_SENDER (source)) {
1864 sess->stats.sender_sources--;
1865 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
1866 sess->stats.sender_sources);
1868 members = sess->stats.active_sources;
1870 if (!sess->source->received_bye && members < pmembers) {
1871 /* some members went away since the previous timeout estimate.
1872 * Perform reverse reconsideration but only when we are not scheduling a
1874 if (arrival->current_time < sess->next_rtcp_check_time) {
1875 GstClockTime time_remaining;
1877 time_remaining = sess->next_rtcp_check_time - arrival->current_time;
1878 sess->next_rtcp_check_time =
1879 gst_util_uint64_scale (time_remaining, members, pmembers);
1881 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
1882 GST_TIME_ARGS (sess->next_rtcp_check_time));
1884 sess->next_rtcp_check_time += arrival->current_time;
1886 /* mark pending reconsider. We only want to signal the reconsideration
1887 * once after we handled all the source in the bye packet */
1893 on_new_ssrc (sess, source);
1895 on_bye_ssrc (sess, source);
1897 g_object_unref (source);
1900 RTP_SESSION_UNLOCK (sess);
1901 /* notify app of reconsideration */
1902 if (sess->callbacks.reconsider)
1903 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
1904 RTP_SESSION_LOCK (sess);
1910 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
1911 RTPArrivalStats * arrival)
1913 GST_DEBUG ("received APP");
1917 * rtp_session_process_rtcp:
1918 * @sess: and #RTPSession
1919 * @buffer: an RTCP buffer
1920 * @current_time: the current system time
1922 * Process an RTCP buffer in the session manager. This function takes ownership
1925 * Returns: a #GstFlowReturn.
1928 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
1929 GstClockTime current_time)
1931 GstRTCPPacket packet;
1932 gboolean more, is_bye = FALSE, do_sync = FALSE;
1933 RTPArrivalStats arrival;
1934 GstFlowReturn result = GST_FLOW_OK;
1936 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1937 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1939 if (!gst_rtcp_buffer_validate (buffer))
1940 goto invalid_packet;
1942 GST_DEBUG ("received RTCP packet");
1944 RTP_SESSION_LOCK (sess);
1945 /* update arrival stats */
1946 update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1);
1951 /* make writable, we might want to change the buffer */
1952 buffer = gst_buffer_make_metadata_writable (buffer);
1954 /* start processing the compound packet */
1955 more = gst_rtcp_buffer_get_first_packet (buffer, &packet);
1959 type = gst_rtcp_packet_get_type (&packet);
1961 /* when we are leaving the session, we should ignore all non-BYE messages */
1962 if (sess->source->received_bye && type != GST_RTCP_TYPE_BYE) {
1963 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
1968 case GST_RTCP_TYPE_SR:
1969 rtp_session_process_sr (sess, &packet, &arrival, &do_sync);
1971 case GST_RTCP_TYPE_RR:
1972 rtp_session_process_rr (sess, &packet, &arrival);
1974 case GST_RTCP_TYPE_SDES:
1975 rtp_session_process_sdes (sess, &packet, &arrival);
1977 case GST_RTCP_TYPE_BYE:
1979 /* don't try to attempt lip-sync anymore for streams with a BYE */
1981 rtp_session_process_bye (sess, &packet, &arrival);
1983 case GST_RTCP_TYPE_APP:
1984 rtp_session_process_app (sess, &packet, &arrival);
1987 GST_WARNING ("got unknown RTCP packet");
1991 more = gst_rtcp_packet_move_to_next (&packet);
1994 /* if we are scheduling a BYE, we only want to count bye packets, else we
1995 * count everything */
1996 if (sess->source->received_bye) {
1998 sess->stats.bye_members++;
1999 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2002 /* keep track of average packet size */
2003 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2005 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
2006 sess->stats.avg_rtcp_packet_size, arrival.bytes);
2007 RTP_SESSION_UNLOCK (sess);
2009 /* notify caller of sr packets in the callback */
2010 if (do_sync && sess->callbacks.sync_rtcp)
2011 result = sess->callbacks.sync_rtcp (sess, sess->source, buffer,
2012 sess->sync_rtcp_user_data);
2014 gst_buffer_unref (buffer);
2021 GST_DEBUG ("invalid RTCP packet received");
2022 gst_buffer_unref (buffer);
2027 gst_buffer_unref (buffer);
2028 RTP_SESSION_UNLOCK (sess);
2029 GST_DEBUG ("ignoring RTP packet because we left");
2035 * rtp_session_send_rtp:
2036 * @sess: an #RTPSession
2037 * @data: pointer to either an RTP buffer or a list of RTP buffers
2038 * @is_list: TRUE when @data is a buffer list
2039 * @current_time: the current system time
2040 * @running_time: the running time of @data
2042 * Send the RTP buffer in the session manager. This function takes ownership of
2045 * Returns: a #GstFlowReturn.
2048 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
2049 GstClockTime current_time, GstClockTime running_time)
2051 GstFlowReturn result;
2053 gboolean prevsender;
2054 gboolean valid_packet;
2057 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2058 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
2061 valid_packet = gst_rtp_buffer_list_validate (GST_BUFFER_LIST_CAST (data));
2063 valid_packet = gst_rtp_buffer_validate (GST_BUFFER_CAST (data));
2067 goto invalid_packet;
2069 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
2071 RTP_SESSION_LOCK (sess);
2072 source = sess->source;
2074 /* update last activity */
2075 source->last_rtp_activity = current_time;
2077 prevsender = RTP_SOURCE_IS_SENDER (source);
2078 oldrate = source->bitrate;
2080 /* we use our own source to send */
2081 result = rtp_source_send_rtp (source, data, is_list, running_time);
2083 if (RTP_SOURCE_IS_SENDER (source) && !prevsender)
2084 sess->stats.sender_sources++;
2085 if (oldrate != source->bitrate)
2086 sess->recalc_bandwidth = TRUE;
2087 RTP_SESSION_UNLOCK (sess);
2094 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
2095 GST_DEBUG ("invalid RTP packet received");
2101 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
2103 *bandwidth += source->bitrate;
2107 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2110 GstClockTime result;
2112 /* recalculate bandwidth when it changed */
2113 if (sess->recalc_bandwidth) {
2116 if (sess->bandwidth > 0)
2117 bandwidth = sess->bandwidth;
2119 /* If it is <= 0, then try to estimate the actual bandwidth */
2120 bandwidth = sess->source->bitrate;
2122 g_hash_table_foreach (sess->cnames, (GHFunc) add_bitrates, &bandwidth);
2126 bandwidth = RTP_STATS_BANDWIDTH;
2128 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
2129 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
2131 sess->recalc_bandwidth = FALSE;
2134 if (sess->source->received_bye) {
2135 result = rtp_stats_calculate_bye_interval (&sess->stats);
2137 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2138 RTP_SOURCE_IS_SENDER (sess->source), first);
2141 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2142 GST_TIME_ARGS (result), first);
2144 if (!deterministic && result != GST_CLOCK_TIME_NONE)
2145 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2147 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2152 /* Stop the current @sess and schedule a BYE message for the other members.
2153 * One must have the session lock to call this function
2155 static GstFlowReturn
2156 rtp_session_schedule_bye_locked (RTPSession * sess, const gchar * reason,
2157 GstClockTime current_time)
2159 GstFlowReturn result = GST_FLOW_OK;
2161 GstClockTime interval;
2163 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2165 source = sess->source;
2167 /* ignore more BYEs */
2168 if (source->received_bye)
2171 /* we have BYE now */
2172 source->received_bye = TRUE;
2173 /* at least one member wants to send a BYE */
2174 g_free (sess->bye_reason);
2175 sess->bye_reason = g_strdup (reason);
2176 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
2177 sess->stats.bye_members = 1;
2178 sess->first_rtcp = TRUE;
2179 sess->sent_bye = FALSE;
2181 /* reschedule transmission */
2182 sess->last_rtcp_send_time = current_time;
2183 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2184 sess->next_rtcp_check_time = current_time + interval;
2186 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2187 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2189 RTP_SESSION_UNLOCK (sess);
2190 /* notify app of reconsideration */
2191 if (sess->callbacks.reconsider)
2192 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2193 RTP_SESSION_LOCK (sess);
2200 * rtp_session_schedule_bye:
2201 * @sess: an #RTPSession
2202 * @reason: a reason or NULL
2203 * @current_time: the current system time
2205 * Stop the current @sess and schedule a BYE message for the other members.
2207 * Returns: a #GstFlowReturn.
2210 rtp_session_schedule_bye (RTPSession * sess, const gchar * reason,
2211 GstClockTime current_time)
2213 GstFlowReturn result = GST_FLOW_OK;
2215 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2217 RTP_SESSION_LOCK (sess);
2218 result = rtp_session_schedule_bye_locked (sess, reason, current_time);
2219 RTP_SESSION_UNLOCK (sess);
2225 * rtp_session_next_timeout:
2226 * @sess: an #RTPSession
2227 * @current_time: the current system time
2229 * Get the next time we should perform session maintenance tasks.
2231 * Returns: a time when rtp_session_on_timeout() should be called with the
2232 * current system time.
2235 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2237 GstClockTime result, interval = 0;
2239 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
2241 RTP_SESSION_LOCK (sess);
2243 result = sess->next_rtcp_check_time;
2245 GST_DEBUG ("current time: %" GST_TIME_FORMAT ", next :%" GST_TIME_FORMAT,
2246 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2248 if (result < current_time) {
2249 GST_DEBUG ("take current time as base");
2250 /* our previous check time expired, start counting from the current time
2252 result = current_time;
2255 if (sess->source->received_bye) {
2256 if (sess->sent_bye) {
2257 GST_DEBUG ("we sent BYE already");
2258 interval = GST_CLOCK_TIME_NONE;
2259 } else if (sess->stats.active_sources >= 50) {
2260 GST_DEBUG ("reconsider BYE, more than 50 sources");
2261 /* reconsider BYE if members >= 50 */
2262 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2265 if (sess->first_rtcp) {
2266 GST_DEBUG ("first RTCP packet");
2267 /* we are called for the first time */
2268 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2269 } else if (sess->next_rtcp_check_time < current_time) {
2270 GST_DEBUG ("old check time expired, getting new timeout");
2271 /* get a new timeout when we need to */
2272 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
2276 if (interval != GST_CLOCK_TIME_NONE)
2279 result = GST_CLOCK_TIME_NONE;
2281 sess->next_rtcp_check_time = result;
2283 GST_DEBUG ("next timeout: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2284 RTP_SESSION_UNLOCK (sess);
2293 GstClockTime current_time;
2295 GstClockTime running_time;
2296 GstClockTime interval;
2297 GstRTCPPacket packet;
2303 session_start_rtcp (RTPSession * sess, ReportData * data)
2305 GstRTCPPacket *packet = &data->packet;
2306 RTPSource *own = sess->source;
2308 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2310 if (RTP_SOURCE_IS_SENDER (own)) {
2313 guint32 packet_count, octet_count;
2315 /* we are a sender, create SR */
2316 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2317 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SR, packet);
2319 /* get latest stats */
2320 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
2321 &ntptime, &rtptime, &packet_count, &octet_count);
2323 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2324 packet_count, octet_count);
2326 /* fill in sender report info */
2327 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2328 ntptime, rtptime, packet_count, octet_count);
2330 /* we are only receiver, create RR */
2331 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2332 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_RR, packet);
2333 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2337 /* construct a Sender or Receiver Report */
2339 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2341 RTPSession *sess = data->sess;
2342 GstRTCPPacket *packet = &data->packet;
2344 /* create a new buffer if needed */
2345 if (data->rtcp == NULL) {
2346 session_start_rtcp (sess, data);
2348 if (gst_rtcp_packet_get_rb_count (packet) < GST_RTCP_MAX_RB_COUNT) {
2349 /* only report about other sender sources */
2350 if (source != sess->source && RTP_SOURCE_IS_SENDER (source)) {
2351 guint8 fractionlost;
2353 guint32 exthighestseq, jitter;
2357 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2358 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2360 /* packet is not yet filled, add report block for this source. */
2361 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2362 exthighestseq, jitter, lsr, dlsr);
2367 /* perform cleanup of sources that timed out */
2369 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2371 gboolean remove = FALSE;
2372 gboolean byetimeout = FALSE;
2373 gboolean sendertimeout = FALSE;
2374 gboolean is_sender, is_active;
2375 RTPSession *sess = data->sess;
2376 GstClockTime interval;
2378 is_sender = RTP_SOURCE_IS_SENDER (source);
2379 is_active = RTP_SOURCE_IS_ACTIVE (source);
2381 /* check for our own source, we don't want to delete our own source. */
2382 if (!(source == sess->source)) {
2383 if (source->received_bye) {
2384 /* if we received a BYE from the source, remove the source after some
2386 if (data->current_time > source->bye_time &&
2387 data->current_time - source->bye_time > sess->stats.bye_timeout) {
2388 GST_DEBUG ("removing BYE source %08x", source->ssrc);
2393 /* sources that were inactive for more than 5 times the deterministic reporting
2394 * interval get timed out. the min timeout is 5 seconds. */
2395 if (data->current_time > source->last_activity) {
2396 interval = MAX (data->interval * 5, 5 * GST_SECOND);
2397 if (data->current_time - source->last_activity > interval) {
2398 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2399 source->ssrc, GST_TIME_ARGS (source->last_activity));
2405 /* senders that did not send for a long time become a receiver, this also
2406 * holds for our own source. */
2408 if (data->current_time > source->last_rtp_activity) {
2409 interval = MAX (data->interval * 2, 5 * GST_SECOND);
2410 if (data->current_time - source->last_rtp_activity > interval) {
2411 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
2412 GST_TIME_FORMAT, source->ssrc,
2413 GST_TIME_ARGS (source->last_rtp_activity));
2414 source->is_sender = FALSE;
2415 sess->stats.sender_sources--;
2416 sendertimeout = TRUE;
2422 sess->total_sources--;
2424 sess->stats.sender_sources--;
2426 sess->stats.active_sources--;
2429 on_bye_timeout (sess, source);
2431 on_timeout (sess, source);
2434 on_sender_timeout (sess, source);
2437 source->closing = remove;
2441 session_sdes (RTPSession * sess, ReportData * data)
2443 GstRTCPPacket *packet = &data->packet;
2444 const GstStructure *sdes;
2447 /* add SDES packet */
2448 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SDES, packet);
2450 gst_rtcp_packet_sdes_add_item (packet, sess->source->ssrc);
2452 sdes = rtp_source_get_sdes_struct (sess->source);
2454 /* add all fields in the structure, the order is not important. */
2455 n_fields = gst_structure_n_fields (sdes);
2456 for (i = 0; i < n_fields; ++i) {
2459 GstRTCPSDESType type;
2461 field = gst_structure_nth_field_name (sdes, i);
2464 value = gst_structure_get_string (sdes, field);
2467 type = gst_rtcp_sdes_name_to_type (field);
2469 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
2470 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
2471 (const guint8 *) value);
2472 } else if (type == GST_RTCP_SDES_PRIV) {
2478 /* don't accept entries that are too big */
2479 prefix_len = strlen (field);
2480 if (prefix_len > 255)
2482 value_len = strlen (value);
2483 if (value_len > 255)
2485 data_len = 1 + prefix_len + value_len;
2489 data[0] = prefix_len;
2490 memcpy (&data[1], field, prefix_len);
2491 memcpy (&data[1 + prefix_len], value, value_len);
2493 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
2497 data->has_sdes = TRUE;
2500 /* schedule a BYE packet */
2502 session_bye (RTPSession * sess, ReportData * data)
2504 GstRTCPPacket *packet = &data->packet;
2507 session_start_rtcp (sess, data);
2510 session_sdes (sess, data);
2512 /* add a BYE packet */
2513 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_BYE, packet);
2514 gst_rtcp_packet_bye_add_ssrc (packet, sess->source->ssrc);
2515 if (sess->bye_reason)
2516 gst_rtcp_packet_bye_set_reason (packet, sess->bye_reason);
2518 /* we have a BYE packet now */
2519 data->is_bye = TRUE;
2523 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
2525 GstClockTime new_send_time, elapsed;
2528 /* no need to check yet */
2529 if (sess->next_rtcp_check_time > current_time) {
2530 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
2531 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
2532 GST_TIME_ARGS (current_time));
2536 /* get elapsed time since we last reported */
2537 elapsed = current_time - sess->last_rtcp_send_time;
2539 /* perform forward reconsideration */
2540 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, data->interval);
2542 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
2543 GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time), GST_TIME_ARGS (elapsed));
2545 new_send_time += sess->last_rtcp_send_time;
2547 /* check if reconsideration */
2548 if (current_time < new_send_time) {
2549 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
2550 GST_TIME_ARGS (new_send_time));
2552 /* store new check time */
2553 sess->next_rtcp_check_time = new_send_time;
2556 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
2558 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
2559 GST_TIME_ARGS (new_send_time));
2560 sess->next_rtcp_check_time = current_time + new_send_time;
2566 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
2568 g_hash_table_insert (hash_table, key, g_object_ref (source));
2572 remove_closing_sources (const gchar * key, RTPSource * source, gpointer * data)
2574 return source->closing;
2578 * rtp_session_on_timeout:
2579 * @sess: an #RTPSession
2580 * @current_time: the current system time
2581 * @ntpnstime: the current NTP time in nanoseconds
2582 * @running_time: the current running_time of the pipeline
2584 * Perform maintenance actions after the timeout obtained with
2585 * rtp_session_next_timeout() expired.
2587 * This function will perform timeouts of receivers and senders, send a BYE
2588 * packet or generate RTCP packets with current session stats.
2590 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
2591 * times, for each packet that should be processed.
2593 * Returns: a #GstFlowReturn.
2596 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
2597 guint64 ntpnstime, GstClockTime running_time)
2599 GstFlowReturn result = GST_FLOW_OK;
2602 GHashTable *table_copy;
2603 gboolean notify = FALSE;
2605 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2607 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT,
2608 GST_TIME_ARGS (current_time), GST_TIME_ARGS (ntpnstime));
2612 data.current_time = current_time;
2613 data.ntpnstime = ntpnstime;
2614 data.is_bye = FALSE;
2615 data.has_sdes = FALSE;
2616 data.running_time = running_time;
2620 RTP_SESSION_LOCK (sess);
2621 /* get a new interval, we need this for various cleanups etc */
2622 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
2624 /* Make a local copy of the hashtable. We need to do this because the
2625 * cleanup stage below releases the session lock. */
2626 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
2627 (GDestroyNotify) g_object_unref);
2628 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2629 (GHFunc) clone_ssrcs_hashtable, table_copy);
2631 /* Clean up the session, mark the source for removing, this might release the
2633 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
2634 g_hash_table_destroy (table_copy);
2636 /* Now remove the marked sources */
2637 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
2638 (GHRFunc) remove_closing_sources, NULL);
2640 /* see if we need to generate SR or RR packets */
2641 if (is_rtcp_time (sess, current_time, &data)) {
2642 if (own->received_bye) {
2643 /* generate BYE instead */
2644 GST_DEBUG ("generating BYE message");
2645 session_bye (sess, &data);
2646 sess->sent_bye = TRUE;
2648 /* loop over all known sources and do something */
2649 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2650 (GHFunc) session_report_blocks, &data);
2655 /* we keep track of the last report time in order to timeout inactive
2656 * receivers or senders */
2657 sess->last_rtcp_send_time = data.current_time;
2658 sess->first_rtcp = FALSE;
2660 /* add SDES for this source when not already added */
2662 session_sdes (sess, &data);
2665 /* check for outdated collisions */
2666 GST_DEBUG ("Timing out collisions");
2667 rtp_source_timeout (sess->source, current_time,
2668 data.interval * RTCP_INTERVAL_COLLISION_TIMEOUT);
2670 if (sess->change_ssrc) {
2671 GST_DEBUG ("need to change our SSRC (%08x)", own->ssrc);
2672 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
2673 GINT_TO_POINTER (own->ssrc));
2675 own->ssrc = rtp_session_create_new_ssrc (sess);
2676 rtp_source_reset (own);
2678 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
2679 GINT_TO_POINTER (own->ssrc), own);
2681 g_free (sess->bye_reason);
2682 sess->bye_reason = NULL;
2683 sess->sent_bye = FALSE;
2684 sess->change_ssrc = FALSE;
2686 GST_DEBUG ("changed our SSRC to %08x", own->ssrc);
2688 RTP_SESSION_UNLOCK (sess);
2691 g_object_notify (G_OBJECT (sess), "internal-ssrc");
2693 /* push out the RTCP packet */
2695 /* close the RTCP packet */
2696 gst_rtcp_buffer_end (data.rtcp);
2698 if (sess->callbacks.send_rtcp) {
2701 packet_size = GST_BUFFER_SIZE (data.rtcp) + sess->header_len;
2703 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
2704 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
2705 sess->stats.avg_rtcp_packet_size, packet_size);
2707 sess->callbacks.send_rtcp (sess, own, data.rtcp, sess->sent_bye,
2708 sess->send_rtcp_user_data);
2710 GST_DEBUG ("freeing packet");
2711 gst_buffer_unref (data.rtcp);