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 */
1619 arrival->have_address = GST_IS_NETBUFFER (buffer);
1620 if (arrival->have_address) {
1621 GstNetBuffer *netbuf = (GstNetBuffer *) buffer;
1623 memcpy (&arrival->address, &netbuf->from, sizeof (GstNetAddress));
1626 arrival->have_address = FALSE;
1631 * rtp_session_process_rtp:
1632 * @sess: and #RTPSession
1633 * @buffer: an RTP buffer
1634 * @current_time: the current system time
1635 * @running_time: the running_time of @buffer
1637 * Process an RTP buffer in the session manager. This function takes ownership
1640 * Returns: a #GstFlowReturn.
1643 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1644 GstClockTime current_time, GstClockTime running_time)
1646 GstFlowReturn result;
1650 gboolean prevsender, prevactive;
1651 RTPArrivalStats arrival;
1656 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1657 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1659 if (!gst_rtp_buffer_validate (buffer))
1660 goto invalid_packet;
1662 RTP_SESSION_LOCK (sess);
1663 /* update arrival stats */
1664 update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1667 /* ignore more RTP packets when we left the session */
1668 if (sess->source->received_bye)
1671 /* get SSRC and look up in session database */
1672 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1673 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1677 prevsender = RTP_SOURCE_IS_SENDER (source);
1678 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1679 oldrate = source->bitrate;
1681 /* copy available csrc for later */
1682 count = gst_rtp_buffer_get_csrc_count (buffer);
1683 /* make sure to not overflow our array. An RTP buffer can maximally contain
1685 count = MIN (count, 16);
1687 for (i = 0; i < count; i++)
1688 csrcs[i] = gst_rtp_buffer_get_csrc (buffer, i);
1690 /* let source process the packet */
1691 result = rtp_source_process_rtp (source, buffer, &arrival);
1693 /* source became active */
1694 if (prevactive != RTP_SOURCE_IS_ACTIVE (source)) {
1695 sess->stats.active_sources++;
1696 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1697 sess->stats.active_sources);
1698 on_ssrc_validated (sess, source);
1700 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1701 sess->stats.sender_sources++;
1702 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1703 sess->stats.sender_sources);
1705 if (oldrate != source->bitrate)
1706 sess->recalc_bandwidth = TRUE;
1709 on_new_ssrc (sess, source);
1711 if (source->validated) {
1714 /* for validated sources, we add the CSRCs as well */
1715 for (i = 0; i < count; i++) {
1717 RTPSource *csrc_src;
1722 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1727 GST_DEBUG ("created new CSRC: %08x", csrc);
1728 rtp_source_set_as_csrc (csrc_src);
1729 if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1730 sess->stats.active_sources++;
1731 on_new_ssrc (sess, csrc_src);
1733 g_object_unref (csrc_src);
1736 g_object_unref (source);
1738 RTP_SESSION_UNLOCK (sess);
1745 gst_buffer_unref (buffer);
1746 GST_DEBUG ("invalid RTP packet received");
1751 gst_buffer_unref (buffer);
1752 RTP_SESSION_UNLOCK (sess);
1753 GST_DEBUG ("ignoring RTP packet because we are leaving");
1758 gst_buffer_unref (buffer);
1759 RTP_SESSION_UNLOCK (sess);
1760 GST_DEBUG ("ignoring packet because its collisioning");
1766 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1767 GstRTCPPacket * packet, RTPArrivalStats * arrival)
1771 count = gst_rtcp_packet_get_rb_count (packet);
1772 for (i = 0; i < count; i++) {
1773 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1774 guint8 fractionlost;
1777 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1778 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1780 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1782 if (ssrc == sess->source->ssrc) {
1783 /* only deal with report blocks for our session, we update the stats of
1784 * the sender of the RTCP message. We could also compare our stats against
1785 * the other sender to see if we are better or worse. */
1786 rtp_source_process_rb (source, arrival->ntpnstime, fractionlost,
1787 packetslost, exthighestseq, jitter, lsr, dlsr);
1790 on_ssrc_active (sess, source);
1793 /* A Sender report contains statistics about how the sender is doing. This
1794 * includes timing informataion such as the relation between RTP and NTP
1795 * timestamps and the number of packets/bytes it sent to us.
1797 * In this report is also included a set of report blocks related to how this
1798 * sender is receiving data (in case we (or somebody else) is also sending stuff
1799 * to it). This info includes the packet loss, jitter and seqnum. It also
1800 * contains information to calculate the round trip time (LSR/DLSR).
1803 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1804 RTPArrivalStats * arrival, gboolean * do_sync)
1806 guint32 senderssrc, rtptime, packet_count, octet_count;
1809 gboolean created, prevsender;
1811 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1812 &packet_count, &octet_count);
1814 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1815 senderssrc, GST_TIME_ARGS (arrival->current_time));
1817 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1821 /* don't try to do lip-sync for sources that sent a BYE */
1822 if (rtp_source_received_bye (source))
1827 prevsender = RTP_SOURCE_IS_SENDER (source);
1829 /* first update the source */
1830 rtp_source_process_sr (source, arrival->current_time, ntptime, rtptime,
1831 packet_count, octet_count);
1833 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1834 sess->stats.sender_sources++;
1835 GST_DEBUG ("source: %08x became sender, %d sender sources", senderssrc,
1836 sess->stats.sender_sources);
1840 on_new_ssrc (sess, source);
1842 rtp_session_process_rb (sess, source, packet, arrival);
1843 g_object_unref (source);
1846 /* A receiver report contains statistics about how a receiver is doing. It
1847 * includes stuff like packet loss, jitter and the seqnum it received last. It
1848 * also contains info to calculate the round trip time.
1850 * We are only interested in how the sender of this report is doing wrt to us.
1853 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1854 RTPArrivalStats * arrival)
1860 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1862 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1864 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1869 on_new_ssrc (sess, source);
1871 rtp_session_process_rb (sess, source, packet, arrival);
1872 g_object_unref (source);
1875 /* Get SDES items and store them in the SSRC */
1877 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1878 RTPArrivalStats * arrival)
1881 gboolean more_items, more_entries;
1883 items = gst_rtcp_packet_sdes_get_item_count (packet);
1884 GST_DEBUG ("got SDES packet with %d items", items);
1886 more_items = gst_rtcp_packet_sdes_first_item (packet);
1888 while (more_items) {
1890 gboolean changed, created, validated;
1894 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1896 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1900 /* find src, no probation when dealing with RTCP */
1901 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1905 sdes = gst_structure_new ("application/x-rtp-source-sdes", NULL);
1907 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1909 while (more_entries) {
1910 GstRTCPSDESType type;
1916 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1918 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1921 if (type == GST_RTCP_SDES_PRIV) {
1922 name = g_strndup ((const gchar *) &data[1], data[0]);
1924 data += data[0] + 1;
1926 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
1929 value = g_strndup ((const gchar *) data, len);
1931 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
1936 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1940 /* takes ownership of sdes */
1941 changed = rtp_source_set_sdes_struct (source, sdes);
1943 validated = !RTP_SOURCE_IS_ACTIVE (source);
1944 source->validated = TRUE;
1947 on_new_ssrc (sess, source);
1949 on_ssrc_validated (sess, source);
1951 on_ssrc_sdes (sess, source);
1953 g_object_unref (source);
1955 more_items = gst_rtcp_packet_sdes_next_item (packet);
1960 /* BYE is sent when a client leaves the session
1963 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
1964 RTPArrivalStats * arrival)
1968 gboolean reconsider = FALSE;
1970 reason = gst_rtcp_packet_bye_get_reason (packet);
1971 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
1973 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
1974 for (i = 0; i < count; i++) {
1977 gboolean created, prevactive, prevsender;
1978 guint pmembers, members;
1980 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
1981 GST_DEBUG ("SSRC: %08x", ssrc);
1983 /* find src and mark bye, no probation when dealing with RTCP */
1984 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1988 /* store time for when we need to time out this source */
1989 source->bye_time = arrival->current_time;
1991 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1992 prevsender = RTP_SOURCE_IS_SENDER (source);
1994 /* let the source handle the rest */
1995 rtp_source_process_bye (source, reason);
1997 pmembers = sess->stats.active_sources;
1999 if (prevactive && !RTP_SOURCE_IS_ACTIVE (source)) {
2000 sess->stats.active_sources--;
2001 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
2002 sess->stats.active_sources);
2004 if (prevsender && !RTP_SOURCE_IS_SENDER (source)) {
2005 sess->stats.sender_sources--;
2006 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
2007 sess->stats.sender_sources);
2009 members = sess->stats.active_sources;
2011 if (!sess->source->received_bye && members < pmembers) {
2012 /* some members went away since the previous timeout estimate.
2013 * Perform reverse reconsideration but only when we are not scheduling a
2015 if (arrival->current_time < sess->next_rtcp_check_time) {
2016 GstClockTime time_remaining;
2018 time_remaining = sess->next_rtcp_check_time - arrival->current_time;
2019 sess->next_rtcp_check_time =
2020 gst_util_uint64_scale (time_remaining, members, pmembers);
2022 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
2023 GST_TIME_ARGS (sess->next_rtcp_check_time));
2025 sess->next_rtcp_check_time += arrival->current_time;
2027 /* mark pending reconsider. We only want to signal the reconsideration
2028 * once after we handled all the source in the bye packet */
2034 on_new_ssrc (sess, source);
2036 on_bye_ssrc (sess, source);
2038 g_object_unref (source);
2041 RTP_SESSION_UNLOCK (sess);
2042 /* notify app of reconsideration */
2043 if (sess->callbacks.reconsider)
2044 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2045 RTP_SESSION_LOCK (sess);
2051 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
2052 RTPArrivalStats * arrival)
2054 GST_DEBUG ("received APP");
2058 rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
2059 guint32 media_ssrc, GstClockTime current_time)
2062 guint32 round_trip = 0;
2064 if (!sess->callbacks.request_key_unit)
2067 src = g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
2068 GINT_TO_POINTER (sender_ssrc));
2073 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE &&
2074 rtp_source_get_last_rb (src, NULL, NULL, NULL, NULL, NULL, NULL,
2076 GstClockTime round_trip_in_ns = gst_util_uint64_scale (round_trip,
2079 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE &&
2080 current_time - sess->last_keyframe_request < round_trip_in_ns) {
2081 GST_DEBUG ("Ignoring PLI because one was send without one RTT (%"
2082 GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")",
2083 GST_TIME_ARGS (current_time - sess->last_keyframe_request),
2084 GST_TIME_ARGS (round_trip_in_ns));;
2089 sess->last_keyframe_request = current_time;
2091 GST_LOG ("received PLI from %X %p(%p)", sender_ssrc,
2092 sess->callbacks.process_rtp, sess->callbacks.request_key_unit);
2094 sess->callbacks.request_key_unit (sess, FALSE,
2095 sess->request_key_unit_user_data);
2099 rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
2100 RTPArrivalStats * arrival, GstClockTime current_time)
2102 GstRTCPType type = gst_rtcp_packet_get_type (packet);
2103 GstRTCPFBType fbtype = gst_rtcp_packet_fb_get_type (packet);
2104 guint32 sender_ssrc = gst_rtcp_packet_fb_get_sender_ssrc (packet);
2105 guint32 media_ssrc = gst_rtcp_packet_fb_get_media_ssrc (packet);
2106 guint length = 4 * (gst_rtcp_packet_get_length (packet) - 2);
2108 GST_DEBUG ("received feedback %d:%d from %08X about %08X"
2109 " with FCI of length %d", type, fbtype, sender_ssrc, media_ssrc, length);
2111 if (g_signal_has_handler_pending (sess,
2112 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0, TRUE)) {
2113 GstBuffer *fci = NULL;
2116 fci = gst_buffer_create_sub (packet->buffer, packet->offset + 72, length);
2117 GST_BUFFER_TIMESTAMP (fci) = arrival->running_time;
2120 RTP_SESSION_UNLOCK (sess);
2121 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0,
2122 type, fbtype, sender_ssrc, media_ssrc, fci);
2123 RTP_SESSION_LOCK (sess);
2126 gst_buffer_unref (fci);
2129 if (sess->rtcp_feedback_retention_window) {
2130 RTPSource *src = g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
2131 GINT_TO_POINTER (media_ssrc));
2134 rtp_source_retain_rtcp_packet (src, packet, arrival->running_time);
2137 if (rtp_source_get_ssrc (sess->source) == media_ssrc) {
2139 case GST_RTCP_TYPE_PSFB:
2141 case GST_RTCP_PSFB_TYPE_PLI:
2142 rtp_session_process_pli (sess, sender_ssrc, media_ssrc,
2149 case GST_RTCP_TYPE_RTPFB:
2157 * rtp_session_process_rtcp:
2158 * @sess: and #RTPSession
2159 * @buffer: an RTCP buffer
2160 * @current_time: the current system time
2161 * @ntpnstime: the current NTP time in nanoseconds
2163 * Process an RTCP buffer in the session manager. This function takes ownership
2166 * Returns: a #GstFlowReturn.
2169 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
2170 GstClockTime current_time, guint64 ntpnstime)
2172 GstRTCPPacket packet;
2173 gboolean more, is_bye = FALSE, do_sync = FALSE;
2174 RTPArrivalStats arrival;
2175 GstFlowReturn result = GST_FLOW_OK;
2177 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2178 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2180 if (!gst_rtcp_buffer_validate (buffer))
2181 goto invalid_packet;
2183 GST_DEBUG ("received RTCP packet");
2185 RTP_SESSION_LOCK (sess);
2186 /* update arrival stats */
2187 update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1,
2193 /* start processing the compound packet */
2194 more = gst_rtcp_buffer_get_first_packet (buffer, &packet);
2198 type = gst_rtcp_packet_get_type (&packet);
2200 /* when we are leaving the session, we should ignore all non-BYE messages */
2201 if (sess->source->received_bye && type != GST_RTCP_TYPE_BYE) {
2202 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
2207 case GST_RTCP_TYPE_SR:
2208 rtp_session_process_sr (sess, &packet, &arrival, &do_sync);
2210 case GST_RTCP_TYPE_RR:
2211 rtp_session_process_rr (sess, &packet, &arrival);
2213 case GST_RTCP_TYPE_SDES:
2214 rtp_session_process_sdes (sess, &packet, &arrival);
2216 case GST_RTCP_TYPE_BYE:
2218 /* don't try to attempt lip-sync anymore for streams with a BYE */
2220 rtp_session_process_bye (sess, &packet, &arrival);
2222 case GST_RTCP_TYPE_APP:
2223 rtp_session_process_app (sess, &packet, &arrival);
2225 case GST_RTCP_TYPE_RTPFB:
2226 case GST_RTCP_TYPE_PSFB:
2227 rtp_session_process_feedback (sess, &packet, &arrival, current_time);
2230 GST_WARNING ("got unknown RTCP packet");
2234 more = gst_rtcp_packet_move_to_next (&packet);
2237 /* if we are scheduling a BYE, we only want to count bye packets, else we
2238 * count everything */
2239 if (sess->source->received_bye) {
2241 sess->stats.bye_members++;
2242 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2245 /* keep track of average packet size */
2246 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2248 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
2249 sess->stats.avg_rtcp_packet_size, arrival.bytes);
2250 RTP_SESSION_UNLOCK (sess);
2252 /* notify caller of sr packets in the callback */
2253 if (do_sync && sess->callbacks.sync_rtcp) {
2254 /* make writable, we might want to change the buffer */
2255 buffer = gst_buffer_make_metadata_writable (buffer);
2257 result = sess->callbacks.sync_rtcp (sess, sess->source, buffer,
2258 sess->sync_rtcp_user_data);
2260 gst_buffer_unref (buffer);
2267 GST_DEBUG ("invalid RTCP packet received");
2268 gst_buffer_unref (buffer);
2273 gst_buffer_unref (buffer);
2274 RTP_SESSION_UNLOCK (sess);
2275 GST_DEBUG ("ignoring RTP packet because we left");
2281 * rtp_session_send_rtp:
2282 * @sess: an #RTPSession
2283 * @data: pointer to either an RTP buffer or a list of RTP buffers
2284 * @is_list: TRUE when @data is a buffer list
2285 * @current_time: the current system time
2286 * @running_time: the running time of @data
2288 * Send the RTP buffer in the session manager. This function takes ownership of
2291 * Returns: a #GstFlowReturn.
2294 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
2295 GstClockTime current_time, GstClockTime running_time)
2297 GstFlowReturn result;
2299 gboolean prevsender;
2300 gboolean valid_packet;
2303 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2304 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
2307 valid_packet = gst_rtp_buffer_list_validate (GST_BUFFER_LIST_CAST (data));
2309 valid_packet = gst_rtp_buffer_validate (GST_BUFFER_CAST (data));
2313 goto invalid_packet;
2315 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
2317 RTP_SESSION_LOCK (sess);
2318 source = sess->source;
2320 /* update last activity */
2321 source->last_rtp_activity = current_time;
2323 prevsender = RTP_SOURCE_IS_SENDER (source);
2324 oldrate = source->bitrate;
2326 /* we use our own source to send */
2327 result = rtp_source_send_rtp (source, data, is_list, running_time);
2329 if (RTP_SOURCE_IS_SENDER (source) && !prevsender)
2330 sess->stats.sender_sources++;
2331 if (oldrate != source->bitrate)
2332 sess->recalc_bandwidth = TRUE;
2333 RTP_SESSION_UNLOCK (sess);
2340 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
2341 GST_DEBUG ("invalid RTP packet received");
2347 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
2349 *bandwidth += source->bitrate;
2353 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2356 GstClockTime result;
2358 /* recalculate bandwidth when it changed */
2359 if (sess->recalc_bandwidth) {
2362 if (sess->bandwidth > 0)
2363 bandwidth = sess->bandwidth;
2365 /* If it is <= 0, then try to estimate the actual bandwidth */
2366 bandwidth = sess->source->bitrate;
2368 g_hash_table_foreach (sess->cnames, (GHFunc) add_bitrates, &bandwidth);
2372 bandwidth = RTP_STATS_BANDWIDTH;
2374 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
2375 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
2377 sess->recalc_bandwidth = FALSE;
2380 if (sess->source->received_bye) {
2381 result = rtp_stats_calculate_bye_interval (&sess->stats);
2383 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2384 RTP_SOURCE_IS_SENDER (sess->source), first);
2387 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2388 GST_TIME_ARGS (result), first);
2390 if (!deterministic && result != GST_CLOCK_TIME_NONE)
2391 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2393 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2398 /* Stop the current @sess and schedule a BYE message for the other members.
2399 * One must have the session lock to call this function
2401 static GstFlowReturn
2402 rtp_session_schedule_bye_locked (RTPSession * sess, const gchar * reason,
2403 GstClockTime current_time)
2405 GstFlowReturn result = GST_FLOW_OK;
2407 GstClockTime interval;
2409 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2411 source = sess->source;
2413 /* ignore more BYEs */
2414 if (source->received_bye)
2417 /* we have BYE now */
2418 source->received_bye = TRUE;
2419 /* at least one member wants to send a BYE */
2420 g_free (sess->bye_reason);
2421 sess->bye_reason = g_strdup (reason);
2422 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
2423 sess->stats.bye_members = 1;
2424 sess->first_rtcp = TRUE;
2425 sess->sent_bye = FALSE;
2426 sess->allow_early = TRUE;
2428 /* reschedule transmission */
2429 sess->last_rtcp_send_time = current_time;
2430 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2431 sess->next_rtcp_check_time = current_time + interval;
2433 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2434 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2436 RTP_SESSION_UNLOCK (sess);
2437 /* notify app of reconsideration */
2438 if (sess->callbacks.reconsider)
2439 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2440 RTP_SESSION_LOCK (sess);
2447 * rtp_session_schedule_bye:
2448 * @sess: an #RTPSession
2449 * @reason: a reason or NULL
2450 * @current_time: the current system time
2452 * Stop the current @sess and schedule a BYE message for the other members.
2454 * Returns: a #GstFlowReturn.
2457 rtp_session_schedule_bye (RTPSession * sess, const gchar * reason,
2458 GstClockTime current_time)
2460 GstFlowReturn result = GST_FLOW_OK;
2462 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2464 RTP_SESSION_LOCK (sess);
2465 result = rtp_session_schedule_bye_locked (sess, reason, current_time);
2466 RTP_SESSION_UNLOCK (sess);
2472 * rtp_session_next_timeout:
2473 * @sess: an #RTPSession
2474 * @current_time: the current system time
2476 * Get the next time we should perform session maintenance tasks.
2478 * Returns: a time when rtp_session_on_timeout() should be called with the
2479 * current system time.
2482 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2484 GstClockTime result, interval = 0;
2486 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
2488 RTP_SESSION_LOCK (sess);
2490 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
2491 result = sess->next_early_rtcp_time;
2495 result = sess->next_rtcp_check_time;
2497 GST_DEBUG ("current time: %" GST_TIME_FORMAT ", next :%" GST_TIME_FORMAT,
2498 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2500 if (result < current_time) {
2501 GST_DEBUG ("take current time as base");
2502 /* our previous check time expired, start counting from the current time
2504 result = current_time;
2507 if (sess->source->received_bye) {
2508 if (sess->sent_bye) {
2509 GST_DEBUG ("we sent BYE already");
2510 interval = GST_CLOCK_TIME_NONE;
2511 } else if (sess->stats.active_sources >= 50) {
2512 GST_DEBUG ("reconsider BYE, more than 50 sources");
2513 /* reconsider BYE if members >= 50 */
2514 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2517 if (sess->first_rtcp) {
2518 GST_DEBUG ("first RTCP packet");
2519 /* we are called for the first time */
2520 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2521 } else if (sess->next_rtcp_check_time < current_time) {
2522 GST_DEBUG ("old check time expired, getting new timeout");
2523 /* get a new timeout when we need to */
2524 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
2528 if (interval != GST_CLOCK_TIME_NONE)
2531 result = GST_CLOCK_TIME_NONE;
2533 sess->next_rtcp_check_time = result;
2537 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2538 ", next time: %" GST_TIME_FORMAT,
2539 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2540 RTP_SESSION_UNLOCK (sess);
2549 GstClockTime current_time;
2551 GstClockTime running_time;
2552 GstClockTime interval;
2553 GstRTCPPacket packet;
2557 gboolean may_suppress;
2561 session_start_rtcp (RTPSession * sess, ReportData * data)
2563 GstRTCPPacket *packet = &data->packet;
2564 RTPSource *own = sess->source;
2566 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2568 if (RTP_SOURCE_IS_SENDER (own)) {
2571 guint32 packet_count, octet_count;
2573 /* we are a sender, create SR */
2574 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2575 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SR, packet);
2577 /* get latest stats */
2578 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
2579 &ntptime, &rtptime, &packet_count, &octet_count);
2581 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2582 packet_count, octet_count);
2584 /* fill in sender report info */
2585 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2586 ntptime, rtptime, packet_count, octet_count);
2588 /* we are only receiver, create RR */
2589 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2590 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_RR, packet);
2591 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2595 /* construct a Sender or Receiver Report */
2597 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2599 RTPSession *sess = data->sess;
2600 GstRTCPPacket *packet = &data->packet;
2602 /* create a new buffer if needed */
2603 if (data->rtcp == NULL) {
2604 session_start_rtcp (sess, data);
2605 } else if (data->is_early) {
2606 /* Put a single RR or SR in minimal compound packets */
2609 if (gst_rtcp_packet_get_rb_count (packet) < GST_RTCP_MAX_RB_COUNT) {
2610 /* only report about other sender sources */
2611 if (source != sess->source && RTP_SOURCE_IS_SENDER (source)) {
2612 guint8 fractionlost;
2614 guint32 exthighestseq, jitter;
2618 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2619 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2621 /* store last generated RR packet */
2622 source->last_rr.is_valid = TRUE;
2623 source->last_rr.fractionlost = fractionlost;
2624 source->last_rr.packetslost = packetslost;
2625 source->last_rr.exthighestseq = exthighestseq;
2626 source->last_rr.jitter = jitter;
2627 source->last_rr.lsr = lsr;
2628 source->last_rr.dlsr = dlsr;
2630 /* packet is not yet filled, add report block for this source. */
2631 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2632 exthighestseq, jitter, lsr, dlsr);
2637 /* perform cleanup of sources that timed out */
2639 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2641 gboolean remove = FALSE;
2642 gboolean byetimeout = FALSE;
2643 gboolean sendertimeout = FALSE;
2644 gboolean is_sender, is_active;
2645 RTPSession *sess = data->sess;
2646 GstClockTime interval;
2648 is_sender = RTP_SOURCE_IS_SENDER (source);
2649 is_active = RTP_SOURCE_IS_ACTIVE (source);
2651 /* check for our own source, we don't want to delete our own source. */
2652 if (!(source == sess->source)) {
2653 if (source->received_bye) {
2654 /* if we received a BYE from the source, remove the source after some
2656 if (data->current_time > source->bye_time &&
2657 data->current_time - source->bye_time > sess->stats.bye_timeout) {
2658 GST_DEBUG ("removing BYE source %08x", source->ssrc);
2663 /* sources that were inactive for more than 5 times the deterministic reporting
2664 * interval get timed out. the min timeout is 5 seconds. */
2665 if (data->current_time > source->last_activity) {
2666 interval = MAX (data->interval * 5, 5 * GST_SECOND);
2667 if (data->current_time - source->last_activity > interval) {
2668 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2669 source->ssrc, GST_TIME_ARGS (source->last_activity));
2675 /* senders that did not send for a long time become a receiver, this also
2676 * holds for our own source. */
2678 if (data->current_time > source->last_rtp_activity) {
2679 interval = MAX (data->interval * 2, 5 * GST_SECOND);
2680 if (data->current_time - source->last_rtp_activity > interval) {
2681 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
2682 GST_TIME_FORMAT, source->ssrc,
2683 GST_TIME_ARGS (source->last_rtp_activity));
2684 source->is_sender = FALSE;
2685 sess->stats.sender_sources--;
2686 sendertimeout = TRUE;
2692 sess->total_sources--;
2694 sess->stats.sender_sources--;
2696 sess->stats.active_sources--;
2699 on_bye_timeout (sess, source);
2701 on_timeout (sess, source);
2704 on_sender_timeout (sess, source);
2707 source->closing = remove;
2711 session_sdes (RTPSession * sess, ReportData * data)
2713 GstRTCPPacket *packet = &data->packet;
2714 const GstStructure *sdes;
2717 /* add SDES packet */
2718 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SDES, packet);
2720 gst_rtcp_packet_sdes_add_item (packet, sess->source->ssrc);
2722 sdes = rtp_source_get_sdes_struct (sess->source);
2724 /* add all fields in the structure, the order is not important. */
2725 n_fields = gst_structure_n_fields (sdes);
2726 for (i = 0; i < n_fields; ++i) {
2729 GstRTCPSDESType type;
2731 field = gst_structure_nth_field_name (sdes, i);
2734 value = gst_structure_get_string (sdes, field);
2737 type = gst_rtcp_sdes_name_to_type (field);
2739 /* Early packets are minimal and only include the CNAME */
2740 if (data->is_early && type != GST_RTCP_SDES_CNAME)
2743 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
2744 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
2745 (const guint8 *) value);
2746 } else if (type == GST_RTCP_SDES_PRIV) {
2752 /* don't accept entries that are too big */
2753 prefix_len = strlen (field);
2754 if (prefix_len > 255)
2756 value_len = strlen (value);
2757 if (value_len > 255)
2759 data_len = 1 + prefix_len + value_len;
2763 data[0] = prefix_len;
2764 memcpy (&data[1], field, prefix_len);
2765 memcpy (&data[1 + prefix_len], value, value_len);
2767 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
2771 data->has_sdes = TRUE;
2774 /* schedule a BYE packet */
2776 session_bye (RTPSession * sess, ReportData * data)
2778 GstRTCPPacket *packet = &data->packet;
2781 session_start_rtcp (sess, data);
2784 session_sdes (sess, data);
2786 /* add a BYE packet */
2787 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_BYE, packet);
2788 gst_rtcp_packet_bye_add_ssrc (packet, sess->source->ssrc);
2789 if (sess->bye_reason)
2790 gst_rtcp_packet_bye_set_reason (packet, sess->bye_reason);
2792 /* we have a BYE packet now */
2793 data->is_bye = TRUE;
2797 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
2799 GstClockTime new_send_time, elapsed;
2801 if (data->is_early && sess->next_early_rtcp_time < current_time)
2804 /* no need to check yet */
2805 if (sess->next_rtcp_check_time > current_time) {
2806 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
2807 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
2808 GST_TIME_ARGS (current_time));
2812 /* get elapsed time since we last reported */
2813 elapsed = current_time - sess->last_rtcp_send_time;
2815 /* perform forward reconsideration */
2816 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, data->interval);
2818 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
2819 GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time), GST_TIME_ARGS (elapsed));
2821 new_send_time += sess->last_rtcp_send_time;
2823 /* check if reconsideration */
2824 if (current_time < new_send_time) {
2825 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
2826 GST_TIME_ARGS (new_send_time));
2827 /* store new check time */
2828 sess->next_rtcp_check_time = new_send_time;
2834 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
2836 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
2837 GST_TIME_ARGS (new_send_time));
2838 sess->next_rtcp_check_time = current_time + new_send_time;
2840 /* Apply the rules from RFC 4585 section 3.5.3 */
2841 if (sess->stats.min_interval != 0 && !sess->first_rtcp) {
2842 GstClockTimeDiff T_rr_current_interval = g_random_double_range (0.5, 1.5) *
2843 sess->stats.min_interval;
2845 /* This will caused the RTCP to be suppressed if no FB packets are added */
2846 if (sess->last_rtcp_send_time + T_rr_current_interval >
2847 sess->next_rtcp_check_time) {
2848 GST_DEBUG ("RTCP packet could be suppressed min: %" GST_TIME_FORMAT
2849 " last: %" GST_TIME_FORMAT
2850 " + T_rr_current_interval: %" GST_TIME_FORMAT
2851 " > sess->next_rtcp_check_time: %" GST_TIME_FORMAT,
2852 GST_TIME_ARGS (sess->stats.min_interval),
2853 GST_TIME_ARGS (sess->last_rtcp_send_time),
2854 GST_TIME_ARGS (T_rr_current_interval),
2855 GST_TIME_ARGS (sess->next_rtcp_check_time));
2856 data->may_suppress = TRUE;
2864 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
2866 g_hash_table_insert (hash_table, key, g_object_ref (source));
2870 remove_closing_sources (const gchar * key, RTPSource * source, gpointer * data)
2872 return source->closing;
2876 * rtp_session_on_timeout:
2877 * @sess: an #RTPSession
2878 * @current_time: the current system time
2879 * @ntpnstime: the current NTP time in nanoseconds
2880 * @running_time: the current running_time of the pipeline
2882 * Perform maintenance actions after the timeout obtained with
2883 * rtp_session_next_timeout() expired.
2885 * This function will perform timeouts of receivers and senders, send a BYE
2886 * packet or generate RTCP packets with current session stats.
2888 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
2889 * times, for each packet that should be processed.
2891 * Returns: a #GstFlowReturn.
2894 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
2895 guint64 ntpnstime, GstClockTime running_time)
2897 GstFlowReturn result = GST_FLOW_OK;
2900 GHashTable *table_copy;
2901 gboolean notify = FALSE;
2903 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2905 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT,
2906 GST_TIME_ARGS (current_time), GST_TIME_ARGS (ntpnstime));
2910 data.current_time = current_time;
2911 data.ntpnstime = ntpnstime;
2912 data.is_bye = FALSE;
2913 data.has_sdes = FALSE;
2914 data.may_suppress = FALSE;
2915 data.running_time = running_time;
2919 RTP_SESSION_LOCK (sess);
2920 /* get a new interval, we need this for various cleanups etc */
2921 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
2923 /* Make a local copy of the hashtable. We need to do this because the
2924 * cleanup stage below releases the session lock. */
2925 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
2926 (GDestroyNotify) g_object_unref);
2927 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2928 (GHFunc) clone_ssrcs_hashtable, table_copy);
2930 /* Clean up the session, mark the source for removing, this might release the
2932 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
2933 g_hash_table_destroy (table_copy);
2935 /* Now remove the marked sources */
2936 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
2937 (GHRFunc) remove_closing_sources, NULL);
2939 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
2940 data.is_early = TRUE;
2942 data.is_early = FALSE;
2944 /* see if we need to generate SR or RR packets */
2945 if (is_rtcp_time (sess, current_time, &data)) {
2946 if (own->received_bye) {
2947 /* generate BYE instead */
2948 GST_DEBUG ("generating BYE message");
2949 session_bye (sess, &data);
2950 sess->sent_bye = TRUE;
2952 /* loop over all known sources and do something */
2953 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2954 (GHFunc) session_report_blocks, &data);
2959 /* we keep track of the last report time in order to timeout inactive
2960 * receivers or senders */
2961 if (!data.is_early && !data.may_suppress)
2962 sess->last_rtcp_send_time = data.current_time;
2963 sess->first_rtcp = FALSE;
2964 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
2966 /* add SDES for this source when not already added */
2968 session_sdes (sess, &data);
2971 /* check for outdated collisions */
2972 GST_DEBUG ("Timing out collisions");
2973 rtp_source_timeout (sess->source, current_time,
2974 data.interval * RTCP_INTERVAL_COLLISION_TIMEOUT,
2975 running_time - sess->rtcp_feedback_retention_window);
2977 if (sess->change_ssrc) {
2978 GST_DEBUG ("need to change our SSRC (%08x)", own->ssrc);
2979 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
2980 GINT_TO_POINTER (own->ssrc));
2982 own->ssrc = rtp_session_create_new_ssrc (sess);
2983 rtp_source_reset (own);
2985 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
2986 GINT_TO_POINTER (own->ssrc), own);
2988 g_free (sess->bye_reason);
2989 sess->bye_reason = NULL;
2990 sess->sent_bye = FALSE;
2991 sess->change_ssrc = FALSE;
2993 GST_DEBUG ("changed our SSRC to %08x", own->ssrc);
2996 sess->allow_early = TRUE;
2998 RTP_SESSION_UNLOCK (sess);
3001 g_object_notify (G_OBJECT (sess), "internal-ssrc");
3003 /* push out the RTCP packet */
3005 gboolean do_not_suppress;
3007 /* Give the user a change to add its own packet */
3008 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
3009 data.rtcp, data.is_early, &do_not_suppress);
3011 if (sess->callbacks.send_rtcp && (do_not_suppress || !data.may_suppress)) {
3014 /* close the RTCP packet */
3015 gst_rtcp_buffer_end (data.rtcp);
3017 packet_size = GST_BUFFER_SIZE (data.rtcp) + sess->header_len;
3019 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
3020 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
3021 sess->stats.avg_rtcp_packet_size, packet_size);
3023 sess->callbacks.send_rtcp (sess, own, data.rtcp, sess->sent_bye,
3024 sess->send_rtcp_user_data);
3026 GST_DEBUG ("freeing packet callback: %p"
3027 " do_not_suppress: %d may_suppress: %d",
3028 sess->callbacks.send_rtcp, do_not_suppress, data.may_suppress);
3029 gst_buffer_unref (data.rtcp);
3037 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
3038 GstClockTimeDiff max_delay)
3040 GstClockTime T_dither_max;
3042 /* Implements the algorithm described in RFC 4585 section 3.5.2 */
3044 RTP_SESSION_LOCK (sess);
3046 /* Check if already requested */
3047 /* RFC 4585 section 3.5.2 step 2 */
3048 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3051 /* Ignore the request a scheduled packet will be in time anyway */
3052 if (current_time + max_delay > sess->next_rtcp_check_time)
3055 /* RFC 4585 section 3.5.2 step 2b */
3056 /* If the total sources is <=2, then there is only us and one peer */
3057 if (sess->total_sources <= 2) {
3060 /* Divide by 2 because l = 0.5 */
3061 T_dither_max = sess->next_rtcp_check_time - sess->last_rtcp_send_time;
3065 /* RFC 4585 section 3.5.2 step 3 */
3066 if (current_time + T_dither_max > sess->next_rtcp_check_time)
3069 /* RFC 4585 section 3.5.2 step 4 */
3070 if (sess->allow_early == FALSE)
3074 /* Schedule an early transmission later */
3075 sess->next_early_rtcp_time = g_random_double () * T_dither_max +
3078 /* If no dithering, schedule it for NOW */
3079 sess->next_early_rtcp_time = current_time;
3082 RTP_SESSION_UNLOCK (sess);
3084 /* notify app of need to send packet early
3085 * and therefore of timeout change */
3086 if (sess->callbacks.reconsider)
3087 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
3093 RTP_SESSION_UNLOCK (sess);
3098 rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc, gboolean fir)
3105 for (i = 0; i < sess->rtcp_pli_requests->len; i++)
3106 if (ssrc == g_array_index (sess->rtcp_pli_requests, guint32, i))
3109 g_array_append_val (sess->rtcp_pli_requests, ssrc);
3113 has_pli_compare_func (gconstpointer a, gconstpointer ignored)
3115 GstRTCPPacket packet;
3117 packet.buffer = (GstBuffer *) a;
3120 if (gst_rtcp_packet_get_type (&packet) == GST_RTCP_TYPE_PSFB &&
3121 gst_rtcp_packet_fb_get_type (&packet) == GST_RTCP_PSFB_TYPE_PLI)
3128 rtp_session_on_sending_rtcp (RTPSession * sess, GstBuffer * buffer,
3131 gboolean ret = FALSE;
3133 RTP_SESSION_LOCK (sess);
3135 while (sess->rtcp_pli_requests->len) {
3136 GstRTCPPacket rtcppacket;
3137 guint media_ssrc = g_array_index (sess->rtcp_pli_requests, guint32, 0);
3138 RTPSource *media_src = g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
3139 GUINT_TO_POINTER (media_ssrc));
3141 if (media_src && !rtp_source_has_retained (media_src,
3142 has_pli_compare_func, NULL)) {
3143 if (gst_rtcp_buffer_add_packet (buffer, GST_RTCP_TYPE_PSFB, &rtcppacket)) {
3144 gst_rtcp_packet_fb_set_type (&rtcppacket, GST_RTCP_PSFB_TYPE_PLI);
3145 gst_rtcp_packet_fb_set_sender_ssrc (&rtcppacket,
3146 rtp_source_get_ssrc (sess->source));
3147 gst_rtcp_packet_fb_set_media_ssrc (&rtcppacket, media_ssrc);
3150 /* Break because the packet is full, will put next request in a
3157 g_array_remove_index (sess->rtcp_pli_requests, 0);
3160 RTP_SESSION_UNLOCK (sess);
3166 rtp_session_send_rtcp (RTPSession * sess, GstClockTimeDiff max_delay)
3170 if (!sess->callbacks.send_rtcp)
3173 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
3175 rtp_session_request_early_rtcp (sess, now, max_delay);