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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
21 * with newer GLib versions (>= 2.31.0) */
22 #define GLIB_DISABLE_DEPRECATION_WARNINGS
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/rtp/gstrtcpbuffer.h>
29 #include <gst/glib-compat-private.h>
31 #include "rtpsession.h"
33 GST_DEBUG_CATEGORY_STATIC (rtp_session_debug);
34 #define GST_CAT_DEFAULT rtp_session_debug
36 /* signals and args */
39 SIGNAL_GET_SOURCE_BY_SSRC,
41 SIGNAL_ON_SSRC_COLLISION,
42 SIGNAL_ON_SSRC_VALIDATED,
43 SIGNAL_ON_SSRC_ACTIVE,
46 SIGNAL_ON_BYE_TIMEOUT,
48 SIGNAL_ON_SENDER_TIMEOUT,
49 SIGNAL_ON_SENDING_RTCP,
50 SIGNAL_ON_FEEDBACK_RTCP,
55 #define DEFAULT_INTERNAL_SOURCE NULL
56 #define DEFAULT_BANDWIDTH RTP_STATS_BANDWIDTH
57 #define DEFAULT_RTCP_FRACTION (RTP_STATS_RTCP_FRACTION * RTP_STATS_BANDWIDTH)
58 #define DEFAULT_RTCP_RR_BANDWIDTH -1
59 #define DEFAULT_RTCP_RS_BANDWIDTH -1
60 #define DEFAULT_RTCP_MTU 1400
61 #define DEFAULT_SDES NULL
62 #define DEFAULT_NUM_SOURCES 0
63 #define DEFAULT_NUM_ACTIVE_SOURCES 0
64 #define DEFAULT_SOURCES NULL
65 #define DEFAULT_RTCP_MIN_INTERVAL (RTP_STATS_MIN_INTERVAL * GST_SECOND)
66 #define DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW (2 * GST_SECOND)
67 #define DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD (3)
68 #define DEFAULT_PROBATION RTP_DEFAULT_PROBATION
77 PROP_RTCP_RR_BANDWIDTH,
78 PROP_RTCP_RS_BANDWIDTH,
82 PROP_NUM_ACTIVE_SOURCES,
85 PROP_RTCP_MIN_INTERVAL,
86 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
87 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
93 /* update average packet size */
94 #define INIT_AVG(avg, val) \
96 #define UPDATE_AVG(avg, val) \
100 (avg) = ((val) + (15 * (avg))) >> 4;
103 /* The number RTCP intervals after which to timeout entries in the
106 #define RTCP_INTERVAL_COLLISION_TIMEOUT 10
108 /* GObject vmethods */
109 static void rtp_session_finalize (GObject * object);
110 static void rtp_session_set_property (GObject * object, guint prop_id,
111 const GValue * value, GParamSpec * pspec);
112 static void rtp_session_get_property (GObject * object, guint prop_id,
113 GValue * value, GParamSpec * pspec);
115 static void rtp_session_send_rtcp (RTPSession * sess, GstClockTime max_delay);
117 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
119 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
121 static guint32 rtp_session_create_new_ssrc (RTPSession * sess);
122 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
123 gboolean * created, RTPPacketInfo * pinfo, gboolean rtp);
124 static RTPSource *obtain_internal_source (RTPSession * sess,
125 guint32 ssrc, gboolean * created);
126 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
127 GstClockTime current_time);
128 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
129 gboolean deterministic, gboolean first);
132 accumulate_trues (GSignalInvocationHint * ihint, GValue * return_accu,
133 const GValue * handler_return, gpointer data)
135 if (g_value_get_boolean (handler_return))
136 g_value_set_boolean (return_accu, TRUE);
142 rtp_session_class_init (RTPSessionClass * klass)
144 GObjectClass *gobject_class;
146 gobject_class = (GObjectClass *) klass;
148 gobject_class->finalize = rtp_session_finalize;
149 gobject_class->set_property = rtp_session_set_property;
150 gobject_class->get_property = rtp_session_get_property;
153 * RTPSession::get-source-by-ssrc:
154 * @session: the object which received the signal
155 * @ssrc: the SSRC of the RTPSource
157 * Request the #RTPSource object with SSRC @ssrc in @session.
159 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
160 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
161 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
162 get_source_by_ssrc), NULL, NULL, g_cclosure_marshal_generic,
163 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
166 * RTPSession::on-new-ssrc:
167 * @session: the object which received the signal
168 * @src: the new RTPSource
170 * Notify of a new SSRC that entered @session.
172 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
173 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
174 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
175 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
178 * RTPSession::on-ssrc-collision:
179 * @session: the object which received the signal
180 * @src: the #RTPSource that caused a collision
182 * Notify when we have an SSRC collision
184 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
185 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
186 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
187 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
190 * RTPSession::on-ssrc-validated:
191 * @session: the object which received the signal
192 * @src: the new validated RTPSource
194 * Notify of a new SSRC that became validated.
196 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
197 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
198 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
199 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
202 * RTPSession::on-ssrc-active:
203 * @session: the object which received the signal
204 * @src: the active RTPSource
206 * Notify of a SSRC that is active, i.e., sending RTCP.
208 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
209 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
210 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
211 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
214 * RTPSession::on-ssrc-sdes:
215 * @session: the object which received the signal
216 * @src: the RTPSource
218 * Notify that a new SDES was received for SSRC.
220 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
221 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
222 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
223 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
226 * RTPSession::on-bye-ssrc:
227 * @session: the object which received the signal
228 * @src: the RTPSource that went away
230 * Notify of an SSRC that became inactive because of a BYE packet.
232 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
233 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
234 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
235 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
238 * RTPSession::on-bye-timeout:
239 * @session: the object which received the signal
240 * @src: the RTPSource that timed out
242 * Notify of an SSRC that has timed out because of BYE
244 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
245 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
246 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
247 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
250 * RTPSession::on-timeout:
251 * @session: the object which received the signal
252 * @src: the RTPSource that timed out
254 * Notify of an SSRC that has timed out
256 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
257 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
258 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
259 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
262 * RTPSession::on-sender-timeout:
263 * @session: the object which received the signal
264 * @src: the RTPSource that timed out
266 * Notify of an SSRC that was a sender but timed out and became a receiver.
268 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
269 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
270 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
271 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
275 * RTPSession::on-sending-rtcp
276 * @session: the object which received the signal
277 * @buffer: the #GstBuffer containing the RTCP packet about to be sent
278 * @early: %TRUE if the packet is early, %FALSE if it is regular
280 * This signal is emitted before sending an RTCP packet, it can be used
281 * to add extra RTCP Packets.
283 * Returns: %TRUE if the RTCP buffer should NOT be suppressed, %FALSE
284 * if suppressing it is acceptable
286 rtp_session_signals[SIGNAL_ON_SENDING_RTCP] =
287 g_signal_new ("on-sending-rtcp", G_TYPE_FROM_CLASS (klass),
288 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_rtcp),
289 accumulate_trues, NULL, g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2,
290 GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_BOOLEAN);
293 * RTPSession::on-feedback-rtcp:
294 * @session: the object which received the signal
295 * @type: Type of RTCP packet, will be %GST_RTCP_TYPE_RTPFB or
296 * %GST_RTCP_TYPE_RTPFB
297 * @fbtype: The type of RTCP FB packet, probably part of #GstRTCPFBType
298 * @sender_ssrc: The SSRC of the sender
299 * @media_ssrc: The SSRC of the media this refers to
300 * @fci: a #GstBuffer with the FCI data from the FB packet or %NULL if
303 * Notify that a RTCP feedback packet has been received
305 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP] =
306 g_signal_new ("on-feedback-rtcp", G_TYPE_FROM_CLASS (klass),
307 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_feedback_rtcp),
308 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 5, G_TYPE_UINT,
309 G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, GST_TYPE_BUFFER);
312 * RTPSession::send-rtcp:
313 * @session: the object which received the signal
314 * @max_delay: The maximum delay after which the feedback will not be useful
317 * Requests that the #RTPSession initiate a new RTCP packet as soon as
318 * possible within the requested delay.
320 rtp_session_signals[SIGNAL_SEND_RTCP] =
321 g_signal_new ("send-rtcp", G_TYPE_FROM_CLASS (klass),
322 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
323 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
324 g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_UINT64);
326 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
327 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
328 "The internal SSRC used for the session (deprecated)",
329 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
331 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
332 g_param_spec_object ("internal-source", "Internal Source",
333 "The internal source element of the session (deprecated)",
334 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
336 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
337 g_param_spec_double ("bandwidth", "Bandwidth",
338 "The bandwidth of the session (0 for auto-discover)",
339 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
340 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
342 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
343 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
344 "The fraction of the bandwidth used for RTCP (or as a real fraction of the RTP bandwidth if < 1)",
345 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
346 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
348 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
349 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
350 "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
351 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
352 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
354 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
355 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
356 "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
357 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
358 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
360 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
361 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
362 "The maximum size of the RTCP packets",
363 16, G_MAXINT16, DEFAULT_RTCP_MTU,
364 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
366 g_object_class_install_property (gobject_class, PROP_SDES,
367 g_param_spec_boxed ("sdes", "SDES",
368 "The SDES items of this session",
369 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
372 g_param_spec_uint ("num-sources", "Num Sources",
373 "The number of sources in the session", 0, G_MAXUINT,
374 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
376 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
377 g_param_spec_uint ("num-active-sources", "Num Active Sources",
378 "The number of active sources in the session", 0, G_MAXUINT,
379 DEFAULT_NUM_ACTIVE_SOURCES,
380 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
384 * Get a GValue Array of all sources in the session.
387 * <title>Getting the #RTPSources of a session
394 * g_object_get (sess, "sources", &arr, NULL);
396 * for (i = 0; i < arr->n_values; i++) {
399 * val = g_value_array_get_nth (arr, i);
400 * source = g_value_get_object (val);
402 * g_value_array_free (arr);
407 g_object_class_install_property (gobject_class, PROP_SOURCES,
408 g_param_spec_boxed ("sources", "Sources",
409 "An array of all known sources in the session",
410 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
412 g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
413 g_param_spec_boolean ("favor-new", "Favor new sources",
414 "Resolve SSRC conflict in favor of new sources", FALSE,
415 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
417 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
418 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
419 "Minimum interval between Regular RTCP packet (in ns)",
420 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
421 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
423 g_object_class_install_property (gobject_class,
424 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
425 g_param_spec_uint64 ("rtcp-feedback-retention-window",
426 "RTCP Feedback retention window",
427 "Duration during which RTCP Feedback packets are retained (in ns)",
428 0, G_MAXUINT64, DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW,
429 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
431 g_object_class_install_property (gobject_class,
432 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
433 g_param_spec_uint ("rtcp-immediate-feedback-threshold",
434 "RTCP Immediate Feedback threshold",
435 "The maximum number of members of a RTP session for which immediate"
437 0, G_MAXUINT, DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
438 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
440 g_object_class_install_property (gobject_class, PROP_PROBATION,
441 g_param_spec_uint ("probation", "Number of probations",
442 "Consecutive packet sequence numbers to accept the source",
443 0, G_MAXUINT, DEFAULT_PROBATION,
444 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
449 * Various session statistics. This property returns a GstStructure
450 * with name application/x-rtp-session-stats with the following fields:
452 * "rtx-drop-count" G_TYPE_UINT The number of retransmission events
453 * dropped (due to bandwidth constraints)
454 * "sent-nack-count" G_TYPE_UINT Number of NACKs sent
455 * "recv-nack-count" G_TYPE_UINT Number of NACKs received
459 g_object_class_install_property (gobject_class, PROP_STATS,
460 g_param_spec_boxed ("stats", "Statistics",
461 "Various statistics", GST_TYPE_STRUCTURE,
462 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
464 klass->get_source_by_ssrc =
465 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
466 klass->send_rtcp = GST_DEBUG_FUNCPTR (rtp_session_send_rtcp);
468 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
472 rtp_session_init (RTPSession * sess)
477 g_mutex_init (&sess->lock);
478 sess->key = g_random_int ();
482 for (i = 0; i < 32; i++) {
484 g_hash_table_new_full (NULL, NULL, NULL,
485 (GDestroyNotify) g_object_unref);
488 rtp_stats_init_defaults (&sess->stats);
489 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
490 rtp_stats_set_min_interval (&sess->stats,
491 (gdouble) DEFAULT_RTCP_MIN_INTERVAL / GST_SECOND);
493 sess->recalc_bandwidth = TRUE;
494 sess->bandwidth = DEFAULT_BANDWIDTH;
495 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
496 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
497 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
499 /* default UDP header length */
500 sess->header_len = 28;
501 sess->mtu = DEFAULT_RTCP_MTU;
503 sess->probation = DEFAULT_PROBATION;
505 /* some default SDES entries */
506 sess->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
508 /* we do not want to leak details like the username or hostname here */
509 str = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
510 gst_structure_set (sess->sdes, "cname", G_TYPE_STRING, str, NULL);
514 /* we do not want to leak the user's real name here */
515 str = g_strdup_printf ("Anon%u", g_random_int ());
516 gst_structure_set (sdes, "name", G_TYPE_STRING, str, NULL);
520 gst_structure_set (sess->sdes, "tool", G_TYPE_STRING, "GStreamer", NULL);
522 /* this is the SSRC we suggest */
523 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
525 sess->first_rtcp = TRUE;
526 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
528 sess->allow_early = TRUE;
529 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
530 sess->rtcp_feedback_retention_window = DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW;
531 sess->rtcp_immediate_feedback_threshold =
532 DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD;
534 sess->last_keyframe_request = GST_CLOCK_TIME_NONE;
536 sess->is_doing_ptp = TRUE;
540 rtp_session_finalize (GObject * object)
545 sess = RTP_SESSION_CAST (object);
547 gst_structure_free (sess->sdes);
549 for (i = 0; i < 32; i++)
550 g_hash_table_destroy (sess->ssrcs[i]);
552 g_mutex_clear (&sess->lock);
554 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
558 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
560 GValue value = { 0 };
562 g_value_init (&value, RTP_TYPE_SOURCE);
563 g_value_take_object (&value, source);
564 /* copies the value */
565 g_value_array_append (arr, &value);
569 rtp_session_create_sources (RTPSession * sess)
574 RTP_SESSION_LOCK (sess);
575 /* get number of elements in the table */
576 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
577 /* create the result value array */
578 res = g_value_array_new (size);
580 /* and copy all values into the array */
581 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
582 RTP_SESSION_UNLOCK (sess);
587 static GstStructure *
588 rtp_session_create_stats (RTPSession * sess)
592 s = gst_structure_new ("application/x-rtp-session-stats",
593 "rtx-drop-count", G_TYPE_UINT, sess->stats.nacks_dropped,
594 "sent-nack-count", G_TYPE_UINT, sess->stats.nacks_sent,
595 "recv-nack-count", G_TYPE_UINT, sess->stats.nacks_received, NULL);
601 rtp_session_set_property (GObject * object, guint prop_id,
602 const GValue * value, GParamSpec * pspec)
606 sess = RTP_SESSION (object);
609 case PROP_INTERNAL_SSRC:
612 RTP_SESSION_LOCK (sess);
613 sess->bandwidth = g_value_get_double (value);
614 sess->recalc_bandwidth = TRUE;
615 RTP_SESSION_UNLOCK (sess);
617 case PROP_RTCP_FRACTION:
618 RTP_SESSION_LOCK (sess);
619 sess->rtcp_bandwidth = g_value_get_double (value);
620 sess->recalc_bandwidth = TRUE;
621 RTP_SESSION_UNLOCK (sess);
623 case PROP_RTCP_RR_BANDWIDTH:
624 RTP_SESSION_LOCK (sess);
625 sess->rtcp_rr_bandwidth = g_value_get_int (value);
626 sess->recalc_bandwidth = TRUE;
627 RTP_SESSION_UNLOCK (sess);
629 case PROP_RTCP_RS_BANDWIDTH:
630 RTP_SESSION_LOCK (sess);
631 sess->rtcp_rs_bandwidth = g_value_get_int (value);
632 sess->recalc_bandwidth = TRUE;
633 RTP_SESSION_UNLOCK (sess);
636 sess->mtu = g_value_get_uint (value);
639 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
642 sess->favor_new = g_value_get_boolean (value);
644 case PROP_RTCP_MIN_INTERVAL:
645 rtp_stats_set_min_interval (&sess->stats,
646 (gdouble) g_value_get_uint64 (value) / GST_SECOND);
647 /* trigger reconsideration */
648 RTP_SESSION_LOCK (sess);
649 sess->next_rtcp_check_time = 0;
650 RTP_SESSION_UNLOCK (sess);
651 if (sess->callbacks.reconsider)
652 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
654 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
655 sess->rtcp_immediate_feedback_threshold = g_value_get_uint (value);
658 sess->probation = g_value_get_uint (value);
661 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
667 rtp_session_get_property (GObject * object, guint prop_id,
668 GValue * value, GParamSpec * pspec)
672 sess = RTP_SESSION (object);
675 case PROP_INTERNAL_SSRC:
676 g_value_set_uint (value, rtp_session_suggest_ssrc (sess));
678 case PROP_INTERNAL_SOURCE:
679 /* FIXME, return a random source */
680 g_value_set_object (value, NULL);
683 g_value_set_double (value, sess->bandwidth);
685 case PROP_RTCP_FRACTION:
686 g_value_set_double (value, sess->rtcp_bandwidth);
688 case PROP_RTCP_RR_BANDWIDTH:
689 g_value_set_int (value, sess->rtcp_rr_bandwidth);
691 case PROP_RTCP_RS_BANDWIDTH:
692 g_value_set_int (value, sess->rtcp_rs_bandwidth);
695 g_value_set_uint (value, sess->mtu);
698 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
700 case PROP_NUM_SOURCES:
701 g_value_set_uint (value, rtp_session_get_num_sources (sess));
703 case PROP_NUM_ACTIVE_SOURCES:
704 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
707 g_value_take_boxed (value, rtp_session_create_sources (sess));
710 g_value_set_boolean (value, sess->favor_new);
712 case PROP_RTCP_MIN_INTERVAL:
713 g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
715 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
716 g_value_set_uint (value, sess->rtcp_immediate_feedback_threshold);
719 g_value_set_uint (value, sess->probation);
722 g_value_take_boxed (value, rtp_session_create_stats (sess));
725 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
731 on_new_ssrc (RTPSession * sess, RTPSource * source)
733 g_object_ref (source);
734 RTP_SESSION_UNLOCK (sess);
735 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
736 RTP_SESSION_LOCK (sess);
737 g_object_unref (source);
741 on_ssrc_collision (RTPSession * sess, RTPSource * source)
743 g_object_ref (source);
744 RTP_SESSION_UNLOCK (sess);
745 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
747 RTP_SESSION_LOCK (sess);
748 g_object_unref (source);
752 on_ssrc_validated (RTPSession * sess, RTPSource * source)
754 g_object_ref (source);
755 RTP_SESSION_UNLOCK (sess);
756 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
758 RTP_SESSION_LOCK (sess);
759 g_object_unref (source);
763 on_ssrc_active (RTPSession * sess, RTPSource * source)
765 g_object_ref (source);
766 RTP_SESSION_UNLOCK (sess);
767 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
768 RTP_SESSION_LOCK (sess);
769 g_object_unref (source);
773 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
775 g_object_ref (source);
776 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
777 RTP_SESSION_UNLOCK (sess);
778 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
779 RTP_SESSION_LOCK (sess);
780 g_object_unref (source);
784 on_bye_ssrc (RTPSession * sess, RTPSource * source)
786 g_object_ref (source);
787 RTP_SESSION_UNLOCK (sess);
788 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
789 RTP_SESSION_LOCK (sess);
790 g_object_unref (source);
794 on_bye_timeout (RTPSession * sess, RTPSource * source)
796 g_object_ref (source);
797 RTP_SESSION_UNLOCK (sess);
798 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
799 RTP_SESSION_LOCK (sess);
800 g_object_unref (source);
804 on_timeout (RTPSession * sess, RTPSource * source)
806 g_object_ref (source);
807 RTP_SESSION_UNLOCK (sess);
808 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
809 RTP_SESSION_LOCK (sess);
810 g_object_unref (source);
814 on_sender_timeout (RTPSession * sess, RTPSource * source)
816 g_object_ref (source);
817 RTP_SESSION_UNLOCK (sess);
818 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
820 RTP_SESSION_LOCK (sess);
821 g_object_unref (source);
827 * Create a new session object.
829 * Returns: a new #RTPSession. g_object_unref() after usage.
832 rtp_session_new (void)
836 sess = g_object_new (RTP_TYPE_SESSION, NULL);
842 * rtp_session_set_callbacks:
843 * @sess: an #RTPSession
844 * @callbacks: callbacks to configure
845 * @user_data: user data passed in the callbacks
847 * Configure a set of callbacks to be notified of actions.
850 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
853 g_return_if_fail (RTP_IS_SESSION (sess));
855 if (callbacks->process_rtp) {
856 sess->callbacks.process_rtp = callbacks->process_rtp;
857 sess->process_rtp_user_data = user_data;
859 if (callbacks->send_rtp) {
860 sess->callbacks.send_rtp = callbacks->send_rtp;
861 sess->send_rtp_user_data = user_data;
863 if (callbacks->send_rtcp) {
864 sess->callbacks.send_rtcp = callbacks->send_rtcp;
865 sess->send_rtcp_user_data = user_data;
867 if (callbacks->sync_rtcp) {
868 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
869 sess->sync_rtcp_user_data = user_data;
871 if (callbacks->clock_rate) {
872 sess->callbacks.clock_rate = callbacks->clock_rate;
873 sess->clock_rate_user_data = user_data;
875 if (callbacks->reconsider) {
876 sess->callbacks.reconsider = callbacks->reconsider;
877 sess->reconsider_user_data = user_data;
879 if (callbacks->request_key_unit) {
880 sess->callbacks.request_key_unit = callbacks->request_key_unit;
881 sess->request_key_unit_user_data = user_data;
883 if (callbacks->request_time) {
884 sess->callbacks.request_time = callbacks->request_time;
885 sess->request_time_user_data = user_data;
887 if (callbacks->notify_nack) {
888 sess->callbacks.notify_nack = callbacks->notify_nack;
889 sess->notify_nack_user_data = user_data;
894 * rtp_session_set_process_rtp_callback:
895 * @sess: an #RTPSession
896 * @callback: callback to set
897 * @user_data: user data passed in the callback
899 * Configure only the process_rtp callback to be notified of the process_rtp action.
902 rtp_session_set_process_rtp_callback (RTPSession * sess,
903 RTPSessionProcessRTP callback, gpointer user_data)
905 g_return_if_fail (RTP_IS_SESSION (sess));
907 sess->callbacks.process_rtp = callback;
908 sess->process_rtp_user_data = user_data;
912 * rtp_session_set_send_rtp_callback:
913 * @sess: an #RTPSession
914 * @callback: callback to set
915 * @user_data: user data passed in the callback
917 * Configure only the send_rtp callback to be notified of the send_rtp action.
920 rtp_session_set_send_rtp_callback (RTPSession * sess,
921 RTPSessionSendRTP callback, gpointer user_data)
923 g_return_if_fail (RTP_IS_SESSION (sess));
925 sess->callbacks.send_rtp = callback;
926 sess->send_rtp_user_data = user_data;
930 * rtp_session_set_send_rtcp_callback:
931 * @sess: an #RTPSession
932 * @callback: callback to set
933 * @user_data: user data passed in the callback
935 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
938 rtp_session_set_send_rtcp_callback (RTPSession * sess,
939 RTPSessionSendRTCP callback, gpointer user_data)
941 g_return_if_fail (RTP_IS_SESSION (sess));
943 sess->callbacks.send_rtcp = callback;
944 sess->send_rtcp_user_data = user_data;
948 * rtp_session_set_sync_rtcp_callback:
949 * @sess: an #RTPSession
950 * @callback: callback to set
951 * @user_data: user data passed in the callback
953 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
956 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
957 RTPSessionSyncRTCP callback, gpointer user_data)
959 g_return_if_fail (RTP_IS_SESSION (sess));
961 sess->callbacks.sync_rtcp = callback;
962 sess->sync_rtcp_user_data = user_data;
966 * rtp_session_set_clock_rate_callback:
967 * @sess: an #RTPSession
968 * @callback: callback to set
969 * @user_data: user data passed in the callback
971 * Configure only the clock_rate callback to be notified of the clock_rate action.
974 rtp_session_set_clock_rate_callback (RTPSession * sess,
975 RTPSessionClockRate callback, gpointer user_data)
977 g_return_if_fail (RTP_IS_SESSION (sess));
979 sess->callbacks.clock_rate = callback;
980 sess->clock_rate_user_data = user_data;
984 * rtp_session_set_reconsider_callback:
985 * @sess: an #RTPSession
986 * @callback: callback to set
987 * @user_data: user data passed in the callback
989 * Configure only the reconsider callback to be notified of the reconsider action.
992 rtp_session_set_reconsider_callback (RTPSession * sess,
993 RTPSessionReconsider callback, gpointer user_data)
995 g_return_if_fail (RTP_IS_SESSION (sess));
997 sess->callbacks.reconsider = callback;
998 sess->reconsider_user_data = user_data;
1002 * rtp_session_set_request_time_callback:
1003 * @sess: an #RTPSession
1004 * @callback: callback to set
1005 * @user_data: user data passed in the callback
1007 * Configure only the request_time callback
1010 rtp_session_set_request_time_callback (RTPSession * sess,
1011 RTPSessionRequestTime callback, gpointer user_data)
1013 g_return_if_fail (RTP_IS_SESSION (sess));
1015 sess->callbacks.request_time = callback;
1016 sess->request_time_user_data = user_data;
1020 * rtp_session_set_bandwidth:
1021 * @sess: an #RTPSession
1022 * @bandwidth: the bandwidth allocated
1024 * Set the session bandwidth in bytes per second.
1027 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
1029 g_return_if_fail (RTP_IS_SESSION (sess));
1031 RTP_SESSION_LOCK (sess);
1032 sess->stats.bandwidth = bandwidth;
1033 RTP_SESSION_UNLOCK (sess);
1037 * rtp_session_get_bandwidth:
1038 * @sess: an #RTPSession
1040 * Get the session bandwidth.
1042 * Returns: the session bandwidth.
1045 rtp_session_get_bandwidth (RTPSession * sess)
1049 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1051 RTP_SESSION_LOCK (sess);
1052 result = sess->stats.bandwidth;
1053 RTP_SESSION_UNLOCK (sess);
1059 * rtp_session_set_rtcp_fraction:
1060 * @sess: an #RTPSession
1061 * @bandwidth: the RTCP bandwidth
1063 * Set the bandwidth in bytes per second that should be used for RTCP
1067 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
1069 g_return_if_fail (RTP_IS_SESSION (sess));
1071 RTP_SESSION_LOCK (sess);
1072 sess->stats.rtcp_bandwidth = bandwidth;
1073 RTP_SESSION_UNLOCK (sess);
1077 * rtp_session_get_rtcp_fraction:
1078 * @sess: an #RTPSession
1080 * Get the session bandwidth used for RTCP.
1082 * Returns: The bandwidth used for RTCP messages.
1085 rtp_session_get_rtcp_fraction (RTPSession * sess)
1089 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
1091 RTP_SESSION_LOCK (sess);
1092 result = sess->stats.rtcp_bandwidth;
1093 RTP_SESSION_UNLOCK (sess);
1099 * rtp_session_get_sdes_struct:
1100 * @sess: an #RTSPSession
1102 * Get the SDES data as a #GstStructure
1104 * Returns: a GstStructure with SDES items for @sess. This function returns a
1105 * copy of the SDES structure, use gst_structure_free() after usage.
1108 rtp_session_get_sdes_struct (RTPSession * sess)
1110 GstStructure *result = NULL;
1112 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1114 RTP_SESSION_LOCK (sess);
1116 result = gst_structure_copy (sess->sdes);
1117 RTP_SESSION_UNLOCK (sess);
1123 * rtp_session_set_sdes_struct:
1124 * @sess: an #RTSPSession
1125 * @sdes: a #GstStructure
1127 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1130 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
1132 g_return_if_fail (sdes);
1133 g_return_if_fail (RTP_IS_SESSION (sess));
1135 RTP_SESSION_LOCK (sess);
1137 gst_structure_free (sess->sdes);
1138 sess->sdes = gst_structure_copy (sdes);
1139 RTP_SESSION_UNLOCK (sess);
1142 static GstFlowReturn
1143 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1145 GstFlowReturn result = GST_FLOW_OK;
1147 if (source->internal) {
1148 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1150 RTP_SESSION_UNLOCK (session);
1152 if (session->callbacks.send_rtp)
1154 session->callbacks.send_rtp (session, source, data,
1155 session->send_rtp_user_data);
1157 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1160 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1161 RTP_SESSION_UNLOCK (session);
1163 if (session->callbacks.process_rtp)
1165 session->callbacks.process_rtp (session, source,
1166 GST_BUFFER_CAST (data), session->process_rtp_user_data);
1168 gst_buffer_unref (GST_BUFFER_CAST (data));
1170 RTP_SESSION_LOCK (session);
1176 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1180 RTP_SESSION_UNLOCK (session);
1182 if (session->callbacks.clock_rate)
1184 session->callbacks.clock_rate (session, pt,
1185 session->clock_rate_user_data);
1189 RTP_SESSION_LOCK (session);
1191 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1196 static RTPSourceCallbacks callbacks = {
1197 (RTPSourcePushRTP) source_push_rtp,
1198 (RTPSourceClockRate) source_clock_rate,
1202 check_collision (RTPSession * sess, RTPSource * source,
1203 RTPPacketInfo * pinfo, gboolean rtp)
1207 /* If we have no pinfo address, we can't do collision checking */
1208 if (!pinfo->address)
1211 ssrc = rtp_source_get_ssrc (source);
1213 if (!source->internal) {
1214 GSocketAddress *from;
1216 /* This is not our local source, but lets check if two remote
1219 from = source->rtp_from;
1221 from = source->rtcp_from;
1225 if (__g_socket_address_equal (from, pinfo->address)) {
1226 /* Address is the same */
1229 GST_LOG ("we have a third-party collision or loop ssrc:%x", ssrc);
1230 if (sess->favor_new) {
1231 if (rtp_source_find_conflicting_address (source,
1232 pinfo->address, pinfo->current_time)) {
1235 buf1 = __g_socket_address_to_string (pinfo->address);
1236 GST_LOG ("Known conflict on %x for %s, dropping packet", ssrc,
1244 /* Current address is not a known conflict, lets assume this is
1245 * a new source. Save old address in possible conflict list
1247 rtp_source_add_conflicting_address (source, from,
1248 pinfo->current_time);
1250 buf1 = __g_socket_address_to_string (from);
1251 buf2 = __g_socket_address_to_string (pinfo->address);
1253 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1254 " saving old as known conflict", ssrc, buf1, buf2);
1257 rtp_source_set_rtp_from (source, pinfo->address);
1259 rtp_source_set_rtcp_from (source, pinfo->address);
1267 /* Don't need to save old addresses, we ignore new sources */
1272 /* We don't already have a from address for RTP, just set it */
1274 rtp_source_set_rtp_from (source, pinfo->address);
1276 rtp_source_set_rtcp_from (source, pinfo->address);
1280 /* FIXME: Log 3rd party collision somehow
1281 * Maybe should be done in upper layer, only the SDES can tell us
1282 * if its a collision or a loop
1285 /* This is sending with our ssrc, is it an address we already know */
1286 if (rtp_source_find_conflicting_address (source, pinfo->address,
1287 pinfo->current_time)) {
1288 /* Its a known conflict, its probably a loop, not a collision
1289 * lets just drop the incoming packet
1291 GST_DEBUG ("Our packets are being looped back to us, dropping");
1293 /* Its a new collision, lets change our SSRC */
1294 rtp_source_add_conflicting_address (source, pinfo->address,
1295 pinfo->current_time);
1297 GST_DEBUG ("Collision for SSRC %x", ssrc);
1298 /* mark the source BYE */
1299 rtp_source_mark_bye (source, "SSRC Collision");
1300 /* if we were suggesting this SSRC, change to something else */
1301 if (sess->suggested_ssrc == ssrc)
1302 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
1304 on_ssrc_collision (sess, source);
1306 rtp_session_schedule_bye_locked (sess, pinfo->current_time);
1315 gboolean is_doing_ptp;
1316 GSocketAddress *new_addr;
1319 /* check if the two given ip addr are the same (do not care about the port) */
1321 ip_addr_equal (GSocketAddress * a, GSocketAddress * b)
1324 g_inet_address_equal (g_inet_socket_address_get_address
1325 (G_INET_SOCKET_ADDRESS (a)),
1326 g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (b)));
1330 compare_rtp_source_addr (const gchar * key, RTPSource * source,
1331 CompareAddrData * data)
1333 /* only compare ip addr of remote sources which are also not closing */
1334 if (!source->internal && !source->closing && source->rtp_from) {
1335 /* look for the first rtp source */
1336 if (!data->new_addr)
1337 data->new_addr = source->rtp_from;
1338 /* compare current ip addr with the first one */
1340 data->is_doing_ptp &= ip_addr_equal (data->new_addr, source->rtp_from);
1345 compare_rtcp_source_addr (const gchar * key, RTPSource * source,
1346 CompareAddrData * data)
1348 /* only compare ip addr of remote sources which are also not closing */
1349 if (!source->internal && !source->closing && source->rtcp_from) {
1350 /* look for the first rtcp source */
1351 if (!data->new_addr)
1352 data->new_addr = source->rtcp_from;
1354 /* compare current ip addr with the first one */
1355 data->is_doing_ptp &= ip_addr_equal (data->new_addr, source->rtcp_from);
1359 /* loop over our non-internal source to know if the session
1360 * is doing point-to-point */
1362 session_update_ptp (RTPSession * sess)
1364 /* to know if the session is doing point to point, the ip addr
1365 * of each non-internal (=remotes) source have to be compared
1368 gboolean is_doing_rtp_ptp = FALSE;
1369 gboolean is_doing_rtcp_ptp = FALSE;
1370 CompareAddrData data;
1372 /* compare the first remote source's ip addr that receive rtp packets
1373 * with other remote rtp source.
1374 * it's enough because the session just needs to know if they are all
1377 data.is_doing_ptp = TRUE;
1378 data.new_addr = NULL;
1379 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
1380 (GHFunc) compare_rtp_source_addr, (gpointer) & data);
1381 is_doing_rtp_ptp = data.is_doing_ptp;
1383 /* same but about rtcp */
1384 data.is_doing_ptp = TRUE;
1385 data.new_addr = NULL;
1386 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
1387 (GHFunc) compare_rtcp_source_addr, (gpointer) & data);
1388 is_doing_rtcp_ptp = data.is_doing_ptp;
1390 /* the session is doing point-to-point if all rtp remote have the same
1391 * ip addr and if all rtcp remote sources have the same ip addr */
1392 sess->is_doing_ptp = is_doing_rtp_ptp && is_doing_rtcp_ptp;
1394 GST_DEBUG ("doing point-to-point: %d", sess->is_doing_ptp);
1398 add_source (RTPSession * sess, RTPSource * src)
1400 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1401 GINT_TO_POINTER (src->ssrc), src);
1402 /* report the new source ASAP */
1403 src->generation = sess->generation;
1404 /* we have one more source now */
1405 sess->total_sources++;
1406 if (RTP_SOURCE_IS_ACTIVE (src))
1407 sess->stats.active_sources++;
1408 if (src->internal) {
1409 sess->stats.internal_sources++;
1410 if (sess->suggested_ssrc != src->ssrc)
1411 sess->suggested_ssrc = src->ssrc;
1414 /* update point-to-point status */
1416 session_update_ptp (sess);
1420 find_source (RTPSession * sess, guint32 ssrc)
1422 return g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1423 GINT_TO_POINTER (ssrc));
1426 /* must be called with the session lock, the returned source needs to be
1427 * unreffed after usage. */
1429 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1430 RTPPacketInfo * pinfo, gboolean rtp)
1434 source = find_source (sess, ssrc);
1435 if (source == NULL) {
1436 /* make new Source in probation and insert */
1437 source = rtp_source_new (ssrc);
1439 GST_DEBUG ("creating new source %08x %p", ssrc, source);
1441 /* for RTP packets we need to set the source in probation. Receiving RTCP
1442 * packets of an SSRC, on the other hand, is a strong indication that we
1443 * are dealing with a valid source. */
1445 g_object_set (source, "probation", sess->probation, NULL);
1447 g_object_set (source, "probation", 0, NULL);
1449 /* store from address, if any */
1450 if (pinfo->address) {
1452 rtp_source_set_rtp_from (source, pinfo->address);
1454 rtp_source_set_rtcp_from (source, pinfo->address);
1457 /* configure a callback on the source */
1458 rtp_source_set_callbacks (source, &callbacks, sess);
1460 add_source (sess, source);
1464 /* check for collision, this updates the address when not previously set */
1465 if (check_collision (sess, source, pinfo, rtp)) {
1468 /* Receiving RTCP packets of an SSRC is a strong indication that we
1469 * are dealing with a valid source. */
1471 g_object_set (source, "probation", 0, NULL);
1473 /* update last activity */
1474 source->last_activity = pinfo->current_time;
1476 source->last_rtp_activity = pinfo->current_time;
1477 g_object_ref (source);
1482 /* must be called with the session lock, the returned source needs to be
1483 * unreffed after usage. */
1485 obtain_internal_source (RTPSession * sess, guint32 ssrc, gboolean * created)
1489 source = find_source (sess, ssrc);
1490 if (source == NULL) {
1491 /* make new internal Source and insert */
1492 source = rtp_source_new (ssrc);
1494 GST_DEBUG ("creating new internal source %08x %p", ssrc, source);
1496 source->validated = TRUE;
1497 source->internal = TRUE;
1498 source->probation = FALSE;
1499 rtp_source_set_sdes_struct (source, gst_structure_copy (sess->sdes));
1500 rtp_source_set_callbacks (source, &callbacks, sess);
1502 add_source (sess, source);
1507 g_object_ref (source);
1513 * rtp_session_suggest_ssrc:
1514 * @sess: a #RTPSession
1516 * Suggest an unused SSRC in @sess.
1518 * Returns: a free unused SSRC
1521 rtp_session_suggest_ssrc (RTPSession * sess)
1525 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1527 RTP_SESSION_LOCK (sess);
1528 result = sess->suggested_ssrc;
1529 RTP_SESSION_UNLOCK (sess);
1535 * rtp_session_add_source:
1536 * @sess: a #RTPSession
1537 * @src: #RTPSource to add
1539 * Add @src to @session.
1541 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1542 * existed in the session.
1545 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1547 gboolean result = FALSE;
1550 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1551 g_return_val_if_fail (src != NULL, FALSE);
1553 RTP_SESSION_LOCK (sess);
1554 find = find_source (sess, src->ssrc);
1556 add_source (sess, src);
1559 RTP_SESSION_UNLOCK (sess);
1565 * rtp_session_get_num_sources:
1566 * @sess: an #RTPSession
1568 * Get the number of sources in @sess.
1570 * Returns: The number of sources in @sess.
1573 rtp_session_get_num_sources (RTPSession * sess)
1577 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1579 RTP_SESSION_LOCK (sess);
1580 result = sess->total_sources;
1581 RTP_SESSION_UNLOCK (sess);
1587 * rtp_session_get_num_active_sources:
1588 * @sess: an #RTPSession
1590 * Get the number of active sources in @sess. A source is considered active when
1591 * it has been validated and has not yet received a BYE RTCP message.
1593 * Returns: The number of active sources in @sess.
1596 rtp_session_get_num_active_sources (RTPSession * sess)
1600 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1602 RTP_SESSION_LOCK (sess);
1603 result = sess->stats.active_sources;
1604 RTP_SESSION_UNLOCK (sess);
1610 * rtp_session_get_source_by_ssrc:
1611 * @sess: an #RTPSession
1614 * Find the source with @ssrc in @sess.
1616 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1617 * g_object_unref() after usage.
1620 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1624 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1626 RTP_SESSION_LOCK (sess);
1627 result = find_source (sess, ssrc);
1629 g_object_ref (result);
1630 RTP_SESSION_UNLOCK (sess);
1635 /* should be called with the SESSION lock */
1637 rtp_session_create_new_ssrc (RTPSession * sess)
1642 ssrc = g_random_int ();
1644 /* see if it exists in the session, we're done if it doesn't */
1645 if (find_source (sess, ssrc) == NULL)
1653 * rtp_session_create_source:
1654 * @sess: an #RTPSession
1656 * Create an #RTPSource for use in @sess. This function will create a source
1657 * with an ssrc that is currently not used by any participants in the session.
1659 * Returns: an #RTPSource.
1662 rtp_session_create_source (RTPSession * sess)
1667 RTP_SESSION_LOCK (sess);
1668 ssrc = rtp_session_create_new_ssrc (sess);
1669 source = rtp_source_new (ssrc);
1670 rtp_source_set_callbacks (source, &callbacks, sess);
1671 /* we need an additional ref for the source in the hashtable */
1672 g_object_ref (source);
1673 add_source (sess, source);
1674 RTP_SESSION_UNLOCK (sess);
1680 update_packet (GstBuffer ** buffer, guint idx, RTPPacketInfo * pinfo)
1682 GstNetAddressMeta *meta;
1684 /* get packet size including header overhead */
1685 pinfo->bytes += gst_buffer_get_size (*buffer) + pinfo->header_len;
1689 GstRTPBuffer rtp = { NULL };
1691 if (!gst_rtp_buffer_map (*buffer, GST_MAP_READ, &rtp))
1692 goto invalid_packet;
1694 pinfo->payload_len += gst_rtp_buffer_get_payload_len (&rtp);
1698 /* only keep info for first buffer */
1699 pinfo->ssrc = gst_rtp_buffer_get_ssrc (&rtp);
1700 pinfo->seqnum = gst_rtp_buffer_get_seq (&rtp);
1701 pinfo->pt = gst_rtp_buffer_get_payload_type (&rtp);
1702 pinfo->rtptime = gst_rtp_buffer_get_timestamp (&rtp);
1703 /* copy available csrc */
1704 pinfo->csrc_count = gst_rtp_buffer_get_csrc_count (&rtp);
1705 for (i = 0; i < pinfo->csrc_count; i++)
1706 pinfo->csrcs[i] = gst_rtp_buffer_get_csrc (&rtp, i);
1708 gst_rtp_buffer_unmap (&rtp);
1712 /* for netbuffer we can store the IP address to check for collisions */
1713 meta = gst_buffer_get_net_address_meta (*buffer);
1715 g_object_unref (pinfo->address);
1717 pinfo->address = G_SOCKET_ADDRESS (g_object_ref (meta->addr));
1719 pinfo->address = NULL;
1727 GST_DEBUG ("invalid RTP packet received");
1732 /* update the RTPPacketInfo structure with the current time and other bits
1733 * about the current buffer we are handling.
1734 * This function is typically called when a validated packet is received.
1735 * This function should be called with the SESSION_LOCK
1738 update_packet_info (RTPSession * sess, RTPPacketInfo * pinfo,
1739 gboolean send, gboolean rtp, gboolean is_list, gpointer data,
1740 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
1746 pinfo->is_list = is_list;
1748 pinfo->current_time = current_time;
1749 pinfo->running_time = running_time;
1750 pinfo->ntpnstime = ntpnstime;
1751 pinfo->header_len = sess->header_len;
1753 pinfo->payload_len = 0;
1757 GstBufferList *list = GST_BUFFER_LIST_CAST (data);
1759 gst_buffer_list_foreach (list, (GstBufferListFunc) update_packet,
1762 GstBuffer *buffer = GST_BUFFER_CAST (data);
1763 res = update_packet (&buffer, 0, pinfo);
1769 clean_packet_info (RTPPacketInfo * pinfo)
1772 g_object_unref (pinfo->address);
1774 gst_mini_object_unref (pinfo->data);
1780 source_update_active (RTPSession * sess, RTPSource * source,
1781 gboolean prevactive)
1783 gboolean active = RTP_SOURCE_IS_ACTIVE (source);
1784 guint32 ssrc = source->ssrc;
1786 if (prevactive == active)
1790 sess->stats.active_sources++;
1791 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1792 sess->stats.active_sources);
1794 sess->stats.active_sources--;
1795 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
1796 sess->stats.active_sources);
1802 source_update_sender (RTPSession * sess, RTPSource * source,
1803 gboolean prevsender)
1805 gboolean sender = RTP_SOURCE_IS_SENDER (source);
1806 guint32 ssrc = source->ssrc;
1808 if (prevsender == sender)
1812 sess->stats.sender_sources++;
1813 if (source->internal)
1814 sess->stats.internal_sender_sources++;
1815 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1816 sess->stats.sender_sources);
1818 sess->stats.sender_sources--;
1819 if (source->internal)
1820 sess->stats.internal_sender_sources--;
1821 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
1822 sess->stats.sender_sources);
1828 * rtp_session_process_rtp:
1829 * @sess: and #RTPSession
1830 * @buffer: an RTP buffer
1831 * @current_time: the current system time
1832 * @running_time: the running_time of @buffer
1834 * Process an RTP buffer in the session manager. This function takes ownership
1837 * Returns: a #GstFlowReturn.
1840 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1841 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
1843 GstFlowReturn result;
1847 gboolean prevsender, prevactive;
1848 RTPPacketInfo pinfo = { 0, };
1851 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1852 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1854 RTP_SESSION_LOCK (sess);
1856 /* update pinfo stats */
1857 if (!update_packet_info (sess, &pinfo, FALSE, TRUE, FALSE, buffer,
1858 current_time, running_time, ntpnstime)) {
1859 GST_DEBUG ("invalid RTP packet received");
1860 RTP_SESSION_UNLOCK (sess);
1861 return rtp_session_process_rtcp (sess, buffer, current_time, ntpnstime);
1866 source = obtain_source (sess, ssrc, &created, &pinfo, TRUE);
1870 prevsender = RTP_SOURCE_IS_SENDER (source);
1871 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1872 oldrate = source->bitrate;
1874 /* let source process the packet */
1875 result = rtp_source_process_rtp (source, &pinfo);
1877 /* source became active */
1878 if (source_update_active (sess, source, prevactive))
1879 on_ssrc_validated (sess, source);
1881 source_update_sender (sess, source, prevsender);
1883 if (oldrate != source->bitrate)
1884 sess->recalc_bandwidth = TRUE;
1887 on_new_ssrc (sess, source);
1889 if (source->validated) {
1893 /* for validated sources, we add the CSRCs as well */
1894 for (i = 0; i < pinfo.csrc_count; i++) {
1896 RTPSource *csrc_src;
1898 csrc = pinfo.csrcs[i];
1901 csrc_src = obtain_source (sess, csrc, &created, &pinfo, TRUE);
1906 GST_DEBUG ("created new CSRC: %08x", csrc);
1907 rtp_source_set_as_csrc (csrc_src);
1908 source_update_active (sess, csrc_src, FALSE);
1909 on_new_ssrc (sess, csrc_src);
1911 g_object_unref (csrc_src);
1914 g_object_unref (source);
1916 RTP_SESSION_UNLOCK (sess);
1918 clean_packet_info (&pinfo);
1925 RTP_SESSION_UNLOCK (sess);
1926 clean_packet_info (&pinfo);
1927 GST_DEBUG ("ignoring packet because its collisioning");
1933 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1934 GstRTCPPacket * packet, RTPPacketInfo * pinfo)
1938 count = gst_rtcp_packet_get_rb_count (packet);
1939 for (i = 0; i < count; i++) {
1940 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1941 guint8 fractionlost;
1945 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1946 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1948 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1950 /* find our own source */
1951 src = find_source (sess, ssrc);
1955 if (src->internal && RTP_SOURCE_IS_ACTIVE (src)) {
1956 /* only deal with report blocks for our session, we update the stats of
1957 * the sender of the RTCP message. We could also compare our stats against
1958 * the other sender to see if we are better or worse. */
1959 /* FIXME, need to keep track who the RB block is from */
1960 rtp_source_process_rb (source, pinfo->ntpnstime, fractionlost,
1961 packetslost, exthighestseq, jitter, lsr, dlsr);
1964 on_ssrc_active (sess, source);
1967 /* A Sender report contains statistics about how the sender is doing. This
1968 * includes timing informataion such as the relation between RTP and NTP
1969 * timestamps and the number of packets/bytes it sent to us.
1971 * In this report is also included a set of report blocks related to how this
1972 * sender is receiving data (in case we (or somebody else) is also sending stuff
1973 * to it). This info includes the packet loss, jitter and seqnum. It also
1974 * contains information to calculate the round trip time (LSR/DLSR).
1977 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1978 RTPPacketInfo * pinfo, gboolean * do_sync)
1980 guint32 senderssrc, rtptime, packet_count, octet_count;
1983 gboolean created, prevsender;
1985 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1986 &packet_count, &octet_count);
1988 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1989 senderssrc, GST_TIME_ARGS (pinfo->current_time));
1991 source = obtain_source (sess, senderssrc, &created, pinfo, FALSE);
1995 /* skip non-bye packets for sources that are marked BYE */
1996 if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
1999 /* don't try to do lip-sync for sources that sent a BYE */
2000 if (RTP_SOURCE_IS_MARKED_BYE (source))
2005 prevsender = RTP_SOURCE_IS_SENDER (source);
2007 /* first update the source */
2008 rtp_source_process_sr (source, pinfo->current_time, ntptime, rtptime,
2009 packet_count, octet_count);
2011 source_update_sender (sess, source, prevsender);
2014 on_new_ssrc (sess, source);
2016 rtp_session_process_rb (sess, source, packet, pinfo);
2019 g_object_unref (source);
2022 /* A receiver report contains statistics about how a receiver is doing. It
2023 * includes stuff like packet loss, jitter and the seqnum it received last. It
2024 * also contains info to calculate the round trip time.
2026 * We are only interested in how the sender of this report is doing wrt to us.
2029 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
2030 RTPPacketInfo * pinfo)
2036 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
2038 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
2040 source = obtain_source (sess, senderssrc, &created, pinfo, FALSE);
2044 /* skip non-bye packets for sources that are marked BYE */
2045 if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
2049 on_new_ssrc (sess, source);
2051 rtp_session_process_rb (sess, source, packet, pinfo);
2054 g_object_unref (source);
2057 /* Get SDES items and store them in the SSRC */
2059 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
2060 RTPPacketInfo * pinfo)
2063 gboolean more_items, more_entries;
2065 items = gst_rtcp_packet_sdes_get_item_count (packet);
2066 GST_DEBUG ("got SDES packet with %d items", items);
2068 more_items = gst_rtcp_packet_sdes_first_item (packet);
2070 while (more_items) {
2072 gboolean changed, created, prevactive;
2076 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
2078 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
2082 /* find src, no probation when dealing with RTCP */
2083 source = obtain_source (sess, ssrc, &created, pinfo, FALSE);
2087 /* skip non-bye packets for sources that are marked BYE */
2088 if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
2091 sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
2093 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
2095 while (more_entries) {
2096 GstRTCPSDESType type;
2102 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
2104 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
2107 if (type == GST_RTCP_SDES_PRIV) {
2108 name = g_strndup ((const gchar *) &data[1], data[0]);
2110 data += data[0] + 1;
2112 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
2115 value = g_strndup ((const gchar *) data, len);
2117 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
2122 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
2126 /* takes ownership of sdes */
2127 changed = rtp_source_set_sdes_struct (source, sdes);
2129 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2130 source->validated = TRUE;
2133 on_new_ssrc (sess, source);
2135 /* source became active */
2136 if (source_update_active (sess, source, prevactive))
2137 on_ssrc_validated (sess, source);
2140 on_ssrc_sdes (sess, source);
2143 g_object_unref (source);
2145 more_items = gst_rtcp_packet_sdes_next_item (packet);
2150 /* BYE is sent when a client leaves the session
2153 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
2154 RTPPacketInfo * pinfo)
2158 gboolean reconsider = FALSE;
2160 reason = gst_rtcp_packet_bye_get_reason (packet);
2161 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
2163 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
2164 for (i = 0; i < count; i++) {
2167 gboolean created, prevactive, prevsender;
2168 guint pmembers, members;
2170 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
2171 GST_DEBUG ("SSRC: %08x", ssrc);
2173 /* find src and mark bye, no probation when dealing with RTCP */
2174 source = obtain_source (sess, ssrc, &created, pinfo, FALSE);
2178 if (source->internal) {
2179 /* our own source, something weird with this packet */
2180 g_object_unref (source);
2184 /* store time for when we need to time out this source */
2185 source->bye_time = pinfo->current_time;
2187 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2188 prevsender = RTP_SOURCE_IS_SENDER (source);
2190 /* mark the source BYE */
2191 rtp_source_mark_bye (source, reason);
2193 pmembers = sess->stats.active_sources;
2195 source_update_active (sess, source, prevactive);
2196 source_update_sender (sess, source, prevsender);
2198 members = sess->stats.active_sources;
2200 if (!sess->scheduled_bye && members < pmembers) {
2201 /* some members went away since the previous timeout estimate.
2202 * Perform reverse reconsideration but only when we are not scheduling a
2204 if (sess->next_rtcp_check_time != GST_CLOCK_TIME_NONE &&
2205 pinfo->current_time < sess->next_rtcp_check_time) {
2206 GstClockTime time_remaining;
2208 time_remaining = sess->next_rtcp_check_time - pinfo->current_time;
2209 sess->next_rtcp_check_time =
2210 gst_util_uint64_scale (time_remaining, members, pmembers);
2212 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
2213 GST_TIME_ARGS (sess->next_rtcp_check_time));
2215 sess->next_rtcp_check_time += pinfo->current_time;
2217 /* mark pending reconsider. We only want to signal the reconsideration
2218 * once after we handled all the source in the bye packet */
2224 on_new_ssrc (sess, source);
2226 on_bye_ssrc (sess, source);
2228 g_object_unref (source);
2231 RTP_SESSION_UNLOCK (sess);
2232 /* notify app of reconsideration */
2233 if (sess->callbacks.reconsider)
2234 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2235 RTP_SESSION_LOCK (sess);
2241 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
2242 RTPPacketInfo * pinfo)
2244 GST_DEBUG ("received APP");
2248 rtp_session_request_local_key_unit (RTPSession * sess, RTPSource * src,
2249 gboolean fir, GstClockTime current_time)
2251 guint32 round_trip = 0;
2253 rtp_source_get_last_rb (src, NULL, NULL, NULL, NULL, NULL, NULL, &round_trip);
2255 if (sess->last_keyframe_request != GST_CLOCK_TIME_NONE && round_trip) {
2256 GstClockTime round_trip_in_ns = gst_util_uint64_scale (round_trip,
2259 if (current_time - sess->last_keyframe_request < 2 * round_trip_in_ns) {
2260 GST_DEBUG ("Ignoring %s request because one was send without one "
2261 "RTT (%" GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")",
2262 fir ? "FIR" : "PLI",
2263 GST_TIME_ARGS (current_time - sess->last_keyframe_request),
2264 GST_TIME_ARGS (round_trip_in_ns));;
2269 sess->last_keyframe_request = current_time;
2271 GST_LOG ("received %s request from %X %p(%p)", fir ? "FIR" : "PLI",
2272 rtp_source_get_ssrc (src), sess->callbacks.process_rtp,
2273 sess->callbacks.request_key_unit);
2275 RTP_SESSION_UNLOCK (sess);
2276 sess->callbacks.request_key_unit (sess, fir,
2277 sess->request_key_unit_user_data);
2278 RTP_SESSION_LOCK (sess);
2284 rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
2285 guint32 media_ssrc, GstClockTime current_time)
2289 if (!sess->callbacks.request_key_unit)
2292 src = find_source (sess, sender_ssrc);
2296 rtp_session_request_local_key_unit (sess, src, FALSE, current_time);
2300 rtp_session_process_fir (RTPSession * sess, guint32 sender_ssrc,
2301 guint8 * fci_data, guint fci_length, GstClockTime current_time)
2306 gboolean our_request = FALSE;
2308 if (!sess->callbacks.request_key_unit)
2314 src = find_source (sess, sender_ssrc);
2316 /* Hack because Google fails to set the sender_ssrc correctly */
2317 if (!src && sender_ssrc == 1) {
2318 GHashTableIter iter;
2320 /* we can't find the source if there are multiple */
2321 if (sess->stats.sender_sources > sess->stats.internal_sender_sources + 1)
2324 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
2325 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
2326 if (!src->internal && rtp_source_is_sender (src))
2334 for (position = 0; position < fci_length; position += 8) {
2335 guint8 *data = fci_data + position;
2338 ssrc = GST_READ_UINT32_BE (data);
2340 own = find_source (sess, ssrc);
2341 if (own->internal) {
2349 rtp_session_request_local_key_unit (sess, src, TRUE, current_time);
2353 rtp_session_process_nack (RTPSession * sess, guint32 sender_ssrc,
2354 guint32 media_ssrc, guint8 * fci_data, guint fci_length,
2355 GstClockTime current_time)
2357 if (!sess->callbacks.notify_nack)
2360 while (fci_length > 0) {
2361 guint16 seqnum, blp;
2363 seqnum = GST_READ_UINT16_BE (fci_data);
2364 blp = GST_READ_UINT16_BE (fci_data + 2);
2366 GST_DEBUG ("NACK #%u, blp %04x", seqnum, blp);
2368 RTP_SESSION_UNLOCK (sess);
2369 sess->callbacks.notify_nack (sess, seqnum, blp,
2370 sess->notify_nack_user_data);
2371 RTP_SESSION_LOCK (sess);
2379 rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
2380 RTPPacketInfo * pinfo, GstClockTime current_time)
2382 GstRTCPType type = gst_rtcp_packet_get_type (packet);
2383 GstRTCPFBType fbtype = gst_rtcp_packet_fb_get_type (packet);
2384 guint32 sender_ssrc = gst_rtcp_packet_fb_get_sender_ssrc (packet);
2385 guint32 media_ssrc = gst_rtcp_packet_fb_get_media_ssrc (packet);
2386 guint8 *fci_data = gst_rtcp_packet_fb_get_fci (packet);
2387 guint fci_length = 4 * gst_rtcp_packet_fb_get_fci_length (packet);
2390 src = find_source (sess, media_ssrc);
2392 /* skip non-bye packets for sources that are marked BYE */
2393 if (sess->scheduled_bye && src && RTP_SOURCE_IS_MARKED_BYE (src))
2396 GST_DEBUG ("received feedback %d:%d from %08X about %08X with FCI of "
2397 "length %d", type, fbtype, sender_ssrc, media_ssrc, fci_length);
2399 sess->stats.nacks_received++;
2401 if (g_signal_has_handler_pending (sess,
2402 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0, TRUE)) {
2403 GstBuffer *fci_buffer = NULL;
2405 if (fci_length > 0) {
2406 fci_buffer = gst_buffer_copy_region (packet->rtcp->buffer,
2407 GST_BUFFER_COPY_MEMORY, fci_data - packet->rtcp->map.data,
2409 GST_BUFFER_TIMESTAMP (fci_buffer) = pinfo->running_time;
2412 RTP_SESSION_UNLOCK (sess);
2413 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0,
2414 type, fbtype, sender_ssrc, media_ssrc, fci_buffer);
2415 RTP_SESSION_LOCK (sess);
2418 gst_buffer_unref (fci_buffer);
2424 if (sess->rtcp_feedback_retention_window) {
2425 rtp_source_retain_rtcp_packet (src, packet, pinfo->running_time);
2428 if (src->internal ||
2429 /* PSFB FIR puts the media ssrc inside the FCI */
2430 (type == GST_RTCP_TYPE_PSFB && fbtype == GST_RTCP_PSFB_TYPE_FIR)) {
2432 case GST_RTCP_TYPE_PSFB:
2434 case GST_RTCP_PSFB_TYPE_PLI:
2435 rtp_session_process_pli (sess, sender_ssrc, media_ssrc,
2438 case GST_RTCP_PSFB_TYPE_FIR:
2439 rtp_session_process_fir (sess, sender_ssrc, fci_data, fci_length,
2446 case GST_RTCP_TYPE_RTPFB:
2448 case GST_RTCP_RTPFB_TYPE_NACK:
2449 rtp_session_process_nack (sess, sender_ssrc, media_ssrc,
2450 fci_data, fci_length, current_time);
2462 * rtp_session_process_rtcp:
2463 * @sess: and #RTPSession
2464 * @buffer: an RTCP buffer
2465 * @current_time: the current system time
2466 * @ntpnstime: the current NTP time in nanoseconds
2468 * Process an RTCP buffer in the session manager. This function takes ownership
2471 * Returns: a #GstFlowReturn.
2474 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
2475 GstClockTime current_time, guint64 ntpnstime)
2477 GstRTCPPacket packet;
2478 gboolean more, is_bye = FALSE, do_sync = FALSE;
2479 RTPPacketInfo pinfo = { 0, };
2480 GstFlowReturn result = GST_FLOW_OK;
2481 GstRTCPBuffer rtcp = { NULL, };
2483 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2484 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2486 if (!gst_rtcp_buffer_validate (buffer))
2487 goto invalid_packet;
2489 GST_DEBUG ("received RTCP packet");
2491 RTP_SESSION_LOCK (sess);
2492 /* update pinfo stats */
2493 update_packet_info (sess, &pinfo, FALSE, FALSE, FALSE, buffer, current_time,
2496 /* start processing the compound packet */
2497 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
2498 more = gst_rtcp_buffer_get_first_packet (&rtcp, &packet);
2502 type = gst_rtcp_packet_get_type (&packet);
2505 case GST_RTCP_TYPE_SR:
2506 rtp_session_process_sr (sess, &packet, &pinfo, &do_sync);
2508 case GST_RTCP_TYPE_RR:
2509 rtp_session_process_rr (sess, &packet, &pinfo);
2511 case GST_RTCP_TYPE_SDES:
2512 rtp_session_process_sdes (sess, &packet, &pinfo);
2514 case GST_RTCP_TYPE_BYE:
2516 /* don't try to attempt lip-sync anymore for streams with a BYE */
2518 rtp_session_process_bye (sess, &packet, &pinfo);
2520 case GST_RTCP_TYPE_APP:
2521 rtp_session_process_app (sess, &packet, &pinfo);
2523 case GST_RTCP_TYPE_RTPFB:
2524 case GST_RTCP_TYPE_PSFB:
2525 rtp_session_process_feedback (sess, &packet, &pinfo, current_time);
2528 GST_WARNING ("got unknown RTCP packet");
2531 more = gst_rtcp_packet_move_to_next (&packet);
2534 gst_rtcp_buffer_unmap (&rtcp);
2536 /* if we are scheduling a BYE, we only want to count bye packets, else we
2537 * count everything */
2538 if (sess->scheduled_bye) {
2540 sess->stats.bye_members++;
2541 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, pinfo.bytes);
2544 /* keep track of average packet size */
2545 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, pinfo.bytes);
2547 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
2548 sess->stats.avg_rtcp_packet_size, pinfo.bytes);
2549 RTP_SESSION_UNLOCK (sess);
2552 clean_packet_info (&pinfo);
2554 /* notify caller of sr packets in the callback */
2555 if (do_sync && sess->callbacks.sync_rtcp) {
2556 result = sess->callbacks.sync_rtcp (sess, buffer,
2557 sess->sync_rtcp_user_data);
2559 gst_buffer_unref (buffer);
2566 GST_DEBUG ("invalid RTCP packet received");
2567 gst_buffer_unref (buffer);
2573 * rtp_session_update_send_caps:
2574 * @sess: an #RTPSession
2577 * Update the caps of the sender in the rtp session.
2580 rtp_session_update_send_caps (RTPSession * sess, GstCaps * caps)
2585 g_return_if_fail (RTP_IS_SESSION (sess));
2586 g_return_if_fail (GST_IS_CAPS (caps));
2588 GST_LOG ("received caps %" GST_PTR_FORMAT, caps);
2590 s = gst_caps_get_structure (caps, 0);
2592 if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
2596 RTP_SESSION_LOCK (sess);
2597 source = obtain_internal_source (sess, ssrc, &created);
2599 rtp_source_update_caps (source, caps);
2600 g_object_unref (source);
2602 RTP_SESSION_UNLOCK (sess);
2607 * rtp_session_send_rtp:
2608 * @sess: an #RTPSession
2609 * @data: pointer to either an RTP buffer or a list of RTP buffers
2610 * @is_list: TRUE when @data is a buffer list
2611 * @current_time: the current system time
2612 * @running_time: the running time of @data
2614 * Send the RTP buffer in the session manager. This function takes ownership of
2617 * Returns: a #GstFlowReturn.
2620 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
2621 GstClockTime current_time, GstClockTime running_time)
2623 GstFlowReturn result;
2625 gboolean prevsender;
2627 RTPPacketInfo pinfo = { 0, };
2630 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2631 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
2633 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
2635 RTP_SESSION_LOCK (sess);
2636 if (!update_packet_info (sess, &pinfo, TRUE, TRUE, is_list, data,
2637 current_time, running_time, -1))
2638 goto invalid_packet;
2640 source = obtain_internal_source (sess, pinfo.ssrc, &created);
2642 /* update last activity */
2643 source->last_rtp_activity = current_time;
2645 prevsender = RTP_SOURCE_IS_SENDER (source);
2646 oldrate = source->bitrate;
2648 /* we use our own source to send */
2649 result = rtp_source_send_rtp (source, &pinfo);
2651 source_update_sender (sess, source, prevsender);
2653 if (oldrate != source->bitrate)
2654 sess->recalc_bandwidth = TRUE;
2655 RTP_SESSION_UNLOCK (sess);
2657 g_object_unref (source);
2658 clean_packet_info (&pinfo);
2664 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
2665 RTP_SESSION_UNLOCK (sess);
2666 GST_DEBUG ("invalid RTP packet received");
2672 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
2674 *bandwidth += source->bitrate;
2677 /* must be called with session lock */
2679 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2682 GstClockTime result;
2684 /* recalculate bandwidth when it changed */
2685 if (sess->recalc_bandwidth) {
2688 if (sess->bandwidth > 0)
2689 bandwidth = sess->bandwidth;
2691 /* If it is <= 0, then try to estimate the actual bandwidth */
2694 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2695 (GHFunc) add_bitrates, &bandwidth);
2698 if (bandwidth < 8000)
2699 bandwidth = RTP_STATS_BANDWIDTH;
2701 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
2702 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
2704 sess->recalc_bandwidth = FALSE;
2707 if (sess->scheduled_bye) {
2708 result = rtp_stats_calculate_bye_interval (&sess->stats);
2710 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2711 sess->stats.internal_sender_sources > 0, first);
2714 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2715 GST_TIME_ARGS (result), first);
2717 if (!deterministic && result != GST_CLOCK_TIME_NONE)
2718 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2720 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2726 source_mark_bye (const gchar * key, RTPSource * source, const gchar * reason)
2728 if (source->internal)
2729 rtp_source_mark_bye (source, reason);
2733 * rtp_session_mark_all_bye:
2734 * @sess: an #RTPSession
2737 * Mark all internal sources of the session as BYE with @reason.
2740 rtp_session_mark_all_bye (RTPSession * sess, const gchar * reason)
2742 g_return_if_fail (RTP_IS_SESSION (sess));
2744 RTP_SESSION_LOCK (sess);
2745 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2746 (GHFunc) source_mark_bye, (gpointer) reason);
2747 RTP_SESSION_UNLOCK (sess);
2750 /* Stop the current @sess and schedule a BYE message for the other members.
2751 * One must have the session lock to call this function
2753 static GstFlowReturn
2754 rtp_session_schedule_bye_locked (RTPSession * sess, GstClockTime current_time)
2756 GstFlowReturn result = GST_FLOW_OK;
2757 GstClockTime interval;
2759 /* nothing to do it we already scheduled bye */
2760 if (sess->scheduled_bye)
2763 /* we schedule BYE now */
2764 sess->scheduled_bye = TRUE;
2765 /* at least one member wants to send a BYE */
2766 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
2767 sess->stats.bye_members = 1;
2768 sess->first_rtcp = TRUE;
2769 sess->allow_early = TRUE;
2771 /* reschedule transmission */
2772 sess->last_rtcp_send_time = current_time;
2773 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2775 if (interval != GST_CLOCK_TIME_NONE)
2776 sess->next_rtcp_check_time = current_time + interval;
2778 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
2780 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2781 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2783 RTP_SESSION_UNLOCK (sess);
2784 /* notify app of reconsideration */
2785 if (sess->callbacks.reconsider)
2786 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2787 RTP_SESSION_LOCK (sess);
2794 * rtp_session_schedule_bye:
2795 * @sess: an #RTPSession
2796 * @current_time: the current system time
2798 * Schedule a BYE message for all sources marked as BYE in @sess.
2800 * Returns: a #GstFlowReturn.
2803 rtp_session_schedule_bye (RTPSession * sess, GstClockTime current_time)
2805 GstFlowReturn result = GST_FLOW_OK;
2807 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2809 RTP_SESSION_LOCK (sess);
2810 result = rtp_session_schedule_bye_locked (sess, current_time);
2811 RTP_SESSION_UNLOCK (sess);
2817 * rtp_session_next_timeout:
2818 * @sess: an #RTPSession
2819 * @current_time: the current system time
2821 * Get the next time we should perform session maintenance tasks.
2823 * Returns: a time when rtp_session_on_timeout() should be called with the
2824 * current system time.
2827 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2829 GstClockTime result, interval = 0;
2831 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
2833 RTP_SESSION_LOCK (sess);
2835 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
2836 GST_DEBUG ("have early rtcp time");
2837 result = sess->next_early_rtcp_time;
2841 result = sess->next_rtcp_check_time;
2843 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2844 ", next time: %" GST_TIME_FORMAT,
2845 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2847 if (result == GST_CLOCK_TIME_NONE || result < current_time) {
2848 GST_DEBUG ("take current time as base");
2849 /* our previous check time expired, start counting from the current time
2851 result = current_time;
2854 if (sess->scheduled_bye) {
2855 if (sess->stats.active_sources >= 50) {
2856 GST_DEBUG ("reconsider BYE, more than 50 sources");
2857 /* reconsider BYE if members >= 50 */
2858 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2861 if (sess->first_rtcp) {
2862 GST_DEBUG ("first RTCP packet");
2863 /* we are called for the first time */
2864 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2865 } else if (sess->next_rtcp_check_time < current_time) {
2866 GST_DEBUG ("old check time expired, getting new timeout");
2867 /* get a new timeout when we need to */
2868 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
2872 if (interval != GST_CLOCK_TIME_NONE)
2875 result = GST_CLOCK_TIME_NONE;
2877 sess->next_rtcp_check_time = result;
2881 GST_DEBUG ("current time: %" GST_TIME_FORMAT
2882 ", next time: %" GST_TIME_FORMAT,
2883 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2884 RTP_SESSION_UNLOCK (sess);
2898 GstRTCPBuffer rtcpbuf;
2901 guint num_to_report;
2906 GstClockTime current_time;
2908 GstClockTime running_time;
2909 GstClockTime interval;
2910 GstRTCPPacket packet;
2913 gboolean may_suppress;
2915 guint nacked_seqnums;
2919 session_start_rtcp (RTPSession * sess, ReportData * data)
2921 GstRTCPPacket *packet = &data->packet;
2922 RTPSource *own = data->source;
2923 GstRTCPBuffer *rtcp = &data->rtcpbuf;
2925 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2926 data->has_sdes = FALSE;
2928 gst_rtcp_buffer_map (data->rtcp, GST_MAP_READWRITE, rtcp);
2930 if (RTP_SOURCE_IS_SENDER (own)) {
2933 guint32 packet_count, octet_count;
2935 /* we are a sender, create SR */
2936 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2937 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SR, packet);
2939 /* get latest stats */
2940 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
2941 &ntptime, &rtptime, &packet_count, &octet_count);
2943 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2944 packet_count, octet_count);
2946 /* fill in sender report info */
2947 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2948 ntptime, rtptime, packet_count, octet_count);
2950 /* we are only receiver, create RR */
2951 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2952 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RR, packet);
2953 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2957 /* construct a Sender or Receiver Report */
2959 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2961 RTPSession *sess = data->sess;
2962 GstRTCPPacket *packet = &data->packet;
2963 guint8 fractionlost;
2965 guint32 exthighestseq, jitter;
2968 /* don't report for sources in future generations */
2969 if (((gint16) (source->generation - sess->generation)) > 0) {
2970 GST_DEBUG ("source %08x generation %u > %u", source->ssrc,
2971 source->generation, sess->generation);
2975 /* only report about other sender */
2976 if (source == data->source)
2979 if (gst_rtcp_packet_get_rb_count (packet) == GST_RTCP_MAX_RB_COUNT) {
2980 GST_DEBUG ("max RB count reached");
2984 if (!RTP_SOURCE_IS_SENDER (source)) {
2985 GST_DEBUG ("source %08x not sender", source->ssrc);
2989 GST_DEBUG ("create RB for SSRC %08x", source->ssrc);
2992 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2993 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2995 /* store last generated RR packet */
2996 source->last_rr.is_valid = TRUE;
2997 source->last_rr.fractionlost = fractionlost;
2998 source->last_rr.packetslost = packetslost;
2999 source->last_rr.exthighestseq = exthighestseq;
3000 source->last_rr.jitter = jitter;
3001 source->last_rr.lsr = lsr;
3002 source->last_rr.dlsr = dlsr;
3004 /* packet is not yet filled, add report block for this source. */
3005 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
3006 exthighestseq, jitter, lsr, dlsr);
3009 /* source is reported, move to next generation */
3010 source->generation = sess->generation + 1;
3012 /* if we reported all sources in this generation, move to next */
3013 if (--data->num_to_report == 0) {
3015 GST_DEBUG ("all reported, generation now %u", sess->generation);
3021 session_add_fir (const gchar * key, RTPSource * source, ReportData * data)
3023 GstRTCPPacket *packet = &data->packet;
3027 if (!source->send_fir)
3030 len = gst_rtcp_packet_fb_get_fci_length (packet);
3031 if (!gst_rtcp_packet_fb_set_fci_length (packet, len + 2))
3032 /* exit because the packet is full, will put next request in a
3036 fci_data = gst_rtcp_packet_fb_get_fci (packet) + (len * 4);
3038 GST_WRITE_UINT32_BE (fci_data, source->ssrc);
3040 fci_data[0] = source->current_send_fir_seqnum;
3041 fci_data[1] = fci_data[2] = fci_data[3] = 0;
3043 source->send_fir = FALSE;
3047 session_fir (RTPSession * sess, ReportData * data)
3049 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3050 GstRTCPPacket *packet = &data->packet;
3052 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
3055 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_FIR);
3056 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3057 gst_rtcp_packet_fb_set_media_ssrc (packet, 0);
3059 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3060 (GHFunc) session_add_fir, data);
3062 if (gst_rtcp_packet_fb_get_fci_length (packet) == 0)
3063 gst_rtcp_packet_remove (packet);
3065 data->may_suppress = FALSE;
3069 has_pli_compare_func (gconstpointer a, gconstpointer ignored)
3071 GstRTCPPacket packet;
3072 GstRTCPBuffer rtcp = { NULL, };
3073 gboolean ret = FALSE;
3075 gst_rtcp_buffer_map ((GstBuffer *) a, GST_MAP_READ, &rtcp);
3077 if (gst_rtcp_buffer_get_first_packet (&rtcp, &packet)) {
3078 if (gst_rtcp_packet_get_type (&packet) == GST_RTCP_TYPE_PSFB &&
3079 gst_rtcp_packet_fb_get_type (&packet) == GST_RTCP_PSFB_TYPE_PLI)
3083 gst_rtcp_buffer_unmap (&rtcp);
3090 session_pli (const gchar * key, RTPSource * source, ReportData * data)
3092 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3093 GstRTCPPacket *packet = &data->packet;
3095 if (!source->send_pli)
3098 if (rtp_source_has_retained (source, has_pli_compare_func, NULL))
3101 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
3102 /* exit because the packet is full, will put next request in a
3106 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_PLI);
3107 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3108 gst_rtcp_packet_fb_set_media_ssrc (packet, source->ssrc);
3110 source->send_pli = FALSE;
3111 data->may_suppress = FALSE;
3114 /* construct NACK */
3116 session_nack (const gchar * key, RTPSource * source, ReportData * data)
3118 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3119 GstRTCPPacket *packet = &data->packet;
3124 if (!source->send_nack)
3127 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RTPFB, packet))
3128 /* exit because the packet is full, will put next request in a
3132 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_RTPFB_TYPE_NACK);
3133 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3134 gst_rtcp_packet_fb_set_media_ssrc (packet, source->ssrc);
3136 nacks = rtp_source_get_nacks (source, &n_nacks);
3137 GST_DEBUG ("%u NACKs", n_nacks);
3138 if (!gst_rtcp_packet_fb_set_fci_length (packet, n_nacks))
3141 fci_data = gst_rtcp_packet_fb_get_fci (packet);
3142 for (i = 0; i < n_nacks; i++) {
3143 GST_WRITE_UINT32_BE (fci_data, nacks[i]);
3145 data->nacked_seqnums++;
3148 rtp_source_clear_nacks (source);
3149 data->may_suppress = FALSE;
3152 /* perform cleanup of sources that timed out */
3154 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
3156 gboolean remove = FALSE;
3157 gboolean byetimeout = FALSE;
3158 gboolean sendertimeout = FALSE;
3159 gboolean is_sender, is_active;
3160 RTPSession *sess = data->sess;
3161 GstClockTime interval, binterval;
3164 GST_DEBUG ("look at %08x, generation %u", source->ssrc, source->generation);
3166 /* check for outdated collisions */
3167 if (source->internal) {
3168 GST_DEBUG ("Timing out collisions for %x", source->ssrc);
3169 rtp_source_timeout (source, data->current_time,
3170 /* "a relatively long time" -- RFC 3550 section 8.2 */
3171 RTP_STATS_MIN_INTERVAL * GST_SECOND * 10,
3172 data->running_time - sess->rtcp_feedback_retention_window);
3175 /* nothing else to do when without RTCP */
3176 if (data->interval == GST_CLOCK_TIME_NONE)
3179 is_sender = RTP_SOURCE_IS_SENDER (source);
3180 is_active = RTP_SOURCE_IS_ACTIVE (source);
3182 /* our own rtcp interval may have been forced low by secondary configuration,
3183 * while sender side may still operate with higher interval,
3184 * so do not just take our interval to decide on timing out sender,
3185 * but take (if data->interval <= 5 * GST_SECOND):
3186 * interval = CLAMP (sender_interval, data->interval, 5 * GST_SECOND)
3187 * where sender_interval is difference between last 2 received RTCP reports
3189 if (data->interval >= 5 * GST_SECOND || source->internal) {
3190 binterval = data->interval;
3192 GST_LOG ("prev_rtcp %" GST_TIME_FORMAT ", last_rtcp %" GST_TIME_FORMAT,
3193 GST_TIME_ARGS (source->stats.prev_rtcptime),
3194 GST_TIME_ARGS (source->stats.last_rtcptime));
3195 /* if not received enough yet, fallback to larger default */
3196 if (source->stats.last_rtcptime > source->stats.prev_rtcptime)
3197 binterval = source->stats.last_rtcptime - source->stats.prev_rtcptime;
3199 binterval = 5 * GST_SECOND;
3200 binterval = CLAMP (binterval, data->interval, 5 * GST_SECOND);
3202 GST_LOG ("timeout base interval %" GST_TIME_FORMAT,
3203 GST_TIME_ARGS (binterval));
3205 if (!source->internal) {
3206 if (source->marked_bye) {
3207 /* if we received a BYE from the source, remove the source after some
3209 if (data->current_time > source->bye_time &&
3210 data->current_time - source->bye_time > sess->stats.bye_timeout) {
3211 GST_DEBUG ("removing BYE source %08x", source->ssrc);
3216 /* sources that were inactive for more than 5 times the deterministic reporting
3217 * interval get timed out. the min timeout is 5 seconds. */
3218 /* mind old time that might pre-date last time going to PLAYING */
3219 btime = MAX (source->last_activity, sess->start_time);
3220 if (data->current_time > btime) {
3221 interval = MAX (binterval * 5, 5 * GST_SECOND);
3222 if (data->current_time - btime > interval) {
3223 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
3224 source->ssrc, GST_TIME_ARGS (btime));
3230 /* senders that did not send for a long time become a receiver, this also
3231 * holds for our own sources. */
3233 /* mind old time that might pre-date last time going to PLAYING */
3234 btime = MAX (source->last_rtp_activity, sess->start_time);
3235 if (data->current_time > btime) {
3236 interval = MAX (binterval * 2, 5 * GST_SECOND);
3237 if (data->current_time - btime > interval) {
3238 if (source->internal && source->sent_bye) {
3239 /* an internal source is BYE and stopped sending RTP, remove */
3240 GST_DEBUG ("internal BYE source %08x timed out, last %"
3241 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
3244 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
3245 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
3246 sendertimeout = TRUE;
3253 sess->total_sources--;
3255 sess->stats.sender_sources--;
3256 if (source->internal)
3257 sess->stats.internal_sender_sources--;
3260 sess->stats.active_sources--;
3262 if (source->internal)
3263 sess->stats.internal_sources--;
3266 on_bye_timeout (sess, source);
3268 on_timeout (sess, source);
3270 if (sendertimeout) {
3271 source->is_sender = FALSE;
3272 sess->stats.sender_sources--;
3273 if (source->internal)
3274 sess->stats.internal_sender_sources--;
3276 on_sender_timeout (sess, source);
3278 /* count how many source to report in this generation */
3279 if (((gint16) (source->generation - sess->generation)) <= 0)
3280 data->num_to_report++;
3282 source->closing = remove;
3286 session_sdes (RTPSession * sess, ReportData * data)
3288 GstRTCPPacket *packet = &data->packet;
3289 const GstStructure *sdes;
3291 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3293 /* add SDES packet */
3294 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SDES, packet);
3296 gst_rtcp_packet_sdes_add_item (packet, data->source->ssrc);
3298 sdes = rtp_source_get_sdes_struct (data->source);
3300 /* add all fields in the structure, the order is not important. */
3301 n_fields = gst_structure_n_fields (sdes);
3302 for (i = 0; i < n_fields; ++i) {
3305 GstRTCPSDESType type;
3307 field = gst_structure_nth_field_name (sdes, i);
3310 value = gst_structure_get_string (sdes, field);
3313 type = gst_rtcp_sdes_name_to_type (field);
3315 /* Early packets are minimal and only include the CNAME */
3316 if (data->is_early && type != GST_RTCP_SDES_CNAME)
3319 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
3320 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
3321 (const guint8 *) value);
3322 } else if (type == GST_RTCP_SDES_PRIV) {
3328 /* don't accept entries that are too big */
3329 prefix_len = strlen (field);
3330 if (prefix_len > 255)
3332 value_len = strlen (value);
3333 if (value_len > 255)
3335 data_len = 1 + prefix_len + value_len;
3339 data[0] = prefix_len;
3340 memcpy (&data[1], field, prefix_len);
3341 memcpy (&data[1 + prefix_len], value, value_len);
3343 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
3347 data->has_sdes = TRUE;
3350 /* schedule a BYE packet */
3352 make_source_bye (RTPSession * sess, RTPSource * source, ReportData * data)
3354 GstRTCPPacket *packet = &data->packet;
3355 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3358 session_sdes (sess, data);
3359 /* add a BYE packet */
3360 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_BYE, packet);
3361 gst_rtcp_packet_bye_add_ssrc (packet, source->ssrc);
3362 if (source->bye_reason)
3363 gst_rtcp_packet_bye_set_reason (packet, source->bye_reason);
3365 /* we have a BYE packet now */
3366 source->sent_bye = TRUE;
3370 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
3372 GstClockTime new_send_time, elapsed;
3373 GstClockTime interval;
3375 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3376 data->is_early = TRUE;
3378 data->is_early = FALSE;
3380 if (data->is_early && sess->next_early_rtcp_time < current_time) {
3381 GST_DEBUG ("early feedback %" GST_TIME_FORMAT " < now %"
3382 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_early_rtcp_time),
3383 GST_TIME_ARGS (current_time));
3387 /* no need to check yet */
3388 if (sess->next_rtcp_check_time == GST_CLOCK_TIME_NONE ||
3389 sess->next_rtcp_check_time > current_time) {
3390 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
3391 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
3392 GST_TIME_ARGS (current_time));
3397 /* get elapsed time since we last reported */
3398 elapsed = current_time - sess->last_rtcp_send_time;
3400 /* take interval and add jitter */
3401 interval = data->interval;
3402 if (interval != GST_CLOCK_TIME_NONE)
3403 interval = rtp_stats_add_rtcp_jitter (&sess->stats, interval);
3405 /* perform forward reconsideration */
3406 if (interval != GST_CLOCK_TIME_NONE) {
3407 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
3408 GST_TIME_FORMAT, GST_TIME_ARGS (interval), GST_TIME_ARGS (elapsed));
3409 new_send_time = interval + sess->last_rtcp_send_time;
3411 new_send_time = sess->last_rtcp_send_time;
3414 if (!data->is_early) {
3415 /* check if reconsideration */
3416 if (new_send_time == GST_CLOCK_TIME_NONE || current_time < new_send_time) {
3417 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
3418 GST_TIME_ARGS (new_send_time));
3419 /* store new check time */
3420 sess->next_rtcp_check_time = new_send_time;
3423 sess->next_rtcp_check_time = current_time + interval;
3424 } else if (interval != GST_CLOCK_TIME_NONE) {
3425 /* Apply the rules from RFC 4585 section 3.5.3 */
3426 if (sess->stats.min_interval != 0 && !sess->first_rtcp) {
3427 GstClockTime T_rr_current_interval =
3428 g_random_double_range (0.5, 1.5) * sess->stats.min_interval;
3430 /* This will caused the RTCP to be suppressed if no FB packets are added */
3431 if (sess->last_rtcp_send_time + T_rr_current_interval > new_send_time) {
3432 GST_DEBUG ("RTCP packet could be suppressed min: %" GST_TIME_FORMAT
3433 " last: %" GST_TIME_FORMAT
3434 " + T_rr_current_interval: %" GST_TIME_FORMAT
3435 " > new_send_time: %" GST_TIME_FORMAT,
3436 GST_TIME_ARGS (sess->stats.min_interval),
3437 GST_TIME_ARGS (sess->last_rtcp_send_time),
3438 GST_TIME_ARGS (T_rr_current_interval),
3439 GST_TIME_ARGS (new_send_time));
3440 data->may_suppress = TRUE;
3445 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
3446 GST_TIME_ARGS (new_send_time));
3452 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
3454 g_hash_table_insert (hash_table, key, g_object_ref (source));
3458 remove_closing_sources (const gchar * key, RTPSource * source,
3461 if (source->closing)
3464 if (source->send_fir)
3465 data->have_fir = TRUE;
3466 if (source->send_pli)
3467 data->have_pli = TRUE;
3468 if (source->send_nack)
3469 data->have_nack = TRUE;
3475 generate_rtcp (const gchar * key, RTPSource * source, ReportData * data)
3477 RTPSession *sess = data->sess;
3478 gboolean is_bye = FALSE;
3479 ReportOutput *output;
3481 /* only generate RTCP for active internal sources */
3482 if (!source->internal || source->sent_bye)
3485 /* ignore other sources when we do the timeout after a scheduled BYE */
3486 if (sess->scheduled_bye && !source->marked_bye)
3489 data->source = source;
3492 session_start_rtcp (sess, data);
3494 if (source->marked_bye) {
3496 make_source_bye (sess, source, data);
3498 } else if (!data->is_early) {
3499 /* loop over all known sources and add report blocks. If we are early, we
3500 * just make a minimal RTCP packet and skip this step */
3501 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3502 (GHFunc) session_report_blocks, data);
3504 if (!data->has_sdes)
3505 session_sdes (sess, data);
3508 session_fir (sess, data);
3511 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3512 (GHFunc) session_pli, data);
3514 if (data->have_nack)
3515 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3516 (GHFunc) session_nack, data);
3518 gst_rtcp_buffer_unmap (&data->rtcpbuf);
3520 output = g_slice_new (ReportOutput);
3521 output->source = g_object_ref (source);
3522 output->is_bye = is_bye;
3523 output->buffer = data->rtcp;
3524 /* queue the RTCP packet to push later */
3525 g_queue_push_tail (&data->output, output);
3529 * rtp_session_on_timeout:
3530 * @sess: an #RTPSession
3531 * @current_time: the current system time
3532 * @ntpnstime: the current NTP time in nanoseconds
3533 * @running_time: the current running_time of the pipeline
3535 * Perform maintenance actions after the timeout obtained with
3536 * rtp_session_next_timeout() expired.
3538 * This function will perform timeouts of receivers and senders, send a BYE
3539 * packet or generate RTCP packets with current session stats.
3541 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
3542 * times, for each packet that should be processed.
3544 * Returns: a #GstFlowReturn.
3547 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
3548 guint64 ntpnstime, GstClockTime running_time)
3550 GstFlowReturn result = GST_FLOW_OK;
3551 ReportData data = { GST_RTCP_BUFFER_INIT };
3552 GHashTable *table_copy;
3553 ReportOutput *output;
3555 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3557 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT
3558 ", running-time %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
3559 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (running_time));
3562 data.current_time = current_time;
3563 data.ntpnstime = ntpnstime;
3564 data.running_time = running_time;
3565 data.num_to_report = 0;
3566 data.may_suppress = FALSE;
3567 data.nacked_seqnums = 0;
3568 g_queue_init (&data.output);
3570 RTP_SESSION_LOCK (sess);
3571 /* get a new interval, we need this for various cleanups etc */
3572 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
3574 GST_DEBUG ("interval %" GST_TIME_FORMAT, GST_TIME_ARGS (data.interval));
3576 /* we need an internal source now */
3577 if (sess->stats.internal_sources == 0) {
3581 source = obtain_internal_source (sess, sess->suggested_ssrc, &created);
3582 g_object_unref (source);
3585 /* Make a local copy of the hashtable. We need to do this because the
3586 * cleanup stage below releases the session lock. */
3587 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
3588 (GDestroyNotify) g_object_unref);
3589 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3590 (GHFunc) clone_ssrcs_hashtable, table_copy);
3592 /* Clean up the session, mark the source for removing, this might release the
3594 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
3595 g_hash_table_destroy (table_copy);
3597 /* Now remove the marked sources */
3598 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
3599 (GHRFunc) remove_closing_sources, &data);
3601 /* update point-to-point status */
3602 session_update_ptp (sess);
3604 /* see if we need to generate SR or RR packets */
3605 if (!is_rtcp_time (sess, current_time, &data))
3608 GST_DEBUG ("doing RTCP generation %u for %u sources, early %d",
3609 sess->generation, data.num_to_report, data.is_early);
3611 /* generate RTCP for all internal sources */
3612 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3613 (GHFunc) generate_rtcp, &data);
3615 /* we keep track of the last report time in order to timeout inactive
3616 * receivers or senders */
3617 if (!data.is_early && !data.may_suppress)
3618 sess->last_rtcp_send_time = data.current_time;
3619 sess->first_rtcp = FALSE;
3620 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
3621 sess->scheduled_bye = FALSE;
3624 RTP_SESSION_UNLOCK (sess);
3626 /* push out the RTCP packets */
3627 while ((output = g_queue_pop_head (&data.output))) {
3628 gboolean do_not_suppress;
3629 GstBuffer *buffer = output->buffer;
3630 RTPSource *source = output->source;
3632 /* Give the user a change to add its own packet */
3633 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
3634 buffer, data.is_early, &do_not_suppress);
3636 if (sess->callbacks.send_rtcp && (do_not_suppress || !data.may_suppress)) {
3639 packet_size = gst_buffer_get_size (buffer) + sess->header_len;
3641 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
3642 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
3643 sess->stats.avg_rtcp_packet_size, packet_size);
3645 sess->callbacks.send_rtcp (sess, source, buffer, output->is_bye,
3646 sess->send_rtcp_user_data);
3647 sess->stats.nacks_sent += data.nacked_seqnums;
3649 GST_DEBUG ("freeing packet callback: %p"
3650 " do_not_suppress: %d may_suppress: %d",
3651 sess->callbacks.send_rtcp, do_not_suppress, data.may_suppress);
3652 sess->stats.nacks_dropped += data.nacked_seqnums;
3653 gst_buffer_unref (buffer);
3655 g_object_unref (source);
3656 g_slice_free (ReportOutput, output);
3662 * rtp_session_request_early_rtcp:
3663 * @sess: an #RTPSession
3664 * @current_time: the current system time
3665 * @max_delay: maximum delay
3667 * Request transmission of early RTCP
3670 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
3671 GstClockTime max_delay)
3673 GstClockTime T_dither_max;
3675 /* Implements the algorithm described in RFC 4585 section 3.5.2 */
3677 RTP_SESSION_LOCK (sess);
3679 /* Check if already requested */
3680 /* RFC 4585 section 3.5.2 step 2 */
3681 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
3682 GST_LOG_OBJECT (sess, "already have next early rtcp time");
3686 if (!GST_CLOCK_TIME_IS_VALID (sess->next_rtcp_check_time)) {
3687 GST_LOG_OBJECT (sess, "no next RTCP check time");
3691 /* Ignore the request a scheduled packet will be in time anyway */
3692 if (current_time + max_delay > sess->next_rtcp_check_time) {
3693 GST_LOG_OBJECT (sess, "next scheduled time is soon %" GST_TIME_FORMAT " + %"
3694 GST_TIME_FORMAT " > %" GST_TIME_FORMAT,
3695 GST_TIME_ARGS (current_time),
3696 GST_TIME_ARGS (max_delay), GST_TIME_ARGS (sess->next_rtcp_check_time));
3700 /* RFC 4585 section 3.5.2 step 2b */
3701 /* If the total sources is <=2, then there is only us and one peer */
3702 /* When there is one auxiliary stream the session can still do point
3705 if (sess->is_doing_ptp) {
3708 /* Divide by 2 because l = 0.5 */
3709 T_dither_max = sess->next_rtcp_check_time - sess->last_rtcp_send_time;
3713 /* RFC 4585 section 3.5.2 step 3 */
3714 if (current_time + T_dither_max > sess->next_rtcp_check_time) {
3715 GST_LOG_OBJECT (sess, "don't send because of dither");
3719 /* RFC 4585 section 3.5.2 step 4
3720 * Don't send if allow_early is FALSE, but not if we are in
3721 * immediate mode, meaning we are part of a group of at most the
3722 * application-specific threshold.
3724 if (sess->total_sources > sess->rtcp_immediate_feedback_threshold &&
3725 sess->allow_early == FALSE) {
3726 GST_LOG_OBJECT (sess, "can't allow early feedback");
3731 /* Schedule an early transmission later */
3732 sess->next_early_rtcp_time = g_random_double () * T_dither_max +
3735 /* If no dithering, schedule it for NOW */
3736 sess->next_early_rtcp_time = current_time;
3739 GST_LOG_OBJECT (sess, "next early RTCP time %" GST_TIME_FORMAT,
3740 GST_TIME_ARGS (sess->next_early_rtcp_time));
3741 RTP_SESSION_UNLOCK (sess);
3743 /* notify app of need to send packet early
3744 * and therefore of timeout change */
3745 if (sess->callbacks.reconsider)
3746 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
3752 RTP_SESSION_UNLOCK (sess);
3756 rtp_session_send_rtcp (RTPSession * sess, GstClockTime max_delay)
3760 if (!sess->callbacks.send_rtcp)
3763 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
3765 rtp_session_request_early_rtcp (sess, now, max_delay);
3769 rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc,
3770 gboolean fir, gint count)
3774 RTP_SESSION_LOCK (sess);
3775 src = find_source (sess, ssrc);
3780 src->send_pli = FALSE;
3781 src->send_fir = TRUE;
3783 if (count == -1 || count != src->last_fir_count)
3784 src->current_send_fir_seqnum++;
3785 src->last_fir_count = count;
3786 } else if (!src->send_fir) {
3787 src->send_pli = TRUE;
3789 RTP_SESSION_UNLOCK (sess);
3791 rtp_session_send_rtcp (sess, 200 * GST_MSECOND);
3798 RTP_SESSION_UNLOCK (sess);
3804 * rtp_session_request_nack:
3805 * @sess: a #RTPSession
3807 * @seqnum: the missing seqnum
3808 * @max_delay: max delay to request NACK
3810 * Request scheduling of a NACK feedback packet for @seqnum in @ssrc.
3812 * Returns: %TRUE if the NACK feedback could be scheduled
3815 rtp_session_request_nack (RTPSession * sess, guint32 ssrc, guint16 seqnum,
3816 GstClockTime max_delay)
3820 RTP_SESSION_LOCK (sess);
3821 source = find_source (sess, ssrc);
3825 GST_DEBUG ("request NACK for %08x, #%u", ssrc, seqnum);
3826 rtp_source_register_nack (source, seqnum);
3827 RTP_SESSION_UNLOCK (sess);
3829 rtp_session_send_rtcp (sess, max_delay);
3836 RTP_SESSION_UNLOCK (sess);