2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include <gst/rtp/gstrtpbuffer.h>
23 #include <gst/rtp/gstrtcpbuffer.h>
24 #include <gst/netbuffer/gstnetbuffer.h>
26 #include "gstrtpbin-marshal.h"
27 #include "rtpsession.h"
29 GST_DEBUG_CATEGORY_STATIC (rtp_session_debug);
30 #define GST_CAT_DEFAULT rtp_session_debug
32 /* signals and args */
35 SIGNAL_GET_SOURCE_BY_SSRC,
37 SIGNAL_ON_SSRC_COLLISION,
38 SIGNAL_ON_SSRC_VALIDATED,
39 SIGNAL_ON_SSRC_ACTIVE,
42 SIGNAL_ON_BYE_TIMEOUT,
44 SIGNAL_ON_SENDER_TIMEOUT,
45 SIGNAL_ON_SENDING_RTCP,
46 SIGNAL_ON_FEEDBACK_RTCP,
51 #define DEFAULT_INTERNAL_SOURCE NULL
52 #define DEFAULT_BANDWIDTH RTP_STATS_BANDWIDTH
53 #define DEFAULT_RTCP_FRACTION (RTP_STATS_RTCP_FRACTION * RTP_STATS_BANDWIDTH)
54 #define DEFAULT_RTCP_RR_BANDWIDTH -1
55 #define DEFAULT_RTCP_RS_BANDWIDTH -1
56 #define DEFAULT_RTCP_MTU 1400
57 #define DEFAULT_SDES NULL
58 #define DEFAULT_NUM_SOURCES 0
59 #define DEFAULT_NUM_ACTIVE_SOURCES 0
60 #define DEFAULT_SOURCES NULL
61 #define DEFAULT_RTCP_MIN_INTERVAL (RTP_STATS_MIN_INTERVAL * GST_SECOND)
62 #define DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW (2 * GST_SECOND)
71 PROP_RTCP_RR_BANDWIDTH,
72 PROP_RTCP_RS_BANDWIDTH,
76 PROP_NUM_ACTIVE_SOURCES,
79 PROP_RTCP_MIN_INTERVAL,
80 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
84 /* update average packet size */
85 #define INIT_AVG(avg, val) \
87 #define UPDATE_AVG(avg, val) \
91 (avg) = ((val) + (15 * (avg))) >> 4;
94 /* The number RTCP intervals after which to timeout entries in the
97 #define RTCP_INTERVAL_COLLISION_TIMEOUT 10
99 /* GObject vmethods */
100 static void rtp_session_finalize (GObject * object);
101 static void rtp_session_set_property (GObject * object, guint prop_id,
102 const GValue * value, GParamSpec * pspec);
103 static void rtp_session_get_property (GObject * object, guint prop_id,
104 GValue * value, GParamSpec * pspec);
106 static gboolean rtp_session_on_sending_rtcp (RTPSession * sess,
107 GstBuffer * buffer, gboolean early);
108 static void rtp_session_send_rtcp (RTPSession * sess,
109 GstClockTimeDiff max_delay);
112 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
114 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
116 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
117 gboolean * created, RTPArrivalStats * arrival, gboolean rtp);
118 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
119 const gchar * reason, GstClockTime current_time);
120 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
121 gboolean deterministic, gboolean first);
124 accumulate_trues (GSignalInvocationHint * ihint, GValue * return_accu,
125 const GValue * handler_return, gpointer data)
127 if (g_value_get_boolean (handler_return))
128 g_value_set_boolean (return_accu, TRUE);
134 rtp_session_class_init (RTPSessionClass * klass)
136 GObjectClass *gobject_class;
138 gobject_class = (GObjectClass *) klass;
140 gobject_class->finalize = rtp_session_finalize;
141 gobject_class->set_property = rtp_session_set_property;
142 gobject_class->get_property = rtp_session_get_property;
145 * RTPSession::get-source-by-ssrc:
146 * @session: the object which received the signal
147 * @ssrc: the SSRC of the RTPSource
149 * Request the #RTPSource object with SSRC @ssrc in @session.
151 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
152 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
153 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
154 get_source_by_ssrc), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
155 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
158 * RTPSession::on-new-ssrc:
159 * @session: the object which received the signal
160 * @src: the new RTPSource
162 * Notify of a new SSRC that entered @session.
164 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
165 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
166 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
167 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
170 * RTPSession::on-ssrc-collision:
171 * @session: the object which received the signal
172 * @src: the #RTPSource that caused a collision
174 * Notify when we have an SSRC collision
176 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
177 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
178 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
179 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
182 * RTPSession::on-ssrc-validated:
183 * @session: the object which received the signal
184 * @src: the new validated RTPSource
186 * Notify of a new SSRC that became validated.
188 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
189 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
190 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
191 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
194 * RTPSession::on-ssrc-active:
195 * @session: the object which received the signal
196 * @src: the active RTPSource
198 * Notify of a SSRC that is active, i.e., sending RTCP.
200 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
201 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
202 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
203 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
206 * RTPSession::on-ssrc-sdes:
207 * @session: the object which received the signal
208 * @src: the RTPSource
210 * Notify that a new SDES was received for SSRC.
212 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
213 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
214 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
215 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
218 * RTPSession::on-bye-ssrc:
219 * @session: the object which received the signal
220 * @src: the RTPSource that went away
222 * Notify of an SSRC that became inactive because of a BYE packet.
224 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
225 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
226 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
227 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
230 * RTPSession::on-bye-timeout:
231 * @session: the object which received the signal
232 * @src: the RTPSource that timed out
234 * Notify of an SSRC that has timed out because of BYE
236 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
237 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
238 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
239 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
242 * RTPSession::on-timeout:
243 * @session: the object which received the signal
244 * @src: the RTPSource that timed out
246 * Notify of an SSRC that has timed out
248 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
249 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
250 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
251 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
254 * RTPSession::on-sender-timeout:
255 * @session: the object which received the signal
256 * @src: the RTPSource that timed out
258 * Notify of an SSRC that was a sender but timed out and became a receiver.
260 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
261 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
262 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
263 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
267 * RTPSession::on-sending-rtcp
268 * @session: the object which received the signal
269 * @buffer: the #GstBuffer containing the RTCP packet about to be sent
270 * @early: %TRUE if the packet is early, %FALSE if it is regular
272 * This signal is emitted before sending an RTCP packet, it can be used
273 * to add extra RTCP Packets.
275 * Returns: %TRUE if the RTCP buffer should NOT be suppressed, %FALSE
276 * if suppressing it is acceptable
278 rtp_session_signals[SIGNAL_ON_SENDING_RTCP] =
279 g_signal_new ("on-sending-rtcp", G_TYPE_FROM_CLASS (klass),
280 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_rtcp),
281 accumulate_trues, NULL, gst_rtp_bin_marshal_BOOLEAN__POINTER_BOOLEAN,
282 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
285 * RTPSession::on-feedback-rtcp:
286 * @session: the object which received the signal
287 * @type: Type of RTCP packet, will be %GST_RTCP_TYPE_RTPFB or
288 * %GST_RTCP_TYPE_RTPFB
289 * @fbtype: The type of RTCP FB packet, probably part of #GstRTCPFBType
290 * @sender_ssrc: The SSRC of the sender
291 * @media_ssrc: The SSRC of the media this refers to
292 * @fci: a #GstBuffer with the FCI data from the FB packet or %NULL if
295 * Notify that a RTCP feedback packet has been received
298 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP] =
299 g_signal_new ("on-feedback-rtcp", G_TYPE_FROM_CLASS (klass),
300 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_feedback_rtcp),
301 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT_UINT_UINT_POINTER,
302 G_TYPE_NONE, 4, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT,
306 * RTPSession::send-rtcp:
307 * @session: the object which received the signal
308 * @max_delay: The maximum delay after which the feedback will not be useful
311 * Requests that the #RTPSession initiate a new RTCP packet as soon as
312 * possible within the requested delay.
315 rtp_session_signals[SIGNAL_SEND_RTCP] =
316 g_signal_new ("send-rtcp", G_TYPE_FROM_CLASS (klass),
317 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
318 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
319 gst_rtp_bin_marshal_VOID__UINT64, G_TYPE_NONE, 1, G_TYPE_UINT64);
321 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
322 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
323 "The internal SSRC used for the session",
324 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
327 g_param_spec_object ("internal-source", "Internal Source",
328 "The internal source element of the session",
329 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
331 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
332 g_param_spec_double ("bandwidth", "Bandwidth",
333 "The bandwidth of the session (0 for auto-discover)",
334 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
335 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
337 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
338 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
339 "The fraction of the bandwidth used for RTCP (or as a real fraction of the RTP bandwidth if < 1)",
340 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
341 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
343 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
344 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
345 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
346 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
347 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
349 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
350 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
351 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
352 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
353 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
355 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
356 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
357 "The maximum size of the RTCP packets",
358 16, G_MAXINT16, DEFAULT_RTCP_MTU,
359 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
361 g_object_class_install_property (gobject_class, PROP_SDES,
362 g_param_spec_boxed ("sdes", "SDES",
363 "The SDES items of this session",
364 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
366 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
367 g_param_spec_uint ("num-sources", "Num Sources",
368 "The number of sources in the session", 0, G_MAXUINT,
369 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
371 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
372 g_param_spec_uint ("num-active-sources", "Num Active Sources",
373 "The number of active sources in the session", 0, G_MAXUINT,
374 DEFAULT_NUM_ACTIVE_SOURCES,
375 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
379 * Get a GValue Array of all sources in the session.
382 * <title>Getting the #RTPSources of a session
389 * g_object_get (sess, "sources", &arr, NULL);
391 * for (i = 0; i < arr->n_values; i++) {
394 * val = g_value_array_get_nth (arr, i);
395 * source = g_value_get_object (val);
397 * g_value_array_free (arr);
402 g_object_class_install_property (gobject_class, PROP_SOURCES,
403 g_param_spec_boxed ("sources", "Sources",
404 "An array of all known sources in the session",
405 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
407 g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
408 g_param_spec_boolean ("favor-new", "Favor new sources",
409 "Resolve SSRC conflict in favor of new sources", FALSE,
410 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
412 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
413 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
414 "Minimum interval between Regular RTCP packet (in ns)",
415 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
416 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
418 g_object_class_install_property (gobject_class,
419 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
420 g_param_spec_uint64 ("rtcp-feedback-retention-window",
421 "RTCP Feedback retention window",
422 "Duration during which RTCP Feedback packets are retained (in ns)",
423 0, G_MAXUINT64, DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW,
424 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
427 klass->get_source_by_ssrc =
428 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
429 klass->on_sending_rtcp = GST_DEBUG_FUNCPTR (rtp_session_on_sending_rtcp);
430 klass->send_rtcp = GST_DEBUG_FUNCPTR (rtp_session_send_rtcp);
432 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
436 rtp_session_init (RTPSession * sess)
441 sess->lock = g_mutex_new ();
442 sess->key = g_random_int ();
446 for (i = 0; i < 32; i++) {
448 g_hash_table_new_full (NULL, NULL, NULL,
449 (GDestroyNotify) g_object_unref);
451 sess->cnames = g_hash_table_new_full (NULL, NULL, g_free, NULL);
453 rtp_stats_init_defaults (&sess->stats);
455 sess->recalc_bandwidth = TRUE;
456 sess->bandwidth = DEFAULT_BANDWIDTH;
457 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
458 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
459 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
461 /* create an active SSRC for this session manager */
462 sess->source = rtp_session_create_source (sess);
463 sess->source->validated = TRUE;
464 sess->source->internal = TRUE;
465 sess->stats.active_sources++;
466 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
468 /* default UDP header length */
469 sess->header_len = 28;
470 sess->mtu = DEFAULT_RTCP_MTU;
472 /* some default SDES entries */
473 str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
474 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_CNAME, str);
477 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_NAME,
479 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_TOOL, "GStreamer");
481 sess->first_rtcp = TRUE;
482 sess->allow_early = TRUE;
483 sess->rtcp_feedback_retention_window = DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW;
485 sess->rtcp_pli_requests = g_array_new (FALSE, FALSE, sizeof (guint32));
487 GST_DEBUG ("%p: session using SSRC: %08x", sess, sess->source->ssrc);
491 rtp_session_finalize (GObject * object)
496 sess = RTP_SESSION_CAST (object);
498 g_mutex_free (sess->lock);
499 for (i = 0; i < 32; i++)
500 g_hash_table_destroy (sess->ssrcs[i]);
502 g_free (sess->bye_reason);
504 g_hash_table_destroy (sess->cnames);
505 g_object_unref (sess->source);
507 g_array_free (sess->rtcp_pli_requests, TRUE);
509 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
513 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
515 GValue value = { 0 };
517 g_value_init (&value, RTP_TYPE_SOURCE);
518 g_value_take_object (&value, source);
519 /* copies the value */
520 g_value_array_append (arr, &value);
524 rtp_session_create_sources (RTPSession * sess)
529 RTP_SESSION_LOCK (sess);
530 /* get number of elements in the table */
531 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
532 /* create the result value array */
533 res = g_value_array_new (size);
535 /* and copy all values into the array */
536 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
537 RTP_SESSION_UNLOCK (sess);
543 rtp_session_set_property (GObject * object, guint prop_id,
544 const GValue * value, GParamSpec * pspec)
548 sess = RTP_SESSION (object);
551 case PROP_INTERNAL_SSRC:
552 rtp_session_set_internal_ssrc (sess, g_value_get_uint (value));
555 sess->bandwidth = g_value_get_double (value);
556 sess->recalc_bandwidth = TRUE;
558 case PROP_RTCP_FRACTION:
559 sess->rtcp_bandwidth = g_value_get_double (value);
560 sess->recalc_bandwidth = TRUE;
562 case PROP_RTCP_RR_BANDWIDTH:
563 sess->rtcp_rr_bandwidth = g_value_get_int (value);
564 sess->recalc_bandwidth = TRUE;
566 case PROP_RTCP_RS_BANDWIDTH:
567 sess->rtcp_rs_bandwidth = g_value_get_int (value);
568 sess->recalc_bandwidth = TRUE;
571 sess->mtu = g_value_get_uint (value);
574 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
577 sess->favor_new = g_value_get_boolean (value);
579 case PROP_RTCP_MIN_INTERVAL:
580 rtp_stats_set_min_interval (&sess->stats,
581 (gdouble) g_value_get_uint64 (value) / GST_SECOND);
584 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
590 rtp_session_get_property (GObject * object, guint prop_id,
591 GValue * value, GParamSpec * pspec)
595 sess = RTP_SESSION (object);
598 case PROP_INTERNAL_SSRC:
599 g_value_set_uint (value, rtp_session_get_internal_ssrc (sess));
601 case PROP_INTERNAL_SOURCE:
602 g_value_take_object (value, rtp_session_get_internal_source (sess));
605 g_value_set_double (value, sess->bandwidth);
607 case PROP_RTCP_FRACTION:
608 g_value_set_double (value, sess->rtcp_bandwidth);
610 case PROP_RTCP_RR_BANDWIDTH:
611 g_value_set_int (value, sess->rtcp_rr_bandwidth);
613 case PROP_RTCP_RS_BANDWIDTH:
614 g_value_set_int (value, sess->rtcp_rs_bandwidth);
617 g_value_set_uint (value, sess->mtu);
620 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
622 case PROP_NUM_SOURCES:
623 g_value_set_uint (value, rtp_session_get_num_sources (sess));
625 case PROP_NUM_ACTIVE_SOURCES:
626 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
629 g_value_take_boxed (value, rtp_session_create_sources (sess));
632 g_value_set_boolean (value, sess->favor_new);
634 case PROP_RTCP_MIN_INTERVAL:
635 g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
638 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
644 on_new_ssrc (RTPSession * sess, RTPSource * source)
646 g_object_ref (source);
647 RTP_SESSION_UNLOCK (sess);
648 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
649 RTP_SESSION_LOCK (sess);
650 g_object_unref (source);
654 on_ssrc_collision (RTPSession * sess, RTPSource * source)
656 g_object_ref (source);
657 RTP_SESSION_UNLOCK (sess);
658 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
660 RTP_SESSION_LOCK (sess);
661 g_object_unref (source);
665 on_ssrc_validated (RTPSession * sess, RTPSource * source)
667 g_object_ref (source);
668 RTP_SESSION_UNLOCK (sess);
669 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
671 RTP_SESSION_LOCK (sess);
672 g_object_unref (source);
676 on_ssrc_active (RTPSession * sess, RTPSource * source)
678 g_object_ref (source);
679 RTP_SESSION_UNLOCK (sess);
680 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
681 RTP_SESSION_LOCK (sess);
682 g_object_unref (source);
686 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
688 g_object_ref (source);
689 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
690 RTP_SESSION_UNLOCK (sess);
691 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
692 RTP_SESSION_LOCK (sess);
693 g_object_unref (source);
697 on_bye_ssrc (RTPSession * sess, RTPSource * source)
699 g_object_ref (source);
700 RTP_SESSION_UNLOCK (sess);
701 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
702 RTP_SESSION_LOCK (sess);
703 g_object_unref (source);
707 on_bye_timeout (RTPSession * sess, RTPSource * source)
709 g_object_ref (source);
710 RTP_SESSION_UNLOCK (sess);
711 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
712 RTP_SESSION_LOCK (sess);
713 g_object_unref (source);
717 on_timeout (RTPSession * sess, RTPSource * source)
719 g_object_ref (source);
720 RTP_SESSION_UNLOCK (sess);
721 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
722 RTP_SESSION_LOCK (sess);
723 g_object_unref (source);
727 on_sender_timeout (RTPSession * sess, RTPSource * source)
729 g_object_ref (source);
730 RTP_SESSION_UNLOCK (sess);
731 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
733 RTP_SESSION_LOCK (sess);
734 g_object_unref (source);
740 * Create a new session object.
742 * Returns: a new #RTPSession. g_object_unref() after usage.
745 rtp_session_new (void)
749 sess = g_object_new (RTP_TYPE_SESSION, NULL);
755 * rtp_session_set_callbacks:
756 * @sess: an #RTPSession
757 * @callbacks: callbacks to configure
758 * @user_data: user data passed in the callbacks
760 * Configure a set of callbacks to be notified of actions.
763 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
766 g_return_if_fail (RTP_IS_SESSION (sess));
768 if (callbacks->process_rtp) {
769 sess->callbacks.process_rtp = callbacks->process_rtp;
770 sess->process_rtp_user_data = user_data;
772 if (callbacks->send_rtp) {
773 sess->callbacks.send_rtp = callbacks->send_rtp;
774 sess->send_rtp_user_data = user_data;
776 if (callbacks->send_rtcp) {
777 sess->callbacks.send_rtcp = callbacks->send_rtcp;
778 sess->send_rtcp_user_data = user_data;
780 if (callbacks->sync_rtcp) {
781 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
782 sess->sync_rtcp_user_data = user_data;
784 if (callbacks->clock_rate) {
785 sess->callbacks.clock_rate = callbacks->clock_rate;
786 sess->clock_rate_user_data = user_data;
788 if (callbacks->reconsider) {
789 sess->callbacks.reconsider = callbacks->reconsider;
790 sess->reconsider_user_data = user_data;
792 if (callbacks->request_key_unit) {
793 sess->callbacks.request_key_unit = callbacks->request_key_unit;
794 sess->request_key_unit_user_data = user_data;
796 if (callbacks->request_time) {
797 sess->callbacks.request_time = callbacks->request_time;
798 sess->request_time_user_data = user_data;
803 * rtp_session_set_process_rtp_callback:
804 * @sess: an #RTPSession
805 * @callback: callback to set
806 * @user_data: user data passed in the callback
808 * Configure only the process_rtp callback to be notified of the process_rtp action.
811 rtp_session_set_process_rtp_callback (RTPSession * sess,
812 RTPSessionProcessRTP callback, gpointer user_data)
814 g_return_if_fail (RTP_IS_SESSION (sess));
816 sess->callbacks.process_rtp = callback;
817 sess->process_rtp_user_data = user_data;
821 * rtp_session_set_send_rtp_callback:
822 * @sess: an #RTPSession
823 * @callback: callback to set
824 * @user_data: user data passed in the callback
826 * Configure only the send_rtp callback to be notified of the send_rtp action.
829 rtp_session_set_send_rtp_callback (RTPSession * sess,
830 RTPSessionSendRTP callback, gpointer user_data)
832 g_return_if_fail (RTP_IS_SESSION (sess));
834 sess->callbacks.send_rtp = callback;
835 sess->send_rtp_user_data = user_data;
839 * rtp_session_set_send_rtcp_callback:
840 * @sess: an #RTPSession
841 * @callback: callback to set
842 * @user_data: user data passed in the callback
844 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
847 rtp_session_set_send_rtcp_callback (RTPSession * sess,
848 RTPSessionSendRTCP callback, gpointer user_data)
850 g_return_if_fail (RTP_IS_SESSION (sess));
852 sess->callbacks.send_rtcp = callback;
853 sess->send_rtcp_user_data = user_data;
857 * rtp_session_set_sync_rtcp_callback:
858 * @sess: an #RTPSession
859 * @callback: callback to set
860 * @user_data: user data passed in the callback
862 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
865 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
866 RTPSessionSyncRTCP callback, gpointer user_data)
868 g_return_if_fail (RTP_IS_SESSION (sess));
870 sess->callbacks.sync_rtcp = callback;
871 sess->sync_rtcp_user_data = user_data;
875 * rtp_session_set_clock_rate_callback:
876 * @sess: an #RTPSession
877 * @callback: callback to set
878 * @user_data: user data passed in the callback
880 * Configure only the clock_rate callback to be notified of the clock_rate action.
883 rtp_session_set_clock_rate_callback (RTPSession * sess,
884 RTPSessionClockRate callback, gpointer user_data)
886 g_return_if_fail (RTP_IS_SESSION (sess));
888 sess->callbacks.clock_rate = callback;
889 sess->clock_rate_user_data = user_data;
893 * rtp_session_set_reconsider_callback:
894 * @sess: an #RTPSession
895 * @callback: callback to set
896 * @user_data: user data passed in the callback
898 * Configure only the reconsider callback to be notified of the reconsider action.
901 rtp_session_set_reconsider_callback (RTPSession * sess,
902 RTPSessionReconsider callback, gpointer user_data)
904 g_return_if_fail (RTP_IS_SESSION (sess));
906 sess->callbacks.reconsider = callback;
907 sess->reconsider_user_data = user_data;
911 * rtp_session_set_request_time_callback:
912 * @sess: an #RTPSession
913 * @callback: callback to set
914 * @user_data: user data passed in the callback
916 * Configure only the request_time callback
919 rtp_session_set_request_time_callback (RTPSession * sess,
920 RTPSessionRequestTime callback, gpointer user_data)
922 g_return_if_fail (RTP_IS_SESSION (sess));
924 sess->callbacks.request_time = callback;
925 sess->request_time_user_data = user_data;
929 * rtp_session_set_bandwidth:
930 * @sess: an #RTPSession
931 * @bandwidth: the bandwidth allocated
933 * Set the session bandwidth in bytes per second.
936 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
938 g_return_if_fail (RTP_IS_SESSION (sess));
940 RTP_SESSION_LOCK (sess);
941 sess->stats.bandwidth = bandwidth;
942 RTP_SESSION_UNLOCK (sess);
946 * rtp_session_get_bandwidth:
947 * @sess: an #RTPSession
949 * Get the session bandwidth.
951 * Returns: the session bandwidth.
954 rtp_session_get_bandwidth (RTPSession * sess)
958 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
960 RTP_SESSION_LOCK (sess);
961 result = sess->stats.bandwidth;
962 RTP_SESSION_UNLOCK (sess);
968 * rtp_session_set_rtcp_fraction:
969 * @sess: an #RTPSession
970 * @bandwidth: the RTCP bandwidth
972 * Set the bandwidth in bytes per second that should be used for RTCP
976 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
978 g_return_if_fail (RTP_IS_SESSION (sess));
980 RTP_SESSION_LOCK (sess);
981 sess->stats.rtcp_bandwidth = bandwidth;
982 RTP_SESSION_UNLOCK (sess);
986 * rtp_session_get_rtcp_fraction:
987 * @sess: an #RTPSession
989 * Get the session bandwidth used for RTCP.
991 * Returns: The bandwidth used for RTCP messages.
994 rtp_session_get_rtcp_fraction (RTPSession * sess)
998 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
1000 RTP_SESSION_LOCK (sess);
1001 result = sess->stats.rtcp_bandwidth;
1002 RTP_SESSION_UNLOCK (sess);
1008 * rtp_session_set_sdes_string:
1009 * @sess: an #RTPSession
1010 * @type: the type of the SDES item
1011 * @item: a null-terminated string to set.
1013 * Store an SDES item of @type in @sess.
1015 * Returns: %FALSE if the data was unchanged @type is invalid.
1018 rtp_session_set_sdes_string (RTPSession * sess, GstRTCPSDESType type,
1023 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1025 RTP_SESSION_LOCK (sess);
1026 result = rtp_source_set_sdes_string (sess->source, type, item);
1027 RTP_SESSION_UNLOCK (sess);
1033 * rtp_session_get_sdes_string:
1034 * @sess: an #RTPSession
1035 * @type: the type of the SDES item
1037 * Get the SDES item of @type from @sess.
1039 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
1040 * valid. g_free() after usage.
1043 rtp_session_get_sdes_string (RTPSession * sess, GstRTCPSDESType type)
1047 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1049 RTP_SESSION_LOCK (sess);
1050 result = rtp_source_get_sdes_string (sess->source, type);
1051 RTP_SESSION_UNLOCK (sess);
1057 * rtp_session_get_sdes_struct:
1058 * @sess: an #RTSPSession
1060 * Get the SDES data as a #GstStructure
1062 * Returns: a GstStructure with SDES items for @sess. This function returns a
1063 * copy of the SDES structure, use gst_structure_free() after usage.
1066 rtp_session_get_sdes_struct (RTPSession * sess)
1068 const GstStructure *sdes;
1069 GstStructure *result = NULL;
1071 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1073 RTP_SESSION_LOCK (sess);
1074 sdes = rtp_source_get_sdes_struct (sess->source);
1076 result = gst_structure_copy (sdes);
1077 RTP_SESSION_UNLOCK (sess);
1083 * rtp_session_set_sdes_struct:
1084 * @sess: an #RTSPSession
1085 * @sdes: a #GstStructure
1087 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1090 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
1092 g_return_if_fail (sdes);
1093 g_return_if_fail (RTP_IS_SESSION (sess));
1095 RTP_SESSION_LOCK (sess);
1096 rtp_source_set_sdes_struct (sess->source, gst_structure_copy (sdes));
1097 RTP_SESSION_UNLOCK (sess);
1100 static GstFlowReturn
1101 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1103 GstFlowReturn result = GST_FLOW_OK;
1105 if (source == session->source) {
1106 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1108 RTP_SESSION_UNLOCK (session);
1110 if (session->callbacks.send_rtp)
1112 session->callbacks.send_rtp (session, source, data,
1113 session->send_rtp_user_data);
1115 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1118 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1119 RTP_SESSION_UNLOCK (session);
1121 if (session->callbacks.process_rtp)
1123 session->callbacks.process_rtp (session, source,
1124 GST_BUFFER_CAST (data), session->process_rtp_user_data);
1126 gst_buffer_unref (GST_BUFFER_CAST (data));
1128 RTP_SESSION_LOCK (session);
1134 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1138 RTP_SESSION_UNLOCK (session);
1140 if (session->callbacks.clock_rate)
1142 session->callbacks.clock_rate (session, pt,
1143 session->clock_rate_user_data);
1147 RTP_SESSION_LOCK (session);
1149 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1154 static RTPSourceCallbacks callbacks = {
1155 (RTPSourcePushRTP) source_push_rtp,
1156 (RTPSourceClockRate) source_clock_rate,
1160 check_collision (RTPSession * sess, RTPSource * source,
1161 RTPArrivalStats * arrival, gboolean rtp)
1163 /* If we have no arrival address, we can't do collision checking */
1164 if (!arrival->have_address)
1167 if (sess->source != source) {
1168 GstNetAddress *from;
1171 /* This is not our local source, but lets check if two remote
1176 from = &source->rtp_from;
1177 have_from = source->have_rtp_from;
1179 from = &source->rtcp_from;
1180 have_from = source->have_rtcp_from;
1184 if (gst_netaddress_equal (from, &arrival->address)) {
1185 /* Address is the same */
1188 GST_LOG ("we have a third-party collision or loop ssrc:%x",
1189 rtp_source_get_ssrc (source));
1190 if (sess->favor_new) {
1191 if (rtp_source_find_conflicting_address (source,
1192 &arrival->address, arrival->current_time)) {
1194 gst_netaddress_to_string (&arrival->address, buf1, 40);
1195 GST_LOG ("Known conflict on %x for %s, dropping packet",
1196 rtp_source_get_ssrc (source), buf1);
1199 gchar buf1[40], buf2[40];
1201 /* Current address is not a known conflict, lets assume this is
1202 * a new source. Save old address in possible conflict list
1204 rtp_source_add_conflicting_address (source, from,
1205 arrival->current_time);
1207 gst_netaddress_to_string (from, buf1, 40);
1208 gst_netaddress_to_string (&arrival->address, buf2, 40);
1209 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1210 " saving old as known conflict",
1211 rtp_source_get_ssrc (source), buf1, buf2);
1214 rtp_source_set_rtp_from (source, &arrival->address);
1216 rtp_source_set_rtcp_from (source, &arrival->address);
1220 /* Don't need to save old addresses, we ignore new sources */
1225 /* We don't already have a from address for RTP, just set it */
1227 rtp_source_set_rtp_from (source, &arrival->address);
1229 rtp_source_set_rtcp_from (source, &arrival->address);
1233 /* FIXME: Log 3rd party collision somehow
1234 * Maybe should be done in upper layer, only the SDES can tell us
1235 * if its a collision or a loop
1238 /* If the source has been inactive for some time, we assume that it has
1239 * simply changed its transport source address. Hence, there is no true
1240 * third-party collision - only a simulated one. */
1241 if (arrival->current_time > source->last_activity) {
1242 GstClockTime inactivity_period =
1243 arrival->current_time - source->last_activity;
1244 if (inactivity_period > 1 * GST_SECOND) {
1245 /* Use new network address */
1247 g_assert (source->have_rtp_from);
1248 rtp_source_set_rtp_from (source, &arrival->address);
1250 g_assert (source->have_rtcp_from);
1251 rtp_source_set_rtcp_from (source, &arrival->address);
1257 /* This is sending with our ssrc, is it an address we already know */
1259 if (rtp_source_find_conflicting_address (source, &arrival->address,
1260 arrival->current_time)) {
1261 /* Its a known conflict, its probably a loop, not a collision
1262 * lets just drop the incoming packet
1264 GST_DEBUG ("Our packets are being looped back to us, dropping");
1266 /* Its a new collision, lets change our SSRC */
1268 rtp_source_add_conflicting_address (source, &arrival->address,
1269 arrival->current_time);
1271 GST_DEBUG ("Collision for SSRC %x", rtp_source_get_ssrc (source));
1272 on_ssrc_collision (sess, source);
1274 rtp_session_schedule_bye_locked (sess, "SSRC Collision",
1275 arrival->current_time);
1277 sess->change_ssrc = TRUE;
1285 /* must be called with the session lock, the returned source needs to be
1286 * unreffed after usage. */
1288 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1289 RTPArrivalStats * arrival, gboolean rtp)
1294 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1295 if (source == NULL) {
1296 /* make new Source in probation and insert */
1297 source = rtp_source_new (ssrc);
1299 /* for RTP packets we need to set the source in probation. Receiving RTCP
1300 * packets of an SSRC, on the other hand, is a strong indication that we
1301 * are dealing with a valid source. */
1303 source->probation = RTP_DEFAULT_PROBATION;
1305 source->probation = 0;
1307 /* store from address, if any */
1308 if (arrival->have_address) {
1310 rtp_source_set_rtp_from (source, &arrival->address);
1312 rtp_source_set_rtcp_from (source, &arrival->address);
1315 /* configure a callback on the source */
1316 rtp_source_set_callbacks (source, &callbacks, sess);
1318 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1321 /* we have one more source now */
1322 sess->total_sources++;
1326 /* check for collision, this updates the address when not previously set */
1327 if (check_collision (sess, source, arrival, rtp)) {
1331 /* update last activity */
1332 source->last_activity = arrival->current_time;
1334 source->last_rtp_activity = arrival->current_time;
1335 g_object_ref (source);
1341 * rtp_session_get_internal_source:
1342 * @sess: a #RTPSession
1344 * Get the internal #RTPSource of @sess.
1346 * Returns: The internal #RTPSource. g_object_unref() after usage.
1349 rtp_session_get_internal_source (RTPSession * sess)
1353 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1355 result = g_object_ref (sess->source);
1361 * rtp_session_set_internal_ssrc:
1362 * @sess: a #RTPSession
1365 * Set the SSRC of @sess to @ssrc.
1368 rtp_session_set_internal_ssrc (RTPSession * sess, guint32 ssrc)
1370 RTP_SESSION_LOCK (sess);
1371 if (ssrc != sess->source->ssrc) {
1372 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
1373 GINT_TO_POINTER (sess->source->ssrc));
1375 GST_DEBUG ("setting internal SSRC to %08x", ssrc);
1376 /* After this call, any receiver of the old SSRC either in RTP or RTCP
1377 * packets will timeout on the old SSRC, we could potentially schedule a
1378 * BYE RTCP for the old SSRC... */
1379 sess->source->ssrc = ssrc;
1380 rtp_source_reset (sess->source);
1382 /* rehash with the new SSRC */
1383 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1384 GINT_TO_POINTER (sess->source->ssrc), sess->source);
1386 RTP_SESSION_UNLOCK (sess);
1388 g_object_notify (G_OBJECT (sess), "internal-ssrc");
1392 * rtp_session_get_internal_ssrc:
1393 * @sess: a #RTPSession
1395 * Get the internal SSRC of @sess.
1397 * Returns: The SSRC of the session.
1400 rtp_session_get_internal_ssrc (RTPSession * sess)
1404 RTP_SESSION_LOCK (sess);
1405 ssrc = sess->source->ssrc;
1406 RTP_SESSION_UNLOCK (sess);
1412 * rtp_session_add_source:
1413 * @sess: a #RTPSession
1414 * @src: #RTPSource to add
1416 * Add @src to @session.
1418 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1419 * existed in the session.
1422 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1424 gboolean result = FALSE;
1427 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1428 g_return_val_if_fail (src != NULL, FALSE);
1430 RTP_SESSION_LOCK (sess);
1432 g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1433 GINT_TO_POINTER (src->ssrc));
1435 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1436 GINT_TO_POINTER (src->ssrc), src);
1437 /* we have one more source now */
1438 sess->total_sources++;
1441 RTP_SESSION_UNLOCK (sess);
1447 * rtp_session_get_num_sources:
1448 * @sess: an #RTPSession
1450 * Get the number of sources in @sess.
1452 * Returns: The number of sources in @sess.
1455 rtp_session_get_num_sources (RTPSession * sess)
1459 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1461 RTP_SESSION_LOCK (sess);
1462 result = sess->total_sources;
1463 RTP_SESSION_UNLOCK (sess);
1469 * rtp_session_get_num_active_sources:
1470 * @sess: an #RTPSession
1472 * Get the number of active sources in @sess. A source is considered active when
1473 * it has been validated and has not yet received a BYE RTCP message.
1475 * Returns: The number of active sources in @sess.
1478 rtp_session_get_num_active_sources (RTPSession * sess)
1482 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1484 RTP_SESSION_LOCK (sess);
1485 result = sess->stats.active_sources;
1486 RTP_SESSION_UNLOCK (sess);
1492 * rtp_session_get_source_by_ssrc:
1493 * @sess: an #RTPSession
1496 * Find the source with @ssrc in @sess.
1498 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1499 * g_object_unref() after usage.
1502 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1506 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1508 RTP_SESSION_LOCK (sess);
1510 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1512 g_object_ref (result);
1513 RTP_SESSION_UNLOCK (sess);
1519 * rtp_session_get_source_by_cname:
1520 * @sess: a #RTPSession
1523 * Find the source with @cname in @sess.
1525 * Returns: a #RTPSource with CNAME @cname or NULL if the source was not found.
1526 * g_object_unref() after usage.
1529 rtp_session_get_source_by_cname (RTPSession * sess, const gchar * cname)
1533 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1534 g_return_val_if_fail (cname != NULL, NULL);
1536 RTP_SESSION_LOCK (sess);
1537 result = g_hash_table_lookup (sess->cnames, cname);
1539 g_object_ref (result);
1540 RTP_SESSION_UNLOCK (sess);
1545 /* should be called with the SESSION lock */
1547 rtp_session_create_new_ssrc (RTPSession * sess)
1552 ssrc = g_random_int ();
1554 /* see if it exists in the session, we're done if it doesn't */
1555 if (g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1556 GINT_TO_POINTER (ssrc)) == NULL)
1564 * rtp_session_create_source:
1565 * @sess: an #RTPSession
1567 * Create an #RTPSource for use in @sess. This function will create a source
1568 * with an ssrc that is currently not used by any participants in the session.
1570 * Returns: an #RTPSource.
1573 rtp_session_create_source (RTPSession * sess)
1578 RTP_SESSION_LOCK (sess);
1579 ssrc = rtp_session_create_new_ssrc (sess);
1580 source = rtp_source_new (ssrc);
1581 rtp_source_set_callbacks (source, &callbacks, sess);
1582 /* we need an additional ref for the source in the hashtable */
1583 g_object_ref (source);
1584 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1586 /* we have one more source now */
1587 sess->total_sources++;
1588 RTP_SESSION_UNLOCK (sess);
1593 /* update the RTPArrivalStats structure with the current time and other bits
1594 * about the current buffer we are handling.
1595 * This function is typically called when a validated packet is received.
1596 * This function should be called with the SESSION_LOCK
1599 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
1600 gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
1601 GstClockTime running_time, guint64 ntpnstime)
1603 /* get time of arrival */
1604 arrival->current_time = current_time;
1605 arrival->running_time = running_time;
1606 arrival->ntpnstime = ntpnstime;
1608 /* get packet size including header overhead */
1609 arrival->bytes = GST_BUFFER_SIZE (buffer) + sess->header_len;
1612 arrival->payload_len = gst_rtp_buffer_get_payload_len (buffer);
1614 arrival->payload_len = 0;
1617 /* for netbuffer we can store the IP address to check for collisions */
1618 arrival->have_address = GST_IS_NETBUFFER (buffer);
1619 if (arrival->have_address) {
1620 GstNetBuffer *netbuf = (GstNetBuffer *) buffer;
1622 memcpy (&arrival->address, &netbuf->from, sizeof (GstNetAddress));
1627 * rtp_session_process_rtp:
1628 * @sess: and #RTPSession
1629 * @buffer: an RTP buffer
1630 * @current_time: the current system time
1631 * @running_time: the running_time of @buffer
1633 * Process an RTP buffer in the session manager. This function takes ownership
1636 * Returns: a #GstFlowReturn.
1639 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1640 GstClockTime current_time, GstClockTime running_time)
1642 GstFlowReturn result;
1646 gboolean prevsender, prevactive;
1647 RTPArrivalStats arrival;
1652 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1653 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1655 if (!gst_rtp_buffer_validate (buffer))
1656 goto invalid_packet;
1658 RTP_SESSION_LOCK (sess);
1659 /* update arrival stats */
1660 update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1663 /* ignore more RTP packets when we left the session */
1664 if (sess->source->received_bye)
1667 /* get SSRC and look up in session database */
1668 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1669 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1673 prevsender = RTP_SOURCE_IS_SENDER (source);
1674 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1675 oldrate = source->bitrate;
1677 /* copy available csrc for later */
1678 count = gst_rtp_buffer_get_csrc_count (buffer);
1679 /* make sure to not overflow our array. An RTP buffer can maximally contain
1681 count = MIN (count, 16);
1683 for (i = 0; i < count; i++)
1684 csrcs[i] = gst_rtp_buffer_get_csrc (buffer, i);
1686 /* let source process the packet */
1687 result = rtp_source_process_rtp (source, buffer, &arrival);
1689 /* source became active */
1690 if (prevactive != RTP_SOURCE_IS_ACTIVE (source)) {
1691 sess->stats.active_sources++;
1692 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1693 sess->stats.active_sources);
1694 on_ssrc_validated (sess, source);
1696 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1697 sess->stats.sender_sources++;
1698 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1699 sess->stats.sender_sources);
1701 if (oldrate != source->bitrate)
1702 sess->recalc_bandwidth = TRUE;
1705 on_new_ssrc (sess, source);
1707 if (source->validated) {
1710 /* for validated sources, we add the CSRCs as well */
1711 for (i = 0; i < count; i++) {
1713 RTPSource *csrc_src;
1718 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1723 GST_DEBUG ("created new CSRC: %08x", csrc);
1724 rtp_source_set_as_csrc (csrc_src);
1725 if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1726 sess->stats.active_sources++;
1727 on_new_ssrc (sess, csrc_src);
1729 g_object_unref (csrc_src);
1732 g_object_unref (source);
1734 RTP_SESSION_UNLOCK (sess);
1741 gst_buffer_unref (buffer);
1742 GST_DEBUG ("invalid RTP packet received");
1747 gst_buffer_unref (buffer);
1748 RTP_SESSION_UNLOCK (sess);
1749 GST_DEBUG ("ignoring RTP packet because we are leaving");
1754 gst_buffer_unref (buffer);
1755 RTP_SESSION_UNLOCK (sess);
1756 GST_DEBUG ("ignoring packet because its collisioning");
1762 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1763 GstRTCPPacket * packet, RTPArrivalStats * arrival)
1767 count = gst_rtcp_packet_get_rb_count (packet);
1768 for (i = 0; i < count; i++) {
1769 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1770 guint8 fractionlost;
1773 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1774 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1776 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1778 if (ssrc == sess->source->ssrc) {
1779 /* only deal with report blocks for our session, we update the stats of
1780 * the sender of the RTCP message. We could also compare our stats against
1781 * the other sender to see if we are better or worse. */
1782 rtp_source_process_rb (source, arrival->ntpnstime, fractionlost,
1783 packetslost, exthighestseq, jitter, lsr, dlsr);
1786 on_ssrc_active (sess, source);
1789 /* A Sender report contains statistics about how the sender is doing. This
1790 * includes timing informataion such as the relation between RTP and NTP
1791 * timestamps and the number of packets/bytes it sent to us.
1793 * In this report is also included a set of report blocks related to how this
1794 * sender is receiving data (in case we (or somebody else) is also sending stuff
1795 * to it). This info includes the packet loss, jitter and seqnum. It also
1796 * contains information to calculate the round trip time (LSR/DLSR).
1799 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1800 RTPArrivalStats * arrival, gboolean * do_sync)
1802 guint32 senderssrc, rtptime, packet_count, octet_count;
1805 gboolean created, prevsender;
1807 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1808 &packet_count, &octet_count);
1810 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1811 senderssrc, GST_TIME_ARGS (arrival->current_time));
1813 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1817 /* don't try to do lip-sync for sources that sent a BYE */
1818 if (rtp_source_received_bye (source))
1823 prevsender = RTP_SOURCE_IS_SENDER (source);
1825 /* first update the source */
1826 rtp_source_process_sr (source, arrival->current_time, ntptime, rtptime,
1827 packet_count, octet_count);
1829 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1830 sess->stats.sender_sources++;
1831 GST_DEBUG ("source: %08x became sender, %d sender sources", senderssrc,
1832 sess->stats.sender_sources);
1836 on_new_ssrc (sess, source);
1838 rtp_session_process_rb (sess, source, packet, arrival);
1839 g_object_unref (source);
1842 /* A receiver report contains statistics about how a receiver is doing. It
1843 * includes stuff like packet loss, jitter and the seqnum it received last. It
1844 * also contains info to calculate the round trip time.
1846 * We are only interested in how the sender of this report is doing wrt to us.
1849 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1850 RTPArrivalStats * arrival)
1856 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1858 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1860 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1865 on_new_ssrc (sess, source);
1867 rtp_session_process_rb (sess, source, packet, arrival);
1868 g_object_unref (source);
1871 /* Get SDES items and store them in the SSRC */
1873 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1874 RTPArrivalStats * arrival)
1877 gboolean more_items, more_entries;
1879 items = gst_rtcp_packet_sdes_get_item_count (packet);
1880 GST_DEBUG ("got SDES packet with %d items", items);
1882 more_items = gst_rtcp_packet_sdes_first_item (packet);
1884 while (more_items) {
1886 gboolean changed, created, validated;
1890 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1892 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1896 /* find src, no probation when dealing with RTCP */
1897 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1901 sdes = gst_structure_new ("application/x-rtp-source-sdes", NULL);
1903 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1905 while (more_entries) {
1906 GstRTCPSDESType type;
1912 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1914 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1917 if (type == GST_RTCP_SDES_PRIV) {
1918 name = g_strndup ((const gchar *) &data[1], data[0]);
1920 data += data[0] + 1;
1922 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
1925 value = g_strndup ((const gchar *) data, len);
1927 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
1932 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1936 /* takes ownership of sdes */
1937 changed = rtp_source_set_sdes_struct (source, sdes);
1939 validated = !RTP_SOURCE_IS_ACTIVE (source);
1940 source->validated = TRUE;
1942 /* source became active */
1944 sess->stats.active_sources++;
1945 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1946 sess->stats.active_sources);
1947 on_ssrc_validated (sess, source);
1951 on_new_ssrc (sess, source);
1953 on_ssrc_sdes (sess, source);
1955 g_object_unref (source);
1957 more_items = gst_rtcp_packet_sdes_next_item (packet);
1962 /* BYE is sent when a client leaves the session
1965 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
1966 RTPArrivalStats * arrival)
1970 gboolean reconsider = FALSE;
1972 reason = gst_rtcp_packet_bye_get_reason (packet);
1973 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
1975 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
1976 for (i = 0; i < count; i++) {
1979 gboolean created, prevactive, prevsender;
1980 guint pmembers, members;
1982 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
1983 GST_DEBUG ("SSRC: %08x", ssrc);
1985 if (ssrc == sess->source->ssrc)
1988 /* find src and mark bye, no probation when dealing with RTCP */
1989 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1993 /* store time for when we need to time out this source */
1994 source->bye_time = arrival->current_time;
1996 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1997 prevsender = RTP_SOURCE_IS_SENDER (source);
1999 /* let the source handle the rest */
2000 rtp_source_process_bye (source, reason);
2002 pmembers = sess->stats.active_sources;
2004 if (prevactive && !RTP_SOURCE_IS_ACTIVE (source)) {
2005 sess->stats.active_sources--;
2006 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
2007 sess->stats.active_sources);
2009 if (prevsender && !RTP_SOURCE_IS_SENDER (source)) {
2010 sess->stats.sender_sources--;
2011 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
2012 sess->stats.sender_sources);
2014 members = sess->stats.active_sources;
2016 if (!sess->source->received_bye && members < pmembers) {
2017 /* some members went away since the previous timeout estimate.
2018 * Perform reverse reconsideration but only when we are not scheduling a
2020 if (arrival->current_time < sess->next_rtcp_check_time) {
2021 GstClockTime time_remaining;
2023 time_remaining = sess->next_rtcp_check_time - arrival->current_time;
2024 sess->next_rtcp_check_time =
2025 gst_util_uint64_scale (time_remaining, members, pmembers);
2027 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
2028 GST_TIME_ARGS (sess->next_rtcp_check_time));
2030 sess->next_rtcp_check_time += arrival->current_time;
2032 /* mark pending reconsider. We only want to signal the reconsideration
2033 * once after we handled all the source in the bye packet */
2039 on_new_ssrc (sess, source);
2041 on_bye_ssrc (sess, source);
2043 g_object_unref (source);
2046 RTP_SESSION_UNLOCK (sess);
2047 /* notify app of reconsideration */
2048 if (sess->callbacks.reconsider)
2049 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2050 RTP_SESSION_LOCK (sess);
2056 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
2057 RTPArrivalStats * arrival)
2059 GST_DEBUG ("received APP");
2063 rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
2064 guint32 media_ssrc, GstClockTime current_time)
2067 guint32 round_trip = 0;
2069 if (!sess->callbacks.request_key_unit)
2072 src = g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
2073 GINT_TO_POINTER (sender_ssrc));
2078 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE &&
2079 rtp_source_get_last_rb (src, NULL, NULL, NULL, NULL, NULL, NULL,
2081 GstClockTime round_trip_in_ns = gst_util_uint64_scale (round_trip,
2084 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE &&
2085 current_time - sess->last_keyframe_request < round_trip_in_ns) {
2086 GST_DEBUG ("Ignoring PLI because one was send without one RTT (%"
2087 GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")",
2088 GST_TIME_ARGS (current_time - sess->last_keyframe_request),
2089 GST_TIME_ARGS (round_trip_in_ns));;
2094 sess->last_keyframe_request = current_time;
2096 GST_LOG ("received PLI from %X %p(%p)", sender_ssrc,
2097 sess->callbacks.process_rtp, sess->callbacks.request_key_unit);
2099 sess->callbacks.request_key_unit (sess, FALSE,
2100 sess->request_key_unit_user_data);
2104 rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
2105 RTPArrivalStats * arrival, GstClockTime current_time)
2107 GstRTCPType type = gst_rtcp_packet_get_type (packet);
2108 GstRTCPFBType fbtype = gst_rtcp_packet_fb_get_type (packet);
2109 guint32 sender_ssrc = gst_rtcp_packet_fb_get_sender_ssrc (packet);
2110 guint32 media_ssrc = gst_rtcp_packet_fb_get_media_ssrc (packet);
2111 guint length = 4 * (gst_rtcp_packet_get_length (packet) - 2);
2113 GST_DEBUG ("received feedback %d:%d from %08X about %08X"
2114 " with FCI of length %d", type, fbtype, sender_ssrc, media_ssrc, length);
2116 if (g_signal_has_handler_pending (sess,
2117 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0, TRUE)) {
2118 GstBuffer *fci = NULL;
2121 fci = gst_buffer_create_sub (packet->buffer, packet->offset + 72, length);
2122 GST_BUFFER_TIMESTAMP (fci) = arrival->running_time;
2125 RTP_SESSION_UNLOCK (sess);
2126 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0,
2127 type, fbtype, sender_ssrc, media_ssrc, fci);
2128 RTP_SESSION_LOCK (sess);
2131 gst_buffer_unref (fci);
2134 if (sess->rtcp_feedback_retention_window) {
2135 RTPSource *src = g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
2136 GINT_TO_POINTER (media_ssrc));
2139 rtp_source_retain_rtcp_packet (src, packet, arrival->running_time);
2142 if (rtp_source_get_ssrc (sess->source) == media_ssrc) {
2144 case GST_RTCP_TYPE_PSFB:
2146 case GST_RTCP_PSFB_TYPE_PLI:
2147 rtp_session_process_pli (sess, sender_ssrc, media_ssrc,
2154 case GST_RTCP_TYPE_RTPFB:
2162 * rtp_session_process_rtcp:
2163 * @sess: and #RTPSession
2164 * @buffer: an RTCP buffer
2165 * @current_time: the current system time
2166 * @ntpnstime: the current NTP time in nanoseconds
2168 * Process an RTCP buffer in the session manager. This function takes ownership
2171 * Returns: a #GstFlowReturn.
2174 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
2175 GstClockTime current_time, guint64 ntpnstime)
2177 GstRTCPPacket packet;
2178 gboolean more, is_bye = FALSE, do_sync = FALSE;
2179 RTPArrivalStats arrival;
2180 GstFlowReturn result = GST_FLOW_OK;
2182 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2183 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2185 if (!gst_rtcp_buffer_validate (buffer))
2186 goto invalid_packet;
2188 GST_DEBUG ("received RTCP packet");
2190 RTP_SESSION_LOCK (sess);
2191 /* update arrival stats */
2192 update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1,
2198 /* start processing the compound packet */
2199 more = gst_rtcp_buffer_get_first_packet (buffer, &packet);
2203 type = gst_rtcp_packet_get_type (&packet);
2205 /* when we are leaving the session, we should ignore all non-BYE messages */
2206 if (sess->source->received_bye && type != GST_RTCP_TYPE_BYE) {
2207 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
2212 case GST_RTCP_TYPE_SR:
2213 rtp_session_process_sr (sess, &packet, &arrival, &do_sync);
2215 case GST_RTCP_TYPE_RR:
2216 rtp_session_process_rr (sess, &packet, &arrival);
2218 case GST_RTCP_TYPE_SDES:
2219 rtp_session_process_sdes (sess, &packet, &arrival);
2221 case GST_RTCP_TYPE_BYE:
2223 /* don't try to attempt lip-sync anymore for streams with a BYE */
2225 rtp_session_process_bye (sess, &packet, &arrival);
2227 case GST_RTCP_TYPE_APP:
2228 rtp_session_process_app (sess, &packet, &arrival);
2230 case GST_RTCP_TYPE_RTPFB:
2231 case GST_RTCP_TYPE_PSFB:
2232 rtp_session_process_feedback (sess, &packet, &arrival, current_time);
2235 GST_WARNING ("got unknown RTCP packet");
2239 more = gst_rtcp_packet_move_to_next (&packet);
2242 /* if we are scheduling a BYE, we only want to count bye packets, else we
2243 * count everything */
2244 if (sess->source->received_bye) {
2246 sess->stats.bye_members++;
2247 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2250 /* keep track of average packet size */
2251 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2253 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
2254 sess->stats.avg_rtcp_packet_size, arrival.bytes);
2255 RTP_SESSION_UNLOCK (sess);
2257 /* notify caller of sr packets in the callback */
2258 if (do_sync && sess->callbacks.sync_rtcp) {
2259 /* make writable, we might want to change the buffer */
2260 buffer = gst_buffer_make_metadata_writable (buffer);
2262 result = sess->callbacks.sync_rtcp (sess, sess->source, buffer,
2263 sess->sync_rtcp_user_data);
2265 gst_buffer_unref (buffer);
2272 GST_DEBUG ("invalid RTCP packet received");
2273 gst_buffer_unref (buffer);
2278 gst_buffer_unref (buffer);
2279 RTP_SESSION_UNLOCK (sess);
2280 GST_DEBUG ("ignoring RTP packet because we left");
2286 * rtp_session_send_rtp:
2287 * @sess: an #RTPSession
2288 * @data: pointer to either an RTP buffer or a list of RTP buffers
2289 * @is_list: TRUE when @data is a buffer list
2290 * @current_time: the current system time
2291 * @running_time: the running time of @data
2293 * Send the RTP buffer in the session manager. This function takes ownership of
2296 * Returns: a #GstFlowReturn.
2299 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
2300 GstClockTime current_time, GstClockTime running_time)
2302 GstFlowReturn result;
2304 gboolean prevsender;
2305 gboolean valid_packet;
2308 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2309 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
2312 valid_packet = gst_rtp_buffer_list_validate (GST_BUFFER_LIST_CAST (data));
2314 valid_packet = gst_rtp_buffer_validate (GST_BUFFER_CAST (data));
2318 goto invalid_packet;
2320 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
2322 RTP_SESSION_LOCK (sess);
2323 source = sess->source;
2325 /* update last activity */
2326 source->last_rtp_activity = current_time;
2328 prevsender = RTP_SOURCE_IS_SENDER (source);
2329 oldrate = source->bitrate;
2331 /* we use our own source to send */
2332 result = rtp_source_send_rtp (source, data, is_list, running_time);
2334 if (RTP_SOURCE_IS_SENDER (source) && !prevsender)
2335 sess->stats.sender_sources++;
2336 if (oldrate != source->bitrate)
2337 sess->recalc_bandwidth = TRUE;
2338 RTP_SESSION_UNLOCK (sess);
2345 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
2346 GST_DEBUG ("invalid RTP packet received");
2352 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
2354 *bandwidth += source->bitrate;
2358 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2361 GstClockTime result;
2363 /* recalculate bandwidth when it changed */
2364 if (sess->recalc_bandwidth) {
2367 if (sess->bandwidth > 0)
2368 bandwidth = sess->bandwidth;
2370 /* If it is <= 0, then try to estimate the actual bandwidth */
2371 bandwidth = sess->source->bitrate;
2373 g_hash_table_foreach (sess->cnames, (GHFunc) add_bitrates, &bandwidth);
2377 bandwidth = RTP_STATS_BANDWIDTH;
2379 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
2380 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
2382 sess->recalc_bandwidth = FALSE;
2385 if (sess->source->received_bye) {
2386 result = rtp_stats_calculate_bye_interval (&sess->stats);
2388 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2389 RTP_SOURCE_IS_SENDER (sess->source), first);
2392 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2393 GST_TIME_ARGS (result), first);
2395 if (!deterministic && result != GST_CLOCK_TIME_NONE)
2396 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2398 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2403 /* Stop the current @sess and schedule a BYE message for the other members.
2404 * One must have the session lock to call this function
2406 static GstFlowReturn
2407 rtp_session_schedule_bye_locked (RTPSession * sess, const gchar * reason,
2408 GstClockTime current_time)
2410 GstFlowReturn result = GST_FLOW_OK;
2412 GstClockTime interval;
2414 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2416 source = sess->source;
2418 /* ignore more BYEs */
2419 if (source->received_bye)
2422 /* we have BYE now */
2423 source->received_bye = TRUE;
2424 /* at least one member wants to send a BYE */
2425 g_free (sess->bye_reason);
2426 sess->bye_reason = g_strdup (reason);
2427 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
2428 sess->stats.bye_members = 1;
2429 sess->first_rtcp = TRUE;
2430 sess->sent_bye = FALSE;
2431 sess->allow_early = TRUE;
2433 /* reschedule transmission */
2434 sess->last_rtcp_send_time = current_time;
2435 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2436 sess->next_rtcp_check_time = current_time + interval;
2438 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2439 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2441 RTP_SESSION_UNLOCK (sess);
2442 /* notify app of reconsideration */
2443 if (sess->callbacks.reconsider)
2444 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2445 RTP_SESSION_LOCK (sess);
2452 * rtp_session_schedule_bye:
2453 * @sess: an #RTPSession
2454 * @reason: a reason or NULL
2455 * @current_time: the current system time
2457 * Stop the current @sess and schedule a BYE message for the other members.
2459 * Returns: a #GstFlowReturn.
2462 rtp_session_schedule_bye (RTPSession * sess, const gchar * reason,
2463 GstClockTime current_time)
2465 GstFlowReturn result = GST_FLOW_OK;
2467 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2469 RTP_SESSION_LOCK (sess);
2470 result = rtp_session_schedule_bye_locked (sess, reason, current_time);
2471 RTP_SESSION_UNLOCK (sess);
2477 * rtp_session_next_timeout:
2478 * @sess: an #RTPSession
2479 * @current_time: the current system time
2481 * Get the next time we should perform session maintenance tasks.
2483 * Returns: a time when rtp_session_on_timeout() should be called with the
2484 * current system time.
2487 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2489 GstClockTime result, interval = 0;
2491 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
2493 RTP_SESSION_LOCK (sess);
2495 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
2496 result = sess->next_early_rtcp_time;
2500 result = sess->next_rtcp_check_time;
2502 GST_DEBUG ("current time: %" GST_TIME_FORMAT ", next :%" GST_TIME_FORMAT,
2503 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2505 if (result < current_time) {
2506 GST_DEBUG ("take current time as base");
2507 /* our previous check time expired, start counting from the current time
2509 result = current_time;
2512 if (sess->source->received_bye) {
2513 if (sess->sent_bye) {
2514 GST_DEBUG ("we sent BYE already");
2515 interval = GST_CLOCK_TIME_NONE;
2516 } else if (sess->stats.active_sources >= 50) {
2517 GST_DEBUG ("reconsider BYE, more than 50 sources");
2518 /* reconsider BYE if members >= 50 */
2519 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2522 if (sess->first_rtcp) {
2523 GST_DEBUG ("first RTCP packet");
2524 /* we are called for the first time */
2525 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2526 } else if (sess->next_rtcp_check_time < current_time) {
2527 GST_DEBUG ("old check time expired, getting new timeout");
2528 /* get a new timeout when we need to */
2529 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
2533 if (interval != GST_CLOCK_TIME_NONE)
2536 result = GST_CLOCK_TIME_NONE;
2538 sess->next_rtcp_check_time = result;
2542 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2543 ", next time: %" GST_TIME_FORMAT,
2544 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2545 RTP_SESSION_UNLOCK (sess);
2554 GstClockTime current_time;
2556 GstClockTime running_time;
2557 GstClockTime interval;
2558 GstRTCPPacket packet;
2562 gboolean may_suppress;
2566 session_start_rtcp (RTPSession * sess, ReportData * data)
2568 GstRTCPPacket *packet = &data->packet;
2569 RTPSource *own = sess->source;
2571 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2573 if (RTP_SOURCE_IS_SENDER (own)) {
2576 guint32 packet_count, octet_count;
2578 /* we are a sender, create SR */
2579 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2580 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SR, packet);
2582 /* get latest stats */
2583 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
2584 &ntptime, &rtptime, &packet_count, &octet_count);
2586 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2587 packet_count, octet_count);
2589 /* fill in sender report info */
2590 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2591 ntptime, rtptime, packet_count, octet_count);
2593 /* we are only receiver, create RR */
2594 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2595 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_RR, packet);
2596 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2600 /* construct a Sender or Receiver Report */
2602 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2604 RTPSession *sess = data->sess;
2605 GstRTCPPacket *packet = &data->packet;
2607 /* create a new buffer if needed */
2608 if (data->rtcp == NULL) {
2609 session_start_rtcp (sess, data);
2610 } else if (data->is_early) {
2611 /* Put a single RR or SR in minimal compound packets */
2614 if (gst_rtcp_packet_get_rb_count (packet) < GST_RTCP_MAX_RB_COUNT) {
2615 /* only report about other sender sources */
2616 if (source != sess->source && RTP_SOURCE_IS_SENDER (source)) {
2617 guint8 fractionlost;
2619 guint32 exthighestseq, jitter;
2623 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2624 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2626 /* store last generated RR packet */
2627 source->last_rr.is_valid = TRUE;
2628 source->last_rr.fractionlost = fractionlost;
2629 source->last_rr.packetslost = packetslost;
2630 source->last_rr.exthighestseq = exthighestseq;
2631 source->last_rr.jitter = jitter;
2632 source->last_rr.lsr = lsr;
2633 source->last_rr.dlsr = dlsr;
2635 /* packet is not yet filled, add report block for this source. */
2636 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2637 exthighestseq, jitter, lsr, dlsr);
2642 /* perform cleanup of sources that timed out */
2644 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2646 gboolean remove = FALSE;
2647 gboolean byetimeout = FALSE;
2648 gboolean sendertimeout = FALSE;
2649 gboolean is_sender, is_active;
2650 RTPSession *sess = data->sess;
2651 GstClockTime interval;
2653 is_sender = RTP_SOURCE_IS_SENDER (source);
2654 is_active = RTP_SOURCE_IS_ACTIVE (source);
2656 /* check for our own source, we don't want to delete our own source. */
2657 if (!(source == sess->source)) {
2658 if (source->received_bye) {
2659 /* if we received a BYE from the source, remove the source after some
2661 if (data->current_time > source->bye_time &&
2662 data->current_time - source->bye_time > sess->stats.bye_timeout) {
2663 GST_DEBUG ("removing BYE source %08x", source->ssrc);
2668 /* sources that were inactive for more than 5 times the deterministic reporting
2669 * interval get timed out. the min timeout is 5 seconds. */
2670 if (data->current_time > source->last_activity) {
2671 interval = MAX (data->interval * 5, 5 * GST_SECOND);
2672 if (data->current_time - source->last_activity > interval) {
2673 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2674 source->ssrc, GST_TIME_ARGS (source->last_activity));
2680 /* senders that did not send for a long time become a receiver, this also
2681 * holds for our own source. */
2683 if (data->current_time > source->last_rtp_activity) {
2684 interval = MAX (data->interval * 2, 5 * GST_SECOND);
2685 if (data->current_time - source->last_rtp_activity > interval) {
2686 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
2687 GST_TIME_FORMAT, source->ssrc,
2688 GST_TIME_ARGS (source->last_rtp_activity));
2689 source->is_sender = FALSE;
2690 sess->stats.sender_sources--;
2691 sendertimeout = TRUE;
2697 sess->total_sources--;
2699 sess->stats.sender_sources--;
2701 sess->stats.active_sources--;
2704 on_bye_timeout (sess, source);
2706 on_timeout (sess, source);
2709 on_sender_timeout (sess, source);
2712 source->closing = remove;
2716 session_sdes (RTPSession * sess, ReportData * data)
2718 GstRTCPPacket *packet = &data->packet;
2719 const GstStructure *sdes;
2722 /* add SDES packet */
2723 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SDES, packet);
2725 gst_rtcp_packet_sdes_add_item (packet, sess->source->ssrc);
2727 sdes = rtp_source_get_sdes_struct (sess->source);
2729 /* add all fields in the structure, the order is not important. */
2730 n_fields = gst_structure_n_fields (sdes);
2731 for (i = 0; i < n_fields; ++i) {
2734 GstRTCPSDESType type;
2736 field = gst_structure_nth_field_name (sdes, i);
2739 value = gst_structure_get_string (sdes, field);
2742 type = gst_rtcp_sdes_name_to_type (field);
2744 /* Early packets are minimal and only include the CNAME */
2745 if (data->is_early && type != GST_RTCP_SDES_CNAME)
2748 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
2749 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
2750 (const guint8 *) value);
2751 } else if (type == GST_RTCP_SDES_PRIV) {
2757 /* don't accept entries that are too big */
2758 prefix_len = strlen (field);
2759 if (prefix_len > 255)
2761 value_len = strlen (value);
2762 if (value_len > 255)
2764 data_len = 1 + prefix_len + value_len;
2768 data[0] = prefix_len;
2769 memcpy (&data[1], field, prefix_len);
2770 memcpy (&data[1 + prefix_len], value, value_len);
2772 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
2776 data->has_sdes = TRUE;
2779 /* schedule a BYE packet */
2781 session_bye (RTPSession * sess, ReportData * data)
2783 GstRTCPPacket *packet = &data->packet;
2786 session_start_rtcp (sess, data);
2789 session_sdes (sess, data);
2791 /* add a BYE packet */
2792 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_BYE, packet);
2793 gst_rtcp_packet_bye_add_ssrc (packet, sess->source->ssrc);
2794 if (sess->bye_reason)
2795 gst_rtcp_packet_bye_set_reason (packet, sess->bye_reason);
2797 /* we have a BYE packet now */
2798 data->is_bye = TRUE;
2802 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
2804 GstClockTime new_send_time, elapsed;
2806 if (data->is_early && sess->next_early_rtcp_time < current_time)
2809 /* no need to check yet */
2810 if (sess->next_rtcp_check_time > current_time) {
2811 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
2812 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
2813 GST_TIME_ARGS (current_time));
2817 /* get elapsed time since we last reported */
2818 elapsed = current_time - sess->last_rtcp_send_time;
2820 /* perform forward reconsideration */
2821 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, data->interval);
2823 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
2824 GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time), GST_TIME_ARGS (elapsed));
2826 new_send_time += sess->last_rtcp_send_time;
2828 /* check if reconsideration */
2829 if (current_time < new_send_time) {
2830 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
2831 GST_TIME_ARGS (new_send_time));
2832 /* store new check time */
2833 sess->next_rtcp_check_time = new_send_time;
2839 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
2841 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
2842 GST_TIME_ARGS (new_send_time));
2843 sess->next_rtcp_check_time = current_time + new_send_time;
2845 /* Apply the rules from RFC 4585 section 3.5.3 */
2846 if (sess->stats.min_interval != 0 && !sess->first_rtcp) {
2847 GstClockTimeDiff T_rr_current_interval = g_random_double_range (0.5, 1.5) *
2848 sess->stats.min_interval;
2850 /* This will caused the RTCP to be suppressed if no FB packets are added */
2851 if (sess->last_rtcp_send_time + T_rr_current_interval >
2852 sess->next_rtcp_check_time) {
2853 GST_DEBUG ("RTCP packet could be suppressed min: %" GST_TIME_FORMAT
2854 " last: %" GST_TIME_FORMAT
2855 " + T_rr_current_interval: %" GST_TIME_FORMAT
2856 " > sess->next_rtcp_check_time: %" GST_TIME_FORMAT,
2857 GST_TIME_ARGS (sess->stats.min_interval),
2858 GST_TIME_ARGS (sess->last_rtcp_send_time),
2859 GST_TIME_ARGS (T_rr_current_interval),
2860 GST_TIME_ARGS (sess->next_rtcp_check_time));
2861 data->may_suppress = TRUE;
2869 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
2871 g_hash_table_insert (hash_table, key, g_object_ref (source));
2875 remove_closing_sources (const gchar * key, RTPSource * source, gpointer * data)
2877 return source->closing;
2881 * rtp_session_on_timeout:
2882 * @sess: an #RTPSession
2883 * @current_time: the current system time
2884 * @ntpnstime: the current NTP time in nanoseconds
2885 * @running_time: the current running_time of the pipeline
2887 * Perform maintenance actions after the timeout obtained with
2888 * rtp_session_next_timeout() expired.
2890 * This function will perform timeouts of receivers and senders, send a BYE
2891 * packet or generate RTCP packets with current session stats.
2893 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
2894 * times, for each packet that should be processed.
2896 * Returns: a #GstFlowReturn.
2899 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
2900 guint64 ntpnstime, GstClockTime running_time)
2902 GstFlowReturn result = GST_FLOW_OK;
2905 GHashTable *table_copy;
2906 gboolean notify = FALSE;
2908 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2910 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT,
2911 GST_TIME_ARGS (current_time), GST_TIME_ARGS (ntpnstime));
2915 data.current_time = current_time;
2916 data.ntpnstime = ntpnstime;
2917 data.is_bye = FALSE;
2918 data.has_sdes = FALSE;
2919 data.may_suppress = FALSE;
2920 data.running_time = running_time;
2924 RTP_SESSION_LOCK (sess);
2925 /* get a new interval, we need this for various cleanups etc */
2926 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
2928 /* Make a local copy of the hashtable. We need to do this because the
2929 * cleanup stage below releases the session lock. */
2930 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
2931 (GDestroyNotify) g_object_unref);
2932 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2933 (GHFunc) clone_ssrcs_hashtable, table_copy);
2935 /* Clean up the session, mark the source for removing, this might release the
2937 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
2938 g_hash_table_destroy (table_copy);
2940 /* Now remove the marked sources */
2941 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
2942 (GHRFunc) remove_closing_sources, NULL);
2944 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
2945 data.is_early = TRUE;
2947 data.is_early = FALSE;
2949 /* see if we need to generate SR or RR packets */
2950 if (is_rtcp_time (sess, current_time, &data)) {
2951 if (own->received_bye) {
2952 /* generate BYE instead */
2953 GST_DEBUG ("generating BYE message");
2954 session_bye (sess, &data);
2955 sess->sent_bye = TRUE;
2957 /* loop over all known sources and do something */
2958 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2959 (GHFunc) session_report_blocks, &data);
2964 /* we keep track of the last report time in order to timeout inactive
2965 * receivers or senders */
2966 if (!data.is_early && !data.may_suppress)
2967 sess->last_rtcp_send_time = data.current_time;
2968 sess->first_rtcp = FALSE;
2969 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
2971 /* add SDES for this source when not already added */
2973 session_sdes (sess, &data);
2976 /* check for outdated collisions */
2977 GST_DEBUG ("Timing out collisions");
2978 rtp_source_timeout (sess->source, current_time,
2979 data.interval * RTCP_INTERVAL_COLLISION_TIMEOUT,
2980 running_time - sess->rtcp_feedback_retention_window);
2982 if (sess->change_ssrc) {
2983 GST_DEBUG ("need to change our SSRC (%08x)", own->ssrc);
2984 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
2985 GINT_TO_POINTER (own->ssrc));
2987 own->ssrc = rtp_session_create_new_ssrc (sess);
2988 rtp_source_reset (own);
2990 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
2991 GINT_TO_POINTER (own->ssrc), own);
2993 g_free (sess->bye_reason);
2994 sess->bye_reason = NULL;
2995 sess->sent_bye = FALSE;
2996 sess->change_ssrc = FALSE;
2998 GST_DEBUG ("changed our SSRC to %08x", own->ssrc);
3001 sess->allow_early = TRUE;
3003 RTP_SESSION_UNLOCK (sess);
3006 g_object_notify (G_OBJECT (sess), "internal-ssrc");
3008 /* push out the RTCP packet */
3010 gboolean do_not_suppress;
3012 /* Give the user a change to add its own packet */
3013 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
3014 data.rtcp, data.is_early, &do_not_suppress);
3016 if (sess->callbacks.send_rtcp && (do_not_suppress || !data.may_suppress)) {
3019 /* close the RTCP packet */
3020 gst_rtcp_buffer_end (data.rtcp);
3022 packet_size = GST_BUFFER_SIZE (data.rtcp) + sess->header_len;
3024 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
3025 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
3026 sess->stats.avg_rtcp_packet_size, packet_size);
3028 sess->callbacks.send_rtcp (sess, own, data.rtcp, sess->sent_bye,
3029 sess->send_rtcp_user_data);
3031 GST_DEBUG ("freeing packet callback: %p"
3032 " do_not_suppress: %d may_suppress: %d",
3033 sess->callbacks.send_rtcp, do_not_suppress, data.may_suppress);
3034 gst_buffer_unref (data.rtcp);
3042 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
3043 GstClockTimeDiff max_delay)
3045 GstClockTime T_dither_max;
3047 /* Implements the algorithm described in RFC 4585 section 3.5.2 */
3049 RTP_SESSION_LOCK (sess);
3051 /* Check if already requested */
3052 /* RFC 4585 section 3.5.2 step 2 */
3053 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3056 /* Ignore the request a scheduled packet will be in time anyway */
3057 if (current_time + max_delay > sess->next_rtcp_check_time)
3060 /* RFC 4585 section 3.5.2 step 2b */
3061 /* If the total sources is <=2, then there is only us and one peer */
3062 if (sess->total_sources <= 2) {
3065 /* Divide by 2 because l = 0.5 */
3066 T_dither_max = sess->next_rtcp_check_time - sess->last_rtcp_send_time;
3070 /* RFC 4585 section 3.5.2 step 3 */
3071 if (current_time + T_dither_max > sess->next_rtcp_check_time)
3074 /* RFC 4585 section 3.5.2 step 4 */
3075 if (sess->allow_early == FALSE)
3079 /* Schedule an early transmission later */
3080 sess->next_early_rtcp_time = g_random_double () * T_dither_max +
3083 /* If no dithering, schedule it for NOW */
3084 sess->next_early_rtcp_time = current_time;
3087 RTP_SESSION_UNLOCK (sess);
3089 /* notify app of need to send packet early
3090 * and therefore of timeout change */
3091 if (sess->callbacks.reconsider)
3092 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
3098 RTP_SESSION_UNLOCK (sess);
3103 rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc, gboolean fir)
3110 for (i = 0; i < sess->rtcp_pli_requests->len; i++)
3111 if (ssrc == g_array_index (sess->rtcp_pli_requests, guint32, i))
3114 g_array_append_val (sess->rtcp_pli_requests, ssrc);
3118 has_pli_compare_func (gconstpointer a, gconstpointer ignored)
3120 GstRTCPPacket packet;
3122 packet.buffer = (GstBuffer *) a;
3125 if (gst_rtcp_packet_get_type (&packet) == GST_RTCP_TYPE_PSFB &&
3126 gst_rtcp_packet_fb_get_type (&packet) == GST_RTCP_PSFB_TYPE_PLI)
3133 rtp_session_on_sending_rtcp (RTPSession * sess, GstBuffer * buffer,
3136 gboolean ret = FALSE;
3138 RTP_SESSION_LOCK (sess);
3140 while (sess->rtcp_pli_requests->len) {
3141 GstRTCPPacket rtcppacket;
3142 guint media_ssrc = g_array_index (sess->rtcp_pli_requests, guint32, 0);
3143 RTPSource *media_src = g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
3144 GUINT_TO_POINTER (media_ssrc));
3146 if (media_src && !rtp_source_has_retained (media_src,
3147 has_pli_compare_func, NULL)) {
3148 if (gst_rtcp_buffer_add_packet (buffer, GST_RTCP_TYPE_PSFB, &rtcppacket)) {
3149 gst_rtcp_packet_fb_set_type (&rtcppacket, GST_RTCP_PSFB_TYPE_PLI);
3150 gst_rtcp_packet_fb_set_sender_ssrc (&rtcppacket,
3151 rtp_source_get_ssrc (sess->source));
3152 gst_rtcp_packet_fb_set_media_ssrc (&rtcppacket, media_ssrc);
3155 /* Break because the packet is full, will put next request in a
3162 g_array_remove_index (sess->rtcp_pli_requests, 0);
3165 RTP_SESSION_UNLOCK (sess);
3171 rtp_session_send_rtcp (RTPSession * sess, GstClockTimeDiff max_delay)
3175 if (!sess->callbacks.send_rtcp)
3178 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
3180 rtp_session_request_early_rtcp (sess, now, max_delay);