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,
45 SIGNAL_ON_SENDING_RTCP,
49 #define DEFAULT_INTERNAL_SOURCE NULL
50 #define DEFAULT_BANDWIDTH RTP_STATS_BANDWIDTH
51 #define DEFAULT_RTCP_FRACTION (RTP_STATS_RTCP_FRACTION * RTP_STATS_BANDWIDTH)
52 #define DEFAULT_RTCP_RR_BANDWIDTH -1
53 #define DEFAULT_RTCP_RS_BANDWIDTH -1
54 #define DEFAULT_RTCP_MTU 1400
55 #define DEFAULT_SDES NULL
56 #define DEFAULT_NUM_SOURCES 0
57 #define DEFAULT_NUM_ACTIVE_SOURCES 0
58 #define DEFAULT_SOURCES NULL
59 #define DEFAULT_RTCP_MIN_INTERVAL (RTP_STATS_MIN_INTERVAL * GST_SECOND)
68 PROP_RTCP_RR_BANDWIDTH,
69 PROP_RTCP_RS_BANDWIDTH,
73 PROP_NUM_ACTIVE_SOURCES,
76 PROP_RTCP_MIN_INTERVAL,
80 /* update average packet size */
81 #define INIT_AVG(avg, val) \
83 #define UPDATE_AVG(avg, val) \
87 (avg) = ((val) + (15 * (avg))) >> 4;
90 /* The number RTCP intervals after which to timeout entries in the
93 #define RTCP_INTERVAL_COLLISION_TIMEOUT 10
95 /* GObject vmethods */
96 static void rtp_session_finalize (GObject * object);
97 static void rtp_session_set_property (GObject * object, guint prop_id,
98 const GValue * value, GParamSpec * pspec);
99 static void rtp_session_get_property (GObject * object, guint prop_id,
100 GValue * value, GParamSpec * pspec);
102 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
104 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
106 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
107 gboolean * created, RTPArrivalStats * arrival, gboolean rtp);
108 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
109 const gchar * reason, GstClockTime current_time);
110 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
111 gboolean deterministic, gboolean first);
114 accumulate_trues (GSignalInvocationHint * ihint, GValue * return_accu,
115 const GValue * handler_return, gpointer data)
117 if (g_value_get_boolean (handler_return))
118 g_value_set_boolean (return_accu, TRUE);
124 rtp_session_class_init (RTPSessionClass * klass)
126 GObjectClass *gobject_class;
128 gobject_class = (GObjectClass *) klass;
130 gobject_class->finalize = rtp_session_finalize;
131 gobject_class->set_property = rtp_session_set_property;
132 gobject_class->get_property = rtp_session_get_property;
135 * RTPSession::get-source-by-ssrc:
136 * @session: the object which received the signal
137 * @ssrc: the SSRC of the RTPSource
139 * Request the #RTPSource object with SSRC @ssrc in @session.
141 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
142 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
143 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
144 get_source_by_ssrc), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
145 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
148 * RTPSession::on-new-ssrc:
149 * @session: the object which received the signal
150 * @src: the new RTPSource
152 * Notify of a new SSRC that entered @session.
154 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
155 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
156 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
157 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
160 * RTPSession::on-ssrc-collision:
161 * @session: the object which received the signal
162 * @src: the #RTPSource that caused a collision
164 * Notify when we have an SSRC collision
166 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
167 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
168 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
169 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
172 * RTPSession::on-ssrc-validated:
173 * @session: the object which received the signal
174 * @src: the new validated RTPSource
176 * Notify of a new SSRC that became validated.
178 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
179 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
180 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
181 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
184 * RTPSession::on-ssrc-active:
185 * @session: the object which received the signal
186 * @src: the active RTPSource
188 * Notify of a SSRC that is active, i.e., sending RTCP.
190 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
191 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
192 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
193 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
196 * RTPSession::on-ssrc-sdes:
197 * @session: the object which received the signal
198 * @src: the RTPSource
200 * Notify that a new SDES was received for SSRC.
202 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
203 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
204 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
205 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
208 * RTPSession::on-bye-ssrc:
209 * @session: the object which received the signal
210 * @src: the RTPSource that went away
212 * Notify of an SSRC that became inactive because of a BYE packet.
214 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
215 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
216 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
217 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
220 * RTPSession::on-bye-timeout:
221 * @session: the object which received the signal
222 * @src: the RTPSource that timed out
224 * Notify of an SSRC that has timed out because of BYE
226 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
227 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
228 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
229 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
232 * RTPSession::on-timeout:
233 * @session: the object which received the signal
234 * @src: the RTPSource that timed out
236 * Notify of an SSRC that has timed out
238 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
239 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
240 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
241 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
244 * RTPSession::on-sender-timeout:
245 * @session: the object which received the signal
246 * @src: the RTPSource that timed out
248 * Notify of an SSRC that was a sender but timed out and became a receiver.
250 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
251 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
252 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
253 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
257 * RTPSession::on-sending-rtcp
258 * @session: the object which received the signal
259 * @buffer: the #GstBuffer containing the RTCP packet about to be sent
260 * @early: %TRUE if the packet is early, %FALSE if it is regular
262 * This signal is emitted before sending an RTCP packet, it can be used
263 * to add extra RTCP Packets.
265 * Returns: %TRUE if the RTCP buffer should NOT be suppressed, %FALSE
266 * if suppressing it is acceptable
268 rtp_session_signals[SIGNAL_ON_SENDING_RTCP] =
269 g_signal_new ("on-sending-rtcp", G_TYPE_FROM_CLASS (klass),
270 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_rtcp),
271 accumulate_trues, NULL, gst_rtp_bin_marshal_BOOLEAN__POINTER_BOOLEAN,
272 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
274 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
275 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
276 "The internal SSRC used for the session",
277 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
279 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
280 g_param_spec_object ("internal-source", "Internal Source",
281 "The internal source element of the session",
282 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
284 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
285 g_param_spec_double ("bandwidth", "Bandwidth",
286 "The bandwidth of the session (0 for auto-discover)",
287 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
288 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
290 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
291 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
292 "The fraction of the bandwidth used for RTCP (or as a real fraction of the RTP bandwidth if < 1)",
293 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
294 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
296 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
297 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
298 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
299 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
300 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
302 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
303 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
304 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
305 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
306 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
308 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
309 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
310 "The maximum size of the RTCP packets",
311 16, G_MAXINT16, DEFAULT_RTCP_MTU,
312 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
314 g_object_class_install_property (gobject_class, PROP_SDES,
315 g_param_spec_boxed ("sdes", "SDES",
316 "The SDES items of this session",
317 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
319 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
320 g_param_spec_uint ("num-sources", "Num Sources",
321 "The number of sources in the session", 0, G_MAXUINT,
322 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
324 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
325 g_param_spec_uint ("num-active-sources", "Num Active Sources",
326 "The number of active sources in the session", 0, G_MAXUINT,
327 DEFAULT_NUM_ACTIVE_SOURCES,
328 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
332 * Get a GValue Array of all sources in the session.
335 * <title>Getting the #RTPSources of a session
342 * g_object_get (sess, "sources", &arr, NULL);
344 * for (i = 0; i < arr->n_values; i++) {
347 * val = g_value_array_get_nth (arr, i);
348 * source = g_value_get_object (val);
350 * g_value_array_free (arr);
355 g_object_class_install_property (gobject_class, PROP_SOURCES,
356 g_param_spec_boxed ("sources", "Sources",
357 "An array of all known sources in the session",
358 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
360 g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
361 g_param_spec_boolean ("favor-new", "Favor new sources",
362 "Resolve SSRC conflict in favor of new sources", FALSE,
363 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
365 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
366 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
367 "Minimum interval between Regular RTCP packet (in ns)",
368 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
369 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371 klass->get_source_by_ssrc =
372 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
374 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
378 rtp_session_init (RTPSession * sess)
383 sess->lock = g_mutex_new ();
384 sess->key = g_random_int ();
388 for (i = 0; i < 32; i++) {
390 g_hash_table_new_full (NULL, NULL, NULL,
391 (GDestroyNotify) g_object_unref);
393 sess->cnames = g_hash_table_new_full (NULL, NULL, g_free, NULL);
395 rtp_stats_init_defaults (&sess->stats);
397 sess->recalc_bandwidth = TRUE;
398 sess->bandwidth = DEFAULT_BANDWIDTH;
399 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
400 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
401 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
403 /* create an active SSRC for this session manager */
404 sess->source = rtp_session_create_source (sess);
405 sess->source->validated = TRUE;
406 sess->source->internal = TRUE;
407 sess->stats.active_sources++;
408 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
410 /* default UDP header length */
411 sess->header_len = 28;
412 sess->mtu = DEFAULT_RTCP_MTU;
414 /* some default SDES entries */
415 str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
416 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_CNAME, str);
419 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_NAME,
421 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_TOOL, "GStreamer");
423 sess->first_rtcp = TRUE;
424 sess->allow_early = TRUE;
426 GST_DEBUG ("%p: session using SSRC: %08x", sess, sess->source->ssrc);
430 rtp_session_finalize (GObject * object)
435 sess = RTP_SESSION_CAST (object);
437 g_mutex_free (sess->lock);
438 for (i = 0; i < 32; i++)
439 g_hash_table_destroy (sess->ssrcs[i]);
441 g_free (sess->bye_reason);
443 g_hash_table_destroy (sess->cnames);
444 g_object_unref (sess->source);
446 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
450 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
452 GValue value = { 0 };
454 g_value_init (&value, RTP_TYPE_SOURCE);
455 g_value_take_object (&value, source);
456 /* copies the value */
457 g_value_array_append (arr, &value);
461 rtp_session_create_sources (RTPSession * sess)
466 RTP_SESSION_LOCK (sess);
467 /* get number of elements in the table */
468 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
469 /* create the result value array */
470 res = g_value_array_new (size);
472 /* and copy all values into the array */
473 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
474 RTP_SESSION_UNLOCK (sess);
480 rtp_session_set_property (GObject * object, guint prop_id,
481 const GValue * value, GParamSpec * pspec)
485 sess = RTP_SESSION (object);
488 case PROP_INTERNAL_SSRC:
489 rtp_session_set_internal_ssrc (sess, g_value_get_uint (value));
492 sess->bandwidth = g_value_get_double (value);
493 sess->recalc_bandwidth = TRUE;
495 case PROP_RTCP_FRACTION:
496 sess->rtcp_bandwidth = g_value_get_double (value);
497 sess->recalc_bandwidth = TRUE;
499 case PROP_RTCP_RR_BANDWIDTH:
500 sess->rtcp_rr_bandwidth = g_value_get_int (value);
501 sess->recalc_bandwidth = TRUE;
503 case PROP_RTCP_RS_BANDWIDTH:
504 sess->rtcp_rs_bandwidth = g_value_get_int (value);
505 sess->recalc_bandwidth = TRUE;
508 sess->mtu = g_value_get_uint (value);
511 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
514 sess->favor_new = g_value_get_boolean (value);
516 case PROP_RTCP_MIN_INTERVAL:
517 rtp_stats_set_min_interval (&sess->stats,
518 (gdouble) g_value_get_uint64 (value) / GST_SECOND);
521 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
527 rtp_session_get_property (GObject * object, guint prop_id,
528 GValue * value, GParamSpec * pspec)
532 sess = RTP_SESSION (object);
535 case PROP_INTERNAL_SSRC:
536 g_value_set_uint (value, rtp_session_get_internal_ssrc (sess));
538 case PROP_INTERNAL_SOURCE:
539 g_value_take_object (value, rtp_session_get_internal_source (sess));
542 g_value_set_double (value, sess->bandwidth);
544 case PROP_RTCP_FRACTION:
545 g_value_set_double (value, sess->rtcp_bandwidth);
547 case PROP_RTCP_RR_BANDWIDTH:
548 g_value_set_int (value, sess->rtcp_rr_bandwidth);
550 case PROP_RTCP_RS_BANDWIDTH:
551 g_value_set_int (value, sess->rtcp_rs_bandwidth);
554 g_value_set_uint (value, sess->mtu);
557 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
559 case PROP_NUM_SOURCES:
560 g_value_set_uint (value, rtp_session_get_num_sources (sess));
562 case PROP_NUM_ACTIVE_SOURCES:
563 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
566 g_value_take_boxed (value, rtp_session_create_sources (sess));
569 g_value_set_boolean (value, sess->favor_new);
571 case PROP_RTCP_MIN_INTERVAL:
572 g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
575 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
581 on_new_ssrc (RTPSession * sess, RTPSource * source)
583 g_object_ref (source);
584 RTP_SESSION_UNLOCK (sess);
585 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
586 RTP_SESSION_LOCK (sess);
587 g_object_unref (source);
591 on_ssrc_collision (RTPSession * sess, RTPSource * source)
593 g_object_ref (source);
594 RTP_SESSION_UNLOCK (sess);
595 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
597 RTP_SESSION_LOCK (sess);
598 g_object_unref (source);
602 on_ssrc_validated (RTPSession * sess, RTPSource * source)
604 g_object_ref (source);
605 RTP_SESSION_UNLOCK (sess);
606 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
608 RTP_SESSION_LOCK (sess);
609 g_object_unref (source);
613 on_ssrc_active (RTPSession * sess, RTPSource * source)
615 g_object_ref (source);
616 RTP_SESSION_UNLOCK (sess);
617 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
618 RTP_SESSION_LOCK (sess);
619 g_object_unref (source);
623 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
625 g_object_ref (source);
626 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
627 RTP_SESSION_UNLOCK (sess);
628 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
629 RTP_SESSION_LOCK (sess);
630 g_object_unref (source);
634 on_bye_ssrc (RTPSession * sess, RTPSource * source)
636 g_object_ref (source);
637 RTP_SESSION_UNLOCK (sess);
638 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
639 RTP_SESSION_LOCK (sess);
640 g_object_unref (source);
644 on_bye_timeout (RTPSession * sess, RTPSource * source)
646 g_object_ref (source);
647 RTP_SESSION_UNLOCK (sess);
648 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
649 RTP_SESSION_LOCK (sess);
650 g_object_unref (source);
654 on_timeout (RTPSession * sess, RTPSource * source)
656 g_object_ref (source);
657 RTP_SESSION_UNLOCK (sess);
658 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
659 RTP_SESSION_LOCK (sess);
660 g_object_unref (source);
664 on_sender_timeout (RTPSession * sess, RTPSource * source)
666 g_object_ref (source);
667 RTP_SESSION_UNLOCK (sess);
668 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
670 RTP_SESSION_LOCK (sess);
671 g_object_unref (source);
677 * Create a new session object.
679 * Returns: a new #RTPSession. g_object_unref() after usage.
682 rtp_session_new (void)
686 sess = g_object_new (RTP_TYPE_SESSION, NULL);
692 * rtp_session_set_callbacks:
693 * @sess: an #RTPSession
694 * @callbacks: callbacks to configure
695 * @user_data: user data passed in the callbacks
697 * Configure a set of callbacks to be notified of actions.
700 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
703 g_return_if_fail (RTP_IS_SESSION (sess));
705 if (callbacks->process_rtp) {
706 sess->callbacks.process_rtp = callbacks->process_rtp;
707 sess->process_rtp_user_data = user_data;
709 if (callbacks->send_rtp) {
710 sess->callbacks.send_rtp = callbacks->send_rtp;
711 sess->send_rtp_user_data = user_data;
713 if (callbacks->send_rtcp) {
714 sess->callbacks.send_rtcp = callbacks->send_rtcp;
715 sess->send_rtcp_user_data = user_data;
717 if (callbacks->sync_rtcp) {
718 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
719 sess->sync_rtcp_user_data = user_data;
721 if (callbacks->clock_rate) {
722 sess->callbacks.clock_rate = callbacks->clock_rate;
723 sess->clock_rate_user_data = user_data;
725 if (callbacks->reconsider) {
726 sess->callbacks.reconsider = callbacks->reconsider;
727 sess->reconsider_user_data = user_data;
732 * rtp_session_set_process_rtp_callback:
733 * @sess: an #RTPSession
734 * @callback: callback to set
735 * @user_data: user data passed in the callback
737 * Configure only the process_rtp callback to be notified of the process_rtp action.
740 rtp_session_set_process_rtp_callback (RTPSession * sess,
741 RTPSessionProcessRTP callback, gpointer user_data)
743 g_return_if_fail (RTP_IS_SESSION (sess));
745 sess->callbacks.process_rtp = callback;
746 sess->process_rtp_user_data = user_data;
750 * rtp_session_set_send_rtp_callback:
751 * @sess: an #RTPSession
752 * @callback: callback to set
753 * @user_data: user data passed in the callback
755 * Configure only the send_rtp callback to be notified of the send_rtp action.
758 rtp_session_set_send_rtp_callback (RTPSession * sess,
759 RTPSessionSendRTP callback, gpointer user_data)
761 g_return_if_fail (RTP_IS_SESSION (sess));
763 sess->callbacks.send_rtp = callback;
764 sess->send_rtp_user_data = user_data;
768 * rtp_session_set_send_rtcp_callback:
769 * @sess: an #RTPSession
770 * @callback: callback to set
771 * @user_data: user data passed in the callback
773 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
776 rtp_session_set_send_rtcp_callback (RTPSession * sess,
777 RTPSessionSendRTCP callback, gpointer user_data)
779 g_return_if_fail (RTP_IS_SESSION (sess));
781 sess->callbacks.send_rtcp = callback;
782 sess->send_rtcp_user_data = user_data;
786 * rtp_session_set_sync_rtcp_callback:
787 * @sess: an #RTPSession
788 * @callback: callback to set
789 * @user_data: user data passed in the callback
791 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
794 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
795 RTPSessionSyncRTCP callback, gpointer user_data)
797 g_return_if_fail (RTP_IS_SESSION (sess));
799 sess->callbacks.sync_rtcp = callback;
800 sess->sync_rtcp_user_data = user_data;
804 * rtp_session_set_clock_rate_callback:
805 * @sess: an #RTPSession
806 * @callback: callback to set
807 * @user_data: user data passed in the callback
809 * Configure only the clock_rate callback to be notified of the clock_rate action.
812 rtp_session_set_clock_rate_callback (RTPSession * sess,
813 RTPSessionClockRate callback, gpointer user_data)
815 g_return_if_fail (RTP_IS_SESSION (sess));
817 sess->callbacks.clock_rate = callback;
818 sess->clock_rate_user_data = user_data;
822 * rtp_session_set_reconsider_callback:
823 * @sess: an #RTPSession
824 * @callback: callback to set
825 * @user_data: user data passed in the callback
827 * Configure only the reconsider callback to be notified of the reconsider action.
830 rtp_session_set_reconsider_callback (RTPSession * sess,
831 RTPSessionReconsider callback, gpointer user_data)
833 g_return_if_fail (RTP_IS_SESSION (sess));
835 sess->callbacks.reconsider = callback;
836 sess->reconsider_user_data = user_data;
840 * rtp_session_set_bandwidth:
841 * @sess: an #RTPSession
842 * @bandwidth: the bandwidth allocated
844 * Set the session bandwidth in bytes per second.
847 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
849 g_return_if_fail (RTP_IS_SESSION (sess));
851 RTP_SESSION_LOCK (sess);
852 sess->stats.bandwidth = bandwidth;
853 RTP_SESSION_UNLOCK (sess);
857 * rtp_session_get_bandwidth:
858 * @sess: an #RTPSession
860 * Get the session bandwidth.
862 * Returns: the session bandwidth.
865 rtp_session_get_bandwidth (RTPSession * sess)
869 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
871 RTP_SESSION_LOCK (sess);
872 result = sess->stats.bandwidth;
873 RTP_SESSION_UNLOCK (sess);
879 * rtp_session_set_rtcp_fraction:
880 * @sess: an #RTPSession
881 * @bandwidth: the RTCP bandwidth
883 * Set the bandwidth in bytes per second that should be used for RTCP
887 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
889 g_return_if_fail (RTP_IS_SESSION (sess));
891 RTP_SESSION_LOCK (sess);
892 sess->stats.rtcp_bandwidth = bandwidth;
893 RTP_SESSION_UNLOCK (sess);
897 * rtp_session_get_rtcp_fraction:
898 * @sess: an #RTPSession
900 * Get the session bandwidth used for RTCP.
902 * Returns: The bandwidth used for RTCP messages.
905 rtp_session_get_rtcp_fraction (RTPSession * sess)
909 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
911 RTP_SESSION_LOCK (sess);
912 result = sess->stats.rtcp_bandwidth;
913 RTP_SESSION_UNLOCK (sess);
919 * rtp_session_set_sdes_string:
920 * @sess: an #RTPSession
921 * @type: the type of the SDES item
922 * @item: a null-terminated string to set.
924 * Store an SDES item of @type in @sess.
926 * Returns: %FALSE if the data was unchanged @type is invalid.
929 rtp_session_set_sdes_string (RTPSession * sess, GstRTCPSDESType type,
934 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
936 RTP_SESSION_LOCK (sess);
937 result = rtp_source_set_sdes_string (sess->source, type, item);
938 RTP_SESSION_UNLOCK (sess);
944 * rtp_session_get_sdes_string:
945 * @sess: an #RTPSession
946 * @type: the type of the SDES item
948 * Get the SDES item of @type from @sess.
950 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
951 * valid. g_free() after usage.
954 rtp_session_get_sdes_string (RTPSession * sess, GstRTCPSDESType type)
958 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
960 RTP_SESSION_LOCK (sess);
961 result = rtp_source_get_sdes_string (sess->source, type);
962 RTP_SESSION_UNLOCK (sess);
968 * rtp_session_get_sdes_struct:
969 * @sess: an #RTSPSession
971 * Get the SDES data as a #GstStructure
973 * Returns: a GstStructure with SDES items for @sess. This function returns a
974 * copy of the SDES structure, use gst_structure_free() after usage.
977 rtp_session_get_sdes_struct (RTPSession * sess)
979 const GstStructure *sdes;
980 GstStructure *result = NULL;
982 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
984 RTP_SESSION_LOCK (sess);
985 sdes = rtp_source_get_sdes_struct (sess->source);
987 result = gst_structure_copy (sdes);
988 RTP_SESSION_UNLOCK (sess);
994 * rtp_session_set_sdes_struct:
995 * @sess: an #RTSPSession
996 * @sdes: a #GstStructure
998 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1001 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
1003 g_return_if_fail (sdes);
1004 g_return_if_fail (RTP_IS_SESSION (sess));
1006 RTP_SESSION_LOCK (sess);
1007 rtp_source_set_sdes_struct (sess->source, gst_structure_copy (sdes));
1008 RTP_SESSION_UNLOCK (sess);
1011 static GstFlowReturn
1012 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1014 GstFlowReturn result = GST_FLOW_OK;
1016 if (source == session->source) {
1017 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1019 RTP_SESSION_UNLOCK (session);
1021 if (session->callbacks.send_rtp)
1023 session->callbacks.send_rtp (session, source, data,
1024 session->send_rtp_user_data);
1026 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1029 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1030 RTP_SESSION_UNLOCK (session);
1032 if (session->callbacks.process_rtp)
1034 session->callbacks.process_rtp (session, source,
1035 GST_BUFFER_CAST (data), session->process_rtp_user_data);
1037 gst_buffer_unref (GST_BUFFER_CAST (data));
1039 RTP_SESSION_LOCK (session);
1045 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1049 RTP_SESSION_UNLOCK (session);
1051 if (session->callbacks.clock_rate)
1053 session->callbacks.clock_rate (session, pt,
1054 session->clock_rate_user_data);
1058 RTP_SESSION_LOCK (session);
1060 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1065 static RTPSourceCallbacks callbacks = {
1066 (RTPSourcePushRTP) source_push_rtp,
1067 (RTPSourceClockRate) source_clock_rate,
1071 check_collision (RTPSession * sess, RTPSource * source,
1072 RTPArrivalStats * arrival, gboolean rtp)
1074 /* If we have no arrival address, we can't do collision checking */
1075 if (!arrival->have_address)
1078 if (sess->source != source) {
1079 GstNetAddress *from;
1082 /* This is not our local source, but lets check if two remote
1087 from = &source->rtp_from;
1088 have_from = source->have_rtp_from;
1090 from = &source->rtcp_from;
1091 have_from = source->have_rtcp_from;
1095 if (gst_netaddress_equal (from, &arrival->address)) {
1096 /* Address is the same */
1099 GST_LOG ("we have a third-party collision or loop ssrc:%x",
1100 rtp_source_get_ssrc (source));
1101 if (sess->favor_new) {
1102 if (rtp_source_find_conflicting_address (source,
1103 &arrival->address, arrival->current_time)) {
1105 gst_netaddress_to_string (&arrival->address, buf1, 40);
1106 GST_LOG ("Known conflict on %x for %s, dropping packet",
1107 rtp_source_get_ssrc (source), buf1);
1110 gchar buf1[40], buf2[40];
1112 /* Current address is not a known conflict, lets assume this is
1113 * a new source. Save old address in possible conflict list
1115 rtp_source_add_conflicting_address (source, from,
1116 arrival->current_time);
1118 gst_netaddress_to_string (from, buf1, 40);
1119 gst_netaddress_to_string (&arrival->address, buf2, 40);
1120 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1121 " saving old as known conflict",
1122 rtp_source_get_ssrc (source), buf1, buf2);
1125 rtp_source_set_rtp_from (source, &arrival->address);
1127 rtp_source_set_rtcp_from (source, &arrival->address);
1131 /* Don't need to save old addresses, we ignore new sources */
1136 /* We don't already have a from address for RTP, just set it */
1138 rtp_source_set_rtp_from (source, &arrival->address);
1140 rtp_source_set_rtcp_from (source, &arrival->address);
1144 /* FIXME: Log 3rd party collision somehow
1145 * Maybe should be done in upper layer, only the SDES can tell us
1146 * if its a collision or a loop
1149 /* If the source has been inactive for some time, we assume that it has
1150 * simply changed its transport source address. Hence, there is no true
1151 * third-party collision - only a simulated one. */
1152 if (arrival->current_time > source->last_activity) {
1153 GstClockTime inactivity_period =
1154 arrival->current_time - source->last_activity;
1155 if (inactivity_period > 1 * GST_SECOND) {
1156 /* Use new network address */
1158 g_assert (source->have_rtp_from);
1159 rtp_source_set_rtp_from (source, &arrival->address);
1161 g_assert (source->have_rtcp_from);
1162 rtp_source_set_rtcp_from (source, &arrival->address);
1168 /* This is sending with our ssrc, is it an address we already know */
1170 if (rtp_source_find_conflicting_address (source, &arrival->address,
1171 arrival->current_time)) {
1172 /* Its a known conflict, its probably a loop, not a collision
1173 * lets just drop the incoming packet
1175 GST_DEBUG ("Our packets are being looped back to us, dropping");
1177 /* Its a new collision, lets change our SSRC */
1179 rtp_source_add_conflicting_address (source, &arrival->address,
1180 arrival->current_time);
1182 GST_DEBUG ("Collision for SSRC %x", rtp_source_get_ssrc (source));
1183 on_ssrc_collision (sess, source);
1185 rtp_session_schedule_bye_locked (sess, "SSRC Collision",
1186 arrival->current_time);
1188 sess->change_ssrc = TRUE;
1196 /* must be called with the session lock, the returned source needs to be
1197 * unreffed after usage. */
1199 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1200 RTPArrivalStats * arrival, gboolean rtp)
1205 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1206 if (source == NULL) {
1207 /* make new Source in probation and insert */
1208 source = rtp_source_new (ssrc);
1210 /* for RTP packets we need to set the source in probation. Receiving RTCP
1211 * packets of an SSRC, on the other hand, is a strong indication that we
1212 * are dealing with a valid source. */
1214 source->probation = RTP_DEFAULT_PROBATION;
1216 source->probation = 0;
1218 /* store from address, if any */
1219 if (arrival->have_address) {
1221 rtp_source_set_rtp_from (source, &arrival->address);
1223 rtp_source_set_rtcp_from (source, &arrival->address);
1226 /* configure a callback on the source */
1227 rtp_source_set_callbacks (source, &callbacks, sess);
1229 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1232 /* we have one more source now */
1233 sess->total_sources++;
1237 /* check for collision, this updates the address when not previously set */
1238 if (check_collision (sess, source, arrival, rtp)) {
1242 /* update last activity */
1243 source->last_activity = arrival->current_time;
1245 source->last_rtp_activity = arrival->current_time;
1246 g_object_ref (source);
1252 * rtp_session_get_internal_source:
1253 * @sess: a #RTPSession
1255 * Get the internal #RTPSource of @sess.
1257 * Returns: The internal #RTPSource. g_object_unref() after usage.
1260 rtp_session_get_internal_source (RTPSession * sess)
1264 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1266 result = g_object_ref (sess->source);
1272 * rtp_session_set_internal_ssrc:
1273 * @sess: a #RTPSession
1276 * Set the SSRC of @sess to @ssrc.
1279 rtp_session_set_internal_ssrc (RTPSession * sess, guint32 ssrc)
1281 RTP_SESSION_LOCK (sess);
1282 if (ssrc != sess->source->ssrc) {
1283 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
1284 GINT_TO_POINTER (sess->source->ssrc));
1286 GST_DEBUG ("setting internal SSRC to %08x", ssrc);
1287 /* After this call, any receiver of the old SSRC either in RTP or RTCP
1288 * packets will timeout on the old SSRC, we could potentially schedule a
1289 * BYE RTCP for the old SSRC... */
1290 sess->source->ssrc = ssrc;
1291 rtp_source_reset (sess->source);
1293 /* rehash with the new SSRC */
1294 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1295 GINT_TO_POINTER (sess->source->ssrc), sess->source);
1297 RTP_SESSION_UNLOCK (sess);
1299 g_object_notify (G_OBJECT (sess), "internal-ssrc");
1303 * rtp_session_get_internal_ssrc:
1304 * @sess: a #RTPSession
1306 * Get the internal SSRC of @sess.
1308 * Returns: The SSRC of the session.
1311 rtp_session_get_internal_ssrc (RTPSession * sess)
1315 RTP_SESSION_LOCK (sess);
1316 ssrc = sess->source->ssrc;
1317 RTP_SESSION_UNLOCK (sess);
1323 * rtp_session_add_source:
1324 * @sess: a #RTPSession
1325 * @src: #RTPSource to add
1327 * Add @src to @session.
1329 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1330 * existed in the session.
1333 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1335 gboolean result = FALSE;
1338 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1339 g_return_val_if_fail (src != NULL, FALSE);
1341 RTP_SESSION_LOCK (sess);
1343 g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1344 GINT_TO_POINTER (src->ssrc));
1346 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1347 GINT_TO_POINTER (src->ssrc), src);
1348 /* we have one more source now */
1349 sess->total_sources++;
1352 RTP_SESSION_UNLOCK (sess);
1358 * rtp_session_get_num_sources:
1359 * @sess: an #RTPSession
1361 * Get the number of sources in @sess.
1363 * Returns: The number of sources in @sess.
1366 rtp_session_get_num_sources (RTPSession * sess)
1370 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1372 RTP_SESSION_LOCK (sess);
1373 result = sess->total_sources;
1374 RTP_SESSION_UNLOCK (sess);
1380 * rtp_session_get_num_active_sources:
1381 * @sess: an #RTPSession
1383 * Get the number of active sources in @sess. A source is considered active when
1384 * it has been validated and has not yet received a BYE RTCP message.
1386 * Returns: The number of active sources in @sess.
1389 rtp_session_get_num_active_sources (RTPSession * sess)
1393 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1395 RTP_SESSION_LOCK (sess);
1396 result = sess->stats.active_sources;
1397 RTP_SESSION_UNLOCK (sess);
1403 * rtp_session_get_source_by_ssrc:
1404 * @sess: an #RTPSession
1407 * Find the source with @ssrc in @sess.
1409 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1410 * g_object_unref() after usage.
1413 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1417 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1419 RTP_SESSION_LOCK (sess);
1421 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1423 g_object_ref (result);
1424 RTP_SESSION_UNLOCK (sess);
1430 * rtp_session_get_source_by_cname:
1431 * @sess: a #RTPSession
1434 * Find the source with @cname in @sess.
1436 * Returns: a #RTPSource with CNAME @cname or NULL if the source was not found.
1437 * g_object_unref() after usage.
1440 rtp_session_get_source_by_cname (RTPSession * sess, const gchar * cname)
1444 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1445 g_return_val_if_fail (cname != NULL, NULL);
1447 RTP_SESSION_LOCK (sess);
1448 result = g_hash_table_lookup (sess->cnames, cname);
1450 g_object_ref (result);
1451 RTP_SESSION_UNLOCK (sess);
1456 /* should be called with the SESSION lock */
1458 rtp_session_create_new_ssrc (RTPSession * sess)
1463 ssrc = g_random_int ();
1465 /* see if it exists in the session, we're done if it doesn't */
1466 if (g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1467 GINT_TO_POINTER (ssrc)) == NULL)
1475 * rtp_session_create_source:
1476 * @sess: an #RTPSession
1478 * Create an #RTPSource for use in @sess. This function will create a source
1479 * with an ssrc that is currently not used by any participants in the session.
1481 * Returns: an #RTPSource.
1484 rtp_session_create_source (RTPSession * sess)
1489 RTP_SESSION_LOCK (sess);
1490 ssrc = rtp_session_create_new_ssrc (sess);
1491 source = rtp_source_new (ssrc);
1492 rtp_source_set_callbacks (source, &callbacks, sess);
1493 /* we need an additional ref for the source in the hashtable */
1494 g_object_ref (source);
1495 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1497 /* we have one more source now */
1498 sess->total_sources++;
1499 RTP_SESSION_UNLOCK (sess);
1504 /* update the RTPArrivalStats structure with the current time and other bits
1505 * about the current buffer we are handling.
1506 * This function is typically called when a validated packet is received.
1507 * This function should be called with the SESSION_LOCK
1510 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
1511 gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
1512 GstClockTime running_time)
1514 /* get time of arrival */
1515 arrival->current_time = current_time;
1516 arrival->running_time = running_time;
1518 /* get packet size including header overhead */
1519 arrival->bytes = GST_BUFFER_SIZE (buffer) + sess->header_len;
1522 arrival->payload_len = gst_rtp_buffer_get_payload_len (buffer);
1524 arrival->payload_len = 0;
1527 /* for netbuffer we can store the IP address to check for collisions */
1528 arrival->have_address = GST_IS_NETBUFFER (buffer);
1529 if (arrival->have_address) {
1530 GstNetBuffer *netbuf = (GstNetBuffer *) buffer;
1532 memcpy (&arrival->address, &netbuf->from, sizeof (GstNetAddress));
1537 * rtp_session_process_rtp:
1538 * @sess: and #RTPSession
1539 * @buffer: an RTP buffer
1540 * @current_time: the current system time
1541 * @running_time: the running_time of @buffer
1543 * Process an RTP buffer in the session manager. This function takes ownership
1546 * Returns: a #GstFlowReturn.
1549 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1550 GstClockTime current_time, GstClockTime running_time)
1552 GstFlowReturn result;
1556 gboolean prevsender, prevactive;
1557 RTPArrivalStats arrival;
1562 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1563 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1565 if (!gst_rtp_buffer_validate (buffer))
1566 goto invalid_packet;
1568 RTP_SESSION_LOCK (sess);
1569 /* update arrival stats */
1570 update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1573 /* ignore more RTP packets when we left the session */
1574 if (sess->source->received_bye)
1577 /* get SSRC and look up in session database */
1578 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1579 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1583 prevsender = RTP_SOURCE_IS_SENDER (source);
1584 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1585 oldrate = source->bitrate;
1587 /* copy available csrc for later */
1588 count = gst_rtp_buffer_get_csrc_count (buffer);
1589 /* make sure to not overflow our array. An RTP buffer can maximally contain
1591 count = MIN (count, 16);
1593 for (i = 0; i < count; i++)
1594 csrcs[i] = gst_rtp_buffer_get_csrc (buffer, i);
1596 /* let source process the packet */
1597 result = rtp_source_process_rtp (source, buffer, &arrival);
1599 /* source became active */
1600 if (prevactive != RTP_SOURCE_IS_ACTIVE (source)) {
1601 sess->stats.active_sources++;
1602 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1603 sess->stats.active_sources);
1604 on_ssrc_validated (sess, source);
1606 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1607 sess->stats.sender_sources++;
1608 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1609 sess->stats.sender_sources);
1611 if (oldrate != source->bitrate)
1612 sess->recalc_bandwidth = TRUE;
1615 on_new_ssrc (sess, source);
1617 if (source->validated) {
1620 /* for validated sources, we add the CSRCs as well */
1621 for (i = 0; i < count; i++) {
1623 RTPSource *csrc_src;
1628 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1633 GST_DEBUG ("created new CSRC: %08x", csrc);
1634 rtp_source_set_as_csrc (csrc_src);
1635 if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1636 sess->stats.active_sources++;
1637 on_new_ssrc (sess, csrc_src);
1639 g_object_unref (csrc_src);
1642 g_object_unref (source);
1644 RTP_SESSION_UNLOCK (sess);
1651 gst_buffer_unref (buffer);
1652 GST_DEBUG ("invalid RTP packet received");
1657 gst_buffer_unref (buffer);
1658 RTP_SESSION_UNLOCK (sess);
1659 GST_DEBUG ("ignoring RTP packet because we are leaving");
1664 gst_buffer_unref (buffer);
1665 RTP_SESSION_UNLOCK (sess);
1666 GST_DEBUG ("ignoring packet because its collisioning");
1672 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1673 GstRTCPPacket * packet, RTPArrivalStats * arrival)
1677 count = gst_rtcp_packet_get_rb_count (packet);
1678 for (i = 0; i < count; i++) {
1679 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1680 guint8 fractionlost;
1683 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1684 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1686 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1688 if (ssrc == sess->source->ssrc) {
1689 /* only deal with report blocks for our session, we update the stats of
1690 * the sender of the RTCP message. We could also compare our stats against
1691 * the other sender to see if we are better or worse. */
1692 rtp_source_process_rb (source, arrival->current_time, fractionlost,
1693 packetslost, exthighestseq, jitter, lsr, dlsr);
1696 on_ssrc_active (sess, source);
1699 /* A Sender report contains statistics about how the sender is doing. This
1700 * includes timing informataion such as the relation between RTP and NTP
1701 * timestamps and the number of packets/bytes it sent to us.
1703 * In this report is also included a set of report blocks related to how this
1704 * sender is receiving data (in case we (or somebody else) is also sending stuff
1705 * to it). This info includes the packet loss, jitter and seqnum. It also
1706 * contains information to calculate the round trip time (LSR/DLSR).
1709 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1710 RTPArrivalStats * arrival, gboolean * do_sync)
1712 guint32 senderssrc, rtptime, packet_count, octet_count;
1715 gboolean created, prevsender;
1717 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1718 &packet_count, &octet_count);
1720 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1721 senderssrc, GST_TIME_ARGS (arrival->current_time));
1723 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1727 /* don't try to do lip-sync for sources that sent a BYE */
1728 if (rtp_source_received_bye (source))
1733 prevsender = RTP_SOURCE_IS_SENDER (source);
1735 /* first update the source */
1736 rtp_source_process_sr (source, arrival->current_time, ntptime, rtptime,
1737 packet_count, octet_count);
1739 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1740 sess->stats.sender_sources++;
1741 GST_DEBUG ("source: %08x became sender, %d sender sources", senderssrc,
1742 sess->stats.sender_sources);
1746 on_new_ssrc (sess, source);
1748 rtp_session_process_rb (sess, source, packet, arrival);
1749 g_object_unref (source);
1752 /* A receiver report contains statistics about how a receiver is doing. It
1753 * includes stuff like packet loss, jitter and the seqnum it received last. It
1754 * also contains info to calculate the round trip time.
1756 * We are only interested in how the sender of this report is doing wrt to us.
1759 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1760 RTPArrivalStats * arrival)
1766 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1768 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1770 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1775 on_new_ssrc (sess, source);
1777 rtp_session_process_rb (sess, source, packet, arrival);
1778 g_object_unref (source);
1781 /* Get SDES items and store them in the SSRC */
1783 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1784 RTPArrivalStats * arrival)
1787 gboolean more_items, more_entries;
1789 items = gst_rtcp_packet_sdes_get_item_count (packet);
1790 GST_DEBUG ("got SDES packet with %d items", items);
1792 more_items = gst_rtcp_packet_sdes_first_item (packet);
1794 while (more_items) {
1796 gboolean changed, created, validated;
1800 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1802 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1806 /* find src, no probation when dealing with RTCP */
1807 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1811 sdes = gst_structure_new ("application/x-rtp-source-sdes", NULL);
1813 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1815 while (more_entries) {
1816 GstRTCPSDESType type;
1822 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1824 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1827 if (type == GST_RTCP_SDES_PRIV) {
1828 name = g_strndup ((const gchar *) &data[1], data[0]);
1830 data += data[0] + 1;
1832 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
1835 value = g_strndup ((const gchar *) data, len);
1837 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
1842 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1846 /* takes ownership of sdes */
1847 changed = rtp_source_set_sdes_struct (source, sdes);
1849 validated = !RTP_SOURCE_IS_ACTIVE (source);
1850 source->validated = TRUE;
1853 on_new_ssrc (sess, source);
1855 on_ssrc_validated (sess, source);
1857 on_ssrc_sdes (sess, source);
1859 g_object_unref (source);
1861 more_items = gst_rtcp_packet_sdes_next_item (packet);
1866 /* BYE is sent when a client leaves the session
1869 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
1870 RTPArrivalStats * arrival)
1874 gboolean reconsider = FALSE;
1876 reason = gst_rtcp_packet_bye_get_reason (packet);
1877 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
1879 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
1880 for (i = 0; i < count; i++) {
1883 gboolean created, prevactive, prevsender;
1884 guint pmembers, members;
1886 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
1887 GST_DEBUG ("SSRC: %08x", ssrc);
1889 /* find src and mark bye, no probation when dealing with RTCP */
1890 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1894 /* store time for when we need to time out this source */
1895 source->bye_time = arrival->current_time;
1897 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1898 prevsender = RTP_SOURCE_IS_SENDER (source);
1900 /* let the source handle the rest */
1901 rtp_source_process_bye (source, reason);
1903 pmembers = sess->stats.active_sources;
1905 if (prevactive && !RTP_SOURCE_IS_ACTIVE (source)) {
1906 sess->stats.active_sources--;
1907 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
1908 sess->stats.active_sources);
1910 if (prevsender && !RTP_SOURCE_IS_SENDER (source)) {
1911 sess->stats.sender_sources--;
1912 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
1913 sess->stats.sender_sources);
1915 members = sess->stats.active_sources;
1917 if (!sess->source->received_bye && members < pmembers) {
1918 /* some members went away since the previous timeout estimate.
1919 * Perform reverse reconsideration but only when we are not scheduling a
1921 if (arrival->current_time < sess->next_rtcp_check_time) {
1922 GstClockTime time_remaining;
1924 time_remaining = sess->next_rtcp_check_time - arrival->current_time;
1925 sess->next_rtcp_check_time =
1926 gst_util_uint64_scale (time_remaining, members, pmembers);
1928 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
1929 GST_TIME_ARGS (sess->next_rtcp_check_time));
1931 sess->next_rtcp_check_time += arrival->current_time;
1933 /* mark pending reconsider. We only want to signal the reconsideration
1934 * once after we handled all the source in the bye packet */
1940 on_new_ssrc (sess, source);
1942 on_bye_ssrc (sess, source);
1944 g_object_unref (source);
1947 RTP_SESSION_UNLOCK (sess);
1948 /* notify app of reconsideration */
1949 if (sess->callbacks.reconsider)
1950 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
1951 RTP_SESSION_LOCK (sess);
1957 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
1958 RTPArrivalStats * arrival)
1960 GST_DEBUG ("received APP");
1964 * rtp_session_process_rtcp:
1965 * @sess: and #RTPSession
1966 * @buffer: an RTCP buffer
1967 * @current_time: the current system time
1969 * Process an RTCP buffer in the session manager. This function takes ownership
1972 * Returns: a #GstFlowReturn.
1975 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
1976 GstClockTime current_time)
1978 GstRTCPPacket packet;
1979 gboolean more, is_bye = FALSE, do_sync = FALSE;
1980 RTPArrivalStats arrival;
1981 GstFlowReturn result = GST_FLOW_OK;
1983 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1984 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1986 if (!gst_rtcp_buffer_validate (buffer))
1987 goto invalid_packet;
1989 GST_DEBUG ("received RTCP packet");
1991 RTP_SESSION_LOCK (sess);
1992 /* update arrival stats */
1993 update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1);
1998 /* make writable, we might want to change the buffer */
1999 buffer = gst_buffer_make_metadata_writable (buffer);
2001 /* start processing the compound packet */
2002 more = gst_rtcp_buffer_get_first_packet (buffer, &packet);
2006 type = gst_rtcp_packet_get_type (&packet);
2008 /* when we are leaving the session, we should ignore all non-BYE messages */
2009 if (sess->source->received_bye && type != GST_RTCP_TYPE_BYE) {
2010 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
2015 case GST_RTCP_TYPE_SR:
2016 rtp_session_process_sr (sess, &packet, &arrival, &do_sync);
2018 case GST_RTCP_TYPE_RR:
2019 rtp_session_process_rr (sess, &packet, &arrival);
2021 case GST_RTCP_TYPE_SDES:
2022 rtp_session_process_sdes (sess, &packet, &arrival);
2024 case GST_RTCP_TYPE_BYE:
2026 /* don't try to attempt lip-sync anymore for streams with a BYE */
2028 rtp_session_process_bye (sess, &packet, &arrival);
2030 case GST_RTCP_TYPE_APP:
2031 rtp_session_process_app (sess, &packet, &arrival);
2034 GST_WARNING ("got unknown RTCP packet");
2038 more = gst_rtcp_packet_move_to_next (&packet);
2041 /* if we are scheduling a BYE, we only want to count bye packets, else we
2042 * count everything */
2043 if (sess->source->received_bye) {
2045 sess->stats.bye_members++;
2046 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2049 /* keep track of average packet size */
2050 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2052 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
2053 sess->stats.avg_rtcp_packet_size, arrival.bytes);
2054 RTP_SESSION_UNLOCK (sess);
2056 /* notify caller of sr packets in the callback */
2057 if (do_sync && sess->callbacks.sync_rtcp)
2058 result = sess->callbacks.sync_rtcp (sess, sess->source, buffer,
2059 sess->sync_rtcp_user_data);
2061 gst_buffer_unref (buffer);
2068 GST_DEBUG ("invalid RTCP packet received");
2069 gst_buffer_unref (buffer);
2074 gst_buffer_unref (buffer);
2075 RTP_SESSION_UNLOCK (sess);
2076 GST_DEBUG ("ignoring RTP packet because we left");
2082 * rtp_session_send_rtp:
2083 * @sess: an #RTPSession
2084 * @data: pointer to either an RTP buffer or a list of RTP buffers
2085 * @is_list: TRUE when @data is a buffer list
2086 * @current_time: the current system time
2087 * @running_time: the running time of @data
2089 * Send the RTP buffer in the session manager. This function takes ownership of
2092 * Returns: a #GstFlowReturn.
2095 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
2096 GstClockTime current_time, GstClockTime running_time)
2098 GstFlowReturn result;
2100 gboolean prevsender;
2101 gboolean valid_packet;
2104 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2105 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
2108 valid_packet = gst_rtp_buffer_list_validate (GST_BUFFER_LIST_CAST (data));
2110 valid_packet = gst_rtp_buffer_validate (GST_BUFFER_CAST (data));
2114 goto invalid_packet;
2116 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
2118 RTP_SESSION_LOCK (sess);
2119 source = sess->source;
2121 /* update last activity */
2122 source->last_rtp_activity = current_time;
2124 prevsender = RTP_SOURCE_IS_SENDER (source);
2125 oldrate = source->bitrate;
2127 /* we use our own source to send */
2128 result = rtp_source_send_rtp (source, data, is_list, running_time);
2130 if (RTP_SOURCE_IS_SENDER (source) && !prevsender)
2131 sess->stats.sender_sources++;
2132 if (oldrate != source->bitrate)
2133 sess->recalc_bandwidth = TRUE;
2134 RTP_SESSION_UNLOCK (sess);
2141 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
2142 GST_DEBUG ("invalid RTP packet received");
2148 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
2150 *bandwidth += source->bitrate;
2154 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2157 GstClockTime result;
2159 /* recalculate bandwidth when it changed */
2160 if (sess->recalc_bandwidth) {
2163 if (sess->bandwidth > 0)
2164 bandwidth = sess->bandwidth;
2166 /* If it is <= 0, then try to estimate the actual bandwidth */
2167 bandwidth = sess->source->bitrate;
2169 g_hash_table_foreach (sess->cnames, (GHFunc) add_bitrates, &bandwidth);
2173 bandwidth = RTP_STATS_BANDWIDTH;
2175 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
2176 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
2178 sess->recalc_bandwidth = FALSE;
2181 if (sess->source->received_bye) {
2182 result = rtp_stats_calculate_bye_interval (&sess->stats);
2184 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2185 RTP_SOURCE_IS_SENDER (sess->source), first);
2188 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2189 GST_TIME_ARGS (result), first);
2191 if (!deterministic && result != GST_CLOCK_TIME_NONE)
2192 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2194 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2199 /* Stop the current @sess and schedule a BYE message for the other members.
2200 * One must have the session lock to call this function
2202 static GstFlowReturn
2203 rtp_session_schedule_bye_locked (RTPSession * sess, const gchar * reason,
2204 GstClockTime current_time)
2206 GstFlowReturn result = GST_FLOW_OK;
2208 GstClockTime interval;
2210 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2212 source = sess->source;
2214 /* ignore more BYEs */
2215 if (source->received_bye)
2218 /* we have BYE now */
2219 source->received_bye = TRUE;
2220 /* at least one member wants to send a BYE */
2221 g_free (sess->bye_reason);
2222 sess->bye_reason = g_strdup (reason);
2223 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
2224 sess->stats.bye_members = 1;
2225 sess->first_rtcp = TRUE;
2226 sess->sent_bye = FALSE;
2227 sess->allow_early = TRUE;
2229 /* reschedule transmission */
2230 sess->last_rtcp_send_time = current_time;
2231 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2232 sess->next_rtcp_check_time = current_time + interval;
2234 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2235 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2237 RTP_SESSION_UNLOCK (sess);
2238 /* notify app of reconsideration */
2239 if (sess->callbacks.reconsider)
2240 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2241 RTP_SESSION_LOCK (sess);
2248 * rtp_session_schedule_bye:
2249 * @sess: an #RTPSession
2250 * @reason: a reason or NULL
2251 * @current_time: the current system time
2253 * Stop the current @sess and schedule a BYE message for the other members.
2255 * Returns: a #GstFlowReturn.
2258 rtp_session_schedule_bye (RTPSession * sess, const gchar * reason,
2259 GstClockTime current_time)
2261 GstFlowReturn result = GST_FLOW_OK;
2263 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2265 RTP_SESSION_LOCK (sess);
2266 result = rtp_session_schedule_bye_locked (sess, reason, current_time);
2267 RTP_SESSION_UNLOCK (sess);
2273 * rtp_session_next_timeout:
2274 * @sess: an #RTPSession
2275 * @current_time: the current system time
2277 * Get the next time we should perform session maintenance tasks.
2279 * Returns: a time when rtp_session_on_timeout() should be called with the
2280 * current system time.
2283 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2285 GstClockTime result, interval = 0;
2287 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
2289 RTP_SESSION_LOCK (sess);
2291 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
2292 result = sess->next_early_rtcp_time;
2296 result = sess->next_rtcp_check_time;
2298 GST_DEBUG ("current time: %" GST_TIME_FORMAT ", next :%" GST_TIME_FORMAT,
2299 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2301 if (result < current_time) {
2302 GST_DEBUG ("take current time as base");
2303 /* our previous check time expired, start counting from the current time
2305 result = current_time;
2308 if (sess->source->received_bye) {
2309 if (sess->sent_bye) {
2310 GST_DEBUG ("we sent BYE already");
2311 interval = GST_CLOCK_TIME_NONE;
2312 } else if (sess->stats.active_sources >= 50) {
2313 GST_DEBUG ("reconsider BYE, more than 50 sources");
2314 /* reconsider BYE if members >= 50 */
2315 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2318 if (sess->first_rtcp) {
2319 GST_DEBUG ("first RTCP packet");
2320 /* we are called for the first time */
2321 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2322 } else if (sess->next_rtcp_check_time < current_time) {
2323 GST_DEBUG ("old check time expired, getting new timeout");
2324 /* get a new timeout when we need to */
2325 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
2329 if (interval != GST_CLOCK_TIME_NONE)
2332 result = GST_CLOCK_TIME_NONE;
2334 sess->next_rtcp_check_time = result;
2338 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2339 ", next time: %" GST_TIME_FORMAT,
2340 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2341 RTP_SESSION_UNLOCK (sess);
2350 GstClockTime current_time;
2352 GstClockTime running_time;
2353 GstClockTime interval;
2354 GstRTCPPacket packet;
2358 gboolean may_suppress;
2362 session_start_rtcp (RTPSession * sess, ReportData * data)
2364 GstRTCPPacket *packet = &data->packet;
2365 RTPSource *own = sess->source;
2367 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2369 if (RTP_SOURCE_IS_SENDER (own)) {
2372 guint32 packet_count, octet_count;
2374 /* we are a sender, create SR */
2375 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2376 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SR, packet);
2378 /* get latest stats */
2379 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
2380 &ntptime, &rtptime, &packet_count, &octet_count);
2382 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2383 packet_count, octet_count);
2385 /* fill in sender report info */
2386 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2387 ntptime, rtptime, packet_count, octet_count);
2389 /* we are only receiver, create RR */
2390 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2391 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_RR, packet);
2392 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2396 /* construct a Sender or Receiver Report */
2398 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2400 RTPSession *sess = data->sess;
2401 GstRTCPPacket *packet = &data->packet;
2403 /* create a new buffer if needed */
2404 if (data->rtcp == NULL) {
2405 session_start_rtcp (sess, data);
2406 } else if (data->is_early) {
2407 /* Put a single RR or SR in minimal compound packets */
2410 if (gst_rtcp_packet_get_rb_count (packet) < GST_RTCP_MAX_RB_COUNT) {
2411 /* only report about other sender sources */
2412 if (source != sess->source && RTP_SOURCE_IS_SENDER (source)) {
2413 guint8 fractionlost;
2415 guint32 exthighestseq, jitter;
2419 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2420 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2422 /* store last generated RR packet */
2423 source->last_rr.is_valid = TRUE;
2424 source->last_rr.fractionlost = fractionlost;
2425 source->last_rr.packetslost = packetslost;
2426 source->last_rr.exthighestseq = exthighestseq;
2427 source->last_rr.jitter = jitter;
2428 source->last_rr.lsr = lsr;
2429 source->last_rr.dlsr = dlsr;
2431 /* packet is not yet filled, add report block for this source. */
2432 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2433 exthighestseq, jitter, lsr, dlsr);
2438 /* perform cleanup of sources that timed out */
2440 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2442 gboolean remove = FALSE;
2443 gboolean byetimeout = FALSE;
2444 gboolean sendertimeout = FALSE;
2445 gboolean is_sender, is_active;
2446 RTPSession *sess = data->sess;
2447 GstClockTime interval;
2449 is_sender = RTP_SOURCE_IS_SENDER (source);
2450 is_active = RTP_SOURCE_IS_ACTIVE (source);
2452 /* check for our own source, we don't want to delete our own source. */
2453 if (!(source == sess->source)) {
2454 if (source->received_bye) {
2455 /* if we received a BYE from the source, remove the source after some
2457 if (data->current_time > source->bye_time &&
2458 data->current_time - source->bye_time > sess->stats.bye_timeout) {
2459 GST_DEBUG ("removing BYE source %08x", source->ssrc);
2464 /* sources that were inactive for more than 5 times the deterministic reporting
2465 * interval get timed out. the min timeout is 5 seconds. */
2466 if (data->current_time > source->last_activity) {
2467 interval = MAX (data->interval * 5, 5 * GST_SECOND);
2468 if (data->current_time - source->last_activity > interval) {
2469 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2470 source->ssrc, GST_TIME_ARGS (source->last_activity));
2476 /* senders that did not send for a long time become a receiver, this also
2477 * holds for our own source. */
2479 if (data->current_time > source->last_rtp_activity) {
2480 interval = MAX (data->interval * 2, 5 * GST_SECOND);
2481 if (data->current_time - source->last_rtp_activity > interval) {
2482 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
2483 GST_TIME_FORMAT, source->ssrc,
2484 GST_TIME_ARGS (source->last_rtp_activity));
2485 source->is_sender = FALSE;
2486 sess->stats.sender_sources--;
2487 sendertimeout = TRUE;
2493 sess->total_sources--;
2495 sess->stats.sender_sources--;
2497 sess->stats.active_sources--;
2500 on_bye_timeout (sess, source);
2502 on_timeout (sess, source);
2505 on_sender_timeout (sess, source);
2508 source->closing = remove;
2512 session_sdes (RTPSession * sess, ReportData * data)
2514 GstRTCPPacket *packet = &data->packet;
2515 const GstStructure *sdes;
2518 /* add SDES packet */
2519 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SDES, packet);
2521 gst_rtcp_packet_sdes_add_item (packet, sess->source->ssrc);
2523 sdes = rtp_source_get_sdes_struct (sess->source);
2525 /* add all fields in the structure, the order is not important. */
2526 n_fields = gst_structure_n_fields (sdes);
2527 for (i = 0; i < n_fields; ++i) {
2530 GstRTCPSDESType type;
2532 field = gst_structure_nth_field_name (sdes, i);
2535 value = gst_structure_get_string (sdes, field);
2538 type = gst_rtcp_sdes_name_to_type (field);
2540 /* Early packets are minimal and only include the CNAME */
2541 if (data->is_early && type != GST_RTCP_SDES_CNAME)
2544 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
2545 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
2546 (const guint8 *) value);
2547 } else if (type == GST_RTCP_SDES_PRIV) {
2553 /* don't accept entries that are too big */
2554 prefix_len = strlen (field);
2555 if (prefix_len > 255)
2557 value_len = strlen (value);
2558 if (value_len > 255)
2560 data_len = 1 + prefix_len + value_len;
2564 data[0] = prefix_len;
2565 memcpy (&data[1], field, prefix_len);
2566 memcpy (&data[1 + prefix_len], value, value_len);
2568 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
2572 data->has_sdes = TRUE;
2575 /* schedule a BYE packet */
2577 session_bye (RTPSession * sess, ReportData * data)
2579 GstRTCPPacket *packet = &data->packet;
2582 session_start_rtcp (sess, data);
2585 session_sdes (sess, data);
2587 /* add a BYE packet */
2588 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_BYE, packet);
2589 gst_rtcp_packet_bye_add_ssrc (packet, sess->source->ssrc);
2590 if (sess->bye_reason)
2591 gst_rtcp_packet_bye_set_reason (packet, sess->bye_reason);
2593 /* we have a BYE packet now */
2594 data->is_bye = TRUE;
2598 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
2600 GstClockTime new_send_time, elapsed;
2602 if (data->is_early && sess->next_early_rtcp_time < current_time)
2605 /* no need to check yet */
2606 if (sess->next_rtcp_check_time > current_time) {
2607 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
2608 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
2609 GST_TIME_ARGS (current_time));
2613 /* get elapsed time since we last reported */
2614 elapsed = current_time - sess->last_rtcp_send_time;
2616 /* perform forward reconsideration */
2617 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, data->interval);
2619 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
2620 GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time), GST_TIME_ARGS (elapsed));
2622 new_send_time += sess->last_rtcp_send_time;
2624 /* check if reconsideration */
2625 if (current_time < new_send_time) {
2626 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
2627 GST_TIME_ARGS (new_send_time));
2628 /* store new check time */
2629 sess->next_rtcp_check_time = new_send_time;
2635 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
2637 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
2638 GST_TIME_ARGS (new_send_time));
2639 sess->next_rtcp_check_time = current_time + new_send_time;
2641 /* Apply the rules from RFC 4585 section 3.5.3 */
2642 if (sess->stats.min_interval != 0 && !sess->first_rtcp) {
2643 GstClockTimeDiff T_rr_current_interval = g_random_double_range (0.5, 1.5) *
2644 sess->stats.min_interval;
2646 /* This will caused the RTCP to be suppressed if no FB packets are added */
2647 if (sess->last_rtcp_send_time + T_rr_current_interval >
2648 sess->next_rtcp_check_time) {
2649 GST_DEBUG ("RTCP packet could be suppressed min: %" GST_TIME_FORMAT
2650 " last: %" GST_TIME_FORMAT
2651 " + T_rr_current_interval: %" GST_TIME_FORMAT
2652 " > sess->next_rtcp_check_time: %" GST_TIME_FORMAT,
2653 GST_TIME_ARGS (sess->stats.min_interval),
2654 GST_TIME_ARGS (sess->last_rtcp_send_time),
2655 GST_TIME_ARGS (T_rr_current_interval),
2656 GST_TIME_ARGS (sess->next_rtcp_check_time));
2657 data->may_suppress = TRUE;
2665 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
2667 g_hash_table_insert (hash_table, key, g_object_ref (source));
2671 remove_closing_sources (const gchar * key, RTPSource * source, gpointer * data)
2673 return source->closing;
2677 * rtp_session_on_timeout:
2678 * @sess: an #RTPSession
2679 * @current_time: the current system time
2680 * @ntpnstime: the current NTP time in nanoseconds
2681 * @running_time: the current running_time of the pipeline
2683 * Perform maintenance actions after the timeout obtained with
2684 * rtp_session_next_timeout() expired.
2686 * This function will perform timeouts of receivers and senders, send a BYE
2687 * packet or generate RTCP packets with current session stats.
2689 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
2690 * times, for each packet that should be processed.
2692 * Returns: a #GstFlowReturn.
2695 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
2696 guint64 ntpnstime, GstClockTime running_time)
2698 GstFlowReturn result = GST_FLOW_OK;
2701 GHashTable *table_copy;
2702 gboolean notify = FALSE;
2704 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2706 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT,
2707 GST_TIME_ARGS (current_time), GST_TIME_ARGS (ntpnstime));
2711 data.current_time = current_time;
2712 data.ntpnstime = ntpnstime;
2713 data.is_bye = FALSE;
2714 data.has_sdes = FALSE;
2715 data.may_suppress = FALSE;
2716 data.running_time = running_time;
2720 RTP_SESSION_LOCK (sess);
2721 /* get a new interval, we need this for various cleanups etc */
2722 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
2724 /* Make a local copy of the hashtable. We need to do this because the
2725 * cleanup stage below releases the session lock. */
2726 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
2727 (GDestroyNotify) g_object_unref);
2728 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2729 (GHFunc) clone_ssrcs_hashtable, table_copy);
2731 /* Clean up the session, mark the source for removing, this might release the
2733 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
2734 g_hash_table_destroy (table_copy);
2736 /* Now remove the marked sources */
2737 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
2738 (GHRFunc) remove_closing_sources, NULL);
2740 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
2741 data.is_early = TRUE;
2743 data.is_early = FALSE;
2745 /* see if we need to generate SR or RR packets */
2746 if (is_rtcp_time (sess, current_time, &data)) {
2747 if (own->received_bye) {
2748 /* generate BYE instead */
2749 GST_DEBUG ("generating BYE message");
2750 session_bye (sess, &data);
2751 sess->sent_bye = TRUE;
2753 /* loop over all known sources and do something */
2754 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2755 (GHFunc) session_report_blocks, &data);
2760 /* we keep track of the last report time in order to timeout inactive
2761 * receivers or senders */
2762 if (!data.is_early && !data.may_suppress)
2763 sess->last_rtcp_send_time = data.current_time;
2764 sess->first_rtcp = FALSE;
2765 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
2767 /* add SDES for this source when not already added */
2769 session_sdes (sess, &data);
2772 /* check for outdated collisions */
2773 GST_DEBUG ("Timing out collisions");
2774 rtp_source_timeout (sess->source, current_time,
2775 data.interval * RTCP_INTERVAL_COLLISION_TIMEOUT);
2777 if (sess->change_ssrc) {
2778 GST_DEBUG ("need to change our SSRC (%08x)", own->ssrc);
2779 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
2780 GINT_TO_POINTER (own->ssrc));
2782 own->ssrc = rtp_session_create_new_ssrc (sess);
2783 rtp_source_reset (own);
2785 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
2786 GINT_TO_POINTER (own->ssrc), own);
2788 g_free (sess->bye_reason);
2789 sess->bye_reason = NULL;
2790 sess->sent_bye = FALSE;
2791 sess->change_ssrc = FALSE;
2793 GST_DEBUG ("changed our SSRC to %08x", own->ssrc);
2796 sess->allow_early = TRUE;
2798 RTP_SESSION_UNLOCK (sess);
2801 g_object_notify (G_OBJECT (sess), "internal-ssrc");
2803 /* push out the RTCP packet */
2805 gboolean do_not_suppress;
2807 /* Give the user a change to add its own packet */
2808 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
2809 data.rtcp, data.is_early, &do_not_suppress);
2811 if (sess->callbacks.send_rtcp && (do_not_suppress || !data.may_suppress)) {
2812 /* close the RTCP packet */
2813 gst_rtcp_buffer_end (data.rtcp);
2815 if (sess->callbacks.send_rtcp) {
2818 packet_size = GST_BUFFER_SIZE (data.rtcp) + sess->header_len;
2820 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
2821 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
2822 sess->stats.avg_rtcp_packet_size, packet_size);
2824 sess->callbacks.send_rtcp (sess, own, data.rtcp, sess->sent_bye,
2825 sess->send_rtcp_user_data);
2827 GST_DEBUG ("freeing packet callback: %p"
2828 " do_not_suppress: %d may_suppress: %d",
2829 sess->callbacks.send_rtcp, do_not_suppress, data.may_suppress);
2830 gst_buffer_unref (data.rtcp);
2838 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
2839 GstClockTimeDiff max_delay)
2841 GstClockTime T_dither_max;
2843 /* Implements the algorithm described in RFC 4585 section 3.5.2 */
2845 RTP_SESSION_LOCK (sess);
2847 /* Check if already requested */
2848 /* RFC 4585 section 3.5.2 step 2 */
2849 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
2852 /* Ignore the request a scheduled packet will be in time anyway */
2853 if (current_time + max_delay > sess->next_rtcp_check_time)
2856 /* RFC 4585 section 3.5.2 step 2b */
2857 /* If the total sources is <=2, then there is only us and one peer */
2858 if (sess->total_sources <= 2) {
2861 /* Divide by 2 because l = 0.5 */
2862 T_dither_max = sess->next_rtcp_check_time - sess->last_rtcp_send_time;
2866 /* RFC 4585 section 3.5.2 step 3 */
2867 if (current_time + T_dither_max > sess->next_rtcp_check_time)
2870 /* RFC 4585 section 3.5.2 step 4 */
2871 if (sess->allow_early == FALSE)
2875 /* Schedule an early transmission later */
2876 sess->next_early_rtcp_time = g_random_double () * T_dither_max +
2879 /* If no dithering, schedule it for NOW */
2880 sess->next_early_rtcp_time = current_time;
2883 RTP_SESSION_UNLOCK (sess);
2885 /* notify app of need to send packet early
2886 * and therefore of timeout change */
2887 if (sess->callbacks.reconsider)
2888 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2894 RTP_SESSION_UNLOCK (sess);