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 gst_rtp_bin_marshal_BOOLEAN__MINIOBJECT_BOOLEAN (GClosure * closure,
135 GValue * return_value G_GNUC_UNUSED, guint n_param_values,
136 const GValue * param_values, gpointer invocation_hint G_GNUC_UNUSED,
137 gpointer marshal_data)
139 typedef gboolean (*GMarshalFunc_BOOLEAN__MINIOBJECT_BOOLEAN) (gpointer data1,
140 gpointer arg_1, gboolean arg_2, gpointer data2);
141 register GMarshalFunc_BOOLEAN__MINIOBJECT_BOOLEAN callback;
142 register GCClosure *cc = (GCClosure *) closure;
143 register gpointer data1, data2;
146 g_return_if_fail (return_value != NULL);
147 g_return_if_fail (n_param_values == 3);
149 if (G_CCLOSURE_SWAP_DATA (closure)) {
150 data1 = closure->data;
151 data2 = g_value_peek_pointer (param_values + 0);
153 data1 = g_value_peek_pointer (param_values + 0);
154 data2 = closure->data;
157 (GMarshalFunc_BOOLEAN__MINIOBJECT_BOOLEAN) (marshal_data ? marshal_data :
160 v_return = callback (data1,
161 gst_value_get_mini_object (param_values + 1),
162 g_value_get_boolean (param_values + 2), data2);
164 g_value_set_boolean (return_value, v_return);
168 gst_rtp_bin_marshal_VOID__UINT_UINT_UINT_UINT_MINIOBJECT (GClosure * closure,
169 GValue * return_value G_GNUC_UNUSED, guint n_param_values,
170 const GValue * param_values, gpointer invocation_hint G_GNUC_UNUSED,
171 gpointer marshal_data)
173 typedef void (*GMarshalFunc_VOID__UINT_UINT_UINT_UINT_MINIOBJECT) (gpointer
174 data1, guint arg_1, guint arg_2, guint arg_3, guint arg_4, gpointer arg_5,
176 register GMarshalFunc_VOID__UINT_UINT_UINT_UINT_MINIOBJECT callback;
177 register GCClosure *cc = (GCClosure *) closure;
178 register gpointer data1, data2;
180 g_return_if_fail (n_param_values == 6);
182 if (G_CCLOSURE_SWAP_DATA (closure)) {
183 data1 = closure->data;
184 data2 = g_value_peek_pointer (param_values + 0);
186 data1 = g_value_peek_pointer (param_values + 0);
187 data2 = closure->data;
190 (GMarshalFunc_VOID__UINT_UINT_UINT_UINT_MINIOBJECT) (marshal_data ?
191 marshal_data : cc->callback);
194 g_value_get_uint (param_values + 1),
195 g_value_get_uint (param_values + 2),
196 g_value_get_uint (param_values + 3),
197 g_value_get_uint (param_values + 4),
198 gst_value_get_mini_object (param_values + 5), data2);
203 rtp_session_class_init (RTPSessionClass * klass)
205 GObjectClass *gobject_class;
207 gobject_class = (GObjectClass *) klass;
209 gobject_class->finalize = rtp_session_finalize;
210 gobject_class->set_property = rtp_session_set_property;
211 gobject_class->get_property = rtp_session_get_property;
214 * RTPSession::get-source-by-ssrc:
215 * @session: the object which received the signal
216 * @ssrc: the SSRC of the RTPSource
218 * Request the #RTPSource object with SSRC @ssrc in @session.
220 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
221 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
222 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
223 get_source_by_ssrc), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
224 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
227 * RTPSession::on-new-ssrc:
228 * @session: the object which received the signal
229 * @src: the new RTPSource
231 * Notify of a new SSRC that entered @session.
233 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
234 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
235 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
236 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
239 * RTPSession::on-ssrc-collision:
240 * @session: the object which received the signal
241 * @src: the #RTPSource that caused a collision
243 * Notify when we have an SSRC collision
245 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
246 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
247 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
248 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
251 * RTPSession::on-ssrc-validated:
252 * @session: the object which received the signal
253 * @src: the new validated RTPSource
255 * Notify of a new SSRC that became validated.
257 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
258 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
259 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
260 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
263 * RTPSession::on-ssrc-active:
264 * @session: the object which received the signal
265 * @src: the active RTPSource
267 * Notify of a SSRC that is active, i.e., sending RTCP.
269 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
270 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
271 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
272 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
275 * RTPSession::on-ssrc-sdes:
276 * @session: the object which received the signal
277 * @src: the RTPSource
279 * Notify that a new SDES was received for SSRC.
281 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
282 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
283 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
284 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
287 * RTPSession::on-bye-ssrc:
288 * @session: the object which received the signal
289 * @src: the RTPSource that went away
291 * Notify of an SSRC that became inactive because of a BYE packet.
293 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
294 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
295 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
296 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
299 * RTPSession::on-bye-timeout:
300 * @session: the object which received the signal
301 * @src: the RTPSource that timed out
303 * Notify of an SSRC that has timed out because of BYE
305 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
306 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
307 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
308 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
311 * RTPSession::on-timeout:
312 * @session: the object which received the signal
313 * @src: the RTPSource that timed out
315 * Notify of an SSRC that has timed out
317 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
318 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
319 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
320 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
323 * RTPSession::on-sender-timeout:
324 * @session: the object which received the signal
325 * @src: the RTPSource that timed out
327 * Notify of an SSRC that was a sender but timed out and became a receiver.
329 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
330 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
331 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
332 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
336 * RTPSession::on-sending-rtcp
337 * @session: the object which received the signal
338 * @buffer: the #GstBuffer containing the RTCP packet about to be sent
339 * @early: %TRUE if the packet is early, %FALSE if it is regular
341 * This signal is emitted before sending an RTCP packet, it can be used
342 * to add extra RTCP Packets.
344 * Returns: %TRUE if the RTCP buffer should NOT be suppressed, %FALSE
345 * if suppressing it is acceptable
347 rtp_session_signals[SIGNAL_ON_SENDING_RTCP] =
348 g_signal_new ("on-sending-rtcp", G_TYPE_FROM_CLASS (klass),
349 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_rtcp),
350 accumulate_trues, NULL, gst_rtp_bin_marshal_BOOLEAN__MINIOBJECT_BOOLEAN,
351 G_TYPE_BOOLEAN, 2, GST_TYPE_BUFFER, G_TYPE_BOOLEAN);
354 * RTPSession::on-feedback-rtcp:
355 * @session: the object which received the signal
356 * @type: Type of RTCP packet, will be %GST_RTCP_TYPE_RTPFB or
357 * %GST_RTCP_TYPE_RTPFB
358 * @fbtype: The type of RTCP FB packet, probably part of #GstRTCPFBType
359 * @sender_ssrc: The SSRC of the sender
360 * @media_ssrc: The SSRC of the media this refers to
361 * @fci: a #GstBuffer with the FCI data from the FB packet or %NULL if
364 * Notify that a RTCP feedback packet has been received
367 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP] =
368 g_signal_new ("on-feedback-rtcp", G_TYPE_FROM_CLASS (klass),
369 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_feedback_rtcp),
370 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_UINT_UINT_UINT_MINIOBJECT,
371 G_TYPE_NONE, 4, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT,
375 * RTPSession::send-rtcp:
376 * @session: the object which received the signal
377 * @max_delay: The maximum delay after which the feedback will not be useful
380 * Requests that the #RTPSession initiate a new RTCP packet as soon as
381 * possible within the requested delay.
384 rtp_session_signals[SIGNAL_SEND_RTCP] =
385 g_signal_new ("send-rtcp", G_TYPE_FROM_CLASS (klass),
386 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
387 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
388 gst_rtp_bin_marshal_VOID__UINT64, G_TYPE_NONE, 1, G_TYPE_UINT64);
390 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
391 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
392 "The internal SSRC used for the session",
393 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
395 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
396 g_param_spec_object ("internal-source", "Internal Source",
397 "The internal source element of the session",
398 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
400 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
401 g_param_spec_double ("bandwidth", "Bandwidth",
402 "The bandwidth of the session (0 for auto-discover)",
403 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
404 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
406 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
407 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
408 "The fraction of the bandwidth used for RTCP (or as a real fraction of the RTP bandwidth if < 1)",
409 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
410 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
412 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
413 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
414 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
415 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
416 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
418 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
419 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
420 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
421 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
422 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
424 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
425 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
426 "The maximum size of the RTCP packets",
427 16, G_MAXINT16, DEFAULT_RTCP_MTU,
428 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
430 g_object_class_install_property (gobject_class, PROP_SDES,
431 g_param_spec_boxed ("sdes", "SDES",
432 "The SDES items of this session",
433 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
435 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
436 g_param_spec_uint ("num-sources", "Num Sources",
437 "The number of sources in the session", 0, G_MAXUINT,
438 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
440 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
441 g_param_spec_uint ("num-active-sources", "Num Active Sources",
442 "The number of active sources in the session", 0, G_MAXUINT,
443 DEFAULT_NUM_ACTIVE_SOURCES,
444 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
448 * Get a GValue Array of all sources in the session.
451 * <title>Getting the #RTPSources of a session
458 * g_object_get (sess, "sources", &arr, NULL);
460 * for (i = 0; i < arr->n_values; i++) {
463 * val = g_value_array_get_nth (arr, i);
464 * source = g_value_get_object (val);
466 * g_value_array_free (arr);
471 g_object_class_install_property (gobject_class, PROP_SOURCES,
472 g_param_spec_boxed ("sources", "Sources",
473 "An array of all known sources in the session",
474 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
476 g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
477 g_param_spec_boolean ("favor-new", "Favor new sources",
478 "Resolve SSRC conflict in favor of new sources", FALSE,
479 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
481 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
482 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
483 "Minimum interval between Regular RTCP packet (in ns)",
484 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
485 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
487 g_object_class_install_property (gobject_class,
488 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
489 g_param_spec_uint64 ("rtcp-feedback-retention-window",
490 "RTCP Feedback retention window",
491 "Duration during which RTCP Feedback packets are retained (in ns)",
492 0, G_MAXUINT64, DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW,
493 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
496 klass->get_source_by_ssrc =
497 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
498 klass->on_sending_rtcp = GST_DEBUG_FUNCPTR (rtp_session_on_sending_rtcp);
499 klass->send_rtcp = GST_DEBUG_FUNCPTR (rtp_session_send_rtcp);
501 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
505 rtp_session_init (RTPSession * sess)
510 sess->lock = g_mutex_new ();
511 sess->key = g_random_int ();
515 for (i = 0; i < 32; i++) {
517 g_hash_table_new_full (NULL, NULL, NULL,
518 (GDestroyNotify) g_object_unref);
520 sess->cnames = g_hash_table_new_full (NULL, NULL, g_free, NULL);
522 rtp_stats_init_defaults (&sess->stats);
524 sess->recalc_bandwidth = TRUE;
525 sess->bandwidth = DEFAULT_BANDWIDTH;
526 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
527 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
528 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
530 /* create an active SSRC for this session manager */
531 sess->source = rtp_session_create_source (sess);
532 sess->source->validated = TRUE;
533 sess->source->internal = TRUE;
534 sess->stats.active_sources++;
535 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
537 /* default UDP header length */
538 sess->header_len = 28;
539 sess->mtu = DEFAULT_RTCP_MTU;
541 /* some default SDES entries */
542 str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
543 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_CNAME, str);
546 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_NAME,
548 rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_TOOL, "GStreamer");
550 sess->first_rtcp = TRUE;
551 sess->allow_early = TRUE;
552 sess->rtcp_feedback_retention_window = DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW;
554 sess->rtcp_pli_requests = g_array_new (FALSE, FALSE, sizeof (guint32));
556 GST_DEBUG ("%p: session using SSRC: %08x", sess, sess->source->ssrc);
560 rtp_session_finalize (GObject * object)
565 sess = RTP_SESSION_CAST (object);
567 g_mutex_free (sess->lock);
568 for (i = 0; i < 32; i++)
569 g_hash_table_destroy (sess->ssrcs[i]);
571 g_free (sess->bye_reason);
573 g_hash_table_destroy (sess->cnames);
574 g_object_unref (sess->source);
576 g_array_free (sess->rtcp_pli_requests, TRUE);
578 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
582 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
584 GValue value = { 0 };
586 g_value_init (&value, RTP_TYPE_SOURCE);
587 g_value_take_object (&value, source);
588 /* copies the value */
589 g_value_array_append (arr, &value);
593 rtp_session_create_sources (RTPSession * sess)
598 RTP_SESSION_LOCK (sess);
599 /* get number of elements in the table */
600 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
601 /* create the result value array */
602 res = g_value_array_new (size);
604 /* and copy all values into the array */
605 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
606 RTP_SESSION_UNLOCK (sess);
612 rtp_session_set_property (GObject * object, guint prop_id,
613 const GValue * value, GParamSpec * pspec)
617 sess = RTP_SESSION (object);
620 case PROP_INTERNAL_SSRC:
621 rtp_session_set_internal_ssrc (sess, g_value_get_uint (value));
624 sess->bandwidth = g_value_get_double (value);
625 sess->recalc_bandwidth = TRUE;
627 case PROP_RTCP_FRACTION:
628 sess->rtcp_bandwidth = g_value_get_double (value);
629 sess->recalc_bandwidth = TRUE;
631 case PROP_RTCP_RR_BANDWIDTH:
632 sess->rtcp_rr_bandwidth = g_value_get_int (value);
633 sess->recalc_bandwidth = TRUE;
635 case PROP_RTCP_RS_BANDWIDTH:
636 sess->rtcp_rs_bandwidth = g_value_get_int (value);
637 sess->recalc_bandwidth = TRUE;
640 sess->mtu = g_value_get_uint (value);
643 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
646 sess->favor_new = g_value_get_boolean (value);
648 case PROP_RTCP_MIN_INTERVAL:
649 rtp_stats_set_min_interval (&sess->stats,
650 (gdouble) g_value_get_uint64 (value) / GST_SECOND);
653 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
659 rtp_session_get_property (GObject * object, guint prop_id,
660 GValue * value, GParamSpec * pspec)
664 sess = RTP_SESSION (object);
667 case PROP_INTERNAL_SSRC:
668 g_value_set_uint (value, rtp_session_get_internal_ssrc (sess));
670 case PROP_INTERNAL_SOURCE:
671 g_value_take_object (value, rtp_session_get_internal_source (sess));
674 g_value_set_double (value, sess->bandwidth);
676 case PROP_RTCP_FRACTION:
677 g_value_set_double (value, sess->rtcp_bandwidth);
679 case PROP_RTCP_RR_BANDWIDTH:
680 g_value_set_int (value, sess->rtcp_rr_bandwidth);
682 case PROP_RTCP_RS_BANDWIDTH:
683 g_value_set_int (value, sess->rtcp_rs_bandwidth);
686 g_value_set_uint (value, sess->mtu);
689 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
691 case PROP_NUM_SOURCES:
692 g_value_set_uint (value, rtp_session_get_num_sources (sess));
694 case PROP_NUM_ACTIVE_SOURCES:
695 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
698 g_value_take_boxed (value, rtp_session_create_sources (sess));
701 g_value_set_boolean (value, sess->favor_new);
703 case PROP_RTCP_MIN_INTERVAL:
704 g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
707 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
713 on_new_ssrc (RTPSession * sess, RTPSource * source)
715 g_object_ref (source);
716 RTP_SESSION_UNLOCK (sess);
717 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
718 RTP_SESSION_LOCK (sess);
719 g_object_unref (source);
723 on_ssrc_collision (RTPSession * sess, RTPSource * source)
725 g_object_ref (source);
726 RTP_SESSION_UNLOCK (sess);
727 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
729 RTP_SESSION_LOCK (sess);
730 g_object_unref (source);
734 on_ssrc_validated (RTPSession * sess, RTPSource * source)
736 g_object_ref (source);
737 RTP_SESSION_UNLOCK (sess);
738 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
740 RTP_SESSION_LOCK (sess);
741 g_object_unref (source);
745 on_ssrc_active (RTPSession * sess, RTPSource * source)
747 g_object_ref (source);
748 RTP_SESSION_UNLOCK (sess);
749 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
750 RTP_SESSION_LOCK (sess);
751 g_object_unref (source);
755 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
757 g_object_ref (source);
758 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
759 RTP_SESSION_UNLOCK (sess);
760 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
761 RTP_SESSION_LOCK (sess);
762 g_object_unref (source);
766 on_bye_ssrc (RTPSession * sess, RTPSource * source)
768 g_object_ref (source);
769 RTP_SESSION_UNLOCK (sess);
770 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
771 RTP_SESSION_LOCK (sess);
772 g_object_unref (source);
776 on_bye_timeout (RTPSession * sess, RTPSource * source)
778 g_object_ref (source);
779 RTP_SESSION_UNLOCK (sess);
780 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
781 RTP_SESSION_LOCK (sess);
782 g_object_unref (source);
786 on_timeout (RTPSession * sess, RTPSource * source)
788 g_object_ref (source);
789 RTP_SESSION_UNLOCK (sess);
790 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
791 RTP_SESSION_LOCK (sess);
792 g_object_unref (source);
796 on_sender_timeout (RTPSession * sess, RTPSource * source)
798 g_object_ref (source);
799 RTP_SESSION_UNLOCK (sess);
800 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
802 RTP_SESSION_LOCK (sess);
803 g_object_unref (source);
809 * Create a new session object.
811 * Returns: a new #RTPSession. g_object_unref() after usage.
814 rtp_session_new (void)
818 sess = g_object_new (RTP_TYPE_SESSION, NULL);
824 * rtp_session_set_callbacks:
825 * @sess: an #RTPSession
826 * @callbacks: callbacks to configure
827 * @user_data: user data passed in the callbacks
829 * Configure a set of callbacks to be notified of actions.
832 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
835 g_return_if_fail (RTP_IS_SESSION (sess));
837 if (callbacks->process_rtp) {
838 sess->callbacks.process_rtp = callbacks->process_rtp;
839 sess->process_rtp_user_data = user_data;
841 if (callbacks->send_rtp) {
842 sess->callbacks.send_rtp = callbacks->send_rtp;
843 sess->send_rtp_user_data = user_data;
845 if (callbacks->send_rtcp) {
846 sess->callbacks.send_rtcp = callbacks->send_rtcp;
847 sess->send_rtcp_user_data = user_data;
849 if (callbacks->sync_rtcp) {
850 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
851 sess->sync_rtcp_user_data = user_data;
853 if (callbacks->clock_rate) {
854 sess->callbacks.clock_rate = callbacks->clock_rate;
855 sess->clock_rate_user_data = user_data;
857 if (callbacks->reconsider) {
858 sess->callbacks.reconsider = callbacks->reconsider;
859 sess->reconsider_user_data = user_data;
861 if (callbacks->request_key_unit) {
862 sess->callbacks.request_key_unit = callbacks->request_key_unit;
863 sess->request_key_unit_user_data = user_data;
865 if (callbacks->request_time) {
866 sess->callbacks.request_time = callbacks->request_time;
867 sess->request_time_user_data = user_data;
872 * rtp_session_set_process_rtp_callback:
873 * @sess: an #RTPSession
874 * @callback: callback to set
875 * @user_data: user data passed in the callback
877 * Configure only the process_rtp callback to be notified of the process_rtp action.
880 rtp_session_set_process_rtp_callback (RTPSession * sess,
881 RTPSessionProcessRTP callback, gpointer user_data)
883 g_return_if_fail (RTP_IS_SESSION (sess));
885 sess->callbacks.process_rtp = callback;
886 sess->process_rtp_user_data = user_data;
890 * rtp_session_set_send_rtp_callback:
891 * @sess: an #RTPSession
892 * @callback: callback to set
893 * @user_data: user data passed in the callback
895 * Configure only the send_rtp callback to be notified of the send_rtp action.
898 rtp_session_set_send_rtp_callback (RTPSession * sess,
899 RTPSessionSendRTP callback, gpointer user_data)
901 g_return_if_fail (RTP_IS_SESSION (sess));
903 sess->callbacks.send_rtp = callback;
904 sess->send_rtp_user_data = user_data;
908 * rtp_session_set_send_rtcp_callback:
909 * @sess: an #RTPSession
910 * @callback: callback to set
911 * @user_data: user data passed in the callback
913 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
916 rtp_session_set_send_rtcp_callback (RTPSession * sess,
917 RTPSessionSendRTCP callback, gpointer user_data)
919 g_return_if_fail (RTP_IS_SESSION (sess));
921 sess->callbacks.send_rtcp = callback;
922 sess->send_rtcp_user_data = user_data;
926 * rtp_session_set_sync_rtcp_callback:
927 * @sess: an #RTPSession
928 * @callback: callback to set
929 * @user_data: user data passed in the callback
931 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
934 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
935 RTPSessionSyncRTCP callback, gpointer user_data)
937 g_return_if_fail (RTP_IS_SESSION (sess));
939 sess->callbacks.sync_rtcp = callback;
940 sess->sync_rtcp_user_data = user_data;
944 * rtp_session_set_clock_rate_callback:
945 * @sess: an #RTPSession
946 * @callback: callback to set
947 * @user_data: user data passed in the callback
949 * Configure only the clock_rate callback to be notified of the clock_rate action.
952 rtp_session_set_clock_rate_callback (RTPSession * sess,
953 RTPSessionClockRate callback, gpointer user_data)
955 g_return_if_fail (RTP_IS_SESSION (sess));
957 sess->callbacks.clock_rate = callback;
958 sess->clock_rate_user_data = user_data;
962 * rtp_session_set_reconsider_callback:
963 * @sess: an #RTPSession
964 * @callback: callback to set
965 * @user_data: user data passed in the callback
967 * Configure only the reconsider callback to be notified of the reconsider action.
970 rtp_session_set_reconsider_callback (RTPSession * sess,
971 RTPSessionReconsider callback, gpointer user_data)
973 g_return_if_fail (RTP_IS_SESSION (sess));
975 sess->callbacks.reconsider = callback;
976 sess->reconsider_user_data = user_data;
980 * rtp_session_set_request_time_callback:
981 * @sess: an #RTPSession
982 * @callback: callback to set
983 * @user_data: user data passed in the callback
985 * Configure only the request_time callback
988 rtp_session_set_request_time_callback (RTPSession * sess,
989 RTPSessionRequestTime callback, gpointer user_data)
991 g_return_if_fail (RTP_IS_SESSION (sess));
993 sess->callbacks.request_time = callback;
994 sess->request_time_user_data = user_data;
998 * rtp_session_set_bandwidth:
999 * @sess: an #RTPSession
1000 * @bandwidth: the bandwidth allocated
1002 * Set the session bandwidth in bytes per second.
1005 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
1007 g_return_if_fail (RTP_IS_SESSION (sess));
1009 RTP_SESSION_LOCK (sess);
1010 sess->stats.bandwidth = bandwidth;
1011 RTP_SESSION_UNLOCK (sess);
1015 * rtp_session_get_bandwidth:
1016 * @sess: an #RTPSession
1018 * Get the session bandwidth.
1020 * Returns: the session bandwidth.
1023 rtp_session_get_bandwidth (RTPSession * sess)
1027 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1029 RTP_SESSION_LOCK (sess);
1030 result = sess->stats.bandwidth;
1031 RTP_SESSION_UNLOCK (sess);
1037 * rtp_session_set_rtcp_fraction:
1038 * @sess: an #RTPSession
1039 * @bandwidth: the RTCP bandwidth
1041 * Set the bandwidth in bytes per second that should be used for RTCP
1045 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
1047 g_return_if_fail (RTP_IS_SESSION (sess));
1049 RTP_SESSION_LOCK (sess);
1050 sess->stats.rtcp_bandwidth = bandwidth;
1051 RTP_SESSION_UNLOCK (sess);
1055 * rtp_session_get_rtcp_fraction:
1056 * @sess: an #RTPSession
1058 * Get the session bandwidth used for RTCP.
1060 * Returns: The bandwidth used for RTCP messages.
1063 rtp_session_get_rtcp_fraction (RTPSession * sess)
1067 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
1069 RTP_SESSION_LOCK (sess);
1070 result = sess->stats.rtcp_bandwidth;
1071 RTP_SESSION_UNLOCK (sess);
1077 * rtp_session_set_sdes_string:
1078 * @sess: an #RTPSession
1079 * @type: the type of the SDES item
1080 * @item: a null-terminated string to set.
1082 * Store an SDES item of @type in @sess.
1084 * Returns: %FALSE if the data was unchanged @type is invalid.
1087 rtp_session_set_sdes_string (RTPSession * sess, GstRTCPSDESType type,
1092 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1094 RTP_SESSION_LOCK (sess);
1095 result = rtp_source_set_sdes_string (sess->source, type, item);
1096 RTP_SESSION_UNLOCK (sess);
1102 * rtp_session_get_sdes_string:
1103 * @sess: an #RTPSession
1104 * @type: the type of the SDES item
1106 * Get the SDES item of @type from @sess.
1108 * Returns: a null-terminated copy of the SDES item or NULL when @type was not
1109 * valid. g_free() after usage.
1112 rtp_session_get_sdes_string (RTPSession * sess, GstRTCPSDESType type)
1116 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1118 RTP_SESSION_LOCK (sess);
1119 result = rtp_source_get_sdes_string (sess->source, type);
1120 RTP_SESSION_UNLOCK (sess);
1126 * rtp_session_get_sdes_struct:
1127 * @sess: an #RTSPSession
1129 * Get the SDES data as a #GstStructure
1131 * Returns: a GstStructure with SDES items for @sess. This function returns a
1132 * copy of the SDES structure, use gst_structure_free() after usage.
1135 rtp_session_get_sdes_struct (RTPSession * sess)
1137 const GstStructure *sdes;
1138 GstStructure *result = NULL;
1140 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1142 RTP_SESSION_LOCK (sess);
1143 sdes = rtp_source_get_sdes_struct (sess->source);
1145 result = gst_structure_copy (sdes);
1146 RTP_SESSION_UNLOCK (sess);
1152 * rtp_session_set_sdes_struct:
1153 * @sess: an #RTSPSession
1154 * @sdes: a #GstStructure
1156 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1159 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
1161 g_return_if_fail (sdes);
1162 g_return_if_fail (RTP_IS_SESSION (sess));
1164 RTP_SESSION_LOCK (sess);
1165 rtp_source_set_sdes_struct (sess->source, gst_structure_copy (sdes));
1166 RTP_SESSION_UNLOCK (sess);
1169 static GstFlowReturn
1170 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1172 GstFlowReturn result = GST_FLOW_OK;
1174 if (source == session->source) {
1175 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1177 RTP_SESSION_UNLOCK (session);
1179 if (session->callbacks.send_rtp)
1181 session->callbacks.send_rtp (session, source, data,
1182 session->send_rtp_user_data);
1184 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1187 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1188 RTP_SESSION_UNLOCK (session);
1190 if (session->callbacks.process_rtp)
1192 session->callbacks.process_rtp (session, source,
1193 GST_BUFFER_CAST (data), session->process_rtp_user_data);
1195 gst_buffer_unref (GST_BUFFER_CAST (data));
1197 RTP_SESSION_LOCK (session);
1203 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1207 RTP_SESSION_UNLOCK (session);
1209 if (session->callbacks.clock_rate)
1211 session->callbacks.clock_rate (session, pt,
1212 session->clock_rate_user_data);
1216 RTP_SESSION_LOCK (session);
1218 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1223 static RTPSourceCallbacks callbacks = {
1224 (RTPSourcePushRTP) source_push_rtp,
1225 (RTPSourceClockRate) source_clock_rate,
1229 check_collision (RTPSession * sess, RTPSource * source,
1230 RTPArrivalStats * arrival, gboolean rtp)
1232 /* If we have no arrival address, we can't do collision checking */
1233 if (!arrival->have_address)
1236 if (sess->source != source) {
1237 GstNetAddress *from;
1240 /* This is not our local source, but lets check if two remote
1245 from = &source->rtp_from;
1246 have_from = source->have_rtp_from;
1248 from = &source->rtcp_from;
1249 have_from = source->have_rtcp_from;
1253 if (gst_netaddress_equal (from, &arrival->address)) {
1254 /* Address is the same */
1257 GST_LOG ("we have a third-party collision or loop ssrc:%x",
1258 rtp_source_get_ssrc (source));
1259 if (sess->favor_new) {
1260 if (rtp_source_find_conflicting_address (source,
1261 &arrival->address, arrival->current_time)) {
1263 gst_netaddress_to_string (&arrival->address, buf1, 40);
1264 GST_LOG ("Known conflict on %x for %s, dropping packet",
1265 rtp_source_get_ssrc (source), buf1);
1268 gchar buf1[40], buf2[40];
1270 /* Current address is not a known conflict, lets assume this is
1271 * a new source. Save old address in possible conflict list
1273 rtp_source_add_conflicting_address (source, from,
1274 arrival->current_time);
1276 gst_netaddress_to_string (from, buf1, 40);
1277 gst_netaddress_to_string (&arrival->address, buf2, 40);
1278 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1279 " saving old as known conflict",
1280 rtp_source_get_ssrc (source), buf1, buf2);
1283 rtp_source_set_rtp_from (source, &arrival->address);
1285 rtp_source_set_rtcp_from (source, &arrival->address);
1289 /* Don't need to save old addresses, we ignore new sources */
1294 /* We don't already have a from address for RTP, just set it */
1296 rtp_source_set_rtp_from (source, &arrival->address);
1298 rtp_source_set_rtcp_from (source, &arrival->address);
1302 /* FIXME: Log 3rd party collision somehow
1303 * Maybe should be done in upper layer, only the SDES can tell us
1304 * if its a collision or a loop
1307 /* If the source has been inactive for some time, we assume that it has
1308 * simply changed its transport source address. Hence, there is no true
1309 * third-party collision - only a simulated one. */
1310 if (arrival->current_time > source->last_activity) {
1311 GstClockTime inactivity_period =
1312 arrival->current_time - source->last_activity;
1313 if (inactivity_period > 1 * GST_SECOND) {
1314 /* Use new network address */
1316 g_assert (source->have_rtp_from);
1317 rtp_source_set_rtp_from (source, &arrival->address);
1319 g_assert (source->have_rtcp_from);
1320 rtp_source_set_rtcp_from (source, &arrival->address);
1326 /* This is sending with our ssrc, is it an address we already know */
1328 if (rtp_source_find_conflicting_address (source, &arrival->address,
1329 arrival->current_time)) {
1330 /* Its a known conflict, its probably a loop, not a collision
1331 * lets just drop the incoming packet
1333 GST_DEBUG ("Our packets are being looped back to us, dropping");
1335 /* Its a new collision, lets change our SSRC */
1337 rtp_source_add_conflicting_address (source, &arrival->address,
1338 arrival->current_time);
1340 GST_DEBUG ("Collision for SSRC %x", rtp_source_get_ssrc (source));
1341 on_ssrc_collision (sess, source);
1343 rtp_session_schedule_bye_locked (sess, "SSRC Collision",
1344 arrival->current_time);
1346 sess->change_ssrc = TRUE;
1354 /* must be called with the session lock, the returned source needs to be
1355 * unreffed after usage. */
1357 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1358 RTPArrivalStats * arrival, gboolean rtp)
1363 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1364 if (source == NULL) {
1365 /* make new Source in probation and insert */
1366 source = rtp_source_new (ssrc);
1368 /* for RTP packets we need to set the source in probation. Receiving RTCP
1369 * packets of an SSRC, on the other hand, is a strong indication that we
1370 * are dealing with a valid source. */
1372 source->probation = RTP_DEFAULT_PROBATION;
1374 source->probation = 0;
1376 /* store from address, if any */
1377 if (arrival->have_address) {
1379 rtp_source_set_rtp_from (source, &arrival->address);
1381 rtp_source_set_rtcp_from (source, &arrival->address);
1384 /* configure a callback on the source */
1385 rtp_source_set_callbacks (source, &callbacks, sess);
1387 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1390 /* we have one more source now */
1391 sess->total_sources++;
1395 /* check for collision, this updates the address when not previously set */
1396 if (check_collision (sess, source, arrival, rtp)) {
1400 /* update last activity */
1401 source->last_activity = arrival->current_time;
1403 source->last_rtp_activity = arrival->current_time;
1404 g_object_ref (source);
1410 * rtp_session_get_internal_source:
1411 * @sess: a #RTPSession
1413 * Get the internal #RTPSource of @sess.
1415 * Returns: The internal #RTPSource. g_object_unref() after usage.
1418 rtp_session_get_internal_source (RTPSession * sess)
1422 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1424 result = g_object_ref (sess->source);
1430 * rtp_session_set_internal_ssrc:
1431 * @sess: a #RTPSession
1434 * Set the SSRC of @sess to @ssrc.
1437 rtp_session_set_internal_ssrc (RTPSession * sess, guint32 ssrc)
1439 RTP_SESSION_LOCK (sess);
1440 if (ssrc != sess->source->ssrc) {
1441 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
1442 GINT_TO_POINTER (sess->source->ssrc));
1444 GST_DEBUG ("setting internal SSRC to %08x", ssrc);
1445 /* After this call, any receiver of the old SSRC either in RTP or RTCP
1446 * packets will timeout on the old SSRC, we could potentially schedule a
1447 * BYE RTCP for the old SSRC... */
1448 sess->source->ssrc = ssrc;
1449 rtp_source_reset (sess->source);
1451 /* rehash with the new SSRC */
1452 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1453 GINT_TO_POINTER (sess->source->ssrc), sess->source);
1455 RTP_SESSION_UNLOCK (sess);
1457 g_object_notify (G_OBJECT (sess), "internal-ssrc");
1461 * rtp_session_get_internal_ssrc:
1462 * @sess: a #RTPSession
1464 * Get the internal SSRC of @sess.
1466 * Returns: The SSRC of the session.
1469 rtp_session_get_internal_ssrc (RTPSession * sess)
1473 RTP_SESSION_LOCK (sess);
1474 ssrc = sess->source->ssrc;
1475 RTP_SESSION_UNLOCK (sess);
1481 * rtp_session_add_source:
1482 * @sess: a #RTPSession
1483 * @src: #RTPSource to add
1485 * Add @src to @session.
1487 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1488 * existed in the session.
1491 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1493 gboolean result = FALSE;
1496 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1497 g_return_val_if_fail (src != NULL, FALSE);
1499 RTP_SESSION_LOCK (sess);
1501 g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1502 GINT_TO_POINTER (src->ssrc));
1504 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1505 GINT_TO_POINTER (src->ssrc), src);
1506 /* we have one more source now */
1507 sess->total_sources++;
1510 RTP_SESSION_UNLOCK (sess);
1516 * rtp_session_get_num_sources:
1517 * @sess: an #RTPSession
1519 * Get the number of sources in @sess.
1521 * Returns: The number of sources in @sess.
1524 rtp_session_get_num_sources (RTPSession * sess)
1528 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1530 RTP_SESSION_LOCK (sess);
1531 result = sess->total_sources;
1532 RTP_SESSION_UNLOCK (sess);
1538 * rtp_session_get_num_active_sources:
1539 * @sess: an #RTPSession
1541 * Get the number of active sources in @sess. A source is considered active when
1542 * it has been validated and has not yet received a BYE RTCP message.
1544 * Returns: The number of active sources in @sess.
1547 rtp_session_get_num_active_sources (RTPSession * sess)
1551 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1553 RTP_SESSION_LOCK (sess);
1554 result = sess->stats.active_sources;
1555 RTP_SESSION_UNLOCK (sess);
1561 * rtp_session_get_source_by_ssrc:
1562 * @sess: an #RTPSession
1565 * Find the source with @ssrc in @sess.
1567 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1568 * g_object_unref() after usage.
1571 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1575 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1577 RTP_SESSION_LOCK (sess);
1579 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1581 g_object_ref (result);
1582 RTP_SESSION_UNLOCK (sess);
1588 * rtp_session_get_source_by_cname:
1589 * @sess: a #RTPSession
1592 * Find the source with @cname in @sess.
1594 * Returns: a #RTPSource with CNAME @cname or NULL if the source was not found.
1595 * g_object_unref() after usage.
1598 rtp_session_get_source_by_cname (RTPSession * sess, const gchar * cname)
1602 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1603 g_return_val_if_fail (cname != NULL, NULL);
1605 RTP_SESSION_LOCK (sess);
1606 result = g_hash_table_lookup (sess->cnames, cname);
1608 g_object_ref (result);
1609 RTP_SESSION_UNLOCK (sess);
1614 /* should be called with the SESSION lock */
1616 rtp_session_create_new_ssrc (RTPSession * sess)
1621 ssrc = g_random_int ();
1623 /* see if it exists in the session, we're done if it doesn't */
1624 if (g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1625 GINT_TO_POINTER (ssrc)) == NULL)
1633 * rtp_session_create_source:
1634 * @sess: an #RTPSession
1636 * Create an #RTPSource for use in @sess. This function will create a source
1637 * with an ssrc that is currently not used by any participants in the session.
1639 * Returns: an #RTPSource.
1642 rtp_session_create_source (RTPSession * sess)
1647 RTP_SESSION_LOCK (sess);
1648 ssrc = rtp_session_create_new_ssrc (sess);
1649 source = rtp_source_new (ssrc);
1650 rtp_source_set_callbacks (source, &callbacks, sess);
1651 /* we need an additional ref for the source in the hashtable */
1652 g_object_ref (source);
1653 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1655 /* we have one more source now */
1656 sess->total_sources++;
1657 RTP_SESSION_UNLOCK (sess);
1662 /* update the RTPArrivalStats structure with the current time and other bits
1663 * about the current buffer we are handling.
1664 * This function is typically called when a validated packet is received.
1665 * This function should be called with the SESSION_LOCK
1668 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
1669 gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
1670 GstClockTime running_time, guint64 ntpnstime)
1672 GstMetaNetAddress *meta;
1674 /* get time of arrival */
1675 arrival->current_time = current_time;
1676 arrival->running_time = running_time;
1677 arrival->ntpnstime = ntpnstime;
1679 /* get packet size including header overhead */
1680 arrival->bytes = GST_BUFFER_SIZE (buffer) + sess->header_len;
1683 arrival->payload_len = gst_rtp_buffer_get_payload_len (buffer);
1685 arrival->payload_len = 0;
1688 /* for netbuffer we can store the IP address to check for collisions */
1689 meta = gst_buffer_get_meta_net_address (buffer);
1691 arrival->have_address = TRUE;
1692 memcpy (&arrival->address, &meta->naddr, sizeof (GstNetAddress));
1694 arrival->have_address = FALSE;
1699 * rtp_session_process_rtp:
1700 * @sess: and #RTPSession
1701 * @buffer: an RTP buffer
1702 * @current_time: the current system time
1703 * @running_time: the running_time of @buffer
1705 * Process an RTP buffer in the session manager. This function takes ownership
1708 * Returns: a #GstFlowReturn.
1711 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1712 GstClockTime current_time, GstClockTime running_time)
1714 GstFlowReturn result;
1718 gboolean prevsender, prevactive;
1719 RTPArrivalStats arrival;
1724 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1725 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1727 if (!gst_rtp_buffer_validate (buffer))
1728 goto invalid_packet;
1730 RTP_SESSION_LOCK (sess);
1731 /* update arrival stats */
1732 update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1735 /* ignore more RTP packets when we left the session */
1736 if (sess->source->received_bye)
1739 /* get SSRC and look up in session database */
1740 ssrc = gst_rtp_buffer_get_ssrc (buffer);
1741 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1745 prevsender = RTP_SOURCE_IS_SENDER (source);
1746 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1747 oldrate = source->bitrate;
1749 /* copy available csrc for later */
1750 count = gst_rtp_buffer_get_csrc_count (buffer);
1751 /* make sure to not overflow our array. An RTP buffer can maximally contain
1753 count = MIN (count, 16);
1755 for (i = 0; i < count; i++)
1756 csrcs[i] = gst_rtp_buffer_get_csrc (buffer, i);
1758 /* let source process the packet */
1759 result = rtp_source_process_rtp (source, buffer, &arrival);
1761 /* source became active */
1762 if (prevactive != RTP_SOURCE_IS_ACTIVE (source)) {
1763 sess->stats.active_sources++;
1764 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1765 sess->stats.active_sources);
1766 on_ssrc_validated (sess, source);
1768 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1769 sess->stats.sender_sources++;
1770 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1771 sess->stats.sender_sources);
1773 if (oldrate != source->bitrate)
1774 sess->recalc_bandwidth = TRUE;
1777 on_new_ssrc (sess, source);
1779 if (source->validated) {
1782 /* for validated sources, we add the CSRCs as well */
1783 for (i = 0; i < count; i++) {
1785 RTPSource *csrc_src;
1790 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1795 GST_DEBUG ("created new CSRC: %08x", csrc);
1796 rtp_source_set_as_csrc (csrc_src);
1797 if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1798 sess->stats.active_sources++;
1799 on_new_ssrc (sess, csrc_src);
1801 g_object_unref (csrc_src);
1804 g_object_unref (source);
1806 RTP_SESSION_UNLOCK (sess);
1813 gst_buffer_unref (buffer);
1814 GST_DEBUG ("invalid RTP packet received");
1819 gst_buffer_unref (buffer);
1820 RTP_SESSION_UNLOCK (sess);
1821 GST_DEBUG ("ignoring RTP packet because we are leaving");
1826 gst_buffer_unref (buffer);
1827 RTP_SESSION_UNLOCK (sess);
1828 GST_DEBUG ("ignoring packet because its collisioning");
1834 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1835 GstRTCPPacket * packet, RTPArrivalStats * arrival)
1839 count = gst_rtcp_packet_get_rb_count (packet);
1840 for (i = 0; i < count; i++) {
1841 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1842 guint8 fractionlost;
1845 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1846 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1848 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1850 if (ssrc == sess->source->ssrc) {
1851 /* only deal with report blocks for our session, we update the stats of
1852 * the sender of the RTCP message. We could also compare our stats against
1853 * the other sender to see if we are better or worse. */
1854 rtp_source_process_rb (source, arrival->ntpnstime, fractionlost,
1855 packetslost, exthighestseq, jitter, lsr, dlsr);
1858 on_ssrc_active (sess, source);
1861 /* A Sender report contains statistics about how the sender is doing. This
1862 * includes timing informataion such as the relation between RTP and NTP
1863 * timestamps and the number of packets/bytes it sent to us.
1865 * In this report is also included a set of report blocks related to how this
1866 * sender is receiving data (in case we (or somebody else) is also sending stuff
1867 * to it). This info includes the packet loss, jitter and seqnum. It also
1868 * contains information to calculate the round trip time (LSR/DLSR).
1871 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1872 RTPArrivalStats * arrival, gboolean * do_sync)
1874 guint32 senderssrc, rtptime, packet_count, octet_count;
1877 gboolean created, prevsender;
1879 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1880 &packet_count, &octet_count);
1882 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1883 senderssrc, GST_TIME_ARGS (arrival->current_time));
1885 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1889 /* don't try to do lip-sync for sources that sent a BYE */
1890 if (rtp_source_received_bye (source))
1895 prevsender = RTP_SOURCE_IS_SENDER (source);
1897 /* first update the source */
1898 rtp_source_process_sr (source, arrival->current_time, ntptime, rtptime,
1899 packet_count, octet_count);
1901 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1902 sess->stats.sender_sources++;
1903 GST_DEBUG ("source: %08x became sender, %d sender sources", senderssrc,
1904 sess->stats.sender_sources);
1908 on_new_ssrc (sess, source);
1910 rtp_session_process_rb (sess, source, packet, arrival);
1911 g_object_unref (source);
1914 /* A receiver report contains statistics about how a receiver is doing. It
1915 * includes stuff like packet loss, jitter and the seqnum it received last. It
1916 * also contains info to calculate the round trip time.
1918 * We are only interested in how the sender of this report is doing wrt to us.
1921 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1922 RTPArrivalStats * arrival)
1928 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1930 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1932 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1937 on_new_ssrc (sess, source);
1939 rtp_session_process_rb (sess, source, packet, arrival);
1940 g_object_unref (source);
1943 /* Get SDES items and store them in the SSRC */
1945 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1946 RTPArrivalStats * arrival)
1949 gboolean more_items, more_entries;
1951 items = gst_rtcp_packet_sdes_get_item_count (packet);
1952 GST_DEBUG ("got SDES packet with %d items", items);
1954 more_items = gst_rtcp_packet_sdes_first_item (packet);
1956 while (more_items) {
1958 gboolean changed, created, validated;
1962 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1964 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1968 /* find src, no probation when dealing with RTCP */
1969 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1973 sdes = gst_structure_new ("application/x-rtp-source-sdes", NULL);
1975 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1977 while (more_entries) {
1978 GstRTCPSDESType type;
1984 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1986 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1989 if (type == GST_RTCP_SDES_PRIV) {
1990 name = g_strndup ((const gchar *) &data[1], data[0]);
1992 data += data[0] + 1;
1994 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
1997 value = g_strndup ((const gchar *) data, len);
1999 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
2004 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
2008 /* takes ownership of sdes */
2009 changed = rtp_source_set_sdes_struct (source, sdes);
2011 validated = !RTP_SOURCE_IS_ACTIVE (source);
2012 source->validated = TRUE;
2014 /* source became active */
2016 sess->stats.active_sources++;
2017 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
2018 sess->stats.active_sources);
2019 on_ssrc_validated (sess, source);
2023 on_new_ssrc (sess, source);
2025 on_ssrc_sdes (sess, source);
2027 g_object_unref (source);
2029 more_items = gst_rtcp_packet_sdes_next_item (packet);
2034 /* BYE is sent when a client leaves the session
2037 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
2038 RTPArrivalStats * arrival)
2042 gboolean reconsider = FALSE;
2044 reason = gst_rtcp_packet_bye_get_reason (packet);
2045 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
2047 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
2048 for (i = 0; i < count; i++) {
2051 gboolean created, prevactive, prevsender;
2052 guint pmembers, members;
2054 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
2055 GST_DEBUG ("SSRC: %08x", ssrc);
2057 if (ssrc == sess->source->ssrc)
2060 /* find src and mark bye, no probation when dealing with RTCP */
2061 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
2065 /* store time for when we need to time out this source */
2066 source->bye_time = arrival->current_time;
2068 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2069 prevsender = RTP_SOURCE_IS_SENDER (source);
2071 /* let the source handle the rest */
2072 rtp_source_process_bye (source, reason);
2074 pmembers = sess->stats.active_sources;
2076 if (prevactive && !RTP_SOURCE_IS_ACTIVE (source)) {
2077 sess->stats.active_sources--;
2078 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
2079 sess->stats.active_sources);
2081 if (prevsender && !RTP_SOURCE_IS_SENDER (source)) {
2082 sess->stats.sender_sources--;
2083 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
2084 sess->stats.sender_sources);
2086 members = sess->stats.active_sources;
2088 if (!sess->source->received_bye && members < pmembers) {
2089 /* some members went away since the previous timeout estimate.
2090 * Perform reverse reconsideration but only when we are not scheduling a
2092 if (arrival->current_time < sess->next_rtcp_check_time) {
2093 GstClockTime time_remaining;
2095 time_remaining = sess->next_rtcp_check_time - arrival->current_time;
2096 sess->next_rtcp_check_time =
2097 gst_util_uint64_scale (time_remaining, members, pmembers);
2099 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
2100 GST_TIME_ARGS (sess->next_rtcp_check_time));
2102 sess->next_rtcp_check_time += arrival->current_time;
2104 /* mark pending reconsider. We only want to signal the reconsideration
2105 * once after we handled all the source in the bye packet */
2111 on_new_ssrc (sess, source);
2113 on_bye_ssrc (sess, source);
2115 g_object_unref (source);
2118 RTP_SESSION_UNLOCK (sess);
2119 /* notify app of reconsideration */
2120 if (sess->callbacks.reconsider)
2121 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2122 RTP_SESSION_LOCK (sess);
2128 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
2129 RTPArrivalStats * arrival)
2131 GST_DEBUG ("received APP");
2135 rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
2136 guint32 media_ssrc, GstClockTime current_time)
2139 guint32 round_trip = 0;
2141 if (!sess->callbacks.request_key_unit)
2144 src = g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
2145 GINT_TO_POINTER (sender_ssrc));
2150 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE &&
2151 rtp_source_get_last_rb (src, NULL, NULL, NULL, NULL, NULL, NULL,
2153 GstClockTime round_trip_in_ns = gst_util_uint64_scale (round_trip,
2156 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE &&
2157 current_time - sess->last_keyframe_request < round_trip_in_ns) {
2158 GST_DEBUG ("Ignoring PLI because one was send without one RTT (%"
2159 GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")",
2160 GST_TIME_ARGS (current_time - sess->last_keyframe_request),
2161 GST_TIME_ARGS (round_trip_in_ns));;
2166 sess->last_keyframe_request = current_time;
2168 GST_LOG ("received PLI from %X %p(%p)", sender_ssrc,
2169 sess->callbacks.process_rtp, sess->callbacks.request_key_unit);
2171 sess->callbacks.request_key_unit (sess, FALSE,
2172 sess->request_key_unit_user_data);
2176 rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
2177 RTPArrivalStats * arrival, GstClockTime current_time)
2179 GstRTCPType type = gst_rtcp_packet_get_type (packet);
2180 GstRTCPFBType fbtype = gst_rtcp_packet_fb_get_type (packet);
2181 guint32 sender_ssrc = gst_rtcp_packet_fb_get_sender_ssrc (packet);
2182 guint32 media_ssrc = gst_rtcp_packet_fb_get_media_ssrc (packet);
2183 guint8 *fci_data = gst_rtcp_packet_fb_get_fci (packet);
2184 guint fci_length = 4 * gst_rtcp_packet_fb_get_fci_length (packet);
2186 GST_DEBUG ("received feedback %d:%d from %08X about %08X with FCI of "
2187 "length %d", type, fbtype, sender_ssrc, media_ssrc, fci_length);
2189 if (g_signal_has_handler_pending (sess,
2190 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0, TRUE)) {
2191 GstBuffer *fci_buffer = NULL;
2193 if (fci_length > 0) {
2194 fci_buffer = gst_buffer_create_sub (packet->buffer,
2195 fci_data - GST_BUFFER_DATA (packet->buffer), fci_length);
2196 GST_BUFFER_TIMESTAMP (fci_buffer) = arrival->running_time;
2199 RTP_SESSION_UNLOCK (sess);
2200 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0,
2201 type, fbtype, sender_ssrc, media_ssrc, fci_buffer);
2202 RTP_SESSION_LOCK (sess);
2205 gst_buffer_unref (fci_buffer);
2208 if (sess->rtcp_feedback_retention_window) {
2209 RTPSource *src = g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
2210 GINT_TO_POINTER (media_ssrc));
2213 rtp_source_retain_rtcp_packet (src, packet, arrival->running_time);
2216 if (rtp_source_get_ssrc (sess->source) == media_ssrc) {
2218 case GST_RTCP_TYPE_PSFB:
2220 case GST_RTCP_PSFB_TYPE_PLI:
2221 rtp_session_process_pli (sess, sender_ssrc, media_ssrc,
2228 case GST_RTCP_TYPE_RTPFB:
2236 * rtp_session_process_rtcp:
2237 * @sess: and #RTPSession
2238 * @buffer: an RTCP buffer
2239 * @current_time: the current system time
2240 * @ntpnstime: the current NTP time in nanoseconds
2242 * Process an RTCP buffer in the session manager. This function takes ownership
2245 * Returns: a #GstFlowReturn.
2248 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
2249 GstClockTime current_time, guint64 ntpnstime)
2251 GstRTCPPacket packet;
2252 gboolean more, is_bye = FALSE, do_sync = FALSE;
2253 RTPArrivalStats arrival;
2254 GstFlowReturn result = GST_FLOW_OK;
2256 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2257 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2259 if (!gst_rtcp_buffer_validate (buffer))
2260 goto invalid_packet;
2262 GST_DEBUG ("received RTCP packet");
2264 RTP_SESSION_LOCK (sess);
2265 /* update arrival stats */
2266 update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1,
2272 /* start processing the compound packet */
2273 more = gst_rtcp_buffer_get_first_packet (buffer, &packet);
2277 type = gst_rtcp_packet_get_type (&packet);
2279 /* when we are leaving the session, we should ignore all non-BYE messages */
2280 if (sess->source->received_bye && type != GST_RTCP_TYPE_BYE) {
2281 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
2286 case GST_RTCP_TYPE_SR:
2287 rtp_session_process_sr (sess, &packet, &arrival, &do_sync);
2289 case GST_RTCP_TYPE_RR:
2290 rtp_session_process_rr (sess, &packet, &arrival);
2292 case GST_RTCP_TYPE_SDES:
2293 rtp_session_process_sdes (sess, &packet, &arrival);
2295 case GST_RTCP_TYPE_BYE:
2297 /* don't try to attempt lip-sync anymore for streams with a BYE */
2299 rtp_session_process_bye (sess, &packet, &arrival);
2301 case GST_RTCP_TYPE_APP:
2302 rtp_session_process_app (sess, &packet, &arrival);
2304 case GST_RTCP_TYPE_RTPFB:
2305 case GST_RTCP_TYPE_PSFB:
2306 rtp_session_process_feedback (sess, &packet, &arrival, current_time);
2309 GST_WARNING ("got unknown RTCP packet");
2313 more = gst_rtcp_packet_move_to_next (&packet);
2316 /* if we are scheduling a BYE, we only want to count bye packets, else we
2317 * count everything */
2318 if (sess->source->received_bye) {
2320 sess->stats.bye_members++;
2321 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2324 /* keep track of average packet size */
2325 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
2327 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
2328 sess->stats.avg_rtcp_packet_size, arrival.bytes);
2329 RTP_SESSION_UNLOCK (sess);
2331 /* notify caller of sr packets in the callback */
2332 if (do_sync && sess->callbacks.sync_rtcp) {
2333 /* make writable, we might want to change the buffer */
2334 buffer = gst_buffer_make_metadata_writable (buffer);
2336 result = sess->callbacks.sync_rtcp (sess, sess->source, buffer,
2337 sess->sync_rtcp_user_data);
2339 gst_buffer_unref (buffer);
2346 GST_DEBUG ("invalid RTCP packet received");
2347 gst_buffer_unref (buffer);
2352 gst_buffer_unref (buffer);
2353 RTP_SESSION_UNLOCK (sess);
2354 GST_DEBUG ("ignoring RTP packet because we left");
2360 * rtp_session_send_rtp:
2361 * @sess: an #RTPSession
2362 * @data: pointer to either an RTP buffer or a list of RTP buffers
2363 * @is_list: TRUE when @data is a buffer list
2364 * @current_time: the current system time
2365 * @running_time: the running time of @data
2367 * Send the RTP buffer in the session manager. This function takes ownership of
2370 * Returns: a #GstFlowReturn.
2373 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
2374 GstClockTime current_time, GstClockTime running_time)
2376 GstFlowReturn result;
2378 gboolean prevsender;
2379 gboolean valid_packet;
2382 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2383 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
2386 valid_packet = gst_rtp_buffer_list_validate (GST_BUFFER_LIST_CAST (data));
2388 valid_packet = gst_rtp_buffer_validate (GST_BUFFER_CAST (data));
2392 goto invalid_packet;
2394 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
2396 RTP_SESSION_LOCK (sess);
2397 source = sess->source;
2399 /* update last activity */
2400 source->last_rtp_activity = current_time;
2402 prevsender = RTP_SOURCE_IS_SENDER (source);
2403 oldrate = source->bitrate;
2405 /* we use our own source to send */
2406 result = rtp_source_send_rtp (source, data, is_list, running_time);
2408 if (RTP_SOURCE_IS_SENDER (source) && !prevsender)
2409 sess->stats.sender_sources++;
2410 if (oldrate != source->bitrate)
2411 sess->recalc_bandwidth = TRUE;
2412 RTP_SESSION_UNLOCK (sess);
2419 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
2420 GST_DEBUG ("invalid RTP packet received");
2426 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
2428 *bandwidth += source->bitrate;
2432 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2435 GstClockTime result;
2437 /* recalculate bandwidth when it changed */
2438 if (sess->recalc_bandwidth) {
2441 if (sess->bandwidth > 0)
2442 bandwidth = sess->bandwidth;
2444 /* If it is <= 0, then try to estimate the actual bandwidth */
2445 bandwidth = sess->source->bitrate;
2447 g_hash_table_foreach (sess->cnames, (GHFunc) add_bitrates, &bandwidth);
2451 bandwidth = RTP_STATS_BANDWIDTH;
2453 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
2454 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
2456 sess->recalc_bandwidth = FALSE;
2459 if (sess->source->received_bye) {
2460 result = rtp_stats_calculate_bye_interval (&sess->stats);
2462 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2463 RTP_SOURCE_IS_SENDER (sess->source), first);
2466 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2467 GST_TIME_ARGS (result), first);
2469 if (!deterministic && result != GST_CLOCK_TIME_NONE)
2470 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2472 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2477 /* Stop the current @sess and schedule a BYE message for the other members.
2478 * One must have the session lock to call this function
2480 static GstFlowReturn
2481 rtp_session_schedule_bye_locked (RTPSession * sess, const gchar * reason,
2482 GstClockTime current_time)
2484 GstFlowReturn result = GST_FLOW_OK;
2486 GstClockTime interval;
2488 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2490 source = sess->source;
2492 /* ignore more BYEs */
2493 if (source->received_bye)
2496 /* we have BYE now */
2497 source->received_bye = TRUE;
2498 /* at least one member wants to send a BYE */
2499 g_free (sess->bye_reason);
2500 sess->bye_reason = g_strdup (reason);
2501 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
2502 sess->stats.bye_members = 1;
2503 sess->first_rtcp = TRUE;
2504 sess->sent_bye = FALSE;
2505 sess->allow_early = TRUE;
2507 /* reschedule transmission */
2508 sess->last_rtcp_send_time = current_time;
2509 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2510 sess->next_rtcp_check_time = current_time + interval;
2512 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2513 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2515 RTP_SESSION_UNLOCK (sess);
2516 /* notify app of reconsideration */
2517 if (sess->callbacks.reconsider)
2518 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2519 RTP_SESSION_LOCK (sess);
2526 * rtp_session_schedule_bye:
2527 * @sess: an #RTPSession
2528 * @reason: a reason or NULL
2529 * @current_time: the current system time
2531 * Stop the current @sess and schedule a BYE message for the other members.
2533 * Returns: a #GstFlowReturn.
2536 rtp_session_schedule_bye (RTPSession * sess, const gchar * reason,
2537 GstClockTime current_time)
2539 GstFlowReturn result = GST_FLOW_OK;
2541 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2543 RTP_SESSION_LOCK (sess);
2544 result = rtp_session_schedule_bye_locked (sess, reason, current_time);
2545 RTP_SESSION_UNLOCK (sess);
2551 * rtp_session_next_timeout:
2552 * @sess: an #RTPSession
2553 * @current_time: the current system time
2555 * Get the next time we should perform session maintenance tasks.
2557 * Returns: a time when rtp_session_on_timeout() should be called with the
2558 * current system time.
2561 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2563 GstClockTime result, interval = 0;
2565 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
2567 RTP_SESSION_LOCK (sess);
2569 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
2570 result = sess->next_early_rtcp_time;
2574 result = sess->next_rtcp_check_time;
2576 GST_DEBUG ("current time: %" GST_TIME_FORMAT ", next :%" GST_TIME_FORMAT,
2577 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2579 if (result < current_time) {
2580 GST_DEBUG ("take current time as base");
2581 /* our previous check time expired, start counting from the current time
2583 result = current_time;
2586 if (sess->source->received_bye) {
2587 if (sess->sent_bye) {
2588 GST_DEBUG ("we sent BYE already");
2589 interval = GST_CLOCK_TIME_NONE;
2590 } else if (sess->stats.active_sources >= 50) {
2591 GST_DEBUG ("reconsider BYE, more than 50 sources");
2592 /* reconsider BYE if members >= 50 */
2593 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2596 if (sess->first_rtcp) {
2597 GST_DEBUG ("first RTCP packet");
2598 /* we are called for the first time */
2599 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2600 } else if (sess->next_rtcp_check_time < current_time) {
2601 GST_DEBUG ("old check time expired, getting new timeout");
2602 /* get a new timeout when we need to */
2603 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
2607 if (interval != GST_CLOCK_TIME_NONE)
2610 result = GST_CLOCK_TIME_NONE;
2612 sess->next_rtcp_check_time = result;
2616 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2617 ", next time: %" GST_TIME_FORMAT,
2618 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2619 RTP_SESSION_UNLOCK (sess);
2628 GstClockTime current_time;
2630 GstClockTime running_time;
2631 GstClockTime interval;
2632 GstRTCPPacket packet;
2636 gboolean may_suppress;
2640 session_start_rtcp (RTPSession * sess, ReportData * data)
2642 GstRTCPPacket *packet = &data->packet;
2643 RTPSource *own = sess->source;
2645 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2647 if (RTP_SOURCE_IS_SENDER (own)) {
2650 guint32 packet_count, octet_count;
2652 /* we are a sender, create SR */
2653 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2654 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SR, packet);
2656 /* get latest stats */
2657 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
2658 &ntptime, &rtptime, &packet_count, &octet_count);
2660 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2661 packet_count, octet_count);
2663 /* fill in sender report info */
2664 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2665 ntptime, rtptime, packet_count, octet_count);
2667 /* we are only receiver, create RR */
2668 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2669 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_RR, packet);
2670 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2674 /* construct a Sender or Receiver Report */
2676 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2678 RTPSession *sess = data->sess;
2679 GstRTCPPacket *packet = &data->packet;
2681 /* create a new buffer if needed */
2682 if (data->rtcp == NULL) {
2683 session_start_rtcp (sess, data);
2684 } else if (data->is_early) {
2685 /* Put a single RR or SR in minimal compound packets */
2688 if (gst_rtcp_packet_get_rb_count (packet) < GST_RTCP_MAX_RB_COUNT) {
2689 /* only report about other sender sources */
2690 if (source != sess->source && RTP_SOURCE_IS_SENDER (source)) {
2691 guint8 fractionlost;
2693 guint32 exthighestseq, jitter;
2697 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2698 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2700 /* store last generated RR packet */
2701 source->last_rr.is_valid = TRUE;
2702 source->last_rr.fractionlost = fractionlost;
2703 source->last_rr.packetslost = packetslost;
2704 source->last_rr.exthighestseq = exthighestseq;
2705 source->last_rr.jitter = jitter;
2706 source->last_rr.lsr = lsr;
2707 source->last_rr.dlsr = dlsr;
2709 /* packet is not yet filled, add report block for this source. */
2710 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2711 exthighestseq, jitter, lsr, dlsr);
2716 /* perform cleanup of sources that timed out */
2718 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2720 gboolean remove = FALSE;
2721 gboolean byetimeout = FALSE;
2722 gboolean sendertimeout = FALSE;
2723 gboolean is_sender, is_active;
2724 RTPSession *sess = data->sess;
2725 GstClockTime interval;
2727 is_sender = RTP_SOURCE_IS_SENDER (source);
2728 is_active = RTP_SOURCE_IS_ACTIVE (source);
2730 /* check for our own source, we don't want to delete our own source. */
2731 if (!(source == sess->source)) {
2732 if (source->received_bye) {
2733 /* if we received a BYE from the source, remove the source after some
2735 if (data->current_time > source->bye_time &&
2736 data->current_time - source->bye_time > sess->stats.bye_timeout) {
2737 GST_DEBUG ("removing BYE source %08x", source->ssrc);
2742 /* sources that were inactive for more than 5 times the deterministic reporting
2743 * interval get timed out. the min timeout is 5 seconds. */
2744 if (data->current_time > source->last_activity) {
2745 interval = MAX (data->interval * 5, 5 * GST_SECOND);
2746 if (data->current_time - source->last_activity > interval) {
2747 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2748 source->ssrc, GST_TIME_ARGS (source->last_activity));
2754 /* senders that did not send for a long time become a receiver, this also
2755 * holds for our own source. */
2757 if (data->current_time > source->last_rtp_activity) {
2758 interval = MAX (data->interval * 2, 5 * GST_SECOND);
2759 if (data->current_time - source->last_rtp_activity > interval) {
2760 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
2761 GST_TIME_FORMAT, source->ssrc,
2762 GST_TIME_ARGS (source->last_rtp_activity));
2763 source->is_sender = FALSE;
2764 sess->stats.sender_sources--;
2765 sendertimeout = TRUE;
2771 sess->total_sources--;
2773 sess->stats.sender_sources--;
2775 sess->stats.active_sources--;
2778 on_bye_timeout (sess, source);
2780 on_timeout (sess, source);
2783 on_sender_timeout (sess, source);
2786 source->closing = remove;
2790 session_sdes (RTPSession * sess, ReportData * data)
2792 GstRTCPPacket *packet = &data->packet;
2793 const GstStructure *sdes;
2796 /* add SDES packet */
2797 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SDES, packet);
2799 gst_rtcp_packet_sdes_add_item (packet, sess->source->ssrc);
2801 sdes = rtp_source_get_sdes_struct (sess->source);
2803 /* add all fields in the structure, the order is not important. */
2804 n_fields = gst_structure_n_fields (sdes);
2805 for (i = 0; i < n_fields; ++i) {
2808 GstRTCPSDESType type;
2810 field = gst_structure_nth_field_name (sdes, i);
2813 value = gst_structure_get_string (sdes, field);
2816 type = gst_rtcp_sdes_name_to_type (field);
2818 /* Early packets are minimal and only include the CNAME */
2819 if (data->is_early && type != GST_RTCP_SDES_CNAME)
2822 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
2823 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
2824 (const guint8 *) value);
2825 } else if (type == GST_RTCP_SDES_PRIV) {
2831 /* don't accept entries that are too big */
2832 prefix_len = strlen (field);
2833 if (prefix_len > 255)
2835 value_len = strlen (value);
2836 if (value_len > 255)
2838 data_len = 1 + prefix_len + value_len;
2842 data[0] = prefix_len;
2843 memcpy (&data[1], field, prefix_len);
2844 memcpy (&data[1 + prefix_len], value, value_len);
2846 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
2850 data->has_sdes = TRUE;
2853 /* schedule a BYE packet */
2855 session_bye (RTPSession * sess, ReportData * data)
2857 GstRTCPPacket *packet = &data->packet;
2860 session_start_rtcp (sess, data);
2863 session_sdes (sess, data);
2865 /* add a BYE packet */
2866 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_BYE, packet);
2867 gst_rtcp_packet_bye_add_ssrc (packet, sess->source->ssrc);
2868 if (sess->bye_reason)
2869 gst_rtcp_packet_bye_set_reason (packet, sess->bye_reason);
2871 /* we have a BYE packet now */
2872 data->is_bye = TRUE;
2876 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
2878 GstClockTime new_send_time, elapsed;
2880 if (data->is_early && sess->next_early_rtcp_time < current_time)
2883 /* no need to check yet */
2884 if (sess->next_rtcp_check_time > current_time) {
2885 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
2886 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
2887 GST_TIME_ARGS (current_time));
2891 /* get elapsed time since we last reported */
2892 elapsed = current_time - sess->last_rtcp_send_time;
2894 /* perform forward reconsideration */
2895 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, data->interval);
2897 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
2898 GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time), GST_TIME_ARGS (elapsed));
2900 new_send_time += sess->last_rtcp_send_time;
2902 /* check if reconsideration */
2903 if (current_time < new_send_time) {
2904 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
2905 GST_TIME_ARGS (new_send_time));
2906 /* store new check time */
2907 sess->next_rtcp_check_time = new_send_time;
2913 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
2915 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
2916 GST_TIME_ARGS (new_send_time));
2917 sess->next_rtcp_check_time = current_time + new_send_time;
2919 /* Apply the rules from RFC 4585 section 3.5.3 */
2920 if (sess->stats.min_interval != 0 && !sess->first_rtcp) {
2921 GstClockTimeDiff T_rr_current_interval = g_random_double_range (0.5, 1.5) *
2922 sess->stats.min_interval;
2924 /* This will caused the RTCP to be suppressed if no FB packets are added */
2925 if (sess->last_rtcp_send_time + T_rr_current_interval >
2926 sess->next_rtcp_check_time) {
2927 GST_DEBUG ("RTCP packet could be suppressed min: %" GST_TIME_FORMAT
2928 " last: %" GST_TIME_FORMAT
2929 " + T_rr_current_interval: %" GST_TIME_FORMAT
2930 " > sess->next_rtcp_check_time: %" GST_TIME_FORMAT,
2931 GST_TIME_ARGS (sess->stats.min_interval),
2932 GST_TIME_ARGS (sess->last_rtcp_send_time),
2933 GST_TIME_ARGS (T_rr_current_interval),
2934 GST_TIME_ARGS (sess->next_rtcp_check_time));
2935 data->may_suppress = TRUE;
2943 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
2945 g_hash_table_insert (hash_table, key, g_object_ref (source));
2949 remove_closing_sources (const gchar * key, RTPSource * source, gpointer * data)
2951 return source->closing;
2955 * rtp_session_on_timeout:
2956 * @sess: an #RTPSession
2957 * @current_time: the current system time
2958 * @ntpnstime: the current NTP time in nanoseconds
2959 * @running_time: the current running_time of the pipeline
2961 * Perform maintenance actions after the timeout obtained with
2962 * rtp_session_next_timeout() expired.
2964 * This function will perform timeouts of receivers and senders, send a BYE
2965 * packet or generate RTCP packets with current session stats.
2967 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
2968 * times, for each packet that should be processed.
2970 * Returns: a #GstFlowReturn.
2973 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
2974 guint64 ntpnstime, GstClockTime running_time)
2976 GstFlowReturn result = GST_FLOW_OK;
2979 GHashTable *table_copy;
2980 gboolean notify = FALSE;
2982 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2984 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT,
2985 GST_TIME_ARGS (current_time), GST_TIME_ARGS (ntpnstime));
2989 data.current_time = current_time;
2990 data.ntpnstime = ntpnstime;
2991 data.is_bye = FALSE;
2992 data.has_sdes = FALSE;
2993 data.may_suppress = FALSE;
2994 data.running_time = running_time;
2998 RTP_SESSION_LOCK (sess);
2999 /* get a new interval, we need this for various cleanups etc */
3000 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
3002 /* Make a local copy of the hashtable. We need to do this because the
3003 * cleanup stage below releases the session lock. */
3004 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
3005 (GDestroyNotify) g_object_unref);
3006 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3007 (GHFunc) clone_ssrcs_hashtable, table_copy);
3009 /* Clean up the session, mark the source for removing, this might release the
3011 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
3012 g_hash_table_destroy (table_copy);
3014 /* Now remove the marked sources */
3015 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
3016 (GHRFunc) remove_closing_sources, NULL);
3018 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3019 data.is_early = TRUE;
3021 data.is_early = FALSE;
3023 /* see if we need to generate SR or RR packets */
3024 if (is_rtcp_time (sess, current_time, &data)) {
3025 if (own->received_bye) {
3026 /* generate BYE instead */
3027 GST_DEBUG ("generating BYE message");
3028 session_bye (sess, &data);
3029 sess->sent_bye = TRUE;
3031 /* loop over all known sources and do something */
3032 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3033 (GHFunc) session_report_blocks, &data);
3038 /* we keep track of the last report time in order to timeout inactive
3039 * receivers or senders */
3040 if (!data.is_early && !data.may_suppress)
3041 sess->last_rtcp_send_time = data.current_time;
3042 sess->first_rtcp = FALSE;
3043 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
3045 /* add SDES for this source when not already added */
3047 session_sdes (sess, &data);
3050 /* check for outdated collisions */
3051 GST_DEBUG ("Timing out collisions");
3052 rtp_source_timeout (sess->source, current_time,
3053 data.interval * RTCP_INTERVAL_COLLISION_TIMEOUT,
3054 running_time - sess->rtcp_feedback_retention_window);
3056 if (sess->change_ssrc) {
3057 GST_DEBUG ("need to change our SSRC (%08x)", own->ssrc);
3058 g_hash_table_steal (sess->ssrcs[sess->mask_idx],
3059 GINT_TO_POINTER (own->ssrc));
3061 own->ssrc = rtp_session_create_new_ssrc (sess);
3062 rtp_source_reset (own);
3064 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
3065 GINT_TO_POINTER (own->ssrc), own);
3067 g_free (sess->bye_reason);
3068 sess->bye_reason = NULL;
3069 sess->sent_bye = FALSE;
3070 sess->change_ssrc = FALSE;
3072 GST_DEBUG ("changed our SSRC to %08x", own->ssrc);
3075 sess->allow_early = TRUE;
3077 RTP_SESSION_UNLOCK (sess);
3080 g_object_notify (G_OBJECT (sess), "internal-ssrc");
3082 /* push out the RTCP packet */
3084 gboolean do_not_suppress;
3086 /* Give the user a change to add its own packet */
3087 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
3088 data.rtcp, data.is_early, &do_not_suppress);
3090 if (sess->callbacks.send_rtcp && (do_not_suppress || !data.may_suppress)) {
3093 /* close the RTCP packet */
3094 gst_rtcp_buffer_end (data.rtcp);
3096 packet_size = GST_BUFFER_SIZE (data.rtcp) + sess->header_len;
3098 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
3099 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
3100 sess->stats.avg_rtcp_packet_size, packet_size);
3102 sess->callbacks.send_rtcp (sess, own, data.rtcp, sess->sent_bye,
3103 sess->send_rtcp_user_data);
3105 GST_DEBUG ("freeing packet callback: %p"
3106 " do_not_suppress: %d may_suppress: %d",
3107 sess->callbacks.send_rtcp, do_not_suppress, data.may_suppress);
3108 gst_buffer_unref (data.rtcp);
3116 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
3117 GstClockTimeDiff max_delay)
3119 GstClockTime T_dither_max;
3121 /* Implements the algorithm described in RFC 4585 section 3.5.2 */
3123 RTP_SESSION_LOCK (sess);
3125 /* Check if already requested */
3126 /* RFC 4585 section 3.5.2 step 2 */
3127 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3130 /* Ignore the request a scheduled packet will be in time anyway */
3131 if (current_time + max_delay > sess->next_rtcp_check_time)
3134 /* RFC 4585 section 3.5.2 step 2b */
3135 /* If the total sources is <=2, then there is only us and one peer */
3136 if (sess->total_sources <= 2) {
3139 /* Divide by 2 because l = 0.5 */
3140 T_dither_max = sess->next_rtcp_check_time - sess->last_rtcp_send_time;
3144 /* RFC 4585 section 3.5.2 step 3 */
3145 if (current_time + T_dither_max > sess->next_rtcp_check_time)
3148 /* RFC 4585 section 3.5.2 step 4 */
3149 if (sess->allow_early == FALSE)
3153 /* Schedule an early transmission later */
3154 sess->next_early_rtcp_time = g_random_double () * T_dither_max +
3157 /* If no dithering, schedule it for NOW */
3158 sess->next_early_rtcp_time = current_time;
3161 RTP_SESSION_UNLOCK (sess);
3163 /* notify app of need to send packet early
3164 * and therefore of timeout change */
3165 if (sess->callbacks.reconsider)
3166 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
3172 RTP_SESSION_UNLOCK (sess);
3177 rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc)
3181 for (i = 0; i < sess->rtcp_pli_requests->len; i++)
3182 if (ssrc == g_array_index (sess->rtcp_pli_requests, guint32, i))
3185 g_array_append_val (sess->rtcp_pli_requests, ssrc);
3189 has_pli_compare_func (gconstpointer a, gconstpointer ignored)
3191 GstRTCPPacket packet;
3193 packet.buffer = (GstBuffer *) a;
3196 if (gst_rtcp_packet_get_type (&packet) == GST_RTCP_TYPE_PSFB &&
3197 gst_rtcp_packet_fb_get_type (&packet) == GST_RTCP_PSFB_TYPE_PLI)
3204 rtp_session_on_sending_rtcp (RTPSession * sess, GstBuffer * buffer,
3207 gboolean ret = FALSE;
3209 RTP_SESSION_LOCK (sess);
3211 while (sess->rtcp_pli_requests->len) {
3212 GstRTCPPacket rtcppacket;
3213 guint media_ssrc = g_array_index (sess->rtcp_pli_requests, guint32, 0);
3214 RTPSource *media_src = g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
3215 GUINT_TO_POINTER (media_ssrc));
3217 if (media_src && !rtp_source_has_retained (media_src,
3218 has_pli_compare_func, NULL)) {
3219 if (gst_rtcp_buffer_add_packet (buffer, GST_RTCP_TYPE_PSFB, &rtcppacket)) {
3220 gst_rtcp_packet_fb_set_type (&rtcppacket, GST_RTCP_PSFB_TYPE_PLI);
3221 gst_rtcp_packet_fb_set_sender_ssrc (&rtcppacket,
3222 rtp_source_get_ssrc (sess->source));
3223 gst_rtcp_packet_fb_set_media_ssrc (&rtcppacket, media_ssrc);
3226 /* Break because the packet is full, will put next request in a
3233 g_array_remove_index (sess->rtcp_pli_requests, 0);
3236 RTP_SESSION_UNLOCK (sess);
3242 rtp_session_send_rtcp (RTPSession * sess, GstClockTimeDiff max_delay)
3246 if (!sess->callbacks.send_rtcp)
3249 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
3251 rtp_session_request_early_rtcp (sess, now, max_delay);