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, we keep this scaled by 16 to keep enough
79 #define UPDATE_AVG(avg, val) \
83 (avg) = ((val) + (15 * (avg)));
85 /* The number RTCP intervals after which to timeout entries in the
88 #define RTCP_INTERVAL_COLLISION_TIMEOUT 10
90 /* GObject vmethods */
91 static void rtp_session_finalize (GObject * object);
92 static void rtp_session_set_property (GObject * object, guint prop_id,
93 const GValue * value, GParamSpec * pspec);
94 static void rtp_session_get_property (GObject * object, guint prop_id,
95 GValue * value, GParamSpec * pspec);
97 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
99 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
101 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
102 gboolean * created, RTPArrivalStats * arrival, gboolean rtp);
103 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
104 const gchar * reason, GstClockTime current_time);
105 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
106 gboolean deterministic, gboolean first);
109 rtp_session_class_init (RTPSessionClass * klass)
111 GObjectClass *gobject_class;
113 gobject_class = (GObjectClass *) klass;
115 gobject_class->finalize = rtp_session_finalize;
116 gobject_class->set_property = rtp_session_set_property;
117 gobject_class->get_property = rtp_session_get_property;
120 * RTPSession::get-source-by-ssrc:
121 * @session: the object which received the signal
122 * @ssrc: the SSRC of the RTPSource
124 * Request the #RTPSource object with SSRC @ssrc in @session.
126 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
127 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
128 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
129 get_source_by_ssrc), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
130 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
133 * RTPSession::on-new-ssrc:
134 * @session: the object which received the signal
135 * @src: the new RTPSource
137 * Notify of a new SSRC that entered @session.
139 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
140 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
141 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
142 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
145 * RTPSession::on-ssrc-collision:
146 * @session: the object which received the signal
147 * @src: the #RTPSource that caused a collision
149 * Notify when we have an SSRC collision
151 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
152 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
153 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
154 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
157 * RTPSession::on-ssrc-validated:
158 * @session: the object which received the signal
159 * @src: the new validated RTPSource
161 * Notify of a new SSRC that became validated.
163 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
164 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
165 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
166 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
169 * RTPSession::on-ssrc-active:
170 * @session: the object which received the signal
171 * @src: the active RTPSource
173 * Notify of a SSRC that is active, i.e., sending RTCP.
175 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
176 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
177 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
178 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
181 * RTPSession::on-ssrc-sdes:
182 * @session: the object which received the signal
183 * @src: the RTPSource
185 * Notify that a new SDES was received for SSRC.
187 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
188 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
189 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
190 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
193 * RTPSession::on-bye-ssrc:
194 * @session: the object which received the signal
195 * @src: the RTPSource that went away
197 * Notify of an SSRC that became inactive because of a BYE packet.
199 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
200 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
201 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
202 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
205 * RTPSession::on-bye-timeout:
206 * @session: the object which received the signal
207 * @src: the RTPSource that timed out
209 * Notify of an SSRC that has timed out because of BYE
211 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
212 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
213 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
214 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
217 * RTPSession::on-timeout:
218 * @session: the object which received the signal
219 * @src: the RTPSource that timed out
221 * Notify of an SSRC that has timed out
223 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
224 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
225 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
226 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
229 * RTPSession::on-sender-timeout:
230 * @session: the object which received the signal
231 * @src: the RTPSource that timed out
233 * Notify of an SSRC that was a sender but timed out and became a receiver.
235 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
236 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
237 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
238 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
241 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
242 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
243 "The internal SSRC used for the session",
244 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
246 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
247 g_param_spec_object ("internal-source", "Internal Source",
248 "The internal source element of the session",
249 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
251 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
252 g_param_spec_double ("bandwidth", "Bandwidth",
253 "The bandwidth of the session (0 for auto-discover)",
254 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
255 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
257 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
258 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
259 "The fraction of the bandwidth used for RTCP (or as a real fraction of the RTP bandwidth if < 1)",
260 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
261 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
263 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
264 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
265 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
266 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH, G_PARAM_READWRITE));
268 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
269 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
270 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
271 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH, G_PARAM_READWRITE));
273 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
274 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
275 "The maximum size of the RTCP packets",
276 16, G_MAXINT16, DEFAULT_RTCP_MTU,
277 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
279 g_object_class_install_property (gobject_class, PROP_SDES,
280 g_param_spec_boxed ("sdes", "SDES",
281 "The SDES items of this session",
282 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
284 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
285 g_param_spec_uint ("num-sources", "Num Sources",
286 "The number of sources in the session", 0, G_MAXUINT,
287 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
289 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
290 g_param_spec_uint ("num-active-sources", "Num Active Sources",
291 "The number of active sources in the session", 0, G_MAXUINT,
292 DEFAULT_NUM_ACTIVE_SOURCES,
293 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
297 * Get a GValue Array of all sources in the session.
300 * <title>Getting the #RTPSources of a session
307 * g_object_get (sess, "sources", &arr, NULL);
309 * for (i = 0; i < arr->n_values; i++) {
312 * val = g_value_array_get_nth (arr, i);
313 * source = g_value_get_object (val);
315 * g_value_array_free (arr);
320 g_object_class_install_property (gobject_class, PROP_SOURCES,
321 g_param_spec_boxed ("sources", "Sources",
322 "An array of all known sources in the session",
323 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
325 g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
326 g_param_spec_boolean ("favor-new", "Favor new sources",
327 "Resolve SSRC conflict in favor of new sources", FALSE,
328 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
331 klass->get_source_by_ssrc =
332 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
334 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
338 rtp_session_init (RTPSession * sess)
343 sess->lock = g_mutex_new ();
344 sess->key = g_random_int ();
348 for (i = 0; i < 32; i++) {
350 g_hash_table_new_full (NULL, NULL, NULL,
351 (GDestroyNotify) g_object_unref);
353 sess->cnames = g_hash_table_new_full (NULL, NULL, g_free, NULL);
355 rtp_stats_init_defaults (&sess->stats);
357 sess->recalc_bandwidth = TRUE;
358 sess->bandwidth = DEFAULT_BANDWIDTH;
359 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
360 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
361 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
363 /* create an active SSRC for this session manager */
364 sess->source = rtp_session_create_source (sess);
365 sess->source->validated = TRUE;
366 sess->source->internal = TRUE;
367 sess->stats.active_sources++;
369 /* default UDP header length */
370 sess->header_len = 28;
371 sess->mtu = DEFAULT_RTCP_MTU;
373 /* some default SDES entries */
374 str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
375 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_CNAME, str);
378 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_NAME,
380 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_TOOL, "GStreamer");
382 sess->first_rtcp = TRUE;
384 GST_DEBUG ("%p: session using SSRC: %08x", sess, sess->source->ssrc);
388 rtp_session_finalize (GObject * object)
393 sess = RTP_SESSION_CAST (object);
395 g_mutex_free (sess->lock);
396 for (i = 0; i < 32; i++)
397 g_hash_table_destroy (sess->ssrcs[i]);
399 g_free (sess->bye_reason);
401 g_hash_table_destroy (sess->cnames);
402 g_object_unref (sess->source);
404 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
408 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
410 GValue value = { 0 };
412 g_value_init (&value, RTP_TYPE_SOURCE);
413 g_value_take_object (&value, source);
414 /* copies the value */
415 g_value_array_append (arr, &value);
419 rtp_session_create_sources (RTPSession * sess)
424 RTP_SESSION_LOCK (sess);
425 /* get number of elements in the table */
426 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
427 /* create the result value array */
428 res = g_value_array_new (size);
430 /* and copy all values into the array */
431 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
432 RTP_SESSION_UNLOCK (sess);
438 rtp_session_set_property (GObject * object, guint prop_id,
439 const GValue * value, GParamSpec * pspec)
443 sess = RTP_SESSION (object);
446 case PROP_INTERNAL_SSRC:
447 rtp_session_set_internal_ssrc (sess, g_value_get_uint (value));
450 sess->bandwidth = g_value_get_double (value);
451 sess->recalc_bandwidth = TRUE;
453 case PROP_RTCP_FRACTION:
454 sess->rtcp_bandwidth = g_value_get_double (value);
455 sess->recalc_bandwidth = TRUE;
457 case PROP_RTCP_RR_BANDWIDTH:
458 sess->rtcp_rr_bandwidth = g_value_get_int (value);
459 sess->recalc_bandwidth = TRUE;
461 case PROP_RTCP_RS_BANDWIDTH:
462 sess->rtcp_rs_bandwidth = g_value_get_int (value);
463 sess->recalc_bandwidth = TRUE;
466 sess->mtu = g_value_get_uint (value);
469 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
472 sess->favor_new = g_value_get_boolean (value);
475 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
481 rtp_session_get_property (GObject * object, guint prop_id,
482 GValue * value, GParamSpec * pspec)
486 sess = RTP_SESSION (object);
489 case PROP_INTERNAL_SSRC:
490 g_value_set_uint (value, rtp_session_get_internal_ssrc (sess));
492 case PROP_INTERNAL_SOURCE:
493 g_value_take_object (value, rtp_session_get_internal_source (sess));
496 g_value_set_double (value, sess->bandwidth);
498 case PROP_RTCP_FRACTION:
499 g_value_set_double (value, sess->rtcp_bandwidth);
501 case PROP_RTCP_RR_BANDWIDTH:
502 g_value_set_int (value, sess->rtcp_rr_bandwidth);
504 case PROP_RTCP_RS_BANDWIDTH:
505 g_value_set_int (value, sess->rtcp_rs_bandwidth);
508 g_value_set_uint (value, sess->mtu);
511 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
513 case PROP_NUM_SOURCES:
514 g_value_set_uint (value, rtp_session_get_num_sources (sess));
516 case PROP_NUM_ACTIVE_SOURCES:
517 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
520 g_value_take_boxed (value, rtp_session_create_sources (sess));
523 g_value_set_boolean (value, sess->favor_new);
526 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
532 on_new_ssrc (RTPSession * sess, RTPSource * source)
534 g_object_ref (source);
535 RTP_SESSION_UNLOCK (sess);
536 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
537 RTP_SESSION_LOCK (sess);
538 g_object_unref (source);
542 on_ssrc_collision (RTPSession * sess, RTPSource * source)
544 g_object_ref (source);
545 RTP_SESSION_UNLOCK (sess);
546 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
548 RTP_SESSION_LOCK (sess);
549 g_object_unref (source);
553 on_ssrc_validated (RTPSession * sess, RTPSource * source)
555 g_object_ref (source);
556 RTP_SESSION_UNLOCK (sess);
557 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
559 RTP_SESSION_LOCK (sess);
560 g_object_unref (source);
564 on_ssrc_active (RTPSession * sess, RTPSource * source)
566 g_object_ref (source);
567 RTP_SESSION_UNLOCK (sess);
568 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
569 RTP_SESSION_LOCK (sess);
570 g_object_unref (source);
574 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
576 g_object_ref (source);
577 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
578 RTP_SESSION_UNLOCK (sess);
579 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
580 RTP_SESSION_LOCK (sess);
581 g_object_unref (source);
585 on_bye_ssrc (RTPSession * sess, RTPSource * source)
587 g_object_ref (source);
588 RTP_SESSION_UNLOCK (sess);
589 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
590 RTP_SESSION_LOCK (sess);
591 g_object_unref (source);
595 on_bye_timeout (RTPSession * sess, RTPSource * source)
597 g_object_ref (source);
598 RTP_SESSION_UNLOCK (sess);
599 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
600 RTP_SESSION_LOCK (sess);
601 g_object_unref (source);
605 on_timeout (RTPSession * sess, RTPSource * source)
607 g_object_ref (source);
608 RTP_SESSION_UNLOCK (sess);
609 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
610 RTP_SESSION_LOCK (sess);
611 g_object_unref (source);
615 on_sender_timeout (RTPSession * sess, RTPSource * source)
617 g_object_ref (source);
618 RTP_SESSION_UNLOCK (sess);
619 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
621 RTP_SESSION_LOCK (sess);
622 g_object_unref (source);
628 * Create a new session object.
630 * Returns: a new #RTPSession. g_object_unref() after usage.
633 rtp_session_new (void)
637 sess = g_object_new (RTP_TYPE_SESSION, NULL);
643 * rtp_session_set_callbacks:
644 * @sess: an #RTPSession
645 * @callbacks: callbacks to configure
646 * @user_data: user data passed in the callbacks
648 * Configure a set of callbacks to be notified of actions.
651 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
654 g_return_if_fail (RTP_IS_SESSION (sess));
656 if (callbacks->process_rtp) {
657 sess->callbacks.process_rtp = callbacks->process_rtp;
658 sess->process_rtp_user_data = user_data;
660 if (callbacks->send_rtp) {
661 sess->callbacks.send_rtp = callbacks->send_rtp;
662 sess->send_rtp_user_data = user_data;
664 if (callbacks->send_rtcp) {
665 sess->callbacks.send_rtcp = callbacks->send_rtcp;
666 sess->send_rtcp_user_data = user_data;
668 if (callbacks->sync_rtcp) {
669 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
670 sess->sync_rtcp_user_data = user_data;
672 if (callbacks->clock_rate) {
673 sess->callbacks.clock_rate = callbacks->clock_rate;
674 sess->clock_rate_user_data = user_data;
676 if (callbacks->reconsider) {
677 sess->callbacks.reconsider = callbacks->reconsider;
678 sess->reconsider_user_data = user_data;
683 * rtp_session_set_process_rtp_callback:
684 * @sess: an #RTPSession
685 * @callback: callback to set
686 * @user_data: user data passed in the callback
688 * Configure only the process_rtp callback to be notified of the process_rtp action.
691 rtp_session_set_process_rtp_callback (RTPSession * sess,
692 RTPSessionProcessRTP callback, gpointer user_data)
694 g_return_if_fail (RTP_IS_SESSION (sess));
696 sess->callbacks.process_rtp = callback;
697 sess->process_rtp_user_data = user_data;
701 * rtp_session_set_send_rtp_callback:
702 * @sess: an #RTPSession
703 * @callback: callback to set
704 * @user_data: user data passed in the callback
706 * Configure only the send_rtp callback to be notified of the send_rtp action.
709 rtp_session_set_send_rtp_callback (RTPSession * sess,
710 RTPSessionSendRTP callback, gpointer user_data)
712 g_return_if_fail (RTP_IS_SESSION (sess));
714 sess->callbacks.send_rtp = callback;
715 sess->send_rtp_user_data = user_data;
719 * rtp_session_set_send_rtcp_callback:
720 * @sess: an #RTPSession
721 * @callback: callback to set
722 * @user_data: user data passed in the callback
724 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
727 rtp_session_set_send_rtcp_callback (RTPSession * sess,
728 RTPSessionSendRTCP callback, gpointer user_data)
730 g_return_if_fail (RTP_IS_SESSION (sess));
732 sess->callbacks.send_rtcp = callback;
733 sess->send_rtcp_user_data = user_data;
737 * rtp_session_set_sync_rtcp_callback:
738 * @sess: an #RTPSession
739 * @callback: callback to set
740 * @user_data: user data passed in the callback
742 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
745 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
746 RTPSessionSyncRTCP callback, gpointer user_data)
748 g_return_if_fail (RTP_IS_SESSION (sess));
750 sess->callbacks.sync_rtcp = callback;
751 sess->sync_rtcp_user_data = user_data;
755 * rtp_session_set_clock_rate_callback:
756 * @sess: an #RTPSession
757 * @callback: callback to set
758 * @user_data: user data passed in the callback
760 * Configure only the clock_rate callback to be notified of the clock_rate action.
763 rtp_session_set_clock_rate_callback (RTPSession * sess,
764 RTPSessionClockRate callback, gpointer user_data)
766 g_return_if_fail (RTP_IS_SESSION (sess));
768 sess->callbacks.clock_rate = callback;
769 sess->clock_rate_user_data = user_data;
773 * rtp_session_set_reconsider_callback:
774 * @sess: an #RTPSession
775 * @callback: callback to set
776 * @user_data: user data passed in the callback
778 * Configure only the reconsider callback to be notified of the reconsider action.
781 rtp_session_set_reconsider_callback (RTPSession * sess,
782 RTPSessionReconsider callback, gpointer user_data)
784 g_return_if_fail (RTP_IS_SESSION (sess));
786 sess->callbacks.reconsider = callback;
787 sess->reconsider_user_data = user_data;
791 * rtp_session_set_bandwidth:
792 * @sess: an #RTPSession
793 * @bandwidth: the bandwidth allocated
795 * Set the session bandwidth in bytes per second.
798 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
800 g_return_if_fail (RTP_IS_SESSION (sess));
802 RTP_SESSION_LOCK (sess);
803 sess->stats.bandwidth = bandwidth;
804 RTP_SESSION_UNLOCK (sess);
808 * rtp_session_get_bandwidth:
809 * @sess: an #RTPSession
811 * Get the session bandwidth.
813 * Returns: the session bandwidth.
816 rtp_session_get_bandwidth (RTPSession * sess)
820 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
822 RTP_SESSION_LOCK (sess);
823 result = sess->stats.bandwidth;
824 RTP_SESSION_UNLOCK (sess);
830 * rtp_session_set_rtcp_fraction:
831 * @sess: an #RTPSession
832 * @bandwidth: the RTCP bandwidth
834 * Set the bandwidth in bytes per second that should be used for RTCP
838 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
840 g_return_if_fail (RTP_IS_SESSION (sess));
842 RTP_SESSION_LOCK (sess);
843 sess->stats.rtcp_bandwidth = bandwidth;
844 RTP_SESSION_UNLOCK (sess);
848 * rtp_session_get_rtcp_fraction:
849 * @sess: an #RTPSession
851 * Get the session bandwidth used for RTCP.
853 * Returns: The bandwidth used for RTCP messages.
856 rtp_session_get_rtcp_fraction (RTPSession * sess)
860 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
862 RTP_SESSION_LOCK (sess);
863 result = sess->stats.rtcp_bandwidth;
864 RTP_SESSION_UNLOCK (sess);
870 * rtp_session_set_sdes_string:
871 * @sess: an #RTPSession
872 * @type: the type of the SDES item
873 * @item: a null-terminated string to set.
875 * Store an SDES item of @type in @sess.
877 * Returns: %FALSE if the data was unchanged @type is invalid.
880 rtp_session_set_sdes_string (RTPSession * sess, GstRTCPSDESType type,
885 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
887 RTP_SESSION_LOCK (sess);
888 result = rtp_source_set_sdes_string (sess->source, type, item);
889 RTP_SESSION_UNLOCK (sess);
895 * rtp_session_get_sdes_string:
896 * @sess: an #RTPSession
897 * @type: the type of the SDES item
899 * Get the SDES item of @type from @sess.
901 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
902 * valid. g_free() after usage.
905 rtp_session_get_sdes_string (RTPSession * sess, GstRTCPSDESType type)
909 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
911 RTP_SESSION_LOCK (sess);
912 result = rtp_source_get_sdes_string (sess->source, type);
913 RTP_SESSION_UNLOCK (sess);
919 * rtp_session_get_sdes_struct:
920 * @sess: an #RTSPSession
922 * Get the SDES data as a #GstStructure
924 * Returns: a GstStructure with SDES items for @sess. This function returns a
925 * copy of the SDES structure, use gst_structure_free() after usage.
928 rtp_session_get_sdes_struct (RTPSession * sess)
930 const GstStructure *sdes;
931 GstStructure *result = NULL;
933 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
935 RTP_SESSION_LOCK (sess);
936 sdes = rtp_source_get_sdes_struct (sess->source);
938 result = gst_structure_copy (sdes);
939 RTP_SESSION_UNLOCK (sess);
945 * rtp_session_set_sdes_struct:
946 * @sess: an #RTSPSession
947 * @sdes: a #GstStructure
949 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
952 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
954 g_return_if_fail (sdes);
955 g_return_if_fail (RTP_IS_SESSION (sess));
957 RTP_SESSION_LOCK (sess);
958 rtp_source_set_sdes_struct (sess->source, gst_structure_copy (sdes));
959 RTP_SESSION_UNLOCK (sess);
963 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
965 GstFlowReturn result = GST_FLOW_OK;
967 if (source == session->source) {
968 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
970 RTP_SESSION_UNLOCK (session);
972 if (session->callbacks.send_rtp)
974 session->callbacks.send_rtp (session, source, data,
975 session->send_rtp_user_data);
977 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
980 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
981 RTP_SESSION_UNLOCK (session);
983 if (session->callbacks.process_rtp)
985 session->callbacks.process_rtp (session, source,
986 GST_BUFFER_CAST (data), session->process_rtp_user_data);
988 gst_buffer_unref (GST_BUFFER_CAST (data));
990 RTP_SESSION_LOCK (session);
996 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1000 RTP_SESSION_UNLOCK (session);
1002 if (session->callbacks.clock_rate)
1004 session->callbacks.clock_rate (session, pt,
1005 session->clock_rate_user_data);
1009 RTP_SESSION_LOCK (session);
1011 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1016 static RTPSourceCallbacks callbacks = {
1017 (RTPSourcePushRTP) source_push_rtp,
1018 (RTPSourceClockRate) source_clock_rate,
1022 check_collision (RTPSession * sess, RTPSource * source,
1023 RTPArrivalStats * arrival, gboolean rtp)
1025 /* If we have no arrival address, we can't do collision checking */
1026 if (!arrival->have_address)
1029 if (sess->source != source) {
1030 GstNetAddress *from;
1033 /* This is not our local source, but lets check if two remote
1038 from = &source->rtp_from;
1039 have_from = source->have_rtp_from;
1041 from = &source->rtcp_from;
1042 have_from = source->have_rtcp_from;
1046 if (gst_netaddress_equal (from, &arrival->address)) {
1047 /* Address is the same */
1050 GST_LOG ("we have a third-party collision or loop ssrc:%x",
1051 rtp_source_get_ssrc (source));
1052 if (sess->favor_new) {
1053 if (rtp_source_find_conflicting_address (source,
1054 &arrival->address, arrival->current_time)) {
1056 gst_netaddress_to_string (&arrival->address, buf1, 40);
1057 GST_LOG ("Known conflict on %x for %s, dropping packet",
1058 rtp_source_get_ssrc (source), buf1);
1061 gchar buf1[40], buf2[40];
1063 /* Current address is not a known conflict, lets assume this is
1064 * a new source. Save old address in possible conflict list
1066 rtp_source_add_conflicting_address (source, from,
1067 arrival->current_time);
1069 gst_netaddress_to_string (from, buf1, 40);
1070 gst_netaddress_to_string (&arrival->address, buf2, 40);
1071 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1072 " saving old as known conflict",
1073 rtp_source_get_ssrc (source), buf1, buf2);
1076 rtp_source_set_rtp_from (source, &arrival->address);
1078 rtp_source_set_rtcp_from (source, &arrival->address);
1082 /* Don't need to save old addresses, we ignore new sources */
1087 /* We don't already have a from address for RTP, just set it */
1089 rtp_source_set_rtp_from (source, &arrival->address);
1091 rtp_source_set_rtcp_from (source, &arrival->address);
1095 /* FIXME: Log 3rd party collision somehow
1096 * Maybe should be done in upper layer, only the SDES can tell us
1097 * if its a collision or a loop
1100 /* This is sending with our ssrc, is it an address we already know */
1102 if (rtp_source_find_conflicting_address (source, &arrival->address,
1103 arrival->current_time)) {
1104 /* Its a known conflict, its probably a loop, not a collision
1105 * lets just drop the incoming packet
1107 GST_DEBUG ("Our packets are being looped back to us, dropping");
1109 /* Its a new collision, lets change our SSRC */
1111 rtp_source_add_conflicting_address (source, &arrival->address,
1112 arrival->current_time);
1114 GST_DEBUG ("Collision for SSRC %x", rtp_source_get_ssrc (source));
1115 on_ssrc_collision (sess, source);
1117 rtp_session_schedule_bye_locked (sess, "SSRC Collision",
1118 arrival->current_time);
1120 sess->change_ssrc = TRUE;
1128 /* must be called with the session lock, the returned source needs to be
1129 * unreffed after usage. */
1131 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1132 RTPArrivalStats * arrival, gboolean rtp)
1137 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1138 if (source == NULL) {
1139 /* make new Source in probation and insert */
1140 source = rtp_source_new (ssrc);
1142 /* for RTP packets we need to set the source in probation. Receiving RTCP
1143 * packets of an SSRC, on the other hand, is a strong indication that we
1144 * are dealing with a valid source. */
1146 source->probation = RTP_DEFAULT_PROBATION;
1148 source->probation = 0;
1150 /* store from address, if any */
1151 if (arrival->have_address) {
1153 rtp_source_set_rtp_from (source, &arrival->address);
1155 rtp_source_set_rtcp_from (source, &arrival->address);
1158 /* configure a callback on the source */
1159 rtp_source_set_callbacks (source, &callbacks, sess);
1161 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1164 /* we have one more source now */
1165 sess->total_sources++;
1169 /* check for collision, this updates the address when not previously set */
1170 if (check_collision (sess, source, arrival, rtp)) {
1174 /* update last activity */
1175 source->last_activity = arrival->current_time;
1177 source->last_rtp_activity = arrival->current_time;
1178 g_object_ref (source);
1184 * rtp_session_get_internal_source:
1185 * @sess: a #RTPSession
1187 * Get the internal #RTPSource of @sess.
1189 * Returns: The internal #RTPSource. g_object_unref() after usage.
1192 rtp_session_get_internal_source (RTPSession * sess)
1196 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1198 result = g_object_ref (sess->source);
1204 * rtp_session_set_internal_ssrc:
1205 * @sess: a #RTPSession
1208 * Set the SSRC of @sess to @ssrc.
1211 rtp_session_set_internal_ssrc (RTPSession * sess, guint32 ssrc)
1213 RTP_SESSION_LOCK (sess);
1214 if (ssrc != sess->source->ssrc) {
1215 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
1216 GINT_TO_POINTER (sess->source->ssrc));
1218 GST_DEBUG ("setting internal SSRC to %08x", ssrc);
1219 /* After this call, any receiver of the old SSRC either in RTP or RTCP
1220 * packets will timeout on the old SSRC, we could potentially schedule a
1221 * BYE RTCP for the old SSRC... */
1222 sess->source->ssrc = ssrc;
1223 rtp_source_reset (sess->source);
1225 /* rehash with the new SSRC */
1226 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1227 GINT_TO_POINTER (sess->source->ssrc), sess->source);
1229 RTP_SESSION_UNLOCK (sess);
1231 g_object_notify (G_OBJECT (sess), "internal-ssrc");
1235 * rtp_session_get_internal_ssrc:
1236 * @sess: a #RTPSession
1238 * Get the internal SSRC of @sess.
1240 * Returns: The SSRC of the session.
1243 rtp_session_get_internal_ssrc (RTPSession * sess)
1247 RTP_SESSION_LOCK (sess);
1248 ssrc = sess->source->ssrc;
1249 RTP_SESSION_UNLOCK (sess);
1255 * rtp_session_add_source:
1256 * @sess: a #RTPSession
1257 * @src: #RTPSource to add
1259 * Add @src to @session.
1261 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1262 * existed in the session.
1265 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1267 gboolean result = FALSE;
1270 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1271 g_return_val_if_fail (src != NULL, FALSE);
1273 RTP_SESSION_LOCK (sess);
1275 g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1276 GINT_TO_POINTER (src->ssrc));
1278 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1279 GINT_TO_POINTER (src->ssrc), src);
1280 /* we have one more source now */
1281 sess->total_sources++;
1284 RTP_SESSION_UNLOCK (sess);
1290 * rtp_session_get_num_sources:
1291 * @sess: an #RTPSession
1293 * Get the number of sources in @sess.
1295 * Returns: The number of sources in @sess.
1298 rtp_session_get_num_sources (RTPSession * sess)
1302 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1304 RTP_SESSION_LOCK (sess);
1305 result = sess->total_sources;
1306 RTP_SESSION_UNLOCK (sess);
1312 * rtp_session_get_num_active_sources:
1313 * @sess: an #RTPSession
1315 * Get the number of active sources in @sess. A source is considered active when
1316 * it has been validated and has not yet received a BYE RTCP message.
1318 * Returns: The number of active sources in @sess.
1321 rtp_session_get_num_active_sources (RTPSession * sess)
1325 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1327 RTP_SESSION_LOCK (sess);
1328 result = sess->stats.active_sources;
1329 RTP_SESSION_UNLOCK (sess);
1335 * rtp_session_get_source_by_ssrc:
1336 * @sess: an #RTPSession
1339 * Find the source with @ssrc in @sess.
1341 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1342 * g_object_unref() after usage.
1345 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1349 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1351 RTP_SESSION_LOCK (sess);
1353 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1355 g_object_ref (result);
1356 RTP_SESSION_UNLOCK (sess);
1362 * rtp_session_get_source_by_cname:
1363 * @sess: a #RTPSession
1366 * Find the source with @cname in @sess.
1368 * Returns: a #RTPSource with CNAME @cname or NULL if the source was not found.
1369 * g_object_unref() after usage.
1372 rtp_session_get_source_by_cname (RTPSession * sess, const gchar * cname)
1376 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1377 g_return_val_if_fail (cname != NULL, NULL);
1379 RTP_SESSION_LOCK (sess);
1380 result = g_hash_table_lookup (sess->cnames, cname);
1382 g_object_ref (result);
1383 RTP_SESSION_UNLOCK (sess);
1388 /* should be called with the SESSION lock */
1390 rtp_session_create_new_ssrc (RTPSession * sess)
1395 ssrc = g_random_int ();
1397 /* see if it exists in the session, we're done if it doesn't */
1398 if (g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1399 GINT_TO_POINTER (ssrc)) == NULL)
1407 * rtp_session_create_source:
1408 * @sess: an #RTPSession
1410 * Create an #RTPSource for use in @sess. This function will create a source
1411 * with an ssrc that is currently not used by any participants in the session.
1413 * Returns: an #RTPSource.
1416 rtp_session_create_source (RTPSession * sess)
1421 RTP_SESSION_LOCK (sess);
1422 ssrc = rtp_session_create_new_ssrc (sess);
1423 source = rtp_source_new (ssrc);
1424 rtp_source_set_callbacks (source, &callbacks, sess);
1425 /* we need an additional ref for the source in the hashtable */
1426 g_object_ref (source);
1427 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1429 /* we have one more source now */
1430 sess->total_sources++;
1431 RTP_SESSION_UNLOCK (sess);
1436 /* update the RTPArrivalStats structure with the current time and other bits
1437 * about the current buffer we are handling.
1438 * This function is typically called when a validated packet is received.
1439 * This function should be called with the SESSION_LOCK
1442 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
1443 gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
1444 GstClockTime running_time)
1446 /* get time of arrival */
1447 arrival->current_time = current_time;
1448 arrival->running_time = running_time;
1450 /* get packet size including header overhead */
1451 arrival->bytes = GST_BUFFER_SIZE (buffer) + sess->header_len;
1454 arrival->payload_len = gst_rtp_buffer_get_payload_len (buffer);
1456 arrival->payload_len = 0;
1459 /* for netbuffer we can store the IP address to check for collisions */
1460 arrival->have_address = GST_IS_NETBUFFER (buffer);
1461 if (arrival->have_address) {
1462 GstNetBuffer *netbuf = (GstNetBuffer *) buffer;
1464 memcpy (&arrival->address, &netbuf->from, sizeof (GstNetAddress));
1469 * rtp_session_process_rtp:
1470 * @sess: and #RTPSession
1471 * @buffer: an RTP buffer
1472 * @current_time: the current system time
1473 * @running_time: the running_time of @buffer
1475 * Process an RTP buffer in the session manager. This function takes ownership
1478 * Returns: a #GstFlowReturn.
1481 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1482 GstClockTime current_time, GstClockTime running_time)
1484 GstFlowReturn result;
1488 gboolean prevsender, prevactive;
1489 RTPArrivalStats arrival;
1494 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1495 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1497 if (!gst_rtp_buffer_validate (buffer))
1498 goto invalid_packet;
1500 RTP_SESSION_LOCK (sess);
1501 /* update arrival stats */
1502 update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1505 /* ignore more RTP packets when we left the session */
1506 if (sess->source->received_bye)
1509 /* get SSRC and look up in session database */
1510 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1511 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1515 prevsender = RTP_SOURCE_IS_SENDER (source);
1516 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1517 oldrate = source->bitrate;
1519 /* copy available csrc for later */
1520 count = gst_rtp_buffer_get_csrc_count (buffer);
1521 /* make sure to not overflow our array. An RTP buffer can maximally contain
1523 count = MIN (count, 16);
1525 for (i = 0; i < count; i++)
1526 csrcs[i] = gst_rtp_buffer_get_csrc (buffer, i);
1528 /* let source process the packet */
1529 result = rtp_source_process_rtp (source, buffer, &arrival);
1531 /* source became active */
1532 if (prevactive != RTP_SOURCE_IS_ACTIVE (source)) {
1533 sess->stats.active_sources++;
1534 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1535 sess->stats.active_sources);
1536 on_ssrc_validated (sess, source);
1538 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1539 sess->stats.sender_sources++;
1540 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1541 sess->stats.sender_sources);
1543 if (oldrate != source->bitrate)
1544 sess->recalc_bandwidth = TRUE;
1547 on_new_ssrc (sess, source);
1549 if (source->validated) {
1552 /* for validated sources, we add the CSRCs as well */
1553 for (i = 0; i < count; i++) {
1555 RTPSource *csrc_src;
1560 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1565 GST_DEBUG ("created new CSRC: %08x", csrc);
1566 rtp_source_set_as_csrc (csrc_src);
1567 if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1568 sess->stats.active_sources++;
1569 on_new_ssrc (sess, csrc_src);
1571 g_object_unref (csrc_src);
1574 g_object_unref (source);
1576 RTP_SESSION_UNLOCK (sess);
1583 gst_buffer_unref (buffer);
1584 GST_DEBUG ("invalid RTP packet received");
1589 gst_buffer_unref (buffer);
1590 RTP_SESSION_UNLOCK (sess);
1591 GST_DEBUG ("ignoring RTP packet because we are leaving");
1596 gst_buffer_unref (buffer);
1597 RTP_SESSION_UNLOCK (sess);
1598 GST_DEBUG ("ignoring packet because its collisioning");
1604 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1605 GstRTCPPacket * packet, RTPArrivalStats * arrival)
1609 count = gst_rtcp_packet_get_rb_count (packet);
1610 for (i = 0; i < count; i++) {
1611 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1612 guint8 fractionlost;
1615 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1616 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1618 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1620 if (ssrc == sess->source->ssrc) {
1621 /* only deal with report blocks for our session, we update the stats of
1622 * the sender of the RTCP message. We could also compare our stats against
1623 * the other sender to see if we are better or worse. */
1624 rtp_source_process_rb (source, arrival->current_time, fractionlost,
1625 packetslost, exthighestseq, jitter, lsr, dlsr);
1627 on_ssrc_active (sess, source);
1632 /* A Sender report contains statistics about how the sender is doing. This
1633 * includes timing informataion such as the relation between RTP and NTP
1634 * timestamps and the number of packets/bytes it sent to us.
1636 * In this report is also included a set of report blocks related to how this
1637 * sender is receiving data (in case we (or somebody else) is also sending stuff
1638 * to it). This info includes the packet loss, jitter and seqnum. It also
1639 * contains information to calculate the round trip time (LSR/DLSR).
1642 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1643 RTPArrivalStats * arrival, gboolean * do_sync)
1645 guint32 senderssrc, rtptime, packet_count, octet_count;
1648 gboolean created, prevsender;
1650 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1651 &packet_count, &octet_count);
1653 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1654 senderssrc, GST_TIME_ARGS (arrival->current_time));
1656 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1660 /* don't try to do lip-sync for sources that sent a BYE */
1661 if (rtp_source_received_bye (source))
1666 prevsender = RTP_SOURCE_IS_SENDER (source);
1668 /* first update the source */
1669 rtp_source_process_sr (source, arrival->current_time, ntptime, rtptime,
1670 packet_count, octet_count);
1672 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1673 sess->stats.sender_sources++;
1674 GST_DEBUG ("source: %08x became sender, %d sender sources", senderssrc,
1675 sess->stats.sender_sources);
1679 on_new_ssrc (sess, source);
1681 rtp_session_process_rb (sess, source, packet, arrival);
1682 g_object_unref (source);
1685 /* A receiver report contains statistics about how a receiver is doing. It
1686 * includes stuff like packet loss, jitter and the seqnum it received last. It
1687 * also contains info to calculate the round trip time.
1689 * We are only interested in how the sender of this report is doing wrt to us.
1692 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1693 RTPArrivalStats * arrival)
1699 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1701 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1703 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1708 on_new_ssrc (sess, source);
1710 rtp_session_process_rb (sess, source, packet, arrival);
1711 g_object_unref (source);
1714 /* Get SDES items and store them in the SSRC */
1716 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1717 RTPArrivalStats * arrival)
1720 gboolean more_items, more_entries;
1722 items = gst_rtcp_packet_sdes_get_item_count (packet);
1723 GST_DEBUG ("got SDES packet with %d items", items);
1725 more_items = gst_rtcp_packet_sdes_first_item (packet);
1727 while (more_items) {
1729 gboolean changed, created;
1733 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1735 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1739 /* find src, no probation when dealing with RTCP */
1740 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1744 sdes = gst_structure_new ("application/x-rtp-source-sdes", NULL);
1746 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1748 while (more_entries) {
1749 GstRTCPSDESType type;
1755 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1757 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1760 if (type == GST_RTCP_SDES_PRIV) {
1761 name = g_strndup ((const gchar *) &data[1], data[0]);
1763 data += data[0] + 1;
1765 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
1768 value = g_strndup ((const gchar *) data, len);
1770 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
1775 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1779 /* takes ownership of sdes */
1780 changed = rtp_source_set_sdes_struct (source, sdes);
1782 source->validated = TRUE;
1785 on_new_ssrc (sess, source);
1787 on_ssrc_sdes (sess, source);
1789 g_object_unref (source);
1791 more_items = gst_rtcp_packet_sdes_next_item (packet);
1796 /* BYE is sent when a client leaves the session
1799 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
1800 RTPArrivalStats * arrival)
1804 gboolean reconsider = FALSE;
1806 reason = gst_rtcp_packet_bye_get_reason (packet);
1807 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
1809 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
1810 for (i = 0; i < count; i++) {
1813 gboolean created, prevactive, prevsender;
1814 guint pmembers, members;
1816 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
1817 GST_DEBUG ("SSRC: %08x", ssrc);
1819 /* find src and mark bye, no probation when dealing with RTCP */
1820 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1824 /* store time for when we need to time out this source */
1825 source->bye_time = arrival->current_time;
1827 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1828 prevsender = RTP_SOURCE_IS_SENDER (source);
1830 /* let the source handle the rest */
1831 rtp_source_process_bye (source, reason);
1833 pmembers = sess->stats.active_sources;
1835 if (prevactive && !RTP_SOURCE_IS_ACTIVE (source)) {
1836 sess->stats.active_sources--;
1837 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
1838 sess->stats.active_sources);
1840 if (prevsender && !RTP_SOURCE_IS_SENDER (source)) {
1841 sess->stats.sender_sources--;
1842 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
1843 sess->stats.sender_sources);
1845 members = sess->stats.active_sources;
1847 if (!sess->source->received_bye && members < pmembers) {
1848 /* some members went away since the previous timeout estimate.
1849 * Perform reverse reconsideration but only when we are not scheduling a
1851 if (arrival->current_time < sess->next_rtcp_check_time) {
1852 GstClockTime time_remaining;
1854 time_remaining = sess->next_rtcp_check_time - arrival->current_time;
1855 sess->next_rtcp_check_time =
1856 gst_util_uint64_scale (time_remaining, members, pmembers);
1858 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
1859 GST_TIME_ARGS (sess->next_rtcp_check_time));
1861 sess->next_rtcp_check_time += arrival->current_time;
1863 /* mark pending reconsider. We only want to signal the reconsideration
1864 * once after we handled all the source in the bye packet */
1870 on_new_ssrc (sess, source);
1872 on_bye_ssrc (sess, source);
1874 g_object_unref (source);
1877 RTP_SESSION_UNLOCK (sess);
1878 /* notify app of reconsideration */
1879 if (sess->callbacks.reconsider)
1880 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
1881 RTP_SESSION_LOCK (sess);
1887 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
1888 RTPArrivalStats * arrival)
1890 GST_DEBUG ("received APP");
1894 * rtp_session_process_rtcp:
1895 * @sess: and #RTPSession
1896 * @buffer: an RTCP buffer
1897 * @current_time: the current system time
1899 * Process an RTCP buffer in the session manager. This function takes ownership
1902 * Returns: a #GstFlowReturn.
1905 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
1906 GstClockTime current_time)
1908 GstRTCPPacket packet;
1909 gboolean more, is_bye = FALSE, do_sync = FALSE;
1910 RTPArrivalStats arrival;
1911 GstFlowReturn result = GST_FLOW_OK;
1913 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1914 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1916 if (!gst_rtcp_buffer_validate (buffer))
1917 goto invalid_packet;
1919 GST_DEBUG ("received RTCP packet");
1921 RTP_SESSION_LOCK (sess);
1922 /* update arrival stats */
1923 update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1);
1928 /* make writable, we might want to change the buffer */
1929 buffer = gst_buffer_make_metadata_writable (buffer);
1931 /* start processing the compound packet */
1932 more = gst_rtcp_buffer_get_first_packet (buffer, &packet);
1936 type = gst_rtcp_packet_get_type (&packet);
1938 /* when we are leaving the session, we should ignore all non-BYE messages */
1939 if (sess->source->received_bye && type != GST_RTCP_TYPE_BYE) {
1940 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
1945 case GST_RTCP_TYPE_SR:
1946 rtp_session_process_sr (sess, &packet, &arrival, &do_sync);
1948 case GST_RTCP_TYPE_RR:
1949 rtp_session_process_rr (sess, &packet, &arrival);
1951 case GST_RTCP_TYPE_SDES:
1952 rtp_session_process_sdes (sess, &packet, &arrival);
1954 case GST_RTCP_TYPE_BYE:
1956 /* don't try to attempt lip-sync anymore for streams with a BYE */
1958 rtp_session_process_bye (sess, &packet, &arrival);
1960 case GST_RTCP_TYPE_APP:
1961 rtp_session_process_app (sess, &packet, &arrival);
1964 GST_WARNING ("got unknown RTCP packet");
1968 more = gst_rtcp_packet_move_to_next (&packet);
1971 /* if we are scheduling a BYE, we only want to count bye packets, else we
1972 * count everything */
1973 if (sess->source->received_bye) {
1975 sess->stats.bye_members++;
1976 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
1979 /* keep track of average packet size */
1980 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
1982 RTP_SESSION_UNLOCK (sess);
1984 /* notify caller of sr packets in the callback */
1985 if (do_sync && sess->callbacks.sync_rtcp)
1986 result = sess->callbacks.sync_rtcp (sess, sess->source, buffer,
1987 sess->sync_rtcp_user_data);
1989 gst_buffer_unref (buffer);
1996 GST_DEBUG ("invalid RTCP packet received");
1997 gst_buffer_unref (buffer);
2002 gst_buffer_unref (buffer);
2003 RTP_SESSION_UNLOCK (sess);
2004 GST_DEBUG ("ignoring RTP packet because we left");
2010 * rtp_session_send_rtp:
2011 * @sess: an #RTPSession
2012 * @data: pointer to either an RTP buffer or a list of RTP buffers
2013 * @is_list: TRUE when @data is a buffer list
2014 * @current_time: the current system time
2015 * @running_time: the running time of @data
2017 * Send the RTP buffer in the session manager. This function takes ownership of
2020 * Returns: a #GstFlowReturn.
2023 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
2024 GstClockTime current_time, GstClockTime running_time)
2026 GstFlowReturn result;
2028 gboolean prevsender;
2029 gboolean valid_packet;
2032 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2033 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
2036 valid_packet = gst_rtp_buffer_list_validate (GST_BUFFER_LIST_CAST (data));
2038 valid_packet = gst_rtp_buffer_validate (GST_BUFFER_CAST (data));
2042 goto invalid_packet;
2044 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
2046 RTP_SESSION_LOCK (sess);
2047 source = sess->source;
2049 /* update last activity */
2050 source->last_rtp_activity = current_time;
2052 prevsender = RTP_SOURCE_IS_SENDER (source);
2053 oldrate = source->bitrate;
2055 /* we use our own source to send */
2056 result = rtp_source_send_rtp (source, data, is_list, running_time);
2058 if (RTP_SOURCE_IS_SENDER (source) && !prevsender)
2059 sess->stats.sender_sources++;
2060 if (oldrate != source->bitrate)
2061 sess->recalc_bandwidth = TRUE;
2062 RTP_SESSION_UNLOCK (sess);
2069 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
2070 GST_DEBUG ("invalid RTP packet received");
2076 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
2078 *bandwidth += source->bitrate;
2082 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2085 GstClockTime result;
2087 /* recalculate bandwidth when it changed */
2088 if (sess->recalc_bandwidth) {
2091 if (sess->bandwidth > 0)
2092 bandwidth = sess->bandwidth;
2094 /* If it is <= 0, then try to estimate the actual bandwidth */
2095 bandwidth = sess->source->bitrate;
2097 g_hash_table_foreach (sess->cnames, (GHFunc) add_bitrates, &bandwidth);
2101 bandwidth = RTP_STATS_BANDWIDTH;
2103 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
2104 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
2106 sess->recalc_bandwidth = FALSE;
2109 if (sess->source->received_bye) {
2110 result = rtp_stats_calculate_bye_interval (&sess->stats);
2112 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2113 RTP_SOURCE_IS_SENDER (sess->source), first);
2116 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2117 GST_TIME_ARGS (result), first);
2119 if (!deterministic && result != GST_CLOCK_TIME_NONE)
2120 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2122 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2127 /* Stop the current @sess and schedule a BYE message for the other members.
2128 * One must have the session lock to call this function
2130 static GstFlowReturn
2131 rtp_session_schedule_bye_locked (RTPSession * sess, const gchar * reason,
2132 GstClockTime current_time)
2134 GstFlowReturn result = GST_FLOW_OK;
2136 GstClockTime interval;
2138 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2140 source = sess->source;
2142 /* ignore more BYEs */
2143 if (source->received_bye)
2146 /* we have BYE now */
2147 source->received_bye = TRUE;
2148 /* at least one member wants to send a BYE */
2149 g_free (sess->bye_reason);
2150 sess->bye_reason = g_strdup (reason);
2151 /* The avg packet size is kept scaled by 16 */
2152 sess->stats.avg_rtcp_packet_size = 100 * 16;
2153 sess->stats.bye_members = 1;
2154 sess->first_rtcp = TRUE;
2155 sess->sent_bye = FALSE;
2157 /* reschedule transmission */
2158 sess->last_rtcp_send_time = current_time;
2159 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2160 sess->next_rtcp_check_time = current_time + interval;
2162 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2163 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2165 RTP_SESSION_UNLOCK (sess);
2166 /* notify app of reconsideration */
2167 if (sess->callbacks.reconsider)
2168 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2169 RTP_SESSION_LOCK (sess);
2176 * rtp_session_schedule_bye:
2177 * @sess: an #RTPSession
2178 * @reason: a reason or NULL
2179 * @current_time: the current system time
2181 * Stop the current @sess and schedule a BYE message for the other members.
2183 * Returns: a #GstFlowReturn.
2186 rtp_session_schedule_bye (RTPSession * sess, const gchar * reason,
2187 GstClockTime current_time)
2189 GstFlowReturn result = GST_FLOW_OK;
2191 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2193 RTP_SESSION_LOCK (sess);
2194 result = rtp_session_schedule_bye_locked (sess, reason, current_time);
2195 RTP_SESSION_UNLOCK (sess);
2201 * rtp_session_next_timeout:
2202 * @sess: an #RTPSession
2203 * @current_time: the current system time
2205 * Get the next time we should perform session maintenance tasks.
2207 * Returns: a time when rtp_session_on_timeout() should be called with the
2208 * current system time.
2211 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2213 GstClockTime result, interval = 0;
2215 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
2217 RTP_SESSION_LOCK (sess);
2219 result = sess->next_rtcp_check_time;
2221 GST_DEBUG ("current time: %" GST_TIME_FORMAT ", next :%" GST_TIME_FORMAT,
2222 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2224 if (result < current_time) {
2225 GST_DEBUG ("take current time as base");
2226 /* our previous check time expired, start counting from the current time
2228 result = current_time;
2231 if (sess->source->received_bye) {
2232 if (sess->sent_bye) {
2233 GST_DEBUG ("we sent BYE already");
2234 interval = GST_CLOCK_TIME_NONE;
2235 } else if (sess->stats.active_sources >= 50) {
2236 GST_DEBUG ("reconsider BYE, more than 50 sources");
2237 /* reconsider BYE if members >= 50 */
2238 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2241 if (sess->first_rtcp) {
2242 GST_DEBUG ("first RTCP packet");
2243 /* we are called for the first time */
2244 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2245 } else if (sess->next_rtcp_check_time < current_time) {
2246 GST_DEBUG ("old check time expired, getting new timeout");
2247 /* get a new timeout when we need to */
2248 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
2252 if (interval != GST_CLOCK_TIME_NONE)
2255 result = GST_CLOCK_TIME_NONE;
2257 sess->next_rtcp_check_time = result;
2259 GST_DEBUG ("next timeout: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2260 RTP_SESSION_UNLOCK (sess);
2269 GstClockTime current_time;
2271 GstClockTime running_time;
2272 GstClockTime interval;
2273 GstRTCPPacket packet;
2279 session_start_rtcp (RTPSession * sess, ReportData * data)
2281 GstRTCPPacket *packet = &data->packet;
2282 RTPSource *own = sess->source;
2284 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2286 if (RTP_SOURCE_IS_SENDER (own)) {
2289 guint32 packet_count, octet_count;
2291 /* we are a sender, create SR */
2292 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2293 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SR, packet);
2295 /* get latest stats */
2296 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
2297 &ntptime, &rtptime, &packet_count, &octet_count);
2299 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2300 packet_count, octet_count);
2302 /* fill in sender report info */
2303 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2304 ntptime, rtptime, packet_count, octet_count);
2306 /* we are only receiver, create RR */
2307 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2308 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_RR, packet);
2309 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2313 /* construct a Sender or Receiver Report */
2315 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2317 RTPSession *sess = data->sess;
2318 GstRTCPPacket *packet = &data->packet;
2320 /* create a new buffer if needed */
2321 if (data->rtcp == NULL) {
2322 session_start_rtcp (sess, data);
2324 if (gst_rtcp_packet_get_rb_count (packet) < GST_RTCP_MAX_RB_COUNT) {
2325 /* only report about other sender sources */
2326 if (source != sess->source && RTP_SOURCE_IS_SENDER (source)) {
2327 guint8 fractionlost;
2329 guint32 exthighestseq, jitter;
2333 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2334 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2336 /* packet is not yet filled, add report block for this source. */
2337 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2338 exthighestseq, jitter, lsr, dlsr);
2343 /* perform cleanup of sources that timed out */
2345 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2347 gboolean remove = FALSE;
2348 gboolean byetimeout = FALSE;
2349 gboolean sendertimeout = FALSE;
2350 gboolean is_sender, is_active;
2351 RTPSession *sess = data->sess;
2352 GstClockTime interval;
2354 is_sender = RTP_SOURCE_IS_SENDER (source);
2355 is_active = RTP_SOURCE_IS_ACTIVE (source);
2357 /* check for our own source, we don't want to delete our own source. */
2358 if (!(source == sess->source)) {
2359 if (source->received_bye) {
2360 /* if we received a BYE from the source, remove the source after some
2362 if (data->current_time > source->bye_time &&
2363 data->current_time - source->bye_time > sess->stats.bye_timeout) {
2364 GST_DEBUG ("removing BYE source %08x", source->ssrc);
2369 /* sources that were inactive for more than 5 times the deterministic reporting
2370 * interval get timed out. the min timeout is 5 seconds. */
2371 if (data->current_time > source->last_activity) {
2372 interval = MAX (data->interval * 5, 5 * GST_SECOND);
2373 if (data->current_time - source->last_activity > interval) {
2374 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2375 source->ssrc, GST_TIME_ARGS (source->last_activity));
2381 /* senders that did not send for a long time become a receiver, this also
2382 * holds for our own source. */
2384 if (data->current_time > source->last_rtp_activity) {
2385 interval = MAX (data->interval * 2, 5 * GST_SECOND);
2386 if (data->current_time - source->last_rtp_activity > interval) {
2387 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
2388 GST_TIME_FORMAT, source->ssrc,
2389 GST_TIME_ARGS (source->last_rtp_activity));
2390 source->is_sender = FALSE;
2391 sess->stats.sender_sources--;
2392 sendertimeout = TRUE;
2398 sess->total_sources--;
2400 sess->stats.sender_sources--;
2402 sess->stats.active_sources--;
2405 on_bye_timeout (sess, source);
2407 on_timeout (sess, source);
2410 on_sender_timeout (sess, source);
2416 session_sdes (RTPSession * sess, ReportData * data)
2418 GstRTCPPacket *packet = &data->packet;
2419 const GstStructure *sdes;
2422 /* add SDES packet */
2423 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SDES, packet);
2425 gst_rtcp_packet_sdes_add_item (packet, sess->source->ssrc);
2427 sdes = rtp_source_get_sdes_struct (sess->source);
2429 /* add all fields in the structure, the order is not important. */
2430 n_fields = gst_structure_n_fields (sdes);
2431 for (i = 0; i < n_fields; ++i) {
2434 GstRTCPSDESType type;
2436 field = gst_structure_nth_field_name (sdes, i);
2439 value = gst_structure_get_string (sdes, field);
2442 type = gst_rtcp_sdes_name_to_type (field);
2444 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
2445 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
2446 (const guint8 *) value);
2447 } else if (type == GST_RTCP_SDES_PRIV) {
2453 /* don't accept entries that are too big */
2454 prefix_len = strlen (field);
2455 if (prefix_len > 255)
2457 value_len = strlen (value);
2458 if (value_len > 255)
2460 data_len = 1 + prefix_len + value_len;
2464 data[0] = prefix_len;
2465 memcpy (&data[1], field, prefix_len);
2466 memcpy (&data[1 + prefix_len], value, value_len);
2468 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
2472 data->has_sdes = TRUE;
2475 /* schedule a BYE packet */
2477 session_bye (RTPSession * sess, ReportData * data)
2479 GstRTCPPacket *packet = &data->packet;
2482 session_start_rtcp (sess, data);
2485 session_sdes (sess, data);
2487 /* add a BYE packet */
2488 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_BYE, packet);
2489 gst_rtcp_packet_bye_add_ssrc (packet, sess->source->ssrc);
2490 if (sess->bye_reason)
2491 gst_rtcp_packet_bye_set_reason (packet, sess->bye_reason);
2493 /* we have a BYE packet now */
2494 data->is_bye = TRUE;
2498 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
2500 GstClockTime new_send_time, elapsed;
2503 /* no need to check yet */
2504 if (sess->next_rtcp_check_time > current_time) {
2505 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
2506 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
2507 GST_TIME_ARGS (current_time));
2511 /* get elapsed time since we last reported */
2512 elapsed = current_time - sess->last_rtcp_send_time;
2514 /* perform forward reconsideration */
2515 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, data->interval);
2517 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
2518 GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time), GST_TIME_ARGS (elapsed));
2520 new_send_time += sess->last_rtcp_send_time;
2522 /* check if reconsideration */
2523 if (current_time < new_send_time) {
2524 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
2525 GST_TIME_ARGS (new_send_time));
2527 /* store new check time */
2528 sess->next_rtcp_check_time = new_send_time;
2531 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
2533 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
2534 GST_TIME_ARGS (new_send_time));
2535 sess->next_rtcp_check_time = current_time + new_send_time;
2541 * rtp_session_on_timeout:
2542 * @sess: an #RTPSession
2543 * @current_time: the current system time
2544 * @ntpnstime: the current NTP time in nanoseconds
2545 * @running_time: the current running_time of the pipeline
2547 * Perform maintenance actions after the timeout obtained with
2548 * rtp_session_next_timeout() expired.
2550 * This function will perform timeouts of receivers and senders, send a BYE
2551 * packet or generate RTCP packets with current session stats.
2553 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
2554 * times, for each packet that should be processed.
2556 * Returns: a #GstFlowReturn.
2559 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
2560 guint64 ntpnstime, GstClockTime running_time)
2562 GstFlowReturn result = GST_FLOW_OK;
2565 gboolean notify = FALSE;
2567 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2569 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT,
2570 GST_TIME_ARGS (current_time), GST_TIME_ARGS (ntpnstime));
2574 data.current_time = current_time;
2575 data.ntpnstime = ntpnstime;
2576 data.is_bye = FALSE;
2577 data.has_sdes = FALSE;
2578 data.running_time = running_time;
2582 RTP_SESSION_LOCK (sess);
2583 /* get a new interval, we need this for various cleanups etc */
2584 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
2586 /* first perform cleanups */
2587 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
2588 (GHRFunc) session_cleanup, &data);
2590 /* see if we need to generate SR or RR packets */
2591 if (is_rtcp_time (sess, current_time, &data)) {
2592 if (own->received_bye) {
2593 /* generate BYE instead */
2594 GST_DEBUG ("generating BYE message");
2595 session_bye (sess, &data);
2596 sess->sent_bye = TRUE;
2598 /* loop over all known sources and do something */
2599 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2600 (GHFunc) session_report_blocks, &data);
2605 /* we keep track of the last report time in order to timeout inactive
2606 * receivers or senders */
2607 sess->last_rtcp_send_time = data.current_time;
2608 sess->first_rtcp = FALSE;
2610 /* add SDES for this source when not already added */
2612 session_sdes (sess, &data);
2615 /* check for outdated collisions */
2616 GST_DEBUG ("Timing out collisions");
2617 rtp_source_timeout (sess->source, current_time,
2618 data.interval * RTCP_INTERVAL_COLLISION_TIMEOUT);
2620 if (sess->change_ssrc) {
2621 GST_DEBUG ("need to change our SSRC (%08x)", own->ssrc);
2622 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
2623 GINT_TO_POINTER (own->ssrc));
2625 own->ssrc = rtp_session_create_new_ssrc (sess);
2626 rtp_source_reset (own);
2628 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
2629 GINT_TO_POINTER (own->ssrc), own);
2631 g_free (sess->bye_reason);
2632 sess->bye_reason = NULL;
2633 sess->sent_bye = FALSE;
2634 sess->change_ssrc = FALSE;
2636 GST_DEBUG ("changed our SSRC to %08x", own->ssrc);
2638 RTP_SESSION_UNLOCK (sess);
2641 g_object_notify (G_OBJECT (sess), "internal-ssrc");
2643 /* push out the RTCP packet */
2645 /* close the RTCP packet */
2646 gst_rtcp_buffer_end (data.rtcp);
2648 GST_DEBUG ("sending packet");
2649 if (sess->callbacks.send_rtcp) {
2650 UPDATE_AVG (sess->stats.avg_rtcp_packet_size,
2651 GST_BUFFER_SIZE (data.rtcp));
2652 result = sess->callbacks.send_rtcp (sess, own, data.rtcp,
2653 sess->sent_bye, sess->send_rtcp_user_data);
2655 GST_DEBUG ("freeing packet");
2656 gst_buffer_unref (data.rtcp);