2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include <gst/rtp/gstrtpbuffer.h>
23 #include <gst/rtp/gstrtcpbuffer.h>
24 #include <gst/netbuffer/gstnetbuffer.h>
26 #include "gstrtpbin-marshal.h"
27 #include "rtpsession.h"
29 GST_DEBUG_CATEGORY_STATIC (rtp_session_debug);
30 #define GST_CAT_DEFAULT rtp_session_debug
32 /* signals and args */
35 SIGNAL_GET_SOURCE_BY_SSRC,
37 SIGNAL_ON_SSRC_COLLISION,
38 SIGNAL_ON_SSRC_VALIDATED,
39 SIGNAL_ON_SSRC_ACTIVE,
42 SIGNAL_ON_BYE_TIMEOUT,
44 SIGNAL_ON_SENDER_TIMEOUT,
48 #define DEFAULT_INTERNAL_SOURCE NULL
49 #define DEFAULT_BANDWIDTH RTP_STATS_BANDWIDTH
50 #define DEFAULT_RTCP_FRACTION RTP_STATS_RTCP_BANDWIDTH
51 #define DEFAULT_RTCP_MTU 1400
52 #define DEFAULT_SDES_CNAME NULL
53 #define DEFAULT_SDES_NAME NULL
54 #define DEFAULT_SDES_EMAIL NULL
55 #define DEFAULT_SDES_PHONE NULL
56 #define DEFAULT_SDES_LOCATION NULL
57 #define DEFAULT_SDES_TOOL NULL
58 #define DEFAULT_SDES_NOTE NULL
59 #define DEFAULT_NUM_SOURCES 0
60 #define DEFAULT_NUM_ACTIVE_SOURCES 0
61 #define DEFAULT_SOURCES NULL
79 PROP_NUM_ACTIVE_SOURCES,
84 /* update average packet size, we keep this scaled by 16 to keep enough
86 #define UPDATE_AVG(avg, val) \
90 (avg) = ((val) + (15 * (avg))) >> 4;
92 /* The number RTCP intervals after which to timeout entries in the
95 #define RTCP_INTERVAL_COLLISION_TIMEOUT 10
97 /* GObject vmethods */
98 static void rtp_session_finalize (GObject * object);
99 static void rtp_session_set_property (GObject * object, guint prop_id,
100 const GValue * value, GParamSpec * pspec);
101 static void rtp_session_get_property (GObject * object, guint prop_id,
102 GValue * value, GParamSpec * pspec);
104 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
106 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
108 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
109 gboolean * created, RTPArrivalStats * arrival, gboolean rtp);
110 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
111 const gchar * reason, GstClockTime current_time);
112 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
113 gboolean deterministic, gboolean first);
116 rtp_session_class_init (RTPSessionClass * klass)
118 GObjectClass *gobject_class;
120 gobject_class = (GObjectClass *) klass;
122 gobject_class->finalize = rtp_session_finalize;
123 gobject_class->set_property = rtp_session_set_property;
124 gobject_class->get_property = rtp_session_get_property;
127 * RTPSession::get-source-by-ssrc:
128 * @session: the object which received the signal
129 * @ssrc: the SSRC of the RTPSource
131 * Request the #RTPSource object with SSRC @ssrc in @session.
133 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
134 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
135 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
136 get_source_by_ssrc), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
137 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
140 * RTPSession::on-new-ssrc:
141 * @session: the object which received the signal
142 * @src: the new RTPSource
144 * Notify of a new SSRC that entered @session.
146 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
147 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
148 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
149 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
152 * RTPSession::on-ssrc-collision:
153 * @session: the object which received the signal
154 * @src: the #RTPSource that caused a collision
156 * Notify when we have an SSRC collision
158 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
159 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
160 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
161 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
164 * RTPSession::on-ssrc-validated:
165 * @session: the object which received the signal
166 * @src: the new validated RTPSource
168 * Notify of a new SSRC that became validated.
170 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
171 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
172 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
173 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
176 * RTPSession::on-ssrc-active:
177 * @session: the object which received the signal
178 * @src: the active RTPSource
180 * Notify of a SSRC that is active, i.e., sending RTCP.
182 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
183 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
184 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
185 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
188 * RTPSession::on-ssrc-sdes:
189 * @session: the object which received the signal
190 * @src: the RTPSource
192 * Notify that a new SDES was received for SSRC.
194 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
195 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
196 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
197 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
200 * RTPSession::on-bye-ssrc:
201 * @session: the object which received the signal
202 * @src: the RTPSource that went away
204 * Notify of an SSRC that became inactive because of a BYE packet.
206 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
207 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
208 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
209 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
212 * RTPSession::on-bye-timeout:
213 * @session: the object which received the signal
214 * @src: the RTPSource that timed out
216 * Notify of an SSRC that has timed out because of BYE
218 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
219 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
220 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
221 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
224 * RTPSession::on-timeout:
225 * @session: the object which received the signal
226 * @src: the RTPSource that timed out
228 * Notify of an SSRC that has timed out
230 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
231 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
232 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
233 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
236 * RTPSession::on-sender-timeout:
237 * @session: the object which received the signal
238 * @src: the RTPSource that timed out
240 * Notify of an SSRC that was a sender but timed out and became a receiver.
242 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
243 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
244 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
245 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
248 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
249 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
250 "The internal SSRC used for the session",
251 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
253 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
254 g_param_spec_object ("internal-source", "Internal Source",
255 "The internal source element of the session",
256 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
258 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
259 g_param_spec_double ("bandwidth", "Bandwidth",
260 "The bandwidth of the session",
261 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
262 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
264 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
265 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
266 "The fraction of the bandwidth used for RTCP",
267 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
268 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
270 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
271 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
272 "The maximum size of the RTCP packets",
273 16, G_MAXINT16, DEFAULT_RTCP_MTU,
274 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
276 g_object_class_install_property (gobject_class, PROP_SDES_CNAME,
277 g_param_spec_string ("sdes-cname", "SDES CNAME",
278 "The CNAME to put in SDES messages of this session",
279 DEFAULT_SDES_CNAME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
281 g_object_class_install_property (gobject_class, PROP_SDES_NAME,
282 g_param_spec_string ("sdes-name", "SDES NAME",
283 "The NAME to put in SDES messages of this session",
284 DEFAULT_SDES_NAME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
286 g_object_class_install_property (gobject_class, PROP_SDES_EMAIL,
287 g_param_spec_string ("sdes-email", "SDES EMAIL",
288 "The EMAIL to put in SDES messages of this session",
289 DEFAULT_SDES_EMAIL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
291 g_object_class_install_property (gobject_class, PROP_SDES_PHONE,
292 g_param_spec_string ("sdes-phone", "SDES PHONE",
293 "The PHONE to put in SDES messages of this session",
294 DEFAULT_SDES_PHONE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
296 g_object_class_install_property (gobject_class, PROP_SDES_LOCATION,
297 g_param_spec_string ("sdes-location", "SDES LOCATION",
298 "The LOCATION to put in SDES messages of this session",
299 DEFAULT_SDES_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
301 g_object_class_install_property (gobject_class, PROP_SDES_TOOL,
302 g_param_spec_string ("sdes-tool", "SDES TOOL",
303 "The TOOL to put in SDES messages of this session",
304 DEFAULT_SDES_TOOL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
306 g_object_class_install_property (gobject_class, PROP_SDES_NOTE,
307 g_param_spec_string ("sdes-note", "SDES NOTE",
308 "The NOTE to put in SDES messages of this session",
309 DEFAULT_SDES_NOTE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
311 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
312 g_param_spec_uint ("num-sources", "Num Sources",
313 "The number of sources in the session", 0, G_MAXUINT,
314 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
316 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
317 g_param_spec_uint ("num-active-sources", "Num Active Sources",
318 "The number of active sources in the session", 0, G_MAXUINT,
319 DEFAULT_NUM_ACTIVE_SOURCES,
320 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
324 * Get a GValue Array of all sources in the session.
327 * <title>Getting the #RTPSources of a session
334 * g_object_get (sess, "sources", &arr, NULL);
336 * for (i = 0; i < arr->n_values; i++) {
339 * val = g_value_array_get_nth (arr, i);
340 * source = g_value_get_object (val);
342 * g_value_array_free (arr);
347 g_object_class_install_property (gobject_class, PROP_SOURCES,
348 g_param_spec_boxed ("sources", "Sources",
349 "An array of all known sources in the session",
350 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
352 klass->get_source_by_ssrc =
353 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
355 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
359 rtp_session_init (RTPSession * sess)
364 sess->lock = g_mutex_new ();
365 sess->key = g_random_int ();
369 for (i = 0; i < 32; i++) {
371 g_hash_table_new_full (NULL, NULL, NULL,
372 (GDestroyNotify) g_object_unref);
374 sess->cnames = g_hash_table_new_full (NULL, NULL, g_free, NULL);
376 rtp_stats_init_defaults (&sess->stats);
378 /* create an active SSRC for this session manager */
379 sess->source = rtp_session_create_source (sess);
380 sess->source->validated = TRUE;
381 sess->source->internal = TRUE;
382 sess->stats.active_sources++;
384 /* default UDP header length */
385 sess->header_len = 28;
386 sess->mtu = DEFAULT_RTCP_MTU;
388 /* some default SDES entries */
389 str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
390 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_CNAME, str);
393 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_NAME,
395 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_TOOL, "GStreamer");
397 sess->first_rtcp = TRUE;
399 GST_DEBUG ("%p: session using SSRC: %08x", sess, sess->source->ssrc);
403 rtp_session_finalize (GObject * object)
408 sess = RTP_SESSION_CAST (object);
410 g_mutex_free (sess->lock);
411 for (i = 0; i < 32; i++)
412 g_hash_table_destroy (sess->ssrcs[i]);
414 g_free (sess->bye_reason);
416 g_hash_table_destroy (sess->cnames);
417 g_object_unref (sess->source);
419 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
423 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
425 GValue value = { 0 };
427 g_value_init (&value, RTP_TYPE_SOURCE);
428 g_value_take_object (&value, source);
429 g_value_array_append (arr, &value);
433 rtp_session_create_sources (RTPSession * sess)
438 RTP_SESSION_LOCK (sess);
439 /* get number of elements in the table */
440 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
441 /* create the result value array */
442 res = g_value_array_new (size);
444 /* and copy all values into the array */
445 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
446 RTP_SESSION_UNLOCK (sess);
452 rtp_session_set_property (GObject * object, guint prop_id,
453 const GValue * value, GParamSpec * pspec)
457 sess = RTP_SESSION (object);
460 case PROP_INTERNAL_SSRC:
461 rtp_session_set_internal_ssrc (sess, g_value_get_uint (value));
464 rtp_session_set_bandwidth (sess, g_value_get_double (value));
466 case PROP_RTCP_FRACTION:
467 rtp_session_set_rtcp_fraction (sess, g_value_get_double (value));
470 sess->mtu = g_value_get_uint (value);
472 case PROP_SDES_CNAME:
473 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_CNAME,
474 g_value_get_string (value));
477 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_NAME,
478 g_value_get_string (value));
480 case PROP_SDES_EMAIL:
481 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_EMAIL,
482 g_value_get_string (value));
484 case PROP_SDES_PHONE:
485 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_PHONE,
486 g_value_get_string (value));
488 case PROP_SDES_LOCATION:
489 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_LOC,
490 g_value_get_string (value));
493 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_TOOL,
494 g_value_get_string (value));
497 rtp_session_set_sdes_string (sess, GST_RTCP_SDES_NOTE,
498 g_value_get_string (value));
501 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
507 rtp_session_get_property (GObject * object, guint prop_id,
508 GValue * value, GParamSpec * pspec)
512 sess = RTP_SESSION (object);
515 case PROP_INTERNAL_SSRC:
516 g_value_set_uint (value, rtp_session_get_internal_ssrc (sess));
518 case PROP_INTERNAL_SOURCE:
519 g_value_take_object (value, rtp_session_get_internal_source (sess));
522 g_value_set_double (value, rtp_session_get_bandwidth (sess));
524 case PROP_RTCP_FRACTION:
525 g_value_set_double (value, rtp_session_get_rtcp_fraction (sess));
528 g_value_set_uint (value, sess->mtu);
530 case PROP_SDES_CNAME:
531 g_value_take_string (value, rtp_session_get_sdes_string (sess,
532 GST_RTCP_SDES_CNAME));
535 g_value_take_string (value, rtp_session_get_sdes_string (sess,
536 GST_RTCP_SDES_NAME));
538 case PROP_SDES_EMAIL:
539 g_value_take_string (value, rtp_session_get_sdes_string (sess,
540 GST_RTCP_SDES_EMAIL));
542 case PROP_SDES_PHONE:
543 g_value_take_string (value, rtp_session_get_sdes_string (sess,
544 GST_RTCP_SDES_PHONE));
546 case PROP_SDES_LOCATION:
547 g_value_take_string (value, rtp_session_get_sdes_string (sess,
551 g_value_take_string (value, rtp_session_get_sdes_string (sess,
552 GST_RTCP_SDES_TOOL));
555 g_value_take_string (value, rtp_session_get_sdes_string (sess,
556 GST_RTCP_SDES_NOTE));
558 case PROP_NUM_SOURCES:
559 g_value_set_uint (value, rtp_session_get_num_sources (sess));
561 case PROP_NUM_ACTIVE_SOURCES:
562 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
565 g_value_take_boxed (value, rtp_session_create_sources (sess));
568 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
574 on_new_ssrc (RTPSession * sess, RTPSource * source)
576 g_object_ref (source);
577 RTP_SESSION_UNLOCK (sess);
578 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
579 RTP_SESSION_LOCK (sess);
580 g_object_unref (source);
584 on_ssrc_collision (RTPSession * sess, RTPSource * source)
586 g_object_ref (source);
587 RTP_SESSION_UNLOCK (sess);
588 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
590 RTP_SESSION_LOCK (sess);
591 g_object_unref (source);
595 on_ssrc_validated (RTPSession * sess, RTPSource * source)
597 g_object_ref (source);
598 RTP_SESSION_UNLOCK (sess);
599 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
601 RTP_SESSION_LOCK (sess);
602 g_object_unref (source);
606 on_ssrc_active (RTPSession * sess, RTPSource * source)
608 g_object_ref (source);
609 RTP_SESSION_UNLOCK (sess);
610 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
611 RTP_SESSION_LOCK (sess);
612 g_object_unref (source);
616 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
618 g_object_ref (source);
619 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
620 RTP_SESSION_UNLOCK (sess);
621 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
622 RTP_SESSION_LOCK (sess);
623 g_object_unref (source);
627 on_bye_ssrc (RTPSession * sess, RTPSource * source)
629 g_object_ref (source);
630 RTP_SESSION_UNLOCK (sess);
631 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
632 RTP_SESSION_LOCK (sess);
633 g_object_unref (source);
637 on_bye_timeout (RTPSession * sess, RTPSource * source)
639 g_object_ref (source);
640 RTP_SESSION_UNLOCK (sess);
641 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
642 RTP_SESSION_LOCK (sess);
643 g_object_unref (source);
647 on_timeout (RTPSession * sess, RTPSource * source)
649 g_object_ref (source);
650 RTP_SESSION_UNLOCK (sess);
651 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
652 RTP_SESSION_LOCK (sess);
653 g_object_unref (source);
657 on_sender_timeout (RTPSession * sess, RTPSource * source)
659 g_object_ref (source);
660 RTP_SESSION_UNLOCK (sess);
661 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
663 RTP_SESSION_LOCK (sess);
664 g_object_unref (source);
670 * Create a new session object.
672 * Returns: a new #RTPSession. g_object_unref() after usage.
675 rtp_session_new (void)
679 sess = g_object_new (RTP_TYPE_SESSION, NULL);
685 * rtp_session_set_callbacks:
686 * @sess: an #RTPSession
687 * @callbacks: callbacks to configure
688 * @user_data: user data passed in the callbacks
690 * Configure a set of callbacks to be notified of actions.
693 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
696 g_return_if_fail (RTP_IS_SESSION (sess));
698 if (callbacks->process_rtp) {
699 sess->callbacks.process_rtp = callbacks->process_rtp;
700 sess->process_rtp_user_data = user_data;
702 if (callbacks->send_rtp) {
703 sess->callbacks.send_rtp = callbacks->send_rtp;
704 sess->send_rtp_user_data = user_data;
706 if (callbacks->send_rtcp) {
707 sess->callbacks.send_rtcp = callbacks->send_rtcp;
708 sess->send_rtcp_user_data = user_data;
710 if (callbacks->sync_rtcp) {
711 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
712 sess->sync_rtcp_user_data = user_data;
714 if (callbacks->clock_rate) {
715 sess->callbacks.clock_rate = callbacks->clock_rate;
716 sess->clock_rate_user_data = user_data;
718 if (callbacks->reconsider) {
719 sess->callbacks.reconsider = callbacks->reconsider;
720 sess->reconsider_user_data = user_data;
725 * rtp_session_set_process_rtp_callback:
726 * @sess: an #RTPSession
727 * @callback: callback to set
728 * @user_data: user data passed in the callback
730 * Configure only the process_rtp callback to be notified of the process_rtp action.
733 rtp_session_set_process_rtp_callback (RTPSession * sess,
734 RTPSessionProcessRTP callback, gpointer user_data)
736 g_return_if_fail (RTP_IS_SESSION (sess));
738 sess->callbacks.process_rtp = callback;
739 sess->process_rtp_user_data = user_data;
743 * rtp_session_set_send_rtp_callback:
744 * @sess: an #RTPSession
745 * @callback: callback to set
746 * @user_data: user data passed in the callback
748 * Configure only the send_rtp callback to be notified of the send_rtp action.
751 rtp_session_set_send_rtp_callback (RTPSession * sess,
752 RTPSessionSendRTP callback, gpointer user_data)
754 g_return_if_fail (RTP_IS_SESSION (sess));
756 sess->callbacks.send_rtp = callback;
757 sess->send_rtp_user_data = user_data;
761 * rtp_session_set_send_rtcp_callback:
762 * @sess: an #RTPSession
763 * @callback: callback to set
764 * @user_data: user data passed in the callback
766 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
769 rtp_session_set_send_rtcp_callback (RTPSession * sess,
770 RTPSessionSendRTCP callback, gpointer user_data)
772 g_return_if_fail (RTP_IS_SESSION (sess));
774 sess->callbacks.send_rtcp = callback;
775 sess->send_rtcp_user_data = user_data;
779 * rtp_session_set_sync_rtcp_callback:
780 * @sess: an #RTPSession
781 * @callback: callback to set
782 * @user_data: user data passed in the callback
784 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
787 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
788 RTPSessionSyncRTCP callback, gpointer user_data)
790 g_return_if_fail (RTP_IS_SESSION (sess));
792 sess->callbacks.sync_rtcp = callback;
793 sess->sync_rtcp_user_data = user_data;
797 * rtp_session_set_clock_rate_callback:
798 * @sess: an #RTPSession
799 * @callback: callback to set
800 * @user_data: user data passed in the callback
802 * Configure only the clock_rate callback to be notified of the clock_rate action.
805 rtp_session_set_clock_rate_callback (RTPSession * sess,
806 RTPSessionClockRate callback, gpointer user_data)
808 g_return_if_fail (RTP_IS_SESSION (sess));
810 sess->callbacks.clock_rate = callback;
811 sess->clock_rate_user_data = user_data;
815 * rtp_session_set_reconsider_callback:
816 * @sess: an #RTPSession
817 * @callback: callback to set
818 * @user_data: user data passed in the callback
820 * Configure only the reconsider callback to be notified of the reconsider action.
823 rtp_session_set_reconsider_callback (RTPSession * sess,
824 RTPSessionReconsider callback, gpointer user_data)
826 g_return_if_fail (RTP_IS_SESSION (sess));
828 sess->callbacks.reconsider = callback;
829 sess->reconsider_user_data = user_data;
833 * rtp_session_set_bandwidth:
834 * @sess: an #RTPSession
835 * @bandwidth: the bandwidth allocated
837 * Set the session bandwidth in bytes per second.
840 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
842 g_return_if_fail (RTP_IS_SESSION (sess));
844 RTP_SESSION_LOCK (sess);
845 sess->stats.bandwidth = bandwidth;
846 RTP_SESSION_UNLOCK (sess);
850 * rtp_session_get_bandwidth:
851 * @sess: an #RTPSession
853 * Get the session bandwidth.
855 * Returns: the session bandwidth.
858 rtp_session_get_bandwidth (RTPSession * sess)
862 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
864 RTP_SESSION_LOCK (sess);
865 result = sess->stats.bandwidth;
866 RTP_SESSION_UNLOCK (sess);
872 * rtp_session_set_rtcp_fraction:
873 * @sess: an #RTPSession
874 * @bandwidth: the RTCP bandwidth
876 * Set the bandwidth that should be used for RTCP
880 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
882 g_return_if_fail (RTP_IS_SESSION (sess));
884 RTP_SESSION_LOCK (sess);
885 sess->stats.rtcp_bandwidth = bandwidth;
886 RTP_SESSION_UNLOCK (sess);
890 * rtp_session_get_rtcp_fraction:
891 * @sess: an #RTPSession
893 * Get the session bandwidth used for RTCP.
895 * Returns: The bandwidth used for RTCP messages.
898 rtp_session_get_rtcp_fraction (RTPSession * sess)
902 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
904 RTP_SESSION_LOCK (sess);
905 result = sess->stats.rtcp_bandwidth;
906 RTP_SESSION_UNLOCK (sess);
912 * rtp_session_set_sdes_string:
913 * @sess: an #RTPSession
914 * @type: the type of the SDES item
915 * @item: a null-terminated string to set.
917 * Store an SDES item of @type in @sess.
919 * Returns: %FALSE if the data was unchanged @type is invalid.
922 rtp_session_set_sdes_string (RTPSession * sess, GstRTCPSDESType type,
927 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
929 RTP_SESSION_LOCK (sess);
930 result = rtp_source_set_sdes_string (sess->source, type, item);
931 RTP_SESSION_UNLOCK (sess);
937 * rtp_session_get_sdes_string:
938 * @sess: an #RTPSession
939 * @type: the type of the SDES item
941 * Get the SDES item of @type from @sess.
943 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
944 * valid. g_free() after usage.
947 rtp_session_get_sdes_string (RTPSession * sess, GstRTCPSDESType type)
951 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
953 RTP_SESSION_LOCK (sess);
954 result = rtp_source_get_sdes_string (sess->source, type);
955 RTP_SESSION_UNLOCK (sess);
961 source_push_rtp (RTPSource * source, GstBuffer * buffer, RTPSession * session)
963 GstFlowReturn result = GST_FLOW_OK;
965 if (source == session->source) {
966 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
968 RTP_SESSION_UNLOCK (session);
970 if (session->callbacks.send_rtp)
972 session->callbacks.send_rtp (session, source, buffer,
973 session->send_rtp_user_data);
975 gst_buffer_unref (buffer);
978 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
979 RTP_SESSION_UNLOCK (session);
981 if (session->callbacks.process_rtp)
983 session->callbacks.process_rtp (session, source, buffer,
984 session->process_rtp_user_data);
986 gst_buffer_unref (buffer);
988 RTP_SESSION_LOCK (session);
994 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
998 RTP_SESSION_UNLOCK (session);
1000 if (session->callbacks.clock_rate)
1002 session->callbacks.clock_rate (session, pt,
1003 session->clock_rate_user_data);
1007 RTP_SESSION_LOCK (session);
1009 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1014 static RTPSourceCallbacks callbacks = {
1015 (RTPSourcePushRTP) source_push_rtp,
1016 (RTPSourceClockRate) source_clock_rate,
1020 * find_add_conflicting_addresses:
1021 * @sess: The session to check in
1022 * @arrival: The arrival stats for the buffer
1024 * Checks if an address which has a conflict is already known,
1025 * otherwise remembers it to prevent loops.
1027 * Returns: TRUE if it was a known conflict, FALSE otherwise
1031 find_add_conflicting_addresses (RTPSession * sess, RTPArrivalStats * arrival)
1034 RTPConflictingAddress *new_conflict;
1036 for (item = g_list_first (sess->conflicting_addresses);
1037 item; item = g_list_next (item)) {
1038 RTPConflictingAddress *known_conflict = item->data;
1040 if (gst_netaddress_equal (&arrival->address, &known_conflict->address)) {
1041 known_conflict->time = arrival->time;
1046 new_conflict = g_new0 (RTPConflictingAddress, 1);
1048 memcpy (&new_conflict->address, &arrival->address, sizeof (GstNetAddress));
1049 new_conflict->time = arrival->time;
1051 sess->conflicting_addresses = g_list_prepend (sess->conflicting_addresses,
1058 check_collision (RTPSession * sess, RTPSource * source,
1059 RTPArrivalStats * arrival, gboolean rtp)
1061 /* If we have no arrival address, we can't do collision checking */
1062 if (!arrival->have_address)
1065 if (sess->source != source) {
1066 /* This is not our local source, but lets check if two remote
1070 if (source->have_rtp_from) {
1071 if (gst_netaddress_equal (&source->rtp_from, &arrival->address))
1072 /* Address is the same */
1075 /* We don't already have a from address for RTP, just set it */
1076 rtp_source_set_rtp_from (source, &arrival->address);
1080 if (source->have_rtcp_from) {
1081 if (gst_netaddress_equal (&source->rtcp_from, &arrival->address))
1082 /* Address is the same */
1085 /* We don't already have a from address for RTCP, just set it */
1086 rtp_source_set_rtcp_from (source, &arrival->address);
1090 /* We received RTP or RTCP from this source before but the network address
1091 * changed. In this case, we have third-party collision or loop */
1092 GST_DEBUG ("we have a third-party collision or loop");
1094 /* FIXME: Log 3rd party collision somehow
1095 * Maybe should be done in upper layer, only the SDES can tell us
1096 * if its a collision or a loop
1099 /* This is sending with our ssrc, is it an address we already know */
1101 if (find_add_conflicting_addresses (sess, arrival)) {
1102 /* Its a known conflict, its probably a loop, not a collision
1103 * lets just drop the incoming packet
1105 GST_DEBUG ("Our packets are being looped back to us, dropping");
1107 /* Its a new collision, lets change our SSRC */
1109 GST_DEBUG ("Collision for SSRC %x", rtp_source_get_ssrc (source));
1110 on_ssrc_collision (sess, source);
1112 rtp_session_schedule_bye_locked (sess, "SSRC Collision", arrival->time);
1114 sess->change_ssrc = TRUE;
1122 /* must be called with the session lock, the returned source needs to be
1123 * unreffed after usage. */
1125 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1126 RTPArrivalStats * arrival, gboolean rtp)
1131 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1132 if (source == NULL) {
1133 /* make new Source in probation and insert */
1134 source = rtp_source_new (ssrc);
1136 /* for RTP packets we need to set the source in probation. Receiving RTCP
1137 * packets of an SSRC, on the other hand, is a strong indication that we
1138 * are dealing with a valid source. */
1140 source->probation = RTP_DEFAULT_PROBATION;
1142 source->probation = 0;
1144 /* store from address, if any */
1145 if (arrival->have_address) {
1147 rtp_source_set_rtp_from (source, &arrival->address);
1149 rtp_source_set_rtcp_from (source, &arrival->address);
1152 /* configure a callback on the source */
1153 rtp_source_set_callbacks (source, &callbacks, sess);
1155 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1158 /* we have one more source now */
1159 sess->total_sources++;
1163 /* check for collision, this updates the address when not previously set */
1164 if (check_collision (sess, source, arrival, rtp)) {
1168 /* update last activity */
1169 source->last_activity = arrival->time;
1171 source->last_rtp_activity = arrival->time;
1172 g_object_ref (source);
1178 * rtp_session_get_internal_source:
1179 * @sess: a #RTPSession
1181 * Get the internal #RTPSource of @sess.
1183 * Returns: The internal #RTPSource. g_object_unref() after usage.
1186 rtp_session_get_internal_source (RTPSession * sess)
1190 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1192 result = g_object_ref (sess->source);
1198 * rtp_session_set_internal_ssrc:
1199 * @sess: a #RTPSession
1202 * Set the SSRC of @sess to @ssrc.
1205 rtp_session_set_internal_ssrc (RTPSession * sess, guint32 ssrc)
1207 RTP_SESSION_LOCK (sess);
1208 if (ssrc != sess->source->ssrc) {
1209 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
1210 GINT_TO_POINTER (sess->source->ssrc));
1212 GST_DEBUG ("setting internal SSRC to %08x", ssrc);
1213 /* After this call, any receiver of the old SSRC either in RTP or RTCP
1214 * packets will timeout on the old SSRC, we could potentially schedule a
1215 * BYE RTCP for the old SSRC... */
1216 sess->source->ssrc = ssrc;
1217 rtp_source_reset (sess->source);
1219 /* rehash with the new SSRC */
1220 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1221 GINT_TO_POINTER (sess->source->ssrc), sess->source);
1223 RTP_SESSION_UNLOCK (sess);
1227 * rtp_session_get_internal_ssrc:
1228 * @sess: a #RTPSession
1230 * Get the internal SSRC of @sess.
1232 * Returns: The SSRC of the session.
1235 rtp_session_get_internal_ssrc (RTPSession * sess)
1239 RTP_SESSION_LOCK (sess);
1240 ssrc = sess->source->ssrc;
1241 RTP_SESSION_UNLOCK (sess);
1247 * rtp_session_add_source:
1248 * @sess: a #RTPSession
1249 * @src: #RTPSource to add
1251 * Add @src to @session.
1253 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1254 * existed in the session.
1257 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1259 gboolean result = FALSE;
1262 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1263 g_return_val_if_fail (src != NULL, FALSE);
1265 RTP_SESSION_LOCK (sess);
1267 g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1268 GINT_TO_POINTER (src->ssrc));
1270 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1271 GINT_TO_POINTER (src->ssrc), src);
1272 /* we have one more source now */
1273 sess->total_sources++;
1276 RTP_SESSION_UNLOCK (sess);
1282 * rtp_session_get_num_sources:
1283 * @sess: an #RTPSession
1285 * Get the number of sources in @sess.
1287 * Returns: The number of sources in @sess.
1290 rtp_session_get_num_sources (RTPSession * sess)
1294 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1296 RTP_SESSION_LOCK (sess);
1297 result = sess->total_sources;
1298 RTP_SESSION_UNLOCK (sess);
1304 * rtp_session_get_num_active_sources:
1305 * @sess: an #RTPSession
1307 * Get the number of active sources in @sess. A source is considered active when
1308 * it has been validated and has not yet received a BYE RTCP message.
1310 * Returns: The number of active sources in @sess.
1313 rtp_session_get_num_active_sources (RTPSession * sess)
1317 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1319 RTP_SESSION_LOCK (sess);
1320 result = sess->stats.active_sources;
1321 RTP_SESSION_UNLOCK (sess);
1327 * rtp_session_get_source_by_ssrc:
1328 * @sess: an #RTPSession
1331 * Find the source with @ssrc in @sess.
1333 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1334 * g_object_unref() after usage.
1337 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1341 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1343 RTP_SESSION_LOCK (sess);
1345 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1347 g_object_ref (result);
1348 RTP_SESSION_UNLOCK (sess);
1354 * rtp_session_get_source_by_cname:
1355 * @sess: a #RTPSession
1358 * Find the source with @cname in @sess.
1360 * Returns: a #RTPSource with CNAME @cname or NULL if the source was not found.
1361 * g_object_unref() after usage.
1364 rtp_session_get_source_by_cname (RTPSession * sess, const gchar * cname)
1368 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1369 g_return_val_if_fail (cname != NULL, NULL);
1371 RTP_SESSION_LOCK (sess);
1372 result = g_hash_table_lookup (sess->cnames, cname);
1374 g_object_ref (result);
1375 RTP_SESSION_UNLOCK (sess);
1381 rtp_session_create_new_ssrc (RTPSession * sess)
1386 ssrc = g_random_int ();
1388 /* see if it exists in the session, we're done if it doesn't */
1389 if (g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1390 GINT_TO_POINTER (ssrc)) == NULL)
1399 * rtp_session_create_source:
1400 * @sess: an #RTPSession
1402 * Create an #RTPSource for use in @sess. This function will create a source
1403 * with an ssrc that is currently not used by any participants in the session.
1405 * Returns: an #RTPSource.
1408 rtp_session_create_source (RTPSession * sess)
1413 RTP_SESSION_LOCK (sess);
1414 ssrc = rtp_session_create_new_ssrc (sess);
1415 source = rtp_source_new (ssrc);
1416 rtp_source_set_callbacks (source, &callbacks, sess);
1417 /* we need an additional ref for the source in the hashtable */
1418 g_object_ref (source);
1419 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1421 /* we have one more source now */
1422 sess->total_sources++;
1423 RTP_SESSION_UNLOCK (sess);
1428 /* update the RTPArrivalStats structure with the current time and other bits
1429 * about the current buffer we are handling.
1430 * This function is typically called when a validated packet is received.
1431 * This function should be called with the SESSION_LOCK
1434 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
1435 gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
1436 GstClockTime running_time, guint64 ntpnstime)
1438 /* get time of arrival */
1439 arrival->time = current_time;
1440 arrival->running_time = running_time;
1441 arrival->ntpnstime = ntpnstime;
1443 /* get packet size including header overhead */
1444 arrival->bytes = GST_BUFFER_SIZE (buffer) + sess->header_len;
1447 arrival->payload_len = gst_rtp_buffer_get_payload_len (buffer);
1449 arrival->payload_len = 0;
1452 /* for netbuffer we can store the IP address to check for collisions */
1453 arrival->have_address = GST_IS_NETBUFFER (buffer);
1454 if (arrival->have_address) {
1455 GstNetBuffer *netbuf = (GstNetBuffer *) buffer;
1457 memcpy (&arrival->address, &netbuf->from, sizeof (GstNetAddress));
1462 * rtp_session_process_rtp:
1463 * @sess: and #RTPSession
1464 * @buffer: an RTP buffer
1465 * @current_time: the current system time
1466 * @ntpnstime: the NTP arrival time in nanoseconds
1468 * Process an RTP buffer in the session manager. This function takes ownership
1471 * Returns: a #GstFlowReturn.
1474 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1475 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
1477 GstFlowReturn result;
1481 gboolean prevsender, prevactive;
1482 RTPArrivalStats arrival;
1484 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1485 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1487 if (!gst_rtp_buffer_validate (buffer))
1488 goto invalid_packet;
1490 RTP_SESSION_LOCK (sess);
1491 /* update arrival stats */
1492 update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1493 running_time, ntpnstime);
1495 /* ignore more RTP packets when we left the session */
1496 if (sess->source->received_bye)
1499 /* get SSRC and look up in session database */
1500 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1501 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1505 prevsender = RTP_SOURCE_IS_SENDER (source);
1506 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1508 /* we need to ref so that we can process the CSRCs later */
1509 gst_buffer_ref (buffer);
1511 /* let source process the packet */
1512 result = rtp_source_process_rtp (source, buffer, &arrival);
1514 /* source became active */
1515 if (prevactive != RTP_SOURCE_IS_ACTIVE (source)) {
1516 sess->stats.active_sources++;
1517 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1518 sess->stats.active_sources);
1519 on_ssrc_validated (sess, source);
1521 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1522 sess->stats.sender_sources++;
1523 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1524 sess->stats.sender_sources);
1528 on_new_ssrc (sess, source);
1530 if (source->validated) {
1534 /* for validated sources, we add the CSRCs as well */
1535 count = gst_rtp_buffer_get_csrc_count (buffer);
1537 for (i = 0; i < count; i++) {
1539 RTPSource *csrc_src;
1541 csrc = gst_rtp_buffer_get_csrc (buffer, i);
1544 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1549 GST_DEBUG ("created new CSRC: %08x", csrc);
1550 rtp_source_set_as_csrc (csrc_src);
1551 if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1552 sess->stats.active_sources++;
1553 on_new_ssrc (sess, csrc_src);
1555 g_object_unref (csrc_src);
1558 g_object_unref (source);
1559 gst_buffer_unref (buffer);
1561 RTP_SESSION_UNLOCK (sess);
1568 gst_buffer_unref (buffer);
1569 GST_DEBUG ("invalid RTP packet received");
1574 gst_buffer_unref (buffer);
1575 RTP_SESSION_UNLOCK (sess);
1576 GST_DEBUG ("ignoring RTP packet because we are leaving");
1581 gst_buffer_unref (buffer);
1582 RTP_SESSION_UNLOCK (sess);
1583 GST_DEBUG ("ignoring packet because its collisioning");
1589 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1590 GstRTCPPacket * packet, RTPArrivalStats * arrival)
1594 count = gst_rtcp_packet_get_rb_count (packet);
1595 for (i = 0; i < count; i++) {
1596 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1597 guint8 fractionlost;
1600 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1601 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1603 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1605 if (ssrc == sess->source->ssrc) {
1606 /* only deal with report blocks for our session, we update the stats of
1607 * the sender of the RTCP message. We could also compare our stats against
1608 * the other sender to see if we are better or worse. */
1609 rtp_source_process_rb (source, arrival->time, fractionlost, packetslost,
1610 exthighestseq, jitter, lsr, dlsr);
1612 on_ssrc_active (sess, source);
1617 /* A Sender report contains statistics about how the sender is doing. This
1618 * includes timing informataion such as the relation between RTP and NTP
1619 * timestamps and the number of packets/bytes it sent to us.
1621 * In this report is also included a set of report blocks related to how this
1622 * sender is receiving data (in case we (or somebody else) is also sending stuff
1623 * to it). This info includes the packet loss, jitter and seqnum. It also
1624 * contains information to calculate the round trip time (LSR/DLSR).
1627 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1628 RTPArrivalStats * arrival)
1630 guint32 senderssrc, rtptime, packet_count, octet_count;
1633 gboolean created, prevsender;
1635 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1636 &packet_count, &octet_count);
1638 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1639 senderssrc, GST_TIME_ARGS (arrival->time));
1641 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1645 prevsender = RTP_SOURCE_IS_SENDER (source);
1647 /* first update the source */
1648 rtp_source_process_sr (source, arrival->time, ntptime, rtptime, packet_count,
1651 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1652 sess->stats.sender_sources++;
1653 GST_DEBUG ("source: %08x became sender, %d sender sources", senderssrc,
1654 sess->stats.sender_sources);
1658 on_new_ssrc (sess, source);
1660 rtp_session_process_rb (sess, source, packet, arrival);
1661 g_object_unref (source);
1664 /* A receiver report contains statistics about how a receiver is doing. It
1665 * includes stuff like packet loss, jitter and the seqnum it received last. It
1666 * also contains info to calculate the round trip time.
1668 * We are only interested in how the sender of this report is doing wrt to us.
1671 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1672 RTPArrivalStats * arrival)
1678 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1680 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1682 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1687 on_new_ssrc (sess, source);
1689 rtp_session_process_rb (sess, source, packet, arrival);
1690 g_object_unref (source);
1693 /* Get SDES items and store them in the SSRC */
1695 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1696 RTPArrivalStats * arrival)
1699 gboolean more_items, more_entries;
1701 items = gst_rtcp_packet_sdes_get_item_count (packet);
1702 GST_DEBUG ("got SDES packet with %d items", items);
1704 more_items = gst_rtcp_packet_sdes_first_item (packet);
1706 while (more_items) {
1708 gboolean changed, created;
1711 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1713 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1717 /* find src, no probation when dealing with RTCP */
1718 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1722 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1724 while (more_entries) {
1725 GstRTCPSDESType type;
1729 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1731 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1734 changed |= rtp_source_set_sdes (source, type, data, len);
1736 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1740 source->validated = TRUE;
1743 on_new_ssrc (sess, source);
1745 on_ssrc_sdes (sess, source);
1747 g_object_unref (source);
1749 more_items = gst_rtcp_packet_sdes_next_item (packet);
1754 /* BYE is sent when a client leaves the session
1757 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
1758 RTPArrivalStats * arrival)
1763 reason = gst_rtcp_packet_bye_get_reason (packet);
1764 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
1766 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
1767 for (i = 0; i < count; i++) {
1770 gboolean created, prevactive, prevsender;
1771 guint pmembers, members;
1773 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
1774 GST_DEBUG ("SSRC: %08x", ssrc);
1776 /* find src and mark bye, no probation when dealing with RTCP */
1777 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1781 /* store time for when we need to time out this source */
1782 source->bye_time = arrival->time;
1784 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1785 prevsender = RTP_SOURCE_IS_SENDER (source);
1787 /* let the source handle the rest */
1788 rtp_source_process_bye (source, reason);
1790 pmembers = sess->stats.active_sources;
1792 if (prevactive && !RTP_SOURCE_IS_ACTIVE (source)) {
1793 sess->stats.active_sources--;
1794 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
1795 sess->stats.active_sources);
1797 if (prevsender && !RTP_SOURCE_IS_SENDER (source)) {
1798 sess->stats.sender_sources--;
1799 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
1800 sess->stats.sender_sources);
1802 members = sess->stats.active_sources;
1804 if (!sess->source->received_bye && members < pmembers) {
1805 /* some members went away since the previous timeout estimate.
1806 * Perform reverse reconsideration but only when we are not scheduling a
1808 if (arrival->time < sess->next_rtcp_check_time) {
1809 GstClockTime time_remaining;
1811 time_remaining = sess->next_rtcp_check_time - arrival->time;
1812 sess->next_rtcp_check_time =
1813 gst_util_uint64_scale (time_remaining, members, pmembers);
1815 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
1816 GST_TIME_ARGS (sess->next_rtcp_check_time));
1818 sess->next_rtcp_check_time += arrival->time;
1820 RTP_SESSION_UNLOCK (sess);
1821 /* notify app of reconsideration */
1822 if (sess->callbacks.reconsider)
1823 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
1824 RTP_SESSION_LOCK (sess);
1829 on_new_ssrc (sess, source);
1831 on_bye_ssrc (sess, source);
1833 g_object_unref (source);
1839 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
1840 RTPArrivalStats * arrival)
1842 GST_DEBUG ("received APP");
1846 * rtp_session_process_rtcp:
1847 * @sess: and #RTPSession
1848 * @buffer: an RTCP buffer
1849 * @current_time: the current system time
1851 * Process an RTCP buffer in the session manager. This function takes ownership
1854 * Returns: a #GstFlowReturn.
1857 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
1858 GstClockTime current_time)
1860 GstRTCPPacket packet;
1861 gboolean more, is_bye = FALSE, is_sr = FALSE;
1862 RTPArrivalStats arrival;
1863 GstFlowReturn result = GST_FLOW_OK;
1865 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1866 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1868 if (!gst_rtcp_buffer_validate (buffer))
1869 goto invalid_packet;
1871 GST_DEBUG ("received RTCP packet");
1873 RTP_SESSION_LOCK (sess);
1874 /* update arrival stats */
1875 update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1, -1);
1880 /* make writable, we might want to change the buffer */
1881 buffer = gst_buffer_make_metadata_writable (buffer);
1883 /* start processing the compound packet */
1884 more = gst_rtcp_buffer_get_first_packet (buffer, &packet);
1888 type = gst_rtcp_packet_get_type (&packet);
1890 /* when we are leaving the session, we should ignore all non-BYE messages */
1891 if (sess->source->received_bye && type != GST_RTCP_TYPE_BYE) {
1892 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
1897 case GST_RTCP_TYPE_SR:
1898 rtp_session_process_sr (sess, &packet, &arrival);
1901 case GST_RTCP_TYPE_RR:
1902 rtp_session_process_rr (sess, &packet, &arrival);
1904 case GST_RTCP_TYPE_SDES:
1905 rtp_session_process_sdes (sess, &packet, &arrival);
1907 case GST_RTCP_TYPE_BYE:
1909 rtp_session_process_bye (sess, &packet, &arrival);
1911 case GST_RTCP_TYPE_APP:
1912 rtp_session_process_app (sess, &packet, &arrival);
1915 GST_WARNING ("got unknown RTCP packet");
1919 more = gst_rtcp_packet_move_to_next (&packet);
1922 /* if we are scheduling a BYE, we only want to count bye packets, else we
1923 * count everything */
1924 if (sess->source->received_bye) {
1926 sess->stats.bye_members++;
1927 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
1930 /* keep track of average packet size */
1931 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
1933 RTP_SESSION_UNLOCK (sess);
1935 /* notify caller of sr packets in the callback */
1936 if (is_sr && sess->callbacks.sync_rtcp)
1937 result = sess->callbacks.sync_rtcp (sess, sess->source, buffer,
1938 sess->sync_rtcp_user_data);
1940 gst_buffer_unref (buffer);
1947 GST_DEBUG ("invalid RTCP packet received");
1948 gst_buffer_unref (buffer);
1953 gst_buffer_unref (buffer);
1954 RTP_SESSION_UNLOCK (sess);
1955 GST_DEBUG ("ignoring RTP packet because we left");
1961 * rtp_session_send_rtp:
1962 * @sess: an #RTPSession
1963 * @buffer: an RTP buffer
1964 * @current_time: the current system time
1965 * @ntpnstime: the NTP time in nanoseconds of when this buffer was captured.
1966 * This is the buffer timestamp converted to NTP time.
1968 * Send the RTP buffer in the session manager. This function takes ownership of
1971 * Returns: a #GstFlowReturn.
1974 rtp_session_send_rtp (RTPSession * sess, GstBuffer * buffer,
1975 GstClockTime current_time, guint64 ntpnstime)
1977 GstFlowReturn result;
1979 gboolean prevsender;
1981 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1982 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1984 if (!gst_rtp_buffer_validate (buffer))
1985 goto invalid_packet;
1987 GST_LOG ("received RTP packet for sending");
1989 RTP_SESSION_LOCK (sess);
1990 source = sess->source;
1992 /* update last activity */
1993 source->last_rtp_activity = current_time;
1995 prevsender = RTP_SOURCE_IS_SENDER (source);
1997 /* we use our own source to send */
1998 result = rtp_source_send_rtp (source, buffer, ntpnstime);
2000 if (RTP_SOURCE_IS_SENDER (source) && !prevsender)
2001 sess->stats.sender_sources++;
2002 RTP_SESSION_UNLOCK (sess);
2009 gst_buffer_unref (buffer);
2010 GST_DEBUG ("invalid RTP packet received");
2016 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2019 GstClockTime result;
2021 if (sess->source->received_bye) {
2022 result = rtp_stats_calculate_bye_interval (&sess->stats);
2024 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2025 RTP_SOURCE_IS_SENDER (sess->source), first);
2028 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2029 GST_TIME_ARGS (result), first);
2032 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2034 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2039 /* Stop the current @sess and schedule a BYE message for the other members.
2040 * One must have the session lock to call this function
2042 static GstFlowReturn
2043 rtp_session_schedule_bye_locked (RTPSession * sess, const gchar * reason,
2044 GstClockTime current_time)
2046 GstFlowReturn result = GST_FLOW_OK;
2048 GstClockTime interval;
2050 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2052 source = sess->source;
2054 /* ignore more BYEs */
2055 if (source->received_bye)
2058 /* we have BYE now */
2059 source->received_bye = TRUE;
2060 /* at least one member wants to send a BYE */
2061 g_free (sess->bye_reason);
2062 sess->bye_reason = g_strdup (reason);
2063 sess->stats.avg_rtcp_packet_size = 100;
2064 sess->stats.bye_members = 1;
2065 sess->first_rtcp = TRUE;
2066 sess->sent_bye = FALSE;
2068 /* reschedule transmission */
2069 sess->last_rtcp_send_time = current_time;
2070 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2071 sess->next_rtcp_check_time = current_time + interval;
2073 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2074 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2076 RTP_SESSION_UNLOCK (sess);
2077 /* notify app of reconsideration */
2078 if (sess->callbacks.reconsider)
2079 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2080 RTP_SESSION_LOCK (sess);
2087 * rtp_session_schedule_bye:
2088 * @sess: an #RTPSession
2089 * @reason: a reason or NULL
2090 * @current_time: the current system time
2092 * Stop the current @sess and schedule a BYE message for the other members.
2094 * Returns: a #GstFlowReturn.
2097 rtp_session_schedule_bye (RTPSession * sess, const gchar * reason,
2098 GstClockTime current_time)
2100 GstFlowReturn result = GST_FLOW_OK;
2102 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2104 RTP_SESSION_LOCK (sess);
2105 result = rtp_session_schedule_bye_locked (sess, reason, current_time);
2106 RTP_SESSION_UNLOCK (sess);
2112 * rtp_session_next_timeout:
2113 * @sess: an #RTPSession
2114 * @current_time: the current system time
2116 * Get the next time we should perform session maintenance tasks.
2118 * Returns: a time when rtp_session_on_timeout() should be called with the
2119 * current system time.
2122 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2124 GstClockTime result;
2126 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2128 RTP_SESSION_LOCK (sess);
2130 result = sess->next_rtcp_check_time;
2132 GST_DEBUG ("current time: %" GST_TIME_FORMAT ", next :%" GST_TIME_FORMAT,
2133 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2135 if (result < current_time) {
2136 GST_DEBUG ("take current time as base");
2137 /* our previous check time expired, start counting from the current time
2139 result = current_time;
2142 if (sess->source->received_bye) {
2143 if (sess->sent_bye) {
2144 GST_DEBUG ("we sent BYE already");
2145 result = GST_CLOCK_TIME_NONE;
2146 } else if (sess->stats.active_sources >= 50) {
2147 GST_DEBUG ("reconsider BYE, more than 50 sources");
2148 /* reconsider BYE if members >= 50 */
2149 result += calculate_rtcp_interval (sess, FALSE, TRUE);
2152 if (sess->first_rtcp) {
2153 GST_DEBUG ("first RTCP packet");
2154 /* we are called for the first time */
2155 result += calculate_rtcp_interval (sess, FALSE, TRUE);
2156 } else if (sess->next_rtcp_check_time < current_time) {
2157 GST_DEBUG ("old check time expired, getting new timeout");
2158 /* get a new timeout when we need to */
2159 result += calculate_rtcp_interval (sess, FALSE, FALSE);
2162 sess->next_rtcp_check_time = result;
2164 GST_DEBUG ("next timeout: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2165 RTP_SESSION_UNLOCK (sess);
2174 GstClockTime current_time;
2176 GstClockTime interval;
2177 GstRTCPPacket packet;
2183 session_start_rtcp (RTPSession * sess, ReportData * data)
2185 GstRTCPPacket *packet = &data->packet;
2186 RTPSource *own = sess->source;
2188 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2190 if (RTP_SOURCE_IS_SENDER (own)) {
2193 guint32 packet_count, octet_count;
2195 /* we are a sender, create SR */
2196 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2197 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SR, packet);
2199 /* get latest stats */
2200 rtp_source_get_new_sr (own, data->ntpnstime, &ntptime, &rtptime,
2201 &packet_count, &octet_count);
2203 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2204 packet_count, octet_count);
2206 /* fill in sender report info */
2207 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2208 ntptime, rtptime, packet_count, octet_count);
2210 /* we are only receiver, create RR */
2211 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2212 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_RR, packet);
2213 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2217 /* construct a Sender or Receiver Report */
2219 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2221 RTPSession *sess = data->sess;
2222 GstRTCPPacket *packet = &data->packet;
2224 /* create a new buffer if needed */
2225 if (data->rtcp == NULL) {
2226 session_start_rtcp (sess, data);
2228 if (gst_rtcp_packet_get_rb_count (packet) < GST_RTCP_MAX_RB_COUNT) {
2229 /* only report about other sender sources */
2230 if (source != sess->source && RTP_SOURCE_IS_SENDER (source)) {
2231 guint8 fractionlost;
2233 guint32 exthighestseq, jitter;
2237 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2238 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2240 /* packet is not yet filled, add report block for this source. */
2241 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2242 exthighestseq, jitter, lsr, dlsr);
2247 /* perform cleanup of sources that timed out */
2249 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2251 gboolean remove = FALSE;
2252 gboolean byetimeout = FALSE;
2253 gboolean sendertimeout = FALSE;
2254 gboolean is_sender, is_active;
2255 RTPSession *sess = data->sess;
2256 GstClockTime interval;
2258 is_sender = RTP_SOURCE_IS_SENDER (source);
2259 is_active = RTP_SOURCE_IS_ACTIVE (source);
2261 /* check for our own source, we don't want to delete our own source. */
2262 if (!(source == sess->source)) {
2263 if (source->received_bye) {
2264 /* if we received a BYE from the source, remove the source after some
2266 if (data->current_time > source->bye_time &&
2267 data->current_time - source->bye_time > sess->stats.bye_timeout) {
2268 GST_DEBUG ("removing BYE source %08x", source->ssrc);
2273 /* sources that were inactive for more than 5 times the deterministic reporting
2274 * interval get timed out. the min timeout is 5 seconds. */
2275 if (data->current_time > source->last_activity) {
2276 interval = MAX (data->interval * 5, 5 * GST_SECOND);
2277 if (data->current_time - source->last_activity > interval) {
2278 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2279 source->ssrc, GST_TIME_ARGS (source->last_activity));
2285 /* senders that did not send for a long time become a receiver, this also
2286 * holds for our own source. */
2288 if (data->current_time > source->last_rtp_activity) {
2289 interval = MAX (data->interval * 2, 5 * GST_SECOND);
2290 if (data->current_time - source->last_rtp_activity > interval) {
2291 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
2292 GST_TIME_FORMAT, source->ssrc,
2293 GST_TIME_ARGS (source->last_rtp_activity));
2294 source->is_sender = FALSE;
2295 sess->stats.sender_sources--;
2296 sendertimeout = TRUE;
2302 sess->total_sources--;
2304 sess->stats.sender_sources--;
2306 sess->stats.active_sources--;
2309 on_bye_timeout (sess, source);
2311 on_timeout (sess, source);
2314 on_sender_timeout (sess, source);
2320 session_sdes (RTPSession * sess, ReportData * data)
2322 GstRTCPPacket *packet = &data->packet;
2326 /* add SDES packet */
2327 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SDES, packet);
2329 gst_rtcp_packet_sdes_add_item (packet, sess->source->ssrc);
2331 rtp_source_get_sdes (sess->source, GST_RTCP_SDES_CNAME, &sdes_data,
2333 gst_rtcp_packet_sdes_add_entry (packet, GST_RTCP_SDES_CNAME, sdes_len,
2336 /* other SDES items must only be added at regular intervals and only when the
2337 * user requests to since it might be a privacy problem */
2339 gst_rtcp_packet_sdes_add_entry (&packet, GST_RTCP_SDES_NAME,
2340 strlen (sess->name), (guint8 *) sess->name);
2341 gst_rtcp_packet_sdes_add_entry (&packet, GST_RTCP_SDES_TOOL,
2342 strlen (sess->tool), (guint8 *) sess->tool);
2345 data->has_sdes = TRUE;
2348 /* schedule a BYE packet */
2350 session_bye (RTPSession * sess, ReportData * data)
2352 GstRTCPPacket *packet = &data->packet;
2355 session_start_rtcp (sess, data);
2358 session_sdes (sess, data);
2360 /* add a BYE packet */
2361 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_BYE, packet);
2362 gst_rtcp_packet_bye_add_ssrc (packet, sess->source->ssrc);
2363 if (sess->bye_reason)
2364 gst_rtcp_packet_bye_set_reason (packet, sess->bye_reason);
2366 /* we have a BYE packet now */
2367 data->is_bye = TRUE;
2371 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
2373 GstClockTime new_send_time, elapsed;
2376 /* no need to check yet */
2377 if (sess->next_rtcp_check_time > current_time) {
2378 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
2379 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
2380 GST_TIME_ARGS (current_time));
2384 /* get elapsed time since we last reported */
2385 elapsed = current_time - sess->last_rtcp_send_time;
2387 /* perform forward reconsideration */
2388 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, data->interval);
2390 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
2391 GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time), GST_TIME_ARGS (elapsed));
2393 new_send_time += sess->last_rtcp_send_time;
2395 /* check if reconsideration */
2396 if (current_time < new_send_time) {
2397 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
2398 GST_TIME_ARGS (new_send_time));
2400 /* store new check time */
2401 sess->next_rtcp_check_time = new_send_time;
2404 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
2406 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
2407 GST_TIME_ARGS (new_send_time));
2408 sess->next_rtcp_check_time = current_time + new_send_time;
2414 * rtp_session_on_timeout:
2415 * @sess: an #RTPSession
2416 * @current_time: the current system time
2417 * @ntpnstime: the current NTP time in nanoseconds
2419 * Perform maintenance actions after the timeout obtained with
2420 * rtp_session_next_timeout() expired.
2422 * This function will perform timeouts of receivers and senders, send a BYE
2423 * packet or generate RTCP packets with current session stats.
2425 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
2426 * times, for each packet that should be processed.
2428 * Returns: a #GstFlowReturn.
2431 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
2434 GstFlowReturn result = GST_FLOW_OK;
2439 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2441 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT,
2442 GST_TIME_ARGS (current_time), GST_TIME_ARGS (ntpnstime));
2446 data.current_time = current_time;
2447 data.ntpnstime = ntpnstime;
2448 data.is_bye = FALSE;
2449 data.has_sdes = FALSE;
2453 RTP_SESSION_LOCK (sess);
2454 /* get a new interval, we need this for various cleanups etc */
2455 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
2457 /* first perform cleanups */
2458 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
2459 (GHRFunc) session_cleanup, &data);
2461 /* see if we need to generate SR or RR packets */
2462 if (is_rtcp_time (sess, current_time, &data)) {
2463 if (own->received_bye) {
2464 /* generate BYE instead */
2465 GST_DEBUG ("generating BYE message");
2466 session_bye (sess, &data);
2467 sess->sent_bye = TRUE;
2469 /* loop over all known sources and do something */
2470 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2471 (GHFunc) session_report_blocks, &data);
2478 /* we keep track of the last report time in order to timeout inactive
2479 * receivers or senders */
2480 sess->last_rtcp_send_time = data.current_time;
2481 sess->first_rtcp = FALSE;
2483 /* add SDES for this source when not already added */
2485 session_sdes (sess, &data);
2487 /* update average RTCP size before sending */
2488 size = GST_BUFFER_SIZE (data.rtcp) + sess->header_len;
2489 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, size);
2492 /* check for outdated collisions */
2493 GST_DEBUG ("checking collision list");
2494 item = g_list_first (sess->conflicting_addresses);
2496 RTPConflictingAddress *known_conflict = item->data;
2497 GList *next_item = g_list_next (item);
2499 if (known_conflict->time < current_time - (data.interval *
2500 RTCP_INTERVAL_COLLISION_TIMEOUT)) {
2501 sess->conflicting_addresses =
2502 g_list_delete_link (sess->conflicting_addresses, item);
2503 GST_DEBUG ("collision %p timed out", known_conflict);
2504 g_free (known_conflict);
2509 if (sess->change_ssrc) {
2510 GST_DEBUG ("need to change our SSRC (%08x)", own->ssrc);
2511 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
2512 GINT_TO_POINTER (own->ssrc));
2514 own->ssrc = rtp_session_create_new_ssrc (sess);
2515 rtp_source_reset (own);
2517 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
2518 GINT_TO_POINTER (own->ssrc), own);
2520 g_free (sess->bye_reason);
2521 sess->bye_reason = NULL;
2522 sess->sent_bye = FALSE;
2523 sess->change_ssrc = FALSE;
2524 GST_DEBUG ("changed our SSRC to %08x", own->ssrc);
2526 RTP_SESSION_UNLOCK (sess);
2528 /* push out the RTCP packet */
2530 /* close the RTCP packet */
2531 gst_rtcp_buffer_end (data.rtcp);
2533 GST_DEBUG ("sending packet");
2534 if (sess->callbacks.send_rtcp)
2535 result = sess->callbacks.send_rtcp (sess, own, data.rtcp,
2536 sess->sent_bye, sess->send_rtcp_user_data);
2538 GST_DEBUG ("freeing packet");
2539 gst_buffer_unref (data.rtcp);