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,
51 SIGNAL_ON_FEEDBACK_RTCP,
53 SIGNAL_SEND_RTCP_FULL,
54 SIGNAL_ON_RECEIVING_RTCP,
55 SIGNAL_ON_NEW_SENDER_SSRC,
56 SIGNAL_ON_SENDER_SSRC_ACTIVE,
60 #define DEFAULT_INTERNAL_SOURCE NULL
61 #define DEFAULT_BANDWIDTH 0.0
62 #define DEFAULT_RTCP_FRACTION RTP_STATS_RTCP_FRACTION
63 #define DEFAULT_RTCP_RR_BANDWIDTH -1
64 #define DEFAULT_RTCP_RS_BANDWIDTH -1
65 #define DEFAULT_RTCP_MTU 1400
66 #define DEFAULT_SDES NULL
67 #define DEFAULT_NUM_SOURCES 0
68 #define DEFAULT_NUM_ACTIVE_SOURCES 0
69 #define DEFAULT_SOURCES NULL
70 #define DEFAULT_RTCP_MIN_INTERVAL (RTP_STATS_MIN_INTERVAL * GST_SECOND)
71 #define DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW (2 * GST_SECOND)
72 #define DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD (3)
73 #define DEFAULT_PROBATION RTP_DEFAULT_PROBATION
74 #define DEFAULT_MAX_DROPOUT_TIME 60000
75 #define DEFAULT_MAX_MISORDER_TIME 2000
76 #define DEFAULT_RTP_PROFILE GST_RTP_PROFILE_AVP
77 #define DEFAULT_RTCP_REDUCED_SIZE FALSE
86 PROP_RTCP_RR_BANDWIDTH,
87 PROP_RTCP_RS_BANDWIDTH,
91 PROP_NUM_ACTIVE_SOURCES,
94 PROP_RTCP_MIN_INTERVAL,
95 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
96 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
98 PROP_MAX_DROPOUT_TIME,
99 PROP_MAX_MISORDER_TIME,
102 PROP_RTCP_REDUCED_SIZE
105 /* update average packet size */
106 #define INIT_AVG(avg, val) \
108 #define UPDATE_AVG(avg, val) \
112 (avg) = ((val) + (15 * (avg))) >> 4;
115 /* GObject vmethods */
116 static void rtp_session_finalize (GObject * object);
117 static void rtp_session_set_property (GObject * object, guint prop_id,
118 const GValue * value, GParamSpec * pspec);
119 static void rtp_session_get_property (GObject * object, guint prop_id,
120 GValue * value, GParamSpec * pspec);
122 static gboolean rtp_session_send_rtcp (RTPSession * sess,
123 GstClockTime max_delay);
124 static gboolean rtp_session_send_rtcp_with_deadline (RTPSession * sess,
125 GstClockTime deadline);
127 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
129 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
131 static guint32 rtp_session_create_new_ssrc (RTPSession * sess);
132 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
133 gboolean * created, RTPPacketInfo * pinfo, gboolean rtp);
134 static RTPSource *obtain_internal_source (RTPSession * sess,
135 guint32 ssrc, gboolean * created, GstClockTime current_time);
136 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
137 GstClockTime current_time);
138 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
139 gboolean deterministic, gboolean first);
142 accumulate_trues (GSignalInvocationHint * ihint, GValue * return_accu,
143 const GValue * handler_return, gpointer data)
145 if (g_value_get_boolean (handler_return))
146 g_value_set_boolean (return_accu, TRUE);
152 rtp_session_class_init (RTPSessionClass * klass)
154 GObjectClass *gobject_class;
156 gobject_class = (GObjectClass *) klass;
158 gobject_class->finalize = rtp_session_finalize;
159 gobject_class->set_property = rtp_session_set_property;
160 gobject_class->get_property = rtp_session_get_property;
163 * RTPSession::get-source-by-ssrc:
164 * @session: the object which received the signal
165 * @ssrc: the SSRC of the RTPSource
167 * Request the #RTPSource object with SSRC @ssrc in @session.
169 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
170 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
171 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
172 get_source_by_ssrc), NULL, NULL, g_cclosure_marshal_generic,
173 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
176 * RTPSession::on-new-ssrc:
177 * @session: the object which received the signal
178 * @src: the new RTPSource
180 * Notify of a new SSRC that entered @session.
182 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
183 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
184 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
185 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
188 * RTPSession::on-ssrc-collision:
189 * @session: the object which received the signal
190 * @src: the #RTPSource that caused a collision
192 * Notify when we have an SSRC collision
194 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
195 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
196 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
197 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
200 * RTPSession::on-ssrc-validated:
201 * @session: the object which received the signal
202 * @src: the new validated RTPSource
204 * Notify of a new SSRC that became validated.
206 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
207 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
208 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
209 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
212 * RTPSession::on-ssrc-active:
213 * @session: the object which received the signal
214 * @src: the active RTPSource
216 * Notify of a SSRC that is active, i.e., sending RTCP.
218 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
219 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
220 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
221 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
224 * RTPSession::on-ssrc-sdes:
225 * @session: the object which received the signal
226 * @src: the RTPSource
228 * Notify that a new SDES was received for SSRC.
230 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
231 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
232 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
233 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
236 * RTPSession::on-bye-ssrc:
237 * @session: the object which received the signal
238 * @src: the RTPSource that went away
240 * Notify of an SSRC that became inactive because of a BYE packet.
242 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
243 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
244 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
245 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
248 * RTPSession::on-bye-timeout:
249 * @session: the object which received the signal
250 * @src: the RTPSource that timed out
252 * Notify of an SSRC that has timed out because of BYE
254 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
255 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
256 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
257 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
260 * RTPSession::on-timeout:
261 * @session: the object which received the signal
262 * @src: the RTPSource that timed out
264 * Notify of an SSRC that has timed out
266 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
267 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
268 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
269 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
272 * RTPSession::on-sender-timeout:
273 * @session: the object which received the signal
274 * @src: the RTPSource that timed out
276 * Notify of an SSRC that was a sender but timed out and became a receiver.
278 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
279 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
280 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
281 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
285 * RTPSession::on-sending-rtcp
286 * @session: the object which received the signal
287 * @buffer: the #GstBuffer containing the RTCP packet about to be sent
288 * @early: %TRUE if the packet is early, %FALSE if it is regular
290 * This signal is emitted before sending an RTCP packet, it can be used
291 * to add extra RTCP Packets.
293 * Returns: %TRUE if the RTCP buffer should NOT be suppressed, %FALSE
294 * if suppressing it is acceptable
296 rtp_session_signals[SIGNAL_ON_SENDING_RTCP] =
297 g_signal_new ("on-sending-rtcp", G_TYPE_FROM_CLASS (klass),
298 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_rtcp),
299 accumulate_trues, NULL, g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2,
300 GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_BOOLEAN);
303 * RTPSession::on-app-rtcp:
304 * @session: the object which received the signal
305 * @subtype: The subtype of the packet
306 * @ssrc: The SSRC/CSRC of the packet
307 * @name: The name of the packet
308 * @data: a #GstBuffer with the application-dependant data or %NULL if
311 * Notify that a RTCP APP packet has been received
313 rtp_session_signals[SIGNAL_ON_APP_RTCP] =
314 g_signal_new ("on-app-rtcp", G_TYPE_FROM_CLASS (klass),
315 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_app_rtcp),
316 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 4,
317 G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING, GST_TYPE_BUFFER);
320 * RTPSession::on-feedback-rtcp:
321 * @session: the object which received the signal
322 * @type: Type of RTCP packet, will be %GST_RTCP_TYPE_RTPFB or
323 * %GST_RTCP_TYPE_RTPFB
324 * @fbtype: The type of RTCP FB packet, probably part of #GstRTCPFBType
325 * @sender_ssrc: The SSRC of the sender
326 * @media_ssrc: The SSRC of the media this refers to
327 * @fci: a #GstBuffer with the FCI data from the FB packet or %NULL if
330 * Notify that a RTCP feedback packet has been received
332 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP] =
333 g_signal_new ("on-feedback-rtcp", G_TYPE_FROM_CLASS (klass),
334 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_feedback_rtcp),
335 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 5, G_TYPE_UINT,
336 G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, GST_TYPE_BUFFER);
339 * RTPSession::send-rtcp:
340 * @session: the object which received the signal
341 * @max_delay: The maximum delay after which the feedback will not be useful
344 * Requests that the #RTPSession initiate a new RTCP packet as soon as
345 * possible within the requested delay.
347 * This sets feedback to %TRUE if not already done before.
349 rtp_session_signals[SIGNAL_SEND_RTCP] =
350 g_signal_new ("send-rtcp", G_TYPE_FROM_CLASS (klass),
351 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
352 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
353 g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_UINT64);
356 * RTPSession::send-rtcp-full:
357 * @session: the object which received the signal
358 * @max_delay: The maximum delay after which the feedback will not be useful
361 * Requests that the #RTPSession initiate a new RTCP packet as soon as
362 * possible within the requested delay.
364 * This sets feedback to %TRUE if not already done before.
366 * Returns: TRUE if the new RTCP packet could be scheduled within the
367 * requested delay, FALSE otherwise.
371 rtp_session_signals[SIGNAL_SEND_RTCP_FULL] =
372 g_signal_new ("send-rtcp-full", G_TYPE_FROM_CLASS (klass),
373 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
374 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
375 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 1, G_TYPE_UINT64);
378 * RTPSession::on-receiving-rtcp
379 * @session: the object which received the signal
380 * @buffer: the #GstBuffer containing the RTCP packet that was received
382 * This signal is emitted when receiving an RTCP packet before it is handled
383 * by the session. It can be used to extract custom information from RTCP packets.
387 rtp_session_signals[SIGNAL_ON_RECEIVING_RTCP] =
388 g_signal_new ("on-receiving-rtcp", G_TYPE_FROM_CLASS (klass),
389 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_receiving_rtcp),
390 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
391 GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE);
394 * RTPSession::on-new-sender-ssrc:
395 * @session: the object which received the signal
396 * @src: the new sender RTPSource
398 * Notify of a new sender SSRC that entered @session.
402 rtp_session_signals[SIGNAL_ON_NEW_SENDER_SSRC] =
403 g_signal_new ("on-new-sender-ssrc", G_TYPE_FROM_CLASS (klass),
404 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_sender_ssrc),
405 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
409 * RTPSession::on-sender-ssrc-active:
410 * @session: the object which received the signal
411 * @src: the active sender RTPSource
413 * Notify of a sender SSRC that is active, i.e., sending RTCP.
417 rtp_session_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE] =
418 g_signal_new ("on-sender-ssrc-active", G_TYPE_FROM_CLASS (klass),
419 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass,
420 on_sender_ssrc_active), NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
421 G_TYPE_NONE, 1, RTP_TYPE_SOURCE);
423 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
424 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
425 "The internal SSRC used for the session (deprecated)",
426 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
428 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
429 g_param_spec_object ("internal-source", "Internal Source",
430 "The internal source element of the session (deprecated)",
431 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
433 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
434 g_param_spec_double ("bandwidth", "Bandwidth",
435 "The bandwidth of the session in bits per second (0 for auto-discover)",
436 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
437 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
439 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
440 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
441 "The fraction of the bandwidth used for RTCP in bits per second (or as a real fraction of the RTP bandwidth if < 1)",
442 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
443 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
445 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
446 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
447 "The RTCP bandwidth used for receivers in bits per second (-1 = default)",
448 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
449 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
451 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
452 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
453 "The RTCP bandwidth used for senders in bits per second (-1 = default)",
454 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
455 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
457 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
458 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
459 "The maximum size of the RTCP packets",
460 16, G_MAXINT16, DEFAULT_RTCP_MTU,
461 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
463 g_object_class_install_property (gobject_class, PROP_SDES,
464 g_param_spec_boxed ("sdes", "SDES",
465 "The SDES items of this session",
466 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
468 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
469 g_param_spec_uint ("num-sources", "Num Sources",
470 "The number of sources in the session", 0, G_MAXUINT,
471 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
473 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
474 g_param_spec_uint ("num-active-sources", "Num Active Sources",
475 "The number of active sources in the session", 0, G_MAXUINT,
476 DEFAULT_NUM_ACTIVE_SOURCES,
477 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
481 * Get a GValue Array of all sources in the session.
484 * <title>Getting the #RTPSources of a session
491 * g_object_get (sess, "sources", &arr, NULL);
493 * for (i = 0; i < arr->n_values; i++) {
496 * val = g_value_array_get_nth (arr, i);
497 * source = g_value_get_object (val);
499 * g_value_array_free (arr);
504 g_object_class_install_property (gobject_class, PROP_SOURCES,
505 g_param_spec_boxed ("sources", "Sources",
506 "An array of all known sources in the session",
507 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
509 g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
510 g_param_spec_boolean ("favor-new", "Favor new sources",
511 "Resolve SSRC conflict in favor of new sources", FALSE,
512 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
514 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
515 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
516 "Minimum interval between Regular RTCP packet (in ns)",
517 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
518 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
520 g_object_class_install_property (gobject_class,
521 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
522 g_param_spec_uint64 ("rtcp-feedback-retention-window",
523 "RTCP Feedback retention window",
524 "Duration during which RTCP Feedback packets are retained (in ns)",
525 0, G_MAXUINT64, DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW,
526 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
528 g_object_class_install_property (gobject_class,
529 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
530 g_param_spec_uint ("rtcp-immediate-feedback-threshold",
531 "RTCP Immediate Feedback threshold",
532 "The maximum number of members of a RTP session for which immediate"
533 " feedback is used (DEPRECATED: has no effect and is not needed)",
534 0, G_MAXUINT, DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
535 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
537 g_object_class_install_property (gobject_class, PROP_PROBATION,
538 g_param_spec_uint ("probation", "Number of probations",
539 "Consecutive packet sequence numbers to accept the source",
540 0, G_MAXUINT, DEFAULT_PROBATION,
541 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
543 g_object_class_install_property (gobject_class, PROP_MAX_DROPOUT_TIME,
544 g_param_spec_uint ("max-dropout-time", "Max dropout time",
545 "The maximum time (milliseconds) of missing packets tolerated.",
546 0, G_MAXUINT, DEFAULT_MAX_DROPOUT_TIME,
547 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
549 g_object_class_install_property (gobject_class, PROP_MAX_MISORDER_TIME,
550 g_param_spec_uint ("max-misorder-time", "Max misorder time",
551 "The maximum time (milliseconds) of misordered packets tolerated.",
552 0, G_MAXUINT, DEFAULT_MAX_MISORDER_TIME,
553 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
558 * Various session statistics. This property returns a GstStructure
559 * with name application/x-rtp-session-stats with the following fields:
561 * "rtx-drop-count" G_TYPE_UINT The number of retransmission events
562 * dropped (due to bandwidth constraints)
563 * "sent-nack-count" G_TYPE_UINT Number of NACKs sent
564 * "recv-nack-count" G_TYPE_UINT Number of NACKs received
565 * "source-stats" G_TYPE_BOXED GValueArray of #RTPSource::stats for all
566 * RTP sources (Since 1.8)
570 g_object_class_install_property (gobject_class, PROP_STATS,
571 g_param_spec_boxed ("stats", "Statistics",
572 "Various statistics", GST_TYPE_STRUCTURE,
573 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
575 g_object_class_install_property (gobject_class, PROP_RTP_PROFILE,
576 g_param_spec_enum ("rtp-profile", "RTP Profile",
577 "RTP profile to use for this session", GST_TYPE_RTP_PROFILE,
578 DEFAULT_RTP_PROFILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
580 g_object_class_install_property (gobject_class, PROP_RTCP_REDUCED_SIZE,
581 g_param_spec_boolean ("rtcp-reduced-size", "RTCP Reduced Size",
582 "Use Reduced Size RTCP for feedback packets",
583 DEFAULT_RTCP_REDUCED_SIZE,
584 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
586 klass->get_source_by_ssrc =
587 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
588 klass->send_rtcp = GST_DEBUG_FUNCPTR (rtp_session_send_rtcp);
590 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
594 rtp_session_init (RTPSession * sess)
599 g_mutex_init (&sess->lock);
600 sess->key = g_random_int ();
604 /* TODO: We currently only use the first hash table but this is the
605 * beginning of an implementation for RFC2762
606 for (i = 0; i < 32; i++) {
608 for (i = 0; i < 1; i++) {
610 g_hash_table_new_full (NULL, NULL, NULL,
611 (GDestroyNotify) g_object_unref);
614 rtp_stats_init_defaults (&sess->stats);
615 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
616 rtp_stats_set_min_interval (&sess->stats,
617 (gdouble) DEFAULT_RTCP_MIN_INTERVAL / GST_SECOND);
619 sess->recalc_bandwidth = TRUE;
620 sess->bandwidth = DEFAULT_BANDWIDTH;
621 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
622 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
623 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
625 /* default UDP header length */
626 sess->header_len = 28;
627 sess->mtu = DEFAULT_RTCP_MTU;
629 sess->probation = DEFAULT_PROBATION;
630 sess->max_dropout_time = DEFAULT_MAX_DROPOUT_TIME;
631 sess->max_misorder_time = DEFAULT_MAX_MISORDER_TIME;
633 /* some default SDES entries */
634 sess->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
636 /* we do not want to leak details like the username or hostname here */
637 str = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
638 gst_structure_set (sess->sdes, "cname", G_TYPE_STRING, str, NULL);
642 /* we do not want to leak the user's real name here */
643 str = g_strdup_printf ("Anon%u", g_random_int ());
644 gst_structure_set (sdes, "name", G_TYPE_STRING, str, NULL);
648 gst_structure_set (sess->sdes, "tool", G_TYPE_STRING, "GStreamer", NULL);
650 /* this is the SSRC we suggest */
651 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
652 sess->internal_ssrc_set = FALSE;
654 sess->first_rtcp = TRUE;
655 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
656 sess->last_rtcp_check_time = GST_CLOCK_TIME_NONE;
657 sess->last_rtcp_send_time = GST_CLOCK_TIME_NONE;
658 sess->last_rtcp_interval = GST_CLOCK_TIME_NONE;
660 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
661 sess->rtcp_feedback_retention_window = DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW;
662 sess->rtcp_immediate_feedback_threshold =
663 DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD;
664 sess->rtp_profile = DEFAULT_RTP_PROFILE;
665 sess->reduced_size_rtcp = DEFAULT_RTCP_REDUCED_SIZE;
667 sess->is_doing_ptp = TRUE;
671 rtp_session_finalize (GObject * object)
676 sess = RTP_SESSION_CAST (object);
678 gst_structure_free (sess->sdes);
680 g_list_free_full (sess->conflicting_addresses,
681 (GDestroyNotify) rtp_conflicting_address_free);
683 /* TODO: Change this again when implementing RFC 2762
684 * for (i = 0; i < 32; i++)
686 for (i = 0; i < 1; i++)
687 g_hash_table_destroy (sess->ssrcs[i]);
689 g_mutex_clear (&sess->lock);
691 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
695 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
697 GValue value = { 0 };
699 g_value_init (&value, RTP_TYPE_SOURCE);
700 g_value_take_object (&value, source);
701 /* copies the value */
702 g_value_array_append (arr, &value);
706 rtp_session_create_sources (RTPSession * sess)
711 RTP_SESSION_LOCK (sess);
712 /* get number of elements in the table */
713 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
714 /* create the result value array */
715 res = g_value_array_new (size);
717 /* and copy all values into the array */
718 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
719 RTP_SESSION_UNLOCK (sess);
725 create_source_stats (gpointer key, RTPSource * source, GValueArray * arr)
730 g_object_get (source, "stats", &s, NULL);
732 g_value_array_append (arr, NULL);
733 value = g_value_array_get_nth (arr, arr->n_values - 1);
734 g_value_init (value, GST_TYPE_STRUCTURE);
735 g_value_take_boxed (value, s);
738 static GstStructure *
739 rtp_session_create_stats (RTPSession * sess)
742 GValueArray *source_stats;
743 GValue source_stats_v = G_VALUE_INIT;
746 RTP_SESSION_LOCK (sess);
747 s = gst_structure_new ("application/x-rtp-session-stats",
748 "rtx-drop-count", G_TYPE_UINT, sess->stats.nacks_dropped,
749 "sent-nack-count", G_TYPE_UINT, sess->stats.nacks_sent,
750 "recv-nack-count", G_TYPE_UINT, sess->stats.nacks_received, NULL);
752 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
753 source_stats = g_value_array_new (size);
754 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
755 (GHFunc) create_source_stats, source_stats);
756 RTP_SESSION_UNLOCK (sess);
758 g_value_init (&source_stats_v, G_TYPE_VALUE_ARRAY);
759 g_value_take_boxed (&source_stats_v, source_stats);
760 gst_structure_take_value (s, "source-stats", &source_stats_v);
766 rtp_session_set_property (GObject * object, guint prop_id,
767 const GValue * value, GParamSpec * pspec)
771 sess = RTP_SESSION (object);
774 case PROP_INTERNAL_SSRC:
775 RTP_SESSION_LOCK (sess);
776 sess->suggested_ssrc = g_value_get_uint (value);
777 sess->internal_ssrc_set = TRUE;
778 sess->internal_ssrc_from_caps_or_property = TRUE;
779 RTP_SESSION_UNLOCK (sess);
780 if (sess->callbacks.reconfigure)
781 sess->callbacks.reconfigure (sess, sess->reconfigure_user_data);
784 RTP_SESSION_LOCK (sess);
785 sess->bandwidth = g_value_get_double (value);
786 sess->recalc_bandwidth = TRUE;
787 RTP_SESSION_UNLOCK (sess);
789 case PROP_RTCP_FRACTION:
790 RTP_SESSION_LOCK (sess);
791 sess->rtcp_bandwidth = g_value_get_double (value);
792 sess->recalc_bandwidth = TRUE;
793 RTP_SESSION_UNLOCK (sess);
795 case PROP_RTCP_RR_BANDWIDTH:
796 RTP_SESSION_LOCK (sess);
797 sess->rtcp_rr_bandwidth = g_value_get_int (value);
798 sess->recalc_bandwidth = TRUE;
799 RTP_SESSION_UNLOCK (sess);
801 case PROP_RTCP_RS_BANDWIDTH:
802 RTP_SESSION_LOCK (sess);
803 sess->rtcp_rs_bandwidth = g_value_get_int (value);
804 sess->recalc_bandwidth = TRUE;
805 RTP_SESSION_UNLOCK (sess);
808 sess->mtu = g_value_get_uint (value);
811 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
814 sess->favor_new = g_value_get_boolean (value);
816 case PROP_RTCP_MIN_INTERVAL:
817 rtp_stats_set_min_interval (&sess->stats,
818 (gdouble) g_value_get_uint64 (value) / GST_SECOND);
819 /* trigger reconsideration */
820 RTP_SESSION_LOCK (sess);
821 sess->next_rtcp_check_time = 0;
822 RTP_SESSION_UNLOCK (sess);
823 if (sess->callbacks.reconsider)
824 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
826 case PROP_RTCP_FEEDBACK_RETENTION_WINDOW:
827 sess->rtcp_feedback_retention_window = g_value_get_uint64 (value);
829 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
830 sess->rtcp_immediate_feedback_threshold = g_value_get_uint (value);
833 sess->probation = g_value_get_uint (value);
835 case PROP_MAX_DROPOUT_TIME:
836 sess->max_dropout_time = g_value_get_uint (value);
838 case PROP_MAX_MISORDER_TIME:
839 sess->max_misorder_time = g_value_get_uint (value);
841 case PROP_RTP_PROFILE:
842 sess->rtp_profile = g_value_get_enum (value);
843 /* trigger reconsideration */
844 RTP_SESSION_LOCK (sess);
845 sess->next_rtcp_check_time = 0;
846 RTP_SESSION_UNLOCK (sess);
847 if (sess->callbacks.reconsider)
848 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
850 case PROP_RTCP_REDUCED_SIZE:
851 sess->reduced_size_rtcp = g_value_get_boolean (value);
854 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
860 rtp_session_get_property (GObject * object, guint prop_id,
861 GValue * value, GParamSpec * pspec)
865 sess = RTP_SESSION (object);
868 case PROP_INTERNAL_SSRC:
869 g_value_set_uint (value, rtp_session_suggest_ssrc (sess, NULL));
871 case PROP_INTERNAL_SOURCE:
872 /* FIXME, return a random source */
873 g_value_set_object (value, NULL);
876 g_value_set_double (value, sess->bandwidth);
878 case PROP_RTCP_FRACTION:
879 g_value_set_double (value, sess->rtcp_bandwidth);
881 case PROP_RTCP_RR_BANDWIDTH:
882 g_value_set_int (value, sess->rtcp_rr_bandwidth);
884 case PROP_RTCP_RS_BANDWIDTH:
885 g_value_set_int (value, sess->rtcp_rs_bandwidth);
888 g_value_set_uint (value, sess->mtu);
891 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
893 case PROP_NUM_SOURCES:
894 g_value_set_uint (value, rtp_session_get_num_sources (sess));
896 case PROP_NUM_ACTIVE_SOURCES:
897 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
900 g_value_take_boxed (value, rtp_session_create_sources (sess));
903 g_value_set_boolean (value, sess->favor_new);
905 case PROP_RTCP_MIN_INTERVAL:
906 g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
908 case PROP_RTCP_FEEDBACK_RETENTION_WINDOW:
909 g_value_set_uint64 (value, sess->rtcp_feedback_retention_window);
911 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
912 g_value_set_uint (value, sess->rtcp_immediate_feedback_threshold);
915 g_value_set_uint (value, sess->probation);
917 case PROP_MAX_DROPOUT_TIME:
918 g_value_set_uint (value, sess->max_dropout_time);
920 case PROP_MAX_MISORDER_TIME:
921 g_value_set_uint (value, sess->max_misorder_time);
924 g_value_take_boxed (value, rtp_session_create_stats (sess));
926 case PROP_RTP_PROFILE:
927 g_value_set_enum (value, sess->rtp_profile);
929 case PROP_RTCP_REDUCED_SIZE:
930 g_value_set_boolean (value, sess->reduced_size_rtcp);
933 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
939 on_new_ssrc (RTPSession * sess, RTPSource * source)
941 g_object_ref (source);
942 RTP_SESSION_UNLOCK (sess);
943 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
944 RTP_SESSION_LOCK (sess);
945 g_object_unref (source);
949 on_ssrc_collision (RTPSession * sess, RTPSource * source)
951 g_object_ref (source);
952 RTP_SESSION_UNLOCK (sess);
953 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
955 RTP_SESSION_LOCK (sess);
956 g_object_unref (source);
960 on_ssrc_validated (RTPSession * sess, RTPSource * source)
962 g_object_ref (source);
963 RTP_SESSION_UNLOCK (sess);
964 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
966 RTP_SESSION_LOCK (sess);
967 g_object_unref (source);
971 on_ssrc_active (RTPSession * sess, RTPSource * source)
973 g_object_ref (source);
974 RTP_SESSION_UNLOCK (sess);
975 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
976 RTP_SESSION_LOCK (sess);
977 g_object_unref (source);
981 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
983 g_object_ref (source);
984 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
985 RTP_SESSION_UNLOCK (sess);
986 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
987 RTP_SESSION_LOCK (sess);
988 g_object_unref (source);
992 on_bye_ssrc (RTPSession * sess, RTPSource * source)
994 g_object_ref (source);
995 RTP_SESSION_UNLOCK (sess);
996 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
997 RTP_SESSION_LOCK (sess);
998 g_object_unref (source);
1002 on_bye_timeout (RTPSession * sess, RTPSource * source)
1004 g_object_ref (source);
1005 RTP_SESSION_UNLOCK (sess);
1006 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
1007 RTP_SESSION_LOCK (sess);
1008 g_object_unref (source);
1012 on_timeout (RTPSession * sess, RTPSource * source)
1014 g_object_ref (source);
1015 RTP_SESSION_UNLOCK (sess);
1016 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
1017 RTP_SESSION_LOCK (sess);
1018 g_object_unref (source);
1022 on_sender_timeout (RTPSession * sess, RTPSource * source)
1024 g_object_ref (source);
1025 RTP_SESSION_UNLOCK (sess);
1026 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
1028 RTP_SESSION_LOCK (sess);
1029 g_object_unref (source);
1033 on_new_sender_ssrc (RTPSession * sess, RTPSource * source)
1035 g_object_ref (source);
1036 RTP_SESSION_UNLOCK (sess);
1037 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SENDER_SSRC], 0,
1039 RTP_SESSION_LOCK (sess);
1040 g_object_unref (source);
1044 on_sender_ssrc_active (RTPSession * sess, RTPSource * source)
1046 g_object_ref (source);
1047 RTP_SESSION_UNLOCK (sess);
1048 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE], 0,
1050 RTP_SESSION_LOCK (sess);
1051 g_object_unref (source);
1057 * Create a new session object.
1059 * Returns: a new #RTPSession. g_object_unref() after usage.
1062 rtp_session_new (void)
1066 sess = g_object_new (RTP_TYPE_SESSION, NULL);
1072 * rtp_session_reset:
1073 * @sess: an #RTPSession
1075 * Reset the sources of @sess.
1078 rtp_session_reset (RTPSession * sess)
1080 g_return_if_fail (RTP_IS_SESSION (sess));
1082 /* remove all sources */
1083 g_hash_table_remove_all (sess->ssrcs[sess->mask_idx]);
1084 sess->total_sources = 0;
1085 sess->stats.sender_sources = 0;
1086 sess->stats.internal_sender_sources = 0;
1087 sess->stats.internal_sources = 0;
1088 sess->stats.active_sources = 0;
1090 sess->generation = 0;
1091 sess->first_rtcp = TRUE;
1092 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
1093 sess->last_rtcp_check_time = GST_CLOCK_TIME_NONE;
1094 sess->last_rtcp_send_time = GST_CLOCK_TIME_NONE;
1095 sess->last_rtcp_interval = GST_CLOCK_TIME_NONE;
1096 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
1097 sess->scheduled_bye = FALSE;
1099 /* reset session stats */
1100 sess->stats.bye_members = 0;
1101 sess->stats.nacks_dropped = 0;
1102 sess->stats.nacks_sent = 0;
1103 sess->stats.nacks_received = 0;
1105 sess->is_doing_ptp = TRUE;
1107 g_list_free_full (sess->conflicting_addresses,
1108 (GDestroyNotify) rtp_conflicting_address_free);
1109 sess->conflicting_addresses = NULL;
1113 * rtp_session_set_callbacks:
1114 * @sess: an #RTPSession
1115 * @callbacks: callbacks to configure
1116 * @user_data: user data passed in the callbacks
1118 * Configure a set of callbacks to be notified of actions.
1121 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
1124 g_return_if_fail (RTP_IS_SESSION (sess));
1126 if (callbacks->process_rtp) {
1127 sess->callbacks.process_rtp = callbacks->process_rtp;
1128 sess->process_rtp_user_data = user_data;
1130 if (callbacks->send_rtp) {
1131 sess->callbacks.send_rtp = callbacks->send_rtp;
1132 sess->send_rtp_user_data = user_data;
1134 if (callbacks->send_rtcp) {
1135 sess->callbacks.send_rtcp = callbacks->send_rtcp;
1136 sess->send_rtcp_user_data = user_data;
1138 if (callbacks->sync_rtcp) {
1139 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
1140 sess->sync_rtcp_user_data = user_data;
1142 if (callbacks->clock_rate) {
1143 sess->callbacks.clock_rate = callbacks->clock_rate;
1144 sess->clock_rate_user_data = user_data;
1146 if (callbacks->reconsider) {
1147 sess->callbacks.reconsider = callbacks->reconsider;
1148 sess->reconsider_user_data = user_data;
1150 if (callbacks->request_key_unit) {
1151 sess->callbacks.request_key_unit = callbacks->request_key_unit;
1152 sess->request_key_unit_user_data = user_data;
1154 if (callbacks->request_time) {
1155 sess->callbacks.request_time = callbacks->request_time;
1156 sess->request_time_user_data = user_data;
1158 if (callbacks->notify_nack) {
1159 sess->callbacks.notify_nack = callbacks->notify_nack;
1160 sess->notify_nack_user_data = user_data;
1162 if (callbacks->reconfigure) {
1163 sess->callbacks.reconfigure = callbacks->reconfigure;
1164 sess->reconfigure_user_data = user_data;
1166 if (callbacks->notify_early_rtcp) {
1167 sess->callbacks.notify_early_rtcp = callbacks->notify_early_rtcp;
1168 sess->notify_early_rtcp_user_data = user_data;
1173 * rtp_session_set_process_rtp_callback:
1174 * @sess: an #RTPSession
1175 * @callback: callback to set
1176 * @user_data: user data passed in the callback
1178 * Configure only the process_rtp callback to be notified of the process_rtp action.
1181 rtp_session_set_process_rtp_callback (RTPSession * sess,
1182 RTPSessionProcessRTP callback, gpointer user_data)
1184 g_return_if_fail (RTP_IS_SESSION (sess));
1186 sess->callbacks.process_rtp = callback;
1187 sess->process_rtp_user_data = user_data;
1191 * rtp_session_set_send_rtp_callback:
1192 * @sess: an #RTPSession
1193 * @callback: callback to set
1194 * @user_data: user data passed in the callback
1196 * Configure only the send_rtp callback to be notified of the send_rtp action.
1199 rtp_session_set_send_rtp_callback (RTPSession * sess,
1200 RTPSessionSendRTP callback, gpointer user_data)
1202 g_return_if_fail (RTP_IS_SESSION (sess));
1204 sess->callbacks.send_rtp = callback;
1205 sess->send_rtp_user_data = user_data;
1209 * rtp_session_set_send_rtcp_callback:
1210 * @sess: an #RTPSession
1211 * @callback: callback to set
1212 * @user_data: user data passed in the callback
1214 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
1217 rtp_session_set_send_rtcp_callback (RTPSession * sess,
1218 RTPSessionSendRTCP callback, gpointer user_data)
1220 g_return_if_fail (RTP_IS_SESSION (sess));
1222 sess->callbacks.send_rtcp = callback;
1223 sess->send_rtcp_user_data = user_data;
1227 * rtp_session_set_sync_rtcp_callback:
1228 * @sess: an #RTPSession
1229 * @callback: callback to set
1230 * @user_data: user data passed in the callback
1232 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
1235 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
1236 RTPSessionSyncRTCP callback, gpointer user_data)
1238 g_return_if_fail (RTP_IS_SESSION (sess));
1240 sess->callbacks.sync_rtcp = callback;
1241 sess->sync_rtcp_user_data = user_data;
1245 * rtp_session_set_clock_rate_callback:
1246 * @sess: an #RTPSession
1247 * @callback: callback to set
1248 * @user_data: user data passed in the callback
1250 * Configure only the clock_rate callback to be notified of the clock_rate action.
1253 rtp_session_set_clock_rate_callback (RTPSession * sess,
1254 RTPSessionClockRate callback, gpointer user_data)
1256 g_return_if_fail (RTP_IS_SESSION (sess));
1258 sess->callbacks.clock_rate = callback;
1259 sess->clock_rate_user_data = user_data;
1263 * rtp_session_set_reconsider_callback:
1264 * @sess: an #RTPSession
1265 * @callback: callback to set
1266 * @user_data: user data passed in the callback
1268 * Configure only the reconsider callback to be notified of the reconsider action.
1271 rtp_session_set_reconsider_callback (RTPSession * sess,
1272 RTPSessionReconsider callback, gpointer user_data)
1274 g_return_if_fail (RTP_IS_SESSION (sess));
1276 sess->callbacks.reconsider = callback;
1277 sess->reconsider_user_data = user_data;
1281 * rtp_session_set_request_time_callback:
1282 * @sess: an #RTPSession
1283 * @callback: callback to set
1284 * @user_data: user data passed in the callback
1286 * Configure only the request_time callback
1289 rtp_session_set_request_time_callback (RTPSession * sess,
1290 RTPSessionRequestTime callback, gpointer user_data)
1292 g_return_if_fail (RTP_IS_SESSION (sess));
1294 sess->callbacks.request_time = callback;
1295 sess->request_time_user_data = user_data;
1299 * rtp_session_set_bandwidth:
1300 * @sess: an #RTPSession
1301 * @bandwidth: the bandwidth allocated
1303 * Set the session bandwidth in bits per second.
1306 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
1308 g_return_if_fail (RTP_IS_SESSION (sess));
1310 RTP_SESSION_LOCK (sess);
1311 sess->stats.bandwidth = bandwidth;
1312 RTP_SESSION_UNLOCK (sess);
1316 * rtp_session_get_bandwidth:
1317 * @sess: an #RTPSession
1319 * Get the session bandwidth.
1321 * Returns: the session bandwidth.
1324 rtp_session_get_bandwidth (RTPSession * sess)
1328 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1330 RTP_SESSION_LOCK (sess);
1331 result = sess->stats.bandwidth;
1332 RTP_SESSION_UNLOCK (sess);
1338 * rtp_session_set_rtcp_fraction:
1339 * @sess: an #RTPSession
1340 * @bandwidth: the RTCP bandwidth
1342 * Set the bandwidth in bits per second that should be used for RTCP
1346 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
1348 g_return_if_fail (RTP_IS_SESSION (sess));
1350 RTP_SESSION_LOCK (sess);
1351 sess->stats.rtcp_bandwidth = bandwidth;
1352 RTP_SESSION_UNLOCK (sess);
1356 * rtp_session_get_rtcp_fraction:
1357 * @sess: an #RTPSession
1359 * Get the session bandwidth used for RTCP.
1361 * Returns: The bandwidth used for RTCP messages.
1364 rtp_session_get_rtcp_fraction (RTPSession * sess)
1368 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
1370 RTP_SESSION_LOCK (sess);
1371 result = sess->stats.rtcp_bandwidth;
1372 RTP_SESSION_UNLOCK (sess);
1378 * rtp_session_get_sdes_struct:
1379 * @sess: an #RTSPSession
1381 * Get the SDES data as a #GstStructure
1383 * Returns: a GstStructure with SDES items for @sess. This function returns a
1384 * copy of the SDES structure, use gst_structure_free() after usage.
1387 rtp_session_get_sdes_struct (RTPSession * sess)
1389 GstStructure *result = NULL;
1391 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1393 RTP_SESSION_LOCK (sess);
1395 result = gst_structure_copy (sess->sdes);
1396 RTP_SESSION_UNLOCK (sess);
1402 source_set_sdes (const gchar * key, RTPSource * source, GstStructure * sdes)
1404 rtp_source_set_sdes_struct (source, gst_structure_copy (sdes));
1408 * rtp_session_set_sdes_struct:
1409 * @sess: an #RTSPSession
1410 * @sdes: a #GstStructure
1412 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1415 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
1417 g_return_if_fail (sdes);
1418 g_return_if_fail (RTP_IS_SESSION (sess));
1420 RTP_SESSION_LOCK (sess);
1422 gst_structure_free (sess->sdes);
1423 sess->sdes = gst_structure_copy (sdes);
1425 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
1426 (GHFunc) source_set_sdes, sess->sdes);
1427 RTP_SESSION_UNLOCK (sess);
1430 static GstFlowReturn
1431 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1433 GstFlowReturn result = GST_FLOW_OK;
1435 if (source->internal) {
1436 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1438 RTP_SESSION_UNLOCK (session);
1440 if (session->callbacks.send_rtp)
1442 session->callbacks.send_rtp (session, source, data,
1443 session->send_rtp_user_data);
1445 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1448 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1449 RTP_SESSION_UNLOCK (session);
1451 if (session->callbacks.process_rtp)
1453 session->callbacks.process_rtp (session, source,
1454 GST_BUFFER_CAST (data), session->process_rtp_user_data);
1456 gst_buffer_unref (GST_BUFFER_CAST (data));
1458 RTP_SESSION_LOCK (session);
1464 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1468 RTP_SESSION_UNLOCK (session);
1470 if (session->callbacks.clock_rate)
1472 session->callbacks.clock_rate (session, pt,
1473 session->clock_rate_user_data);
1477 RTP_SESSION_LOCK (session);
1479 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1484 static RTPSourceCallbacks callbacks = {
1485 (RTPSourcePushRTP) source_push_rtp,
1486 (RTPSourceClockRate) source_clock_rate,
1491 * rtp_session_find_conflicting_address:
1492 * @session: The session the packet came in
1493 * @address: address to check for
1494 * @time: The time when the packet that is possibly in conflict arrived
1496 * Checks if an address which has a conflict is already known. If it is
1497 * a known conflict, remember the time
1499 * Returns: TRUE if it was a known conflict, FALSE otherwise
1502 rtp_session_find_conflicting_address (RTPSession * session,
1503 GSocketAddress * address, GstClockTime time)
1505 return find_conflicting_address (session->conflicting_addresses, address,
1510 * rtp_session_add_conflicting_address:
1511 * @session: The session the packet came in
1512 * @address: address to remember
1513 * @time: The time when the packet that is in conflict arrived
1515 * Adds a new conflict address
1518 rtp_session_add_conflicting_address (RTPSession * sess,
1519 GSocketAddress * address, GstClockTime time)
1521 sess->conflicting_addresses =
1522 add_conflicting_address (sess->conflicting_addresses, address, time);
1527 check_collision (RTPSession * sess, RTPSource * source,
1528 RTPPacketInfo * pinfo, gboolean rtp)
1532 /* If we have no pinfo address, we can't do collision checking */
1533 if (!pinfo->address)
1536 ssrc = rtp_source_get_ssrc (source);
1538 if (!source->internal) {
1539 GSocketAddress *from;
1541 /* This is not our local source, but lets check if two remote
1544 from = source->rtp_from;
1546 from = source->rtcp_from;
1550 if (__g_socket_address_equal (from, pinfo->address)) {
1551 /* Address is the same */
1554 GST_LOG ("we have a third-party collision or loop ssrc:%x", ssrc);
1555 if (sess->favor_new) {
1556 if (rtp_source_find_conflicting_address (source,
1557 pinfo->address, pinfo->current_time)) {
1560 buf1 = __g_socket_address_to_string (pinfo->address);
1561 GST_LOG ("Known conflict on %x for %s, dropping packet", ssrc,
1569 /* Current address is not a known conflict, lets assume this is
1570 * a new source. Save old address in possible conflict list
1572 rtp_source_add_conflicting_address (source, from,
1573 pinfo->current_time);
1575 buf1 = __g_socket_address_to_string (from);
1576 buf2 = __g_socket_address_to_string (pinfo->address);
1578 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1579 " saving old as known conflict", ssrc, buf1, buf2);
1582 rtp_source_set_rtp_from (source, pinfo->address);
1584 rtp_source_set_rtcp_from (source, pinfo->address);
1592 /* Don't need to save old addresses, we ignore new sources */
1597 /* We don't already have a from address for RTP, just set it */
1599 rtp_source_set_rtp_from (source, pinfo->address);
1601 rtp_source_set_rtcp_from (source, pinfo->address);
1605 /* FIXME: Log 3rd party collision somehow
1606 * Maybe should be done in upper layer, only the SDES can tell us
1607 * if its a collision or a loop
1610 /* This is sending with our ssrc, is it an address we already know */
1611 if (rtp_session_find_conflicting_address (sess, pinfo->address,
1612 pinfo->current_time)) {
1613 /* Its a known conflict, its probably a loop, not a collision
1614 * lets just drop the incoming packet
1616 GST_DEBUG ("Our packets are being looped back to us, dropping");
1618 /* Its a new collision, lets change our SSRC */
1619 rtp_session_add_conflicting_address (sess, pinfo->address,
1620 pinfo->current_time);
1622 GST_DEBUG ("Collision for SSRC %x", ssrc);
1623 /* mark the source BYE */
1624 rtp_source_mark_bye (source, "SSRC Collision");
1625 /* if we were suggesting this SSRC, change to something else */
1626 if (sess->suggested_ssrc == ssrc) {
1627 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
1628 sess->internal_ssrc_set = TRUE;
1631 on_ssrc_collision (sess, source);
1633 rtp_session_schedule_bye_locked (sess, pinfo->current_time);
1642 gboolean is_doing_ptp;
1643 GSocketAddress *new_addr;
1646 /* check if the two given ip addr are the same (do not care about the port) */
1648 ip_addr_equal (GSocketAddress * a, GSocketAddress * b)
1651 g_inet_address_equal (g_inet_socket_address_get_address
1652 (G_INET_SOCKET_ADDRESS (a)),
1653 g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (b)));
1657 compare_rtp_source_addr (const gchar * key, RTPSource * source,
1658 CompareAddrData * data)
1660 /* only compare ip addr of remote sources which are also not closing */
1661 if (!source->internal && !source->closing && source->rtp_from) {
1662 /* look for the first rtp source */
1663 if (!data->new_addr)
1664 data->new_addr = source->rtp_from;
1665 /* compare current ip addr with the first one */
1667 data->is_doing_ptp &= ip_addr_equal (data->new_addr, source->rtp_from);
1672 compare_rtcp_source_addr (const gchar * key, RTPSource * source,
1673 CompareAddrData * data)
1675 /* only compare ip addr of remote sources which are also not closing */
1676 if (!source->internal && !source->closing && source->rtcp_from) {
1677 /* look for the first rtcp source */
1678 if (!data->new_addr)
1679 data->new_addr = source->rtcp_from;
1681 /* compare current ip addr with the first one */
1682 data->is_doing_ptp &= ip_addr_equal (data->new_addr, source->rtcp_from);
1686 /* loop over our non-internal source to know if the session
1687 * is doing point-to-point */
1689 session_update_ptp (RTPSession * sess)
1691 /* to know if the session is doing point to point, the ip addr
1692 * of each non-internal (=remotes) source have to be compared
1695 gboolean is_doing_rtp_ptp;
1696 gboolean is_doing_rtcp_ptp;
1697 CompareAddrData data;
1699 /* compare the first remote source's ip addr that receive rtp packets
1700 * with other remote rtp source.
1701 * it's enough because the session just needs to know if they are all
1704 data.is_doing_ptp = TRUE;
1705 data.new_addr = NULL;
1706 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
1707 (GHFunc) compare_rtp_source_addr, (gpointer) & data);
1708 is_doing_rtp_ptp = data.is_doing_ptp;
1710 /* same but about rtcp */
1711 data.is_doing_ptp = TRUE;
1712 data.new_addr = NULL;
1713 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
1714 (GHFunc) compare_rtcp_source_addr, (gpointer) & data);
1715 is_doing_rtcp_ptp = data.is_doing_ptp;
1717 /* the session is doing point-to-point if all rtp remote have the same
1718 * ip addr and if all rtcp remote sources have the same ip addr */
1719 sess->is_doing_ptp = is_doing_rtp_ptp && is_doing_rtcp_ptp;
1721 GST_DEBUG ("doing point-to-point: %d", sess->is_doing_ptp);
1725 add_source (RTPSession * sess, RTPSource * src)
1727 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1728 GINT_TO_POINTER (src->ssrc), src);
1729 /* report the new source ASAP */
1730 src->generation = sess->generation;
1731 /* we have one more source now */
1732 sess->total_sources++;
1733 if (RTP_SOURCE_IS_ACTIVE (src))
1734 sess->stats.active_sources++;
1735 if (src->internal) {
1736 sess->stats.internal_sources++;
1737 if (!sess->internal_ssrc_from_caps_or_property
1738 && sess->suggested_ssrc != src->ssrc) {
1739 sess->suggested_ssrc = src->ssrc;
1740 sess->internal_ssrc_set = TRUE;
1744 /* update point-to-point status */
1746 session_update_ptp (sess);
1750 find_source (RTPSession * sess, guint32 ssrc)
1752 return g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1753 GINT_TO_POINTER (ssrc));
1756 /* must be called with the session lock, the returned source needs to be
1757 * unreffed after usage. */
1759 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1760 RTPPacketInfo * pinfo, gboolean rtp)
1764 source = find_source (sess, ssrc);
1765 if (source == NULL) {
1766 /* make new Source in probation and insert */
1767 source = rtp_source_new (ssrc);
1769 GST_DEBUG ("creating new source %08x %p", ssrc, source);
1771 /* for RTP packets we need to set the source in probation. Receiving RTCP
1772 * packets of an SSRC, on the other hand, is a strong indication that we
1773 * are dealing with a valid source. */
1774 g_object_set (source, "probation", rtp ? sess->probation : 0,
1775 "max-dropout-time", sess->max_dropout_time, "max-misorder-time",
1776 sess->max_misorder_time, NULL);
1778 /* store from address, if any */
1779 if (pinfo->address) {
1781 rtp_source_set_rtp_from (source, pinfo->address);
1783 rtp_source_set_rtcp_from (source, pinfo->address);
1786 /* configure a callback on the source */
1787 rtp_source_set_callbacks (source, &callbacks, sess);
1789 add_source (sess, source);
1793 /* check for collision, this updates the address when not previously set */
1794 if (check_collision (sess, source, pinfo, rtp)) {
1797 /* Receiving RTCP packets of an SSRC is a strong indication that we
1798 * are dealing with a valid source. */
1800 g_object_set (source, "probation", 0, NULL);
1802 /* update last activity */
1803 source->last_activity = pinfo->current_time;
1805 source->last_rtp_activity = pinfo->current_time;
1806 g_object_ref (source);
1811 /* must be called with the session lock, the returned source needs to be
1812 * unreffed after usage. */
1814 obtain_internal_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1815 GstClockTime current_time)
1819 source = find_source (sess, ssrc);
1820 if (source == NULL) {
1821 /* make new internal Source and insert */
1822 source = rtp_source_new (ssrc);
1824 GST_DEBUG ("creating new internal source %08x %p", ssrc, source);
1826 source->validated = TRUE;
1827 source->internal = TRUE;
1828 source->probation = FALSE;
1829 rtp_source_set_sdes_struct (source, gst_structure_copy (sess->sdes));
1830 rtp_source_set_callbacks (source, &callbacks, sess);
1832 add_source (sess, source);
1837 /* update last activity */
1838 if (current_time != GST_CLOCK_TIME_NONE) {
1839 source->last_activity = current_time;
1840 source->last_rtp_activity = current_time;
1842 g_object_ref (source);
1848 * rtp_session_suggest_ssrc:
1849 * @sess: a #RTPSession
1850 * @is_random: if the suggested ssrc is random
1852 * Suggest an unused SSRC in @sess.
1854 * Returns: a free unused SSRC
1857 rtp_session_suggest_ssrc (RTPSession * sess, gboolean * is_random)
1861 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1863 RTP_SESSION_LOCK (sess);
1864 result = sess->suggested_ssrc;
1866 *is_random = !sess->internal_ssrc_set;
1867 RTP_SESSION_UNLOCK (sess);
1873 * rtp_session_add_source:
1874 * @sess: a #RTPSession
1875 * @src: #RTPSource to add
1877 * Add @src to @session.
1879 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1880 * existed in the session.
1883 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1885 gboolean result = FALSE;
1888 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1889 g_return_val_if_fail (src != NULL, FALSE);
1891 RTP_SESSION_LOCK (sess);
1892 find = find_source (sess, src->ssrc);
1894 add_source (sess, src);
1897 RTP_SESSION_UNLOCK (sess);
1903 * rtp_session_get_num_sources:
1904 * @sess: an #RTPSession
1906 * Get the number of sources in @sess.
1908 * Returns: The number of sources in @sess.
1911 rtp_session_get_num_sources (RTPSession * sess)
1915 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1917 RTP_SESSION_LOCK (sess);
1918 result = sess->total_sources;
1919 RTP_SESSION_UNLOCK (sess);
1925 * rtp_session_get_num_active_sources:
1926 * @sess: an #RTPSession
1928 * Get the number of active sources in @sess. A source is considered active when
1929 * it has been validated and has not yet received a BYE RTCP message.
1931 * Returns: The number of active sources in @sess.
1934 rtp_session_get_num_active_sources (RTPSession * sess)
1938 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1940 RTP_SESSION_LOCK (sess);
1941 result = sess->stats.active_sources;
1942 RTP_SESSION_UNLOCK (sess);
1948 * rtp_session_get_source_by_ssrc:
1949 * @sess: an #RTPSession
1952 * Find the source with @ssrc in @sess.
1954 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1955 * g_object_unref() after usage.
1958 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1962 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1964 RTP_SESSION_LOCK (sess);
1965 result = find_source (sess, ssrc);
1967 g_object_ref (result);
1968 RTP_SESSION_UNLOCK (sess);
1973 /* should be called with the SESSION lock */
1975 rtp_session_create_new_ssrc (RTPSession * sess)
1980 ssrc = g_random_int ();
1982 /* see if it exists in the session, we're done if it doesn't */
1983 if (find_source (sess, ssrc) == NULL)
1990 update_packet (GstBuffer ** buffer, guint idx, RTPPacketInfo * pinfo)
1992 GstNetAddressMeta *meta;
1994 /* get packet size including header overhead */
1995 pinfo->bytes += gst_buffer_get_size (*buffer) + pinfo->header_len;
1999 GstRTPBuffer rtp = { NULL };
2001 if (!gst_rtp_buffer_map (*buffer, GST_MAP_READ, &rtp))
2002 goto invalid_packet;
2004 pinfo->payload_len += gst_rtp_buffer_get_payload_len (&rtp);
2008 /* only keep info for first buffer */
2009 pinfo->ssrc = gst_rtp_buffer_get_ssrc (&rtp);
2010 pinfo->seqnum = gst_rtp_buffer_get_seq (&rtp);
2011 pinfo->pt = gst_rtp_buffer_get_payload_type (&rtp);
2012 pinfo->rtptime = gst_rtp_buffer_get_timestamp (&rtp);
2013 /* copy available csrc */
2014 pinfo->csrc_count = gst_rtp_buffer_get_csrc_count (&rtp);
2015 for (i = 0; i < pinfo->csrc_count; i++)
2016 pinfo->csrcs[i] = gst_rtp_buffer_get_csrc (&rtp, i);
2018 gst_rtp_buffer_unmap (&rtp);
2022 /* for netbuffer we can store the IP address to check for collisions */
2023 meta = gst_buffer_get_net_address_meta (*buffer);
2025 g_object_unref (pinfo->address);
2027 pinfo->address = G_SOCKET_ADDRESS (g_object_ref (meta->addr));
2029 pinfo->address = NULL;
2037 GST_DEBUG ("invalid RTP packet received");
2042 /* update the RTPPacketInfo structure with the current time and other bits
2043 * about the current buffer we are handling.
2044 * This function is typically called when a validated packet is received.
2045 * This function should be called with the RTP_SESSION_LOCK
2048 update_packet_info (RTPSession * sess, RTPPacketInfo * pinfo,
2049 gboolean send, gboolean rtp, gboolean is_list, gpointer data,
2050 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
2056 pinfo->is_list = is_list;
2058 pinfo->current_time = current_time;
2059 pinfo->running_time = running_time;
2060 pinfo->ntpnstime = ntpnstime;
2061 pinfo->header_len = sess->header_len;
2063 pinfo->payload_len = 0;
2067 GstBufferList *list = GST_BUFFER_LIST_CAST (data);
2069 gst_buffer_list_foreach (list, (GstBufferListFunc) update_packet,
2072 GstBuffer *buffer = GST_BUFFER_CAST (data);
2073 res = update_packet (&buffer, 0, pinfo);
2079 clean_packet_info (RTPPacketInfo * pinfo)
2082 g_object_unref (pinfo->address);
2084 gst_mini_object_unref (pinfo->data);
2090 source_update_active (RTPSession * sess, RTPSource * source,
2091 gboolean prevactive)
2093 gboolean active = RTP_SOURCE_IS_ACTIVE (source);
2094 guint32 ssrc = source->ssrc;
2096 if (prevactive == active)
2100 sess->stats.active_sources++;
2101 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
2102 sess->stats.active_sources);
2104 sess->stats.active_sources--;
2105 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
2106 sess->stats.active_sources);
2112 source_update_sender (RTPSession * sess, RTPSource * source,
2113 gboolean prevsender)
2115 gboolean sender = RTP_SOURCE_IS_SENDER (source);
2116 guint32 ssrc = source->ssrc;
2118 if (prevsender == sender)
2122 sess->stats.sender_sources++;
2123 if (source->internal)
2124 sess->stats.internal_sender_sources++;
2125 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
2126 sess->stats.sender_sources);
2128 sess->stats.sender_sources--;
2129 if (source->internal)
2130 sess->stats.internal_sender_sources--;
2131 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
2132 sess->stats.sender_sources);
2138 * rtp_session_process_rtp:
2139 * @sess: and #RTPSession
2140 * @buffer: an RTP buffer
2141 * @current_time: the current system time
2142 * @running_time: the running_time of @buffer
2144 * Process an RTP buffer in the session manager. This function takes ownership
2147 * Returns: a #GstFlowReturn.
2150 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
2151 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
2153 GstFlowReturn result;
2157 gboolean prevsender, prevactive;
2158 RTPPacketInfo pinfo = { 0, };
2161 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2162 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2164 RTP_SESSION_LOCK (sess);
2166 /* update pinfo stats */
2167 if (!update_packet_info (sess, &pinfo, FALSE, TRUE, FALSE, buffer,
2168 current_time, running_time, ntpnstime)) {
2169 GST_DEBUG ("invalid RTP packet received");
2170 RTP_SESSION_UNLOCK (sess);
2171 return rtp_session_process_rtcp (sess, buffer, current_time, running_time,
2177 source = obtain_source (sess, ssrc, &created, &pinfo, TRUE);
2181 prevsender = RTP_SOURCE_IS_SENDER (source);
2182 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2183 oldrate = source->bitrate;
2185 /* let source process the packet */
2186 result = rtp_source_process_rtp (source, &pinfo);
2188 /* source became active */
2189 if (source_update_active (sess, source, prevactive))
2190 on_ssrc_validated (sess, source);
2192 source_update_sender (sess, source, prevsender);
2194 if (oldrate != source->bitrate)
2195 sess->recalc_bandwidth = TRUE;
2198 on_new_ssrc (sess, source);
2200 if (source->validated) {
2204 /* for validated sources, we add the CSRCs as well */
2205 for (i = 0; i < pinfo.csrc_count; i++) {
2207 RTPSource *csrc_src;
2209 csrc = pinfo.csrcs[i];
2212 csrc_src = obtain_source (sess, csrc, &created, &pinfo, TRUE);
2217 GST_DEBUG ("created new CSRC: %08x", csrc);
2218 rtp_source_set_as_csrc (csrc_src);
2219 source_update_active (sess, csrc_src, FALSE);
2220 on_new_ssrc (sess, csrc_src);
2222 g_object_unref (csrc_src);
2225 g_object_unref (source);
2227 RTP_SESSION_UNLOCK (sess);
2229 clean_packet_info (&pinfo);
2236 RTP_SESSION_UNLOCK (sess);
2237 clean_packet_info (&pinfo);
2238 GST_DEBUG ("ignoring packet because its collisioning");
2244 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
2245 GstRTCPPacket * packet, RTPPacketInfo * pinfo)
2249 count = gst_rtcp_packet_get_rb_count (packet);
2250 for (i = 0; i < count; i++) {
2251 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
2252 guint8 fractionlost;
2256 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
2257 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2259 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
2261 /* find our own source */
2262 src = find_source (sess, ssrc);
2266 if (src->internal && RTP_SOURCE_IS_ACTIVE (src)) {
2267 /* only deal with report blocks for our session, we update the stats of
2268 * the sender of the RTCP message. We could also compare our stats against
2269 * the other sender to see if we are better or worse. */
2270 /* FIXME, need to keep track who the RB block is from */
2271 rtp_source_process_rb (source, pinfo->ntpnstime, fractionlost,
2272 packetslost, exthighestseq, jitter, lsr, dlsr);
2275 on_ssrc_active (sess, source);
2278 /* A Sender report contains statistics about how the sender is doing. This
2279 * includes timing informataion such as the relation between RTP and NTP
2280 * timestamps and the number of packets/bytes it sent to us.
2282 * In this report is also included a set of report blocks related to how this
2283 * sender is receiving data (in case we (or somebody else) is also sending stuff
2284 * to it). This info includes the packet loss, jitter and seqnum. It also
2285 * contains information to calculate the round trip time (LSR/DLSR).
2288 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
2289 RTPPacketInfo * pinfo, gboolean * do_sync)
2291 guint32 senderssrc, rtptime, packet_count, octet_count;
2294 gboolean created, prevsender;
2296 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
2297 &packet_count, &octet_count);
2299 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
2300 senderssrc, GST_TIME_ARGS (pinfo->current_time));
2302 source = obtain_source (sess, senderssrc, &created, pinfo, FALSE);
2306 /* skip non-bye packets for sources that are marked BYE */
2307 if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
2310 /* don't try to do lip-sync for sources that sent a BYE */
2311 if (RTP_SOURCE_IS_MARKED_BYE (source))
2316 prevsender = RTP_SOURCE_IS_SENDER (source);
2318 /* first update the source */
2319 rtp_source_process_sr (source, pinfo->current_time, ntptime, rtptime,
2320 packet_count, octet_count);
2322 source_update_sender (sess, source, prevsender);
2325 on_new_ssrc (sess, source);
2327 rtp_session_process_rb (sess, source, packet, pinfo);
2330 g_object_unref (source);
2333 /* A receiver report contains statistics about how a receiver is doing. It
2334 * includes stuff like packet loss, jitter and the seqnum it received last. It
2335 * also contains info to calculate the round trip time.
2337 * We are only interested in how the sender of this report is doing wrt to us.
2340 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
2341 RTPPacketInfo * pinfo)
2347 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
2349 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
2351 source = obtain_source (sess, senderssrc, &created, pinfo, FALSE);
2355 /* skip non-bye packets for sources that are marked BYE */
2356 if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
2360 on_new_ssrc (sess, source);
2362 rtp_session_process_rb (sess, source, packet, pinfo);
2365 g_object_unref (source);
2368 /* Get SDES items and store them in the SSRC */
2370 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
2371 RTPPacketInfo * pinfo)
2374 gboolean more_items, more_entries;
2376 items = gst_rtcp_packet_sdes_get_item_count (packet);
2377 GST_DEBUG ("got SDES packet with %d items", items);
2379 more_items = gst_rtcp_packet_sdes_first_item (packet);
2381 while (more_items) {
2383 gboolean changed, created, prevactive;
2387 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
2389 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
2393 /* find src, no probation when dealing with RTCP */
2394 source = obtain_source (sess, ssrc, &created, pinfo, FALSE);
2398 /* skip non-bye packets for sources that are marked BYE */
2399 if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
2402 sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
2404 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
2406 while (more_entries) {
2407 GstRTCPSDESType type;
2413 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
2415 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
2418 if (type == GST_RTCP_SDES_PRIV) {
2419 name = g_strndup ((const gchar *) &data[1], data[0]);
2421 data += data[0] + 1;
2423 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
2426 value = g_strndup ((const gchar *) data, len);
2428 if (g_utf8_validate (value, -1, NULL)) {
2429 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
2431 GST_WARNING ("ignore SDES field %s with non-utf8 data %s", name, value);
2437 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
2441 /* takes ownership of sdes */
2442 changed = rtp_source_set_sdes_struct (source, sdes);
2444 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2445 source->validated = TRUE;
2448 on_new_ssrc (sess, source);
2450 /* source became active */
2451 if (source_update_active (sess, source, prevactive))
2452 on_ssrc_validated (sess, source);
2455 on_ssrc_sdes (sess, source);
2458 g_object_unref (source);
2460 more_items = gst_rtcp_packet_sdes_next_item (packet);
2465 /* BYE is sent when a client leaves the session
2468 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
2469 RTPPacketInfo * pinfo)
2473 gboolean reconsider = FALSE;
2475 reason = gst_rtcp_packet_bye_get_reason (packet);
2476 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
2478 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
2479 for (i = 0; i < count; i++) {
2482 gboolean prevactive, prevsender;
2483 guint pmembers, members;
2485 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
2486 GST_DEBUG ("SSRC: %08x", ssrc);
2488 /* find src and mark bye, no probation when dealing with RTCP */
2489 source = find_source (sess, ssrc);
2490 if (!source || source->internal) {
2491 GST_DEBUG ("Ignoring suspicious BYE packet (reason: %s)",
2492 !source ? "can't find source" : "has internal source SSRC");
2496 /* store time for when we need to time out this source */
2497 source->bye_time = pinfo->current_time;
2499 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2500 prevsender = RTP_SOURCE_IS_SENDER (source);
2502 /* mark the source BYE */
2503 rtp_source_mark_bye (source, reason);
2505 pmembers = sess->stats.active_sources;
2507 source_update_active (sess, source, prevactive);
2508 source_update_sender (sess, source, prevsender);
2510 members = sess->stats.active_sources;
2512 if (!sess->scheduled_bye && members < pmembers) {
2513 /* some members went away since the previous timeout estimate.
2514 * Perform reverse reconsideration but only when we are not scheduling a
2516 if (sess->next_rtcp_check_time != GST_CLOCK_TIME_NONE &&
2517 pinfo->current_time < sess->next_rtcp_check_time) {
2518 GstClockTime time_remaining;
2520 /* Scale our next RTCP check time according to the change of numbers
2521 * of members. But only if a) this is the first RTCP, or b) this is not
2522 * a feedback session, or c) this is a feedback session but we schedule
2523 * for every RTCP interval (aka no t-rr-interval set).
2525 * FIXME: a) and b) are not great as we will possibly go below Tmin
2526 * for non-feedback profiles and in case of a) below
2527 * Tmin/t-rr-interval in any case.
2529 if (sess->last_rtcp_send_time == GST_CLOCK_TIME_NONE ||
2530 !(sess->rtp_profile == GST_RTP_PROFILE_AVPF
2531 || sess->rtp_profile == GST_RTP_PROFILE_SAVPF) ||
2532 sess->next_rtcp_check_time - sess->last_rtcp_send_time ==
2533 sess->last_rtcp_interval) {
2534 time_remaining = sess->next_rtcp_check_time - pinfo->current_time;
2535 sess->next_rtcp_check_time =
2536 gst_util_uint64_scale (time_remaining, members, pmembers);
2537 sess->next_rtcp_check_time += pinfo->current_time;
2539 sess->last_rtcp_interval =
2540 gst_util_uint64_scale (sess->last_rtcp_interval, members, pmembers);
2542 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
2543 GST_TIME_ARGS (sess->next_rtcp_check_time));
2545 /* mark pending reconsider. We only want to signal the reconsideration
2546 * once after we handled all the source in the bye packet */
2551 on_bye_ssrc (sess, source);
2554 RTP_SESSION_UNLOCK (sess);
2555 /* notify app of reconsideration */
2556 if (sess->callbacks.reconsider)
2557 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2558 RTP_SESSION_LOCK (sess);
2565 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
2566 RTPPacketInfo * pinfo)
2568 GST_DEBUG ("received APP");
2570 if (g_signal_has_handler_pending (sess,
2571 rtp_session_signals[SIGNAL_ON_APP_RTCP], 0, TRUE)) {
2572 GstBuffer *data_buffer = NULL;
2573 guint16 data_length;
2576 data_length = gst_rtcp_packet_app_get_data_length (packet) * 4;
2577 if (data_length > 0) {
2578 guint8 *data = gst_rtcp_packet_app_get_data (packet);
2579 data_buffer = gst_buffer_copy_region (packet->rtcp->buffer,
2580 GST_BUFFER_COPY_MEMORY, data - packet->rtcp->map.data, data_length);
2581 GST_BUFFER_PTS (data_buffer) = pinfo->running_time;
2584 memcpy (name, gst_rtcp_packet_app_get_name (packet), 4);
2587 RTP_SESSION_UNLOCK (sess);
2588 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_APP_RTCP], 0,
2589 gst_rtcp_packet_app_get_subtype (packet),
2590 gst_rtcp_packet_app_get_ssrc (packet), name, data_buffer);
2591 RTP_SESSION_LOCK (sess);
2594 gst_buffer_unref (data_buffer);
2599 rtp_session_request_local_key_unit (RTPSession * sess, RTPSource * src,
2600 guint32 media_ssrc, gboolean fir, GstClockTime current_time)
2602 guint32 round_trip = 0;
2604 rtp_source_get_last_rb (src, NULL, NULL, NULL, NULL, NULL, NULL, &round_trip);
2606 if (src->last_keyframe_request != GST_CLOCK_TIME_NONE && round_trip) {
2607 GstClockTime round_trip_in_ns = gst_util_uint64_scale (round_trip,
2610 /* Sanity check to avoid always ignoring PLI/FIR if we receive RTCP
2611 * packets with erroneous values resulting in crazy high RTT. */
2612 if (round_trip_in_ns > 5 * GST_SECOND)
2613 round_trip_in_ns = GST_SECOND / 2;
2615 if (current_time - src->last_keyframe_request < 2 * round_trip_in_ns) {
2616 GST_DEBUG ("Ignoring %s request from %X because one was send without one "
2617 "RTT (%" GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")",
2618 fir ? "FIR" : "PLI", rtp_source_get_ssrc (src),
2619 GST_TIME_ARGS (current_time - src->last_keyframe_request),
2620 GST_TIME_ARGS (round_trip_in_ns));
2625 src->last_keyframe_request = current_time;
2627 GST_LOG ("received %s request from %X about %X %p(%p)", fir ? "FIR" : "PLI",
2628 rtp_source_get_ssrc (src), media_ssrc, sess->callbacks.process_rtp,
2629 sess->callbacks.request_key_unit);
2631 RTP_SESSION_UNLOCK (sess);
2632 sess->callbacks.request_key_unit (sess, media_ssrc, fir,
2633 sess->request_key_unit_user_data);
2634 RTP_SESSION_LOCK (sess);
2640 rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
2641 guint32 media_ssrc, GstClockTime current_time)
2645 if (!sess->callbacks.request_key_unit)
2648 src = find_source (sess, sender_ssrc);
2650 /* try to find a src with media_ssrc instead */
2651 src = find_source (sess, media_ssrc);
2656 rtp_session_request_local_key_unit (sess, src, media_ssrc, FALSE,
2661 rtp_session_process_fir (RTPSession * sess, guint32 sender_ssrc,
2662 guint32 media_ssrc, guint8 * fci_data, guint fci_length,
2663 GstClockTime current_time)
2668 gboolean our_request = FALSE;
2670 if (!sess->callbacks.request_key_unit)
2676 src = find_source (sess, sender_ssrc);
2678 /* Hack because Google fails to set the sender_ssrc correctly */
2679 if (!src && sender_ssrc == 1) {
2680 GHashTableIter iter;
2682 /* we can't find the source if there are multiple */
2683 if (sess->stats.sender_sources > sess->stats.internal_sender_sources + 1)
2686 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
2687 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
2688 if (!src->internal && rtp_source_is_sender (src))
2696 for (position = 0; position < fci_length; position += 8) {
2697 guint8 *data = fci_data + position;
2700 ssrc = GST_READ_UINT32_BE (data);
2702 own = find_source (sess, ssrc);
2706 if (own->internal) {
2714 rtp_session_request_local_key_unit (sess, src, media_ssrc, TRUE,
2719 rtp_session_process_nack (RTPSession * sess, guint32 sender_ssrc,
2720 guint32 media_ssrc, guint8 * fci_data, guint fci_length,
2721 GstClockTime current_time)
2723 sess->stats.nacks_received++;
2725 if (!sess->callbacks.notify_nack)
2728 while (fci_length > 0) {
2729 guint16 seqnum, blp;
2731 seqnum = GST_READ_UINT16_BE (fci_data);
2732 blp = GST_READ_UINT16_BE (fci_data + 2);
2734 GST_DEBUG ("NACK #%u, blp %04x, SSRC 0x%08x", seqnum, blp, media_ssrc);
2736 RTP_SESSION_UNLOCK (sess);
2737 sess->callbacks.notify_nack (sess, seqnum, blp, media_ssrc,
2738 sess->notify_nack_user_data);
2739 RTP_SESSION_LOCK (sess);
2747 rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
2748 RTPPacketInfo * pinfo, GstClockTime current_time)
2751 GstRTCPFBType fbtype;
2752 guint32 sender_ssrc, media_ssrc;
2757 /* The feedback packet must include both sender SSRC and media SSRC */
2758 if (packet->length < 2)
2761 type = gst_rtcp_packet_get_type (packet);
2762 fbtype = gst_rtcp_packet_fb_get_type (packet);
2763 sender_ssrc = gst_rtcp_packet_fb_get_sender_ssrc (packet);
2764 media_ssrc = gst_rtcp_packet_fb_get_media_ssrc (packet);
2766 src = find_source (sess, media_ssrc);
2768 /* skip non-bye packets for sources that are marked BYE */
2769 if (sess->scheduled_bye && src && RTP_SOURCE_IS_MARKED_BYE (src))
2775 fci_data = gst_rtcp_packet_fb_get_fci (packet);
2776 fci_length = gst_rtcp_packet_fb_get_fci_length (packet) * sizeof (guint32);
2778 GST_DEBUG ("received feedback %d:%d from %08X about %08X with FCI of "
2779 "length %d", type, fbtype, sender_ssrc, media_ssrc, fci_length);
2781 if (g_signal_has_handler_pending (sess,
2782 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0, TRUE)) {
2783 GstBuffer *fci_buffer = NULL;
2785 if (fci_length > 0) {
2786 fci_buffer = gst_buffer_copy_region (packet->rtcp->buffer,
2787 GST_BUFFER_COPY_MEMORY, fci_data - packet->rtcp->map.data,
2789 GST_BUFFER_PTS (fci_buffer) = pinfo->running_time;
2792 RTP_SESSION_UNLOCK (sess);
2793 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0,
2794 type, fbtype, sender_ssrc, media_ssrc, fci_buffer);
2795 RTP_SESSION_LOCK (sess);
2798 gst_buffer_unref (fci_buffer);
2801 if (src && sess->rtcp_feedback_retention_window != GST_CLOCK_TIME_NONE) {
2802 rtp_source_retain_rtcp_packet (src, packet, pinfo->running_time);
2805 if ((src && src->internal) ||
2806 /* PSFB FIR puts the media ssrc inside the FCI */
2807 (type == GST_RTCP_TYPE_PSFB && fbtype == GST_RTCP_PSFB_TYPE_FIR)) {
2809 case GST_RTCP_TYPE_PSFB:
2811 case GST_RTCP_PSFB_TYPE_PLI:
2813 src->stats.recv_pli_count++;
2814 rtp_session_process_pli (sess, sender_ssrc, media_ssrc,
2817 case GST_RTCP_PSFB_TYPE_FIR:
2819 src->stats.recv_fir_count++;
2820 rtp_session_process_fir (sess, sender_ssrc, media_ssrc, fci_data,
2821 fci_length, current_time);
2827 case GST_RTCP_TYPE_RTPFB:
2829 case GST_RTCP_RTPFB_TYPE_NACK:
2831 src->stats.recv_nack_count++;
2832 rtp_session_process_nack (sess, sender_ssrc, media_ssrc,
2833 fci_data, fci_length, current_time);
2844 g_object_unref (src);
2848 * rtp_session_process_rtcp:
2849 * @sess: and #RTPSession
2850 * @buffer: an RTCP buffer
2851 * @current_time: the current system time
2852 * @ntpnstime: the current NTP time in nanoseconds
2854 * Process an RTCP buffer in the session manager. This function takes ownership
2857 * Returns: a #GstFlowReturn.
2860 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
2861 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
2863 GstRTCPPacket packet;
2864 gboolean more, is_bye = FALSE, do_sync = FALSE;
2865 RTPPacketInfo pinfo = { 0, };
2866 GstFlowReturn result = GST_FLOW_OK;
2867 GstRTCPBuffer rtcp = { NULL, };
2869 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2870 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2872 if (!gst_rtcp_buffer_validate_reduced (buffer))
2873 goto invalid_packet;
2875 GST_DEBUG ("received RTCP packet");
2877 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_RECEIVING_RTCP], 0,
2880 RTP_SESSION_LOCK (sess);
2881 /* update pinfo stats */
2882 update_packet_info (sess, &pinfo, FALSE, FALSE, FALSE, buffer, current_time,
2883 running_time, ntpnstime);
2885 /* start processing the compound packet */
2886 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
2887 more = gst_rtcp_buffer_get_first_packet (&rtcp, &packet);
2891 type = gst_rtcp_packet_get_type (&packet);
2894 case GST_RTCP_TYPE_SR:
2895 rtp_session_process_sr (sess, &packet, &pinfo, &do_sync);
2897 case GST_RTCP_TYPE_RR:
2898 rtp_session_process_rr (sess, &packet, &pinfo);
2900 case GST_RTCP_TYPE_SDES:
2901 rtp_session_process_sdes (sess, &packet, &pinfo);
2903 case GST_RTCP_TYPE_BYE:
2905 /* don't try to attempt lip-sync anymore for streams with a BYE */
2907 rtp_session_process_bye (sess, &packet, &pinfo);
2909 case GST_RTCP_TYPE_APP:
2910 rtp_session_process_app (sess, &packet, &pinfo);
2912 case GST_RTCP_TYPE_RTPFB:
2913 case GST_RTCP_TYPE_PSFB:
2914 rtp_session_process_feedback (sess, &packet, &pinfo, current_time);
2916 case GST_RTCP_TYPE_XR:
2917 /* FIXME: This block is added to downgrade warning level.
2918 * Once the parser is implemented, it should be replaced with
2919 * a proper process function. */
2920 GST_DEBUG ("got RTCP XR packet, but ignored");
2923 GST_WARNING ("got unknown RTCP packet type: %d", type);
2926 more = gst_rtcp_packet_move_to_next (&packet);
2929 gst_rtcp_buffer_unmap (&rtcp);
2931 /* if we are scheduling a BYE, we only want to count bye packets, else we
2932 * count everything */
2933 if (sess->scheduled_bye && is_bye) {
2934 sess->bye_stats.bye_members++;
2935 UPDATE_AVG (sess->bye_stats.avg_rtcp_packet_size, pinfo.bytes);
2938 /* keep track of average packet size */
2939 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, pinfo.bytes);
2941 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
2942 sess->stats.avg_rtcp_packet_size, pinfo.bytes);
2943 RTP_SESSION_UNLOCK (sess);
2946 clean_packet_info (&pinfo);
2948 /* notify caller of sr packets in the callback */
2949 if (do_sync && sess->callbacks.sync_rtcp) {
2950 result = sess->callbacks.sync_rtcp (sess, buffer,
2951 sess->sync_rtcp_user_data);
2953 gst_buffer_unref (buffer);
2960 GST_DEBUG ("invalid RTCP packet received");
2961 gst_buffer_unref (buffer);
2967 * rtp_session_update_send_caps:
2968 * @sess: an #RTPSession
2971 * Update the caps of the sender in the rtp session.
2974 rtp_session_update_send_caps (RTPSession * sess, GstCaps * caps)
2979 g_return_if_fail (RTP_IS_SESSION (sess));
2980 g_return_if_fail (GST_IS_CAPS (caps));
2982 GST_LOG ("received caps %" GST_PTR_FORMAT, caps);
2984 s = gst_caps_get_structure (caps, 0);
2986 if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
2990 RTP_SESSION_LOCK (sess);
2991 source = obtain_internal_source (sess, ssrc, &created, GST_CLOCK_TIME_NONE);
2992 sess->suggested_ssrc = ssrc;
2993 sess->internal_ssrc_set = TRUE;
2994 sess->internal_ssrc_from_caps_or_property = TRUE;
2996 rtp_source_update_caps (source, caps);
2999 on_new_sender_ssrc (sess, source);
3001 g_object_unref (source);
3004 if (gst_structure_get_uint (s, "rtx-ssrc", &ssrc)) {
3006 obtain_internal_source (sess, ssrc, &created, GST_CLOCK_TIME_NONE);
3008 rtp_source_update_caps (source, caps);
3011 on_new_sender_ssrc (sess, source);
3013 g_object_unref (source);
3016 RTP_SESSION_UNLOCK (sess);
3018 sess->internal_ssrc_from_caps_or_property = FALSE;
3023 * rtp_session_send_rtp:
3024 * @sess: an #RTPSession
3025 * @data: pointer to either an RTP buffer or a list of RTP buffers
3026 * @is_list: TRUE when @data is a buffer list
3027 * @current_time: the current system time
3028 * @running_time: the running time of @data
3030 * Send the RTP data (a buffer or buffer list) in the session manager. This
3031 * function takes ownership of @data.
3033 * Returns: a #GstFlowReturn.
3036 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
3037 GstClockTime current_time, GstClockTime running_time)
3039 GstFlowReturn result;
3041 gboolean prevsender;
3043 RTPPacketInfo pinfo = { 0, };
3046 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3047 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
3049 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
3051 RTP_SESSION_LOCK (sess);
3052 if (!update_packet_info (sess, &pinfo, TRUE, TRUE, is_list, data,
3053 current_time, running_time, -1))
3054 goto invalid_packet;
3056 source = obtain_internal_source (sess, pinfo.ssrc, &created, current_time);
3058 on_new_sender_ssrc (sess, source);
3060 if (!source->internal)
3061 /* FIXME: Send GstRTPCollision upstream */
3064 prevsender = RTP_SOURCE_IS_SENDER (source);
3065 oldrate = source->bitrate;
3067 /* we use our own source to send */
3068 result = rtp_source_send_rtp (source, &pinfo);
3070 source_update_sender (sess, source, prevsender);
3072 if (oldrate != source->bitrate)
3073 sess->recalc_bandwidth = TRUE;
3074 RTP_SESSION_UNLOCK (sess);
3076 g_object_unref (source);
3077 clean_packet_info (&pinfo);
3083 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3084 RTP_SESSION_UNLOCK (sess);
3085 GST_DEBUG ("invalid RTP packet received");
3090 g_object_unref (source);
3091 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3092 RTP_SESSION_UNLOCK (sess);
3093 GST_WARNING ("non-internal source with same ssrc %08x, drop packet",
3100 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
3102 *bandwidth += source->bitrate;
3105 /* must be called with session lock */
3107 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
3110 GstClockTime result;
3111 RTPSessionStats *stats;
3113 /* recalculate bandwidth when it changed */
3114 if (sess->recalc_bandwidth) {
3117 if (sess->bandwidth > 0)
3118 bandwidth = sess->bandwidth;
3120 /* If it is <= 0, then try to estimate the actual bandwidth */
3123 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3124 (GHFunc) add_bitrates, &bandwidth);
3126 if (bandwidth < RTP_STATS_BANDWIDTH)
3127 bandwidth = RTP_STATS_BANDWIDTH;
3129 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
3130 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
3132 sess->recalc_bandwidth = FALSE;
3135 if (sess->scheduled_bye) {
3136 stats = &sess->bye_stats;
3137 result = rtp_stats_calculate_bye_interval (stats);
3139 session_update_ptp (sess);
3141 stats = &sess->stats;
3142 result = rtp_stats_calculate_rtcp_interval (stats,
3143 stats->internal_sender_sources > 0, sess->rtp_profile,
3144 sess->is_doing_ptp, first);
3147 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
3148 GST_TIME_ARGS (result), first);
3150 if (!deterministic && result != GST_CLOCK_TIME_NONE)
3151 result = rtp_stats_add_rtcp_jitter (stats, result);
3153 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
3159 source_mark_bye (const gchar * key, RTPSource * source, const gchar * reason)
3161 if (source->internal)
3162 rtp_source_mark_bye (source, reason);
3166 * rtp_session_mark_all_bye:
3167 * @sess: an #RTPSession
3170 * Mark all internal sources of the session as BYE with @reason.
3173 rtp_session_mark_all_bye (RTPSession * sess, const gchar * reason)
3175 g_return_if_fail (RTP_IS_SESSION (sess));
3177 RTP_SESSION_LOCK (sess);
3178 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3179 (GHFunc) source_mark_bye, (gpointer) reason);
3180 RTP_SESSION_UNLOCK (sess);
3183 /* Stop the current @sess and schedule a BYE message for the other members.
3184 * One must have the session lock to call this function
3186 static GstFlowReturn
3187 rtp_session_schedule_bye_locked (RTPSession * sess, GstClockTime current_time)
3189 GstFlowReturn result = GST_FLOW_OK;
3190 GstClockTime interval;
3192 /* nothing to do it we already scheduled bye */
3193 if (sess->scheduled_bye)
3196 /* we schedule BYE now */
3197 sess->scheduled_bye = TRUE;
3198 /* at least one member wants to send a BYE */
3199 memcpy (&sess->bye_stats, &sess->stats, sizeof (RTPSessionStats));
3200 INIT_AVG (sess->bye_stats.avg_rtcp_packet_size, 100);
3201 sess->bye_stats.bye_members = 1;
3202 sess->first_rtcp = TRUE;
3204 /* reschedule transmission */
3205 sess->last_rtcp_send_time = current_time;
3206 sess->last_rtcp_check_time = current_time;
3207 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
3209 if (interval != GST_CLOCK_TIME_NONE)
3210 sess->next_rtcp_check_time = current_time + interval;
3212 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
3213 sess->last_rtcp_interval = interval;
3215 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
3216 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
3218 RTP_SESSION_UNLOCK (sess);
3219 /* notify app of reconsideration */
3220 if (sess->callbacks.reconsider)
3221 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
3222 RTP_SESSION_LOCK (sess);
3229 * rtp_session_schedule_bye:
3230 * @sess: an #RTPSession
3231 * @current_time: the current system time
3233 * Schedule a BYE message for all sources marked as BYE in @sess.
3235 * Returns: a #GstFlowReturn.
3238 rtp_session_schedule_bye (RTPSession * sess, GstClockTime current_time)
3240 GstFlowReturn result;
3242 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3244 RTP_SESSION_LOCK (sess);
3245 result = rtp_session_schedule_bye_locked (sess, current_time);
3246 RTP_SESSION_UNLOCK (sess);
3252 * rtp_session_next_timeout:
3253 * @sess: an #RTPSession
3254 * @current_time: the current system time
3256 * Get the next time we should perform session maintenance tasks.
3258 * Returns: a time when rtp_session_on_timeout() should be called with the
3259 * current system time.
3262 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
3264 GstClockTime result, interval = 0;
3266 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
3268 RTP_SESSION_LOCK (sess);
3270 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
3271 GST_DEBUG ("have early rtcp time");
3272 result = sess->next_early_rtcp_time;
3276 result = sess->next_rtcp_check_time;
3278 GST_DEBUG ("current time: %" GST_TIME_FORMAT
3279 ", next time: %" GST_TIME_FORMAT,
3280 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
3282 if (result == GST_CLOCK_TIME_NONE || result < current_time) {
3283 GST_DEBUG ("take current time as base");
3284 /* our previous check time expired, start counting from the current time
3286 result = current_time;
3289 if (sess->scheduled_bye) {
3290 if (sess->bye_stats.active_sources >= 50) {
3291 GST_DEBUG ("reconsider BYE, more than 50 sources");
3292 /* reconsider BYE if members >= 50 */
3293 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
3294 sess->last_rtcp_interval = interval;
3297 if (sess->first_rtcp) {
3298 GST_DEBUG ("first RTCP packet");
3299 /* we are called for the first time */
3300 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
3301 sess->last_rtcp_interval = interval;
3302 } else if (sess->next_rtcp_check_time < current_time) {
3303 GST_DEBUG ("old check time expired, getting new timeout");
3304 /* get a new timeout when we need to */
3305 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
3306 sess->last_rtcp_interval = interval;
3308 if ((sess->rtp_profile == GST_RTP_PROFILE_AVPF
3309 || sess->rtp_profile == GST_RTP_PROFILE_SAVPF)
3310 && interval != GST_CLOCK_TIME_NONE) {
3311 /* Apply the rules from RFC 4585 section 3.5.3 */
3312 if (sess->stats.min_interval != 0) {
3313 GstClockTime T_rr_current_interval = g_random_double_range (0.5,
3314 1.5) * sess->stats.min_interval * GST_SECOND;
3316 if (T_rr_current_interval > interval) {
3317 GST_DEBUG ("Adjusting interval for t-rr-interval: %" GST_TIME_FORMAT
3318 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (T_rr_current_interval),
3319 GST_TIME_ARGS (interval));
3320 interval = T_rr_current_interval;
3327 if (interval != GST_CLOCK_TIME_NONE)
3330 result = GST_CLOCK_TIME_NONE;
3332 sess->next_rtcp_check_time = result;
3336 GST_DEBUG ("current time: %" GST_TIME_FORMAT
3337 ", next time: %" GST_TIME_FORMAT,
3338 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
3339 RTP_SESSION_UNLOCK (sess);
3353 GstRTCPBuffer rtcpbuf;
3356 guint num_to_report;
3361 GstClockTime current_time;
3363 GstClockTime running_time;
3364 GstClockTime interval;
3365 GstRTCPPacket packet;
3368 gboolean may_suppress;
3370 guint nacked_seqnums;
3374 session_start_rtcp (RTPSession * sess, ReportData * data)
3376 GstRTCPPacket *packet = &data->packet;
3377 RTPSource *own = data->source;
3378 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3380 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
3381 data->has_sdes = FALSE;
3383 gst_rtcp_buffer_map (data->rtcp, GST_MAP_READWRITE, rtcp);
3385 if (data->is_early && sess->reduced_size_rtcp)
3388 if (RTP_SOURCE_IS_SENDER (own)) {
3391 guint32 packet_count, octet_count;
3393 /* we are a sender, create SR */
3394 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
3395 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SR, packet);
3397 /* get latest stats */
3398 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
3399 &ntptime, &rtptime, &packet_count, &octet_count);
3401 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
3402 packet_count, octet_count);
3404 /* fill in sender report info */
3405 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
3406 ntptime, rtptime, packet_count, octet_count);
3408 /* we are only receiver, create RR */
3409 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
3410 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RR, packet);
3411 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
3415 /* construct a Sender or Receiver Report */
3417 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
3419 RTPSession *sess = data->sess;
3420 GstRTCPPacket *packet = &data->packet;
3421 guint8 fractionlost;
3423 guint32 exthighestseq, jitter;
3426 /* don't report for sources in future generations */
3427 if (((gint16) (source->generation - sess->generation)) > 0) {
3428 GST_DEBUG ("source %08x generation %u > %u", source->ssrc,
3429 source->generation, sess->generation);
3433 if (g_hash_table_contains (source->reported_in_sr_of,
3434 GUINT_TO_POINTER (data->source->ssrc))) {
3435 GST_DEBUG ("source %08x already reported in this generation", source->ssrc);
3439 if (gst_rtcp_packet_get_rb_count (packet) == GST_RTCP_MAX_RB_COUNT) {
3440 GST_DEBUG ("max RB count reached");
3444 /* only report about remote sources */
3445 if (source->internal)
3448 if (!RTP_SOURCE_IS_SENDER (source)) {
3449 GST_DEBUG ("source %08x not sender", source->ssrc);
3453 if (source->disable_rtcp) {
3454 GST_DEBUG ("source %08x has RTCP disabled", source->ssrc);
3458 GST_DEBUG ("create RB for SSRC %08x", source->ssrc);
3461 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
3462 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
3464 /* store last generated RR packet */
3465 source->last_rr.is_valid = TRUE;
3466 source->last_rr.fractionlost = fractionlost;
3467 source->last_rr.packetslost = packetslost;
3468 source->last_rr.exthighestseq = exthighestseq;
3469 source->last_rr.jitter = jitter;
3470 source->last_rr.lsr = lsr;
3471 source->last_rr.dlsr = dlsr;
3473 /* packet is not yet filled, add report block for this source. */
3474 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
3475 exthighestseq, jitter, lsr, dlsr);
3478 g_hash_table_add (source->reported_in_sr_of,
3479 GUINT_TO_POINTER (data->source->ssrc));
3484 session_add_fir (const gchar * key, RTPSource * source, ReportData * data)
3486 GstRTCPPacket *packet = &data->packet;
3490 if (!source->send_fir)
3493 len = gst_rtcp_packet_fb_get_fci_length (packet);
3494 if (!gst_rtcp_packet_fb_set_fci_length (packet, len + 2))
3495 /* exit because the packet is full, will put next request in a
3499 fci_data = gst_rtcp_packet_fb_get_fci (packet) + (len * 4);
3501 GST_WRITE_UINT32_BE (fci_data, source->ssrc);
3503 fci_data[0] = source->current_send_fir_seqnum;
3504 fci_data[1] = fci_data[2] = fci_data[3] = 0;
3506 source->send_fir = FALSE;
3507 source->stats.sent_fir_count++;
3511 session_fir (RTPSession * sess, ReportData * data)
3513 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3514 GstRTCPPacket *packet = &data->packet;
3516 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
3519 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_FIR);
3520 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3521 gst_rtcp_packet_fb_set_media_ssrc (packet, 0);
3523 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3524 (GHFunc) session_add_fir, data);
3526 if (gst_rtcp_packet_fb_get_fci_length (packet) == 0)
3527 gst_rtcp_packet_remove (packet);
3529 data->may_suppress = FALSE;
3533 has_pli_compare_func (gconstpointer a, gconstpointer ignored)
3535 GstRTCPPacket packet;
3536 GstRTCPBuffer rtcp = { NULL, };
3537 gboolean ret = FALSE;
3539 gst_rtcp_buffer_map ((GstBuffer *) a, GST_MAP_READ, &rtcp);
3541 if (gst_rtcp_buffer_get_first_packet (&rtcp, &packet)) {
3542 if (gst_rtcp_packet_get_type (&packet) == GST_RTCP_TYPE_PSFB &&
3543 gst_rtcp_packet_fb_get_type (&packet) == GST_RTCP_PSFB_TYPE_PLI)
3547 gst_rtcp_buffer_unmap (&rtcp);
3554 session_pli (const gchar * key, RTPSource * source, ReportData * data)
3556 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3557 GstRTCPPacket *packet = &data->packet;
3559 if (!source->send_pli)
3562 if (rtp_source_has_retained (source, has_pli_compare_func, NULL))
3565 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
3566 /* exit because the packet is full, will put next request in a
3570 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_PLI);
3571 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3572 gst_rtcp_packet_fb_set_media_ssrc (packet, source->ssrc);
3574 source->send_pli = FALSE;
3575 data->may_suppress = FALSE;
3577 source->stats.sent_pli_count++;
3580 /* construct NACK */
3582 session_nack (const gchar * key, RTPSource * source, ReportData * data)
3584 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3585 GstRTCPPacket *packet = &data->packet;
3587 GstClockTime *nack_deadlines;
3588 guint n_nacks, i = 0;
3589 guint nacked_seqnums = 0;
3590 guint16 n_fb_nacks = 0;
3593 if (!source->send_nack)
3596 nacks = rtp_source_get_nacks (source, &n_nacks);
3597 nack_deadlines = rtp_source_get_nack_deadlines (source, NULL);
3598 GST_DEBUG ("%u NACKs current time %" GST_TIME_FORMAT, n_nacks,
3599 GST_TIME_ARGS (data->current_time));
3601 /* cleanup expired nacks */
3602 for (i = 0; i < n_nacks; i++) {
3603 GST_DEBUG ("#%u deadline %" GST_TIME_FORMAT, nacks[i],
3604 GST_TIME_ARGS (nack_deadlines[i]));
3605 if (nack_deadlines[i] >= data->current_time)
3609 GST_WARNING ("Removing %u expired NACKS", i);
3610 rtp_source_clear_nacks (source, i);
3616 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RTPFB, packet))
3617 /* exit because the packet is full, will put next request in a
3621 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_RTPFB_TYPE_NACK);
3622 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3623 gst_rtcp_packet_fb_set_media_ssrc (packet, source->ssrc);
3625 if (!gst_rtcp_packet_fb_set_fci_length (packet, 1)) {
3626 gst_rtcp_packet_remove (packet);
3627 GST_WARNING ("no nacks fit in the packet");
3631 fci_data = gst_rtcp_packet_fb_get_fci (packet);
3632 for (i = 0; i < n_nacks; i = nacked_seqnums) {
3633 guint16 seqnum = nacks[i];
3637 if (!gst_rtcp_packet_fb_set_fci_length (packet, n_fb_nacks + 1))
3643 for (j = i + 1; j < n_nacks; j++) {
3646 diff = gst_rtp_buffer_compare_seqnum (seqnum, nacks[j]);
3647 GST_TRACE ("[%u][%u] %u %u diff %i", i, j, seqnum, nacks[j], diff);
3651 blp |= 1 << (diff - 1);
3655 GST_WRITE_UINT32_BE (fci_data, seqnum << 16 | blp);
3659 data->nacked_seqnums += nacked_seqnums;
3660 rtp_source_clear_nacks (source, nacked_seqnums);
3661 data->may_suppress = FALSE;
3662 source->stats.sent_nack_count += n_fb_nacks;
3664 GST_DEBUG ("Sent %u seqnums into %u FB NACKs", nacked_seqnums, n_fb_nacks);
3667 /* perform cleanup of sources that timed out */
3669 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
3671 gboolean remove = FALSE;
3672 gboolean byetimeout = FALSE;
3673 gboolean sendertimeout = FALSE;
3674 gboolean is_sender, is_active;
3675 RTPSession *sess = data->sess;
3676 GstClockTime interval, binterval;
3679 GST_DEBUG ("look at %08x, generation %u", source->ssrc, source->generation);
3681 /* check for outdated collisions */
3682 if (source->internal) {
3683 GST_DEBUG ("Timing out collisions for %x", source->ssrc);
3684 rtp_source_timeout (source, data->current_time, data->running_time,
3685 sess->rtcp_feedback_retention_window);
3688 /* nothing else to do when without RTCP */
3689 if (data->interval == GST_CLOCK_TIME_NONE)
3692 is_sender = RTP_SOURCE_IS_SENDER (source);
3693 is_active = RTP_SOURCE_IS_ACTIVE (source);
3695 /* our own rtcp interval may have been forced low by secondary configuration,
3696 * while sender side may still operate with higher interval,
3697 * so do not just take our interval to decide on timing out sender,
3698 * but take (if data->interval <= 5 * GST_SECOND):
3699 * interval = CLAMP (sender_interval, data->interval, 5 * GST_SECOND)
3700 * where sender_interval is difference between last 2 received RTCP reports
3702 if (data->interval >= 5 * GST_SECOND || source->internal) {
3703 binterval = data->interval;
3705 GST_LOG ("prev_rtcp %" GST_TIME_FORMAT ", last_rtcp %" GST_TIME_FORMAT,
3706 GST_TIME_ARGS (source->stats.prev_rtcptime),
3707 GST_TIME_ARGS (source->stats.last_rtcptime));
3708 /* if not received enough yet, fallback to larger default */
3709 if (source->stats.last_rtcptime > source->stats.prev_rtcptime)
3710 binterval = source->stats.last_rtcptime - source->stats.prev_rtcptime;
3712 binterval = 5 * GST_SECOND;
3713 binterval = CLAMP (binterval, data->interval, 5 * GST_SECOND);
3715 GST_LOG ("timeout base interval %" GST_TIME_FORMAT,
3716 GST_TIME_ARGS (binterval));
3718 if (!source->internal && source->marked_bye) {
3719 /* if we received a BYE from the source, remove the source after some
3721 if (data->current_time > source->bye_time &&
3722 data->current_time - source->bye_time > sess->stats.bye_timeout) {
3723 GST_DEBUG ("removing BYE source %08x", source->ssrc);
3729 if (source->internal && source->sent_bye) {
3730 GST_DEBUG ("removing internal source that has sent BYE %08x", source->ssrc);
3734 /* sources that were inactive for more than 5 times the deterministic reporting
3735 * interval get timed out. the min timeout is 5 seconds. */
3736 /* mind old time that might pre-date last time going to PLAYING */
3737 btime = MAX (source->last_activity, sess->start_time);
3738 if (data->current_time > btime) {
3739 interval = MAX (binterval * 5, 5 * GST_SECOND);
3740 if (data->current_time - btime > interval) {
3741 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
3742 source->ssrc, GST_TIME_ARGS (btime));
3743 if (source->internal) {
3744 /* this is an internal source that is not using our suggested ssrc.
3745 * since there must be another source using this ssrc, we can remove
3746 * this one instead of making it a receiver forever */
3747 if (source->ssrc != sess->suggested_ssrc) {
3748 rtp_source_mark_bye (source, "timed out");
3749 /* do not schedule bye here, since we are inside the RTCP timeout
3750 * processing and scheduling bye will interfere with SR/RR sending */
3758 /* senders that did not send for a long time become a receiver, this also
3759 * holds for our own sources. */
3761 /* mind old time that might pre-date last time going to PLAYING */
3762 btime = MAX (source->last_rtp_activity, sess->start_time);
3763 if (data->current_time > btime) {
3764 interval = MAX (binterval * 2, 5 * GST_SECOND);
3765 if (data->current_time - btime > interval) {
3766 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
3767 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
3768 sendertimeout = TRUE;
3774 sess->total_sources--;
3776 sess->stats.sender_sources--;
3777 if (source->internal)
3778 sess->stats.internal_sender_sources--;
3781 sess->stats.active_sources--;
3783 if (source->internal)
3784 sess->stats.internal_sources--;
3787 on_bye_timeout (sess, source);
3789 on_timeout (sess, source);
3791 if (sendertimeout) {
3792 source->is_sender = FALSE;
3793 sess->stats.sender_sources--;
3794 if (source->internal)
3795 sess->stats.internal_sender_sources--;
3797 on_sender_timeout (sess, source);
3799 /* count how many source to report in this generation */
3800 if (((gint16) (source->generation - sess->generation)) <= 0)
3801 data->num_to_report++;
3803 source->closing = remove;
3807 session_sdes (RTPSession * sess, ReportData * data)
3809 GstRTCPPacket *packet = &data->packet;
3810 const GstStructure *sdes;
3812 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3814 /* add SDES packet */
3815 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SDES, packet);
3817 gst_rtcp_packet_sdes_add_item (packet, data->source->ssrc);
3819 sdes = rtp_source_get_sdes_struct (data->source);
3821 /* add all fields in the structure, the order is not important. */
3822 n_fields = gst_structure_n_fields (sdes);
3823 for (i = 0; i < n_fields; ++i) {
3826 GstRTCPSDESType type;
3828 field = gst_structure_nth_field_name (sdes, i);
3831 value = gst_structure_get_string (sdes, field);
3834 type = gst_rtcp_sdes_name_to_type (field);
3836 /* Early packets are minimal and only include the CNAME */
3837 if (data->is_early && type != GST_RTCP_SDES_CNAME)
3840 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
3841 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
3842 (const guint8 *) value);
3843 } else if (type == GST_RTCP_SDES_PRIV) {
3849 /* don't accept entries that are too big */
3850 prefix_len = strlen (field);
3851 if (prefix_len > 255)
3853 value_len = strlen (value);
3854 if (value_len > 255)
3856 data_len = 1 + prefix_len + value_len;
3860 data[0] = prefix_len;
3861 memcpy (&data[1], field, prefix_len);
3862 memcpy (&data[1 + prefix_len], value, value_len);
3864 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
3868 data->has_sdes = TRUE;
3871 /* schedule a BYE packet */
3873 make_source_bye (RTPSession * sess, RTPSource * source, ReportData * data)
3875 GstRTCPPacket *packet = &data->packet;
3876 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3879 session_sdes (sess, data);
3880 /* add a BYE packet */
3881 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_BYE, packet);
3882 gst_rtcp_packet_bye_add_ssrc (packet, source->ssrc);
3883 if (source->bye_reason)
3884 gst_rtcp_packet_bye_set_reason (packet, source->bye_reason);
3886 /* we have a BYE packet now */
3887 source->sent_bye = TRUE;
3891 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
3893 GstClockTime new_send_time;
3894 GstClockTime interval;
3895 RTPSessionStats *stats;
3897 if (sess->scheduled_bye)
3898 stats = &sess->bye_stats;
3900 stats = &sess->stats;
3902 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3903 data->is_early = TRUE;
3905 data->is_early = FALSE;
3907 if (data->is_early && sess->next_early_rtcp_time <= current_time) {
3908 GST_DEBUG ("early feedback %" GST_TIME_FORMAT " <= now %"
3909 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_early_rtcp_time),
3910 GST_TIME_ARGS (current_time));
3911 } else if (sess->next_rtcp_check_time == GST_CLOCK_TIME_NONE ||
3912 sess->next_rtcp_check_time > current_time) {
3913 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
3914 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
3915 GST_TIME_ARGS (current_time));
3919 /* take interval and add jitter */
3920 interval = data->interval;
3921 if (interval != GST_CLOCK_TIME_NONE)
3922 interval = rtp_stats_add_rtcp_jitter (stats, interval);
3924 if (sess->last_rtcp_check_time != GST_CLOCK_TIME_NONE) {
3925 /* perform forward reconsideration */
3926 if (interval != GST_CLOCK_TIME_NONE) {
3927 GstClockTime elapsed;
3929 /* get elapsed time since we last reported */
3930 elapsed = current_time - sess->last_rtcp_check_time;
3932 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
3933 GST_TIME_FORMAT, GST_TIME_ARGS (interval), GST_TIME_ARGS (elapsed));
3934 new_send_time = interval + sess->last_rtcp_check_time;
3936 new_send_time = sess->last_rtcp_check_time;
3939 /* If this is the first RTCP packet, we can reconsider anything based
3940 * on the last RTCP send time because there was none.
3942 g_warn_if_fail (!data->is_early);
3943 data->is_early = FALSE;
3944 new_send_time = current_time;
3947 if (!data->is_early) {
3948 /* check if reconsideration */
3949 if (new_send_time == GST_CLOCK_TIME_NONE || current_time < new_send_time) {
3950 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
3951 GST_TIME_ARGS (new_send_time));
3952 /* store new check time */
3953 sess->next_rtcp_check_time = new_send_time;
3954 sess->last_rtcp_interval = interval;
3958 sess->last_rtcp_interval = interval;
3959 if ((sess->rtp_profile == GST_RTP_PROFILE_AVPF
3960 || sess->rtp_profile == GST_RTP_PROFILE_SAVPF)
3961 && interval != GST_CLOCK_TIME_NONE) {
3962 /* Apply the rules from RFC 4585 section 3.5.3 */
3963 if (stats->min_interval != 0 && !sess->first_rtcp) {
3964 GstClockTime T_rr_current_interval =
3965 g_random_double_range (0.5, 1.5) * stats->min_interval * GST_SECOND;
3967 if (T_rr_current_interval > interval) {
3968 GST_DEBUG ("Adjusting interval for t-rr-interval: %" GST_TIME_FORMAT
3969 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (T_rr_current_interval),
3970 GST_TIME_ARGS (interval));
3971 interval = T_rr_current_interval;
3975 sess->next_rtcp_check_time = current_time + interval;
3979 GST_DEBUG ("can send RTCP now, next %" GST_TIME_FORMAT,
3980 GST_TIME_ARGS (sess->next_rtcp_check_time));
3986 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
3988 g_hash_table_insert (hash_table, key, g_object_ref (source));
3992 remove_closing_sources (const gchar * key, RTPSource * source,
3995 if (source->closing)
3998 if (source->send_fir)
3999 data->have_fir = TRUE;
4000 if (source->send_pli)
4001 data->have_pli = TRUE;
4002 if (source->send_nack)
4003 data->have_nack = TRUE;
4009 generate_rtcp (const gchar * key, RTPSource * source, ReportData * data)
4011 RTPSession *sess = data->sess;
4012 gboolean is_bye = FALSE;
4013 ReportOutput *output;
4015 /* only generate RTCP for active internal sources */
4016 if (!source->internal || source->sent_bye)
4019 /* ignore other sources when we do the timeout after a scheduled BYE */
4020 if (sess->scheduled_bye && !source->marked_bye)
4023 /* skip if RTCP is disabled */
4024 if (source->disable_rtcp) {
4025 GST_DEBUG ("source %08x has RTCP disabled", source->ssrc);
4029 data->source = source;
4032 session_start_rtcp (sess, data);
4034 if (source->marked_bye) {
4036 make_source_bye (sess, source, data);
4038 } else if (!data->is_early) {
4039 /* loop over all known sources and add report blocks. If we are early, we
4040 * just make a minimal RTCP packet and skip this step */
4041 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4042 (GHFunc) session_report_blocks, data);
4044 if (!data->has_sdes && (!data->is_early || !sess->reduced_size_rtcp))
4045 session_sdes (sess, data);
4048 session_fir (sess, data);
4051 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4052 (GHFunc) session_pli, data);
4054 if (data->have_nack)
4055 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4056 (GHFunc) session_nack, data);
4058 gst_rtcp_buffer_unmap (&data->rtcpbuf);
4060 output = g_slice_new (ReportOutput);
4061 output->source = g_object_ref (source);
4062 output->is_bye = is_bye;
4063 output->buffer = data->rtcp;
4064 /* queue the RTCP packet to push later */
4065 g_queue_push_tail (&data->output, output);
4069 update_generation (const gchar * key, RTPSource * source, ReportData * data)
4071 RTPSession *sess = data->sess;
4073 if (g_hash_table_size (source->reported_in_sr_of) >=
4074 sess->stats.internal_sources) {
4075 /* source is reported, move to next generation */
4076 source->generation = sess->generation + 1;
4077 g_hash_table_remove_all (source->reported_in_sr_of);
4079 GST_LOG ("reported source %x, new generation: %d", source->ssrc,
4080 source->generation);
4082 /* if we reported all sources in this generation, move to next */
4083 if (--data->num_to_report == 0) {
4085 GST_DEBUG ("all reported, generation now %u", sess->generation);
4091 schedule_remaining_nacks (const gchar * key, RTPSource * source,
4094 RTPSession *sess = data->sess;
4095 GstClockTime *nack_deadlines;
4096 GstClockTime deadline;
4099 if (!source->send_nack)
4102 /* the scheduling is entirely based on available bandwidth, just take the
4103 * biggest seqnum, which will have the largest deadline to request early
4105 nack_deadlines = rtp_source_get_nack_deadlines (source, &n_nacks);
4106 deadline = nack_deadlines[n_nacks - 1];
4107 RTP_SESSION_UNLOCK (sess);
4108 rtp_session_send_rtcp_with_deadline (sess, deadline);
4109 RTP_SESSION_LOCK (sess);
4113 rtp_session_are_all_sources_bye (RTPSession * sess)
4115 GHashTableIter iter;
4118 RTP_SESSION_LOCK (sess);
4119 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
4120 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
4121 if (src->internal && !src->sent_bye) {
4122 RTP_SESSION_UNLOCK (sess);
4126 RTP_SESSION_UNLOCK (sess);
4132 * rtp_session_on_timeout:
4133 * @sess: an #RTPSession
4134 * @current_time: the current system time
4135 * @ntpnstime: the current NTP time in nanoseconds
4136 * @running_time: the current running_time of the pipeline
4138 * Perform maintenance actions after the timeout obtained with
4139 * rtp_session_next_timeout() expired.
4141 * This function will perform timeouts of receivers and senders, send a BYE
4142 * packet or generate RTCP packets with current session stats.
4144 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
4145 * times, for each packet that should be processed.
4147 * Returns: a #GstFlowReturn.
4150 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
4151 guint64 ntpnstime, GstClockTime running_time)
4153 GstFlowReturn result = GST_FLOW_OK;
4154 ReportData data = { GST_RTCP_BUFFER_INIT };
4155 GHashTable *table_copy;
4156 ReportOutput *output;
4157 gboolean all_empty = FALSE;
4159 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
4161 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT
4162 ", running-time %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
4163 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (running_time));
4166 data.current_time = current_time;
4167 data.ntpnstime = ntpnstime;
4168 data.running_time = running_time;
4169 data.num_to_report = 0;
4170 data.may_suppress = FALSE;
4171 data.nacked_seqnums = 0;
4172 g_queue_init (&data.output);
4174 RTP_SESSION_LOCK (sess);
4175 /* get a new interval, we need this for various cleanups etc */
4176 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
4178 GST_DEBUG ("interval %" GST_TIME_FORMAT, GST_TIME_ARGS (data.interval));
4180 /* we need an internal source now */
4181 if (sess->stats.internal_sources == 0) {
4185 source = obtain_internal_source (sess, sess->suggested_ssrc, &created,
4187 sess->internal_ssrc_set = TRUE;
4190 on_new_sender_ssrc (sess, source);
4192 g_object_unref (source);
4195 sess->conflicting_addresses =
4196 timeout_conflicting_addresses (sess->conflicting_addresses, current_time);
4198 /* Make a local copy of the hashtable. We need to do this because the
4199 * cleanup stage below releases the session lock. */
4200 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
4201 (GDestroyNotify) g_object_unref);
4202 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4203 (GHFunc) clone_ssrcs_hashtable, table_copy);
4205 /* Clean up the session, mark the source for removing, this might release the
4207 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
4208 g_hash_table_destroy (table_copy);
4210 /* Now remove the marked sources */
4211 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
4212 (GHRFunc) remove_closing_sources, &data);
4214 /* update point-to-point status */
4215 session_update_ptp (sess);
4217 /* see if we need to generate SR or RR packets */
4218 if (!is_rtcp_time (sess, current_time, &data))
4221 /* check if all the buffers are empty afer generation */
4225 ("doing RTCP generation %u for %u sources, early %d, may suppress %d",
4226 sess->generation, data.num_to_report, data.is_early, data.may_suppress);
4228 /* generate RTCP for all internal sources */
4229 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4230 (GHFunc) generate_rtcp, &data);
4232 /* update the generation for all the sources that have been reported */
4233 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4234 (GHFunc) update_generation, &data);
4236 /* we keep track of the last report time in order to timeout inactive
4237 * receivers or senders */
4238 if (!data.is_early) {
4239 GST_DEBUG ("Time since last regular RTCP: %" GST_TIME_FORMAT " - %"
4240 GST_TIME_FORMAT " = %" GST_TIME_FORMAT,
4241 GST_TIME_ARGS (data.current_time),
4242 GST_TIME_ARGS (sess->last_rtcp_send_time),
4243 GST_TIME_ARGS (data.current_time - sess->last_rtcp_send_time));
4244 sess->last_rtcp_send_time = data.current_time;
4247 GST_DEBUG ("Time since last RTCP: %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT
4248 " = %" GST_TIME_FORMAT, GST_TIME_ARGS (data.current_time),
4249 GST_TIME_ARGS (sess->last_rtcp_check_time),
4250 GST_TIME_ARGS (data.current_time - sess->last_rtcp_check_time));
4251 sess->last_rtcp_check_time = data.current_time;
4252 sess->first_rtcp = FALSE;
4253 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
4254 sess->scheduled_bye = FALSE;
4257 RTP_SESSION_UNLOCK (sess);
4259 /* notify about updated statistics */
4260 g_object_notify (G_OBJECT (sess), "stats");
4262 /* push out the RTCP packets */
4263 while ((output = g_queue_pop_head (&data.output))) {
4264 gboolean do_not_suppress, empty_buffer;
4265 GstBuffer *buffer = output->buffer;
4266 RTPSource *source = output->source;
4268 /* Give the user a change to add its own packet */
4269 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
4270 buffer, data.is_early, &do_not_suppress);
4272 empty_buffer = gst_buffer_get_size (buffer) == 0;
4277 if (sess->callbacks.send_rtcp &&
4278 !empty_buffer && (do_not_suppress || !data.may_suppress)) {
4281 packet_size = gst_buffer_get_size (buffer) + sess->header_len;
4283 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
4284 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
4285 sess->stats.avg_rtcp_packet_size, packet_size);
4287 sess->callbacks.send_rtcp (sess, source, buffer,
4288 rtp_session_are_all_sources_bye (sess), sess->send_rtcp_user_data);
4290 RTP_SESSION_LOCK (sess);
4291 sess->stats.nacks_sent += data.nacked_seqnums;
4292 on_sender_ssrc_active (sess, source);
4293 RTP_SESSION_UNLOCK (sess);
4295 GST_DEBUG ("freeing packet callback: %p"
4296 " empty_buffer: %d, "
4297 " do_not_suppress: %d may_suppress: %d", sess->callbacks.send_rtcp,
4298 empty_buffer, do_not_suppress, data.may_suppress);
4299 if (!empty_buffer) {
4300 RTP_SESSION_LOCK (sess);
4301 sess->stats.nacks_dropped += data.nacked_seqnums;
4302 RTP_SESSION_UNLOCK (sess);
4304 gst_buffer_unref (buffer);
4306 g_object_unref (source);
4307 g_slice_free (ReportOutput, output);
4311 GST_ERROR ("generated empty RTCP messages for all the sources");
4313 /* schedule remaining nacks */
4314 RTP_SESSION_LOCK (sess);
4315 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4316 (GHFunc) schedule_remaining_nacks, &data);
4317 RTP_SESSION_UNLOCK (sess);
4323 * rtp_session_request_early_rtcp:
4324 * @sess: an #RTPSession
4325 * @current_time: the current system time
4326 * @max_delay: maximum delay
4328 * Request transmission of early RTCP
4330 * Returns: %TRUE if the related RTCP can be scheduled.
4333 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
4334 GstClockTime max_delay)
4336 GstClockTime T_dither_max, T_rr, offset = 0;
4338 gboolean allow_early;
4340 /* Implements the algorithm described in RFC 4585 section 3.5.2 */
4342 RTP_SESSION_LOCK (sess);
4344 /* We assume a feedback profile if something is requesting RTCP
4346 sess->rtp_profile = GST_RTP_PROFILE_AVPF;
4348 /* Check if already requested */
4349 /* RFC 4585 section 3.5.2 step 2 */
4350 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
4351 GST_LOG_OBJECT (sess, "already have next early rtcp time");
4352 ret = (current_time + max_delay > sess->next_early_rtcp_time);
4356 if (!GST_CLOCK_TIME_IS_VALID (sess->next_rtcp_check_time)) {
4357 GST_LOG_OBJECT (sess, "no next RTCP check time");
4362 /* RFC 4585 section 3.5.3 step 1
4363 * If no regular RTCP packet has been sent before, then a regular
4364 * RTCP packet has to be scheduled first and FB messages might be
4367 if (!GST_CLOCK_TIME_IS_VALID (sess->last_rtcp_send_time)) {
4368 GST_LOG_OBJECT (sess, "no RTCP sent yet");
4370 if (current_time + max_delay > sess->next_rtcp_check_time) {
4371 GST_LOG_OBJECT (sess,
4372 "next scheduled time is soon %" GST_TIME_FORMAT " + %" GST_TIME_FORMAT
4373 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
4374 GST_TIME_ARGS (max_delay),
4375 GST_TIME_ARGS (sess->next_rtcp_check_time));
4378 GST_LOG_OBJECT (sess,
4379 "can't allow early feedback, next scheduled time is too late %"
4380 GST_TIME_FORMAT " + %" GST_TIME_FORMAT " < %" GST_TIME_FORMAT,
4381 GST_TIME_ARGS (current_time), GST_TIME_ARGS (max_delay),
4382 GST_TIME_ARGS (sess->next_rtcp_check_time));
4388 T_rr = sess->last_rtcp_interval;
4390 /* RFC 4585 section 3.5.2 step 2b */
4391 /* If the total sources is <=2, then there is only us and one peer */
4392 /* When there is one auxiliary stream the session can still do point
4395 if (sess->is_doing_ptp) {
4398 /* Divide by 2 because l = 0.5 */
4399 T_dither_max = T_rr;
4403 /* RFC 4585 section 3.5.2 step 3 */
4404 if (current_time + T_dither_max > sess->next_rtcp_check_time) {
4405 GST_LOG_OBJECT (sess,
4406 "don't send because of dither, next scheduled time is too soon %"
4407 GST_TIME_FORMAT " + %" GST_TIME_FORMAT " > %" GST_TIME_FORMAT,
4408 GST_TIME_ARGS (current_time), GST_TIME_ARGS (T_dither_max),
4409 GST_TIME_ARGS (sess->next_rtcp_check_time));
4410 ret = T_dither_max <= max_delay;
4414 /* RFC 4585 section 3.5.2 step 4a and
4415 * RFC 4585 section 3.5.2 step 6 */
4416 allow_early = FALSE;
4417 if (sess->last_rtcp_check_time == sess->last_rtcp_send_time) {
4418 /* Last time we sent a full RTCP packet, we can now immediately
4419 * send an early one as allow_early was reset to TRUE */
4421 } else if (sess->last_rtcp_check_time + T_rr <= current_time + max_delay) {
4422 /* Last packet we sent was an early RTCP packet and more than
4423 * T_rr has passed since then, meaning we would have suppressed
4424 * a regular RTCP packet already and reset allow_early to TRUE */
4427 /* We have to offset a bit as T_rr has not passed yet, but will before
4429 if (sess->last_rtcp_check_time + T_rr > current_time)
4430 offset = (sess->last_rtcp_check_time + T_rr) - current_time;
4432 GST_DEBUG_OBJECT (sess,
4433 "can't allow early RTCP yet: last regular %" GST_TIME_FORMAT ", %"
4434 GST_TIME_FORMAT " + %" GST_TIME_FORMAT " > %" GST_TIME_FORMAT " + %"
4435 GST_TIME_FORMAT, GST_TIME_ARGS (sess->last_rtcp_send_time),
4436 GST_TIME_ARGS (sess->last_rtcp_check_time), GST_TIME_ARGS (T_rr),
4437 GST_TIME_ARGS (current_time), GST_TIME_ARGS (max_delay));
4441 /* Ignore the request a scheduled packet will be in time anyway */
4442 if (current_time + max_delay > sess->next_rtcp_check_time) {
4443 GST_LOG_OBJECT (sess,
4444 "next scheduled time is soon %" GST_TIME_FORMAT " + %" GST_TIME_FORMAT
4445 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
4446 GST_TIME_ARGS (max_delay),
4447 GST_TIME_ARGS (sess->next_rtcp_check_time));
4450 GST_LOG_OBJECT (sess,
4451 "can't allow early feedback and next scheduled time is too late %"
4452 GST_TIME_FORMAT " + %" GST_TIME_FORMAT " < %" GST_TIME_FORMAT,
4453 GST_TIME_ARGS (current_time), GST_TIME_ARGS (max_delay),
4454 GST_TIME_ARGS (sess->next_rtcp_check_time));
4460 /* RFC 4585 section 3.5.2 step 4b */
4462 /* Schedule an early transmission later */
4463 sess->next_early_rtcp_time = g_random_double () * T_dither_max +
4464 current_time + offset;
4466 /* If no dithering, schedule it for NOW */
4467 sess->next_early_rtcp_time = current_time + offset;
4470 GST_LOG_OBJECT (sess, "next early RTCP time %" GST_TIME_FORMAT
4471 ", next regular RTCP time %" GST_TIME_FORMAT,
4472 GST_TIME_ARGS (sess->next_early_rtcp_time),
4473 GST_TIME_ARGS (sess->next_rtcp_check_time));
4474 RTP_SESSION_UNLOCK (sess);
4476 /* notify app of need to send packet early
4477 * and therefore of timeout change */
4478 if (sess->callbacks.reconsider)
4479 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
4485 RTP_SESSION_UNLOCK (sess);
4491 rtp_session_send_rtcp_internal (RTPSession * sess, GstClockTime now,
4492 GstClockTime max_delay)
4494 /* notify the application that we intend to send early RTCP */
4495 if (sess->callbacks.notify_early_rtcp)
4496 sess->callbacks.notify_early_rtcp (sess, sess->notify_early_rtcp_user_data);
4498 return rtp_session_request_early_rtcp (sess, now, max_delay);
4502 rtp_session_send_rtcp_with_deadline (RTPSession * sess, GstClockTime deadline)
4504 GstClockTime now, max_delay;
4506 if (!sess->callbacks.send_rtcp)
4509 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
4514 max_delay = deadline - now;
4516 return rtp_session_send_rtcp_internal (sess, now, max_delay);
4520 rtp_session_send_rtcp (RTPSession * sess, GstClockTime max_delay)
4524 if (!sess->callbacks.send_rtcp)
4527 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
4529 return rtp_session_send_rtcp_internal (sess, now, max_delay);
4533 rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc,
4534 gboolean fir, gint count)
4538 RTP_SESSION_LOCK (sess);
4539 src = find_source (sess, ssrc);
4544 src->send_pli = FALSE;
4545 src->send_fir = TRUE;
4547 if (count == -1 || count != src->last_fir_count)
4548 src->current_send_fir_seqnum++;
4549 src->last_fir_count = count;
4550 } else if (!src->send_fir) {
4551 src->send_pli = TRUE;
4553 RTP_SESSION_UNLOCK (sess);
4555 if (!rtp_session_send_rtcp (sess, 5 * GST_SECOND)) {
4556 GST_DEBUG ("FIR/PLI not sent early, sending with next regular RTCP");
4564 RTP_SESSION_UNLOCK (sess);
4570 * rtp_session_request_nack:
4571 * @sess: a #RTPSession
4573 * @seqnum: the missing seqnum
4574 * @max_delay: max delay to request NACK
4576 * Request scheduling of a NACK feedback packet for @seqnum in @ssrc.
4578 * Returns: %TRUE if the NACK feedback could be scheduled
4581 rtp_session_request_nack (RTPSession * sess, guint32 ssrc, guint16 seqnum,
4582 GstClockTime max_delay)
4587 if (!sess->callbacks.send_rtcp)
4590 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
4592 RTP_SESSION_LOCK (sess);
4593 source = find_source (sess, ssrc);
4597 GST_DEBUG ("request NACK for SSRC %08x, #%u, deadline %" GST_TIME_FORMAT,
4598 ssrc, seqnum, GST_TIME_ARGS (now + max_delay));
4599 rtp_source_register_nack (source, seqnum, now + max_delay);
4600 RTP_SESSION_UNLOCK (sess);
4602 if (!rtp_session_send_rtcp_internal (sess, now, max_delay)) {
4603 GST_DEBUG ("NACK not sent early, sending with next regular RTCP");
4611 RTP_SESSION_UNLOCK (sess);