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);
125 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
127 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
129 static guint32 rtp_session_create_new_ssrc (RTPSession * sess);
130 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
131 gboolean * created, RTPPacketInfo * pinfo, gboolean rtp);
132 static RTPSource *obtain_internal_source (RTPSession * sess,
133 guint32 ssrc, gboolean * created, GstClockTime current_time);
134 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
135 GstClockTime current_time);
136 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
137 gboolean deterministic, gboolean first);
140 accumulate_trues (GSignalInvocationHint * ihint, GValue * return_accu,
141 const GValue * handler_return, gpointer data)
143 if (g_value_get_boolean (handler_return))
144 g_value_set_boolean (return_accu, TRUE);
150 rtp_session_class_init (RTPSessionClass * klass)
152 GObjectClass *gobject_class;
154 gobject_class = (GObjectClass *) klass;
156 gobject_class->finalize = rtp_session_finalize;
157 gobject_class->set_property = rtp_session_set_property;
158 gobject_class->get_property = rtp_session_get_property;
161 * RTPSession::get-source-by-ssrc:
162 * @session: the object which received the signal
163 * @ssrc: the SSRC of the RTPSource
165 * Request the #RTPSource object with SSRC @ssrc in @session.
167 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
168 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
169 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
170 get_source_by_ssrc), NULL, NULL, g_cclosure_marshal_generic,
171 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
174 * RTPSession::on-new-ssrc:
175 * @session: the object which received the signal
176 * @src: the new RTPSource
178 * Notify of a new SSRC that entered @session.
180 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
181 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
182 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
183 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
186 * RTPSession::on-ssrc-collision:
187 * @session: the object which received the signal
188 * @src: the #RTPSource that caused a collision
190 * Notify when we have an SSRC collision
192 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
193 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
194 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
195 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
198 * RTPSession::on-ssrc-validated:
199 * @session: the object which received the signal
200 * @src: the new validated RTPSource
202 * Notify of a new SSRC that became validated.
204 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
205 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
206 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
207 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
210 * RTPSession::on-ssrc-active:
211 * @session: the object which received the signal
212 * @src: the active RTPSource
214 * Notify of a SSRC that is active, i.e., sending RTCP.
216 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
217 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
218 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
219 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
222 * RTPSession::on-ssrc-sdes:
223 * @session: the object which received the signal
224 * @src: the RTPSource
226 * Notify that a new SDES was received for SSRC.
228 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
229 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
230 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
231 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
234 * RTPSession::on-bye-ssrc:
235 * @session: the object which received the signal
236 * @src: the RTPSource that went away
238 * Notify of an SSRC that became inactive because of a BYE packet.
240 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
241 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
242 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
243 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
246 * RTPSession::on-bye-timeout:
247 * @session: the object which received the signal
248 * @src: the RTPSource that timed out
250 * Notify of an SSRC that has timed out because of BYE
252 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
253 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
254 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
255 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
258 * RTPSession::on-timeout:
259 * @session: the object which received the signal
260 * @src: the RTPSource that timed out
262 * Notify of an SSRC that has timed out
264 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
265 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
266 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
267 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
270 * RTPSession::on-sender-timeout:
271 * @session: the object which received the signal
272 * @src: the RTPSource that timed out
274 * Notify of an SSRC that was a sender but timed out and became a receiver.
276 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
277 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
278 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
279 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
283 * RTPSession::on-sending-rtcp
284 * @session: the object which received the signal
285 * @buffer: the #GstBuffer containing the RTCP packet about to be sent
286 * @early: %TRUE if the packet is early, %FALSE if it is regular
288 * This signal is emitted before sending an RTCP packet, it can be used
289 * to add extra RTCP Packets.
291 * Returns: %TRUE if the RTCP buffer should NOT be suppressed, %FALSE
292 * if suppressing it is acceptable
294 rtp_session_signals[SIGNAL_ON_SENDING_RTCP] =
295 g_signal_new ("on-sending-rtcp", G_TYPE_FROM_CLASS (klass),
296 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_rtcp),
297 accumulate_trues, NULL, g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2,
298 GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_BOOLEAN);
301 * RTPSession::on-app-rtcp:
302 * @session: the object which received the signal
303 * @subtype: The subtype of the packet
304 * @ssrc: The SSRC/CSRC of the packet
305 * @name: The name of the packet
306 * @data: a #GstBuffer with the application-dependant data or %NULL if
309 * Notify that a RTCP APP packet has been received
311 rtp_session_signals[SIGNAL_ON_APP_RTCP] =
312 g_signal_new ("on-app-rtcp", G_TYPE_FROM_CLASS (klass),
313 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_app_rtcp),
314 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 4,
315 G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING, GST_TYPE_BUFFER);
318 * RTPSession::on-feedback-rtcp:
319 * @session: the object which received the signal
320 * @type: Type of RTCP packet, will be %GST_RTCP_TYPE_RTPFB or
321 * %GST_RTCP_TYPE_RTPFB
322 * @fbtype: The type of RTCP FB packet, probably part of #GstRTCPFBType
323 * @sender_ssrc: The SSRC of the sender
324 * @media_ssrc: The SSRC of the media this refers to
325 * @fci: a #GstBuffer with the FCI data from the FB packet or %NULL if
328 * Notify that a RTCP feedback packet has been received
330 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP] =
331 g_signal_new ("on-feedback-rtcp", G_TYPE_FROM_CLASS (klass),
332 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_feedback_rtcp),
333 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 5, G_TYPE_UINT,
334 G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, GST_TYPE_BUFFER);
337 * RTPSession::send-rtcp:
338 * @session: the object which received the signal
339 * @max_delay: The maximum delay after which the feedback will not be useful
342 * Requests that the #RTPSession initiate a new RTCP packet as soon as
343 * possible within the requested delay.
345 * This sets feedback to %TRUE if not already done before.
347 rtp_session_signals[SIGNAL_SEND_RTCP] =
348 g_signal_new ("send-rtcp", G_TYPE_FROM_CLASS (klass),
349 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
350 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
351 g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_UINT64);
354 * RTPSession::send-rtcp-full:
355 * @session: the object which received the signal
356 * @max_delay: The maximum delay after which the feedback will not be useful
359 * Requests that the #RTPSession initiate a new RTCP packet as soon as
360 * possible within the requested delay.
362 * This sets feedback to %TRUE if not already done before.
364 * Returns: TRUE if the new RTCP packet could be scheduled within the
365 * requested delay, FALSE otherwise.
369 rtp_session_signals[SIGNAL_SEND_RTCP_FULL] =
370 g_signal_new ("send-rtcp-full", G_TYPE_FROM_CLASS (klass),
371 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
372 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
373 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 1, G_TYPE_UINT64);
376 * RTPSession::on-receiving-rtcp
377 * @session: the object which received the signal
378 * @buffer: the #GstBuffer containing the RTCP packet that was received
380 * This signal is emitted when receiving an RTCP packet before it is handled
381 * by the session. It can be used to extract custom information from RTCP packets.
385 rtp_session_signals[SIGNAL_ON_RECEIVING_RTCP] =
386 g_signal_new ("on-receiving-rtcp", G_TYPE_FROM_CLASS (klass),
387 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_receiving_rtcp),
388 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
389 GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE);
392 * RTPSession::on-new-sender-ssrc:
393 * @session: the object which received the signal
394 * @src: the new sender RTPSource
396 * Notify of a new sender SSRC that entered @session.
400 rtp_session_signals[SIGNAL_ON_NEW_SENDER_SSRC] =
401 g_signal_new ("on-new-sender-ssrc", G_TYPE_FROM_CLASS (klass),
402 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_sender_ssrc),
403 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
407 * RTPSession::on-sender-ssrc-active:
408 * @session: the object which received the signal
409 * @src: the active sender RTPSource
411 * Notify of a sender SSRC that is active, i.e., sending RTCP.
415 rtp_session_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE] =
416 g_signal_new ("on-sender-ssrc-active", G_TYPE_FROM_CLASS (klass),
417 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass,
418 on_sender_ssrc_active), NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
419 G_TYPE_NONE, 1, RTP_TYPE_SOURCE);
421 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
422 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
423 "The internal SSRC used for the session (deprecated)",
424 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
426 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
427 g_param_spec_object ("internal-source", "Internal Source",
428 "The internal source element of the session (deprecated)",
429 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
431 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
432 g_param_spec_double ("bandwidth", "Bandwidth",
433 "The bandwidth of the session in bits per second (0 for auto-discover)",
434 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
435 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
437 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
438 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
439 "The fraction of the bandwidth used for RTCP in bits per second (or as a real fraction of the RTP bandwidth if < 1)",
440 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
441 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
443 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
444 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
445 "The RTCP bandwidth used for receivers in bits per second (-1 = default)",
446 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
447 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
449 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
450 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
451 "The RTCP bandwidth used for senders in bits per second (-1 = default)",
452 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
453 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
455 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
456 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
457 "The maximum size of the RTCP packets",
458 16, G_MAXINT16, DEFAULT_RTCP_MTU,
459 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
461 g_object_class_install_property (gobject_class, PROP_SDES,
462 g_param_spec_boxed ("sdes", "SDES",
463 "The SDES items of this session",
464 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
466 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
467 g_param_spec_uint ("num-sources", "Num Sources",
468 "The number of sources in the session", 0, G_MAXUINT,
469 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
471 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
472 g_param_spec_uint ("num-active-sources", "Num Active Sources",
473 "The number of active sources in the session", 0, G_MAXUINT,
474 DEFAULT_NUM_ACTIVE_SOURCES,
475 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
479 * Get a GValue Array of all sources in the session.
482 * <title>Getting the #RTPSources of a session
489 * g_object_get (sess, "sources", &arr, NULL);
491 * for (i = 0; i < arr->n_values; i++) {
494 * val = g_value_array_get_nth (arr, i);
495 * source = g_value_get_object (val);
497 * g_value_array_free (arr);
502 g_object_class_install_property (gobject_class, PROP_SOURCES,
503 g_param_spec_boxed ("sources", "Sources",
504 "An array of all known sources in the session",
505 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
507 g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
508 g_param_spec_boolean ("favor-new", "Favor new sources",
509 "Resolve SSRC conflict in favor of new sources", FALSE,
510 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
512 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
513 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
514 "Minimum interval between Regular RTCP packet (in ns)",
515 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
516 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
518 g_object_class_install_property (gobject_class,
519 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
520 g_param_spec_uint64 ("rtcp-feedback-retention-window",
521 "RTCP Feedback retention window",
522 "Duration during which RTCP Feedback packets are retained (in ns)",
523 0, G_MAXUINT64, DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW,
524 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
526 g_object_class_install_property (gobject_class,
527 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
528 g_param_spec_uint ("rtcp-immediate-feedback-threshold",
529 "RTCP Immediate Feedback threshold",
530 "The maximum number of members of a RTP session for which immediate"
531 " feedback is used (DEPRECATED: has no effect and is not needed)",
532 0, G_MAXUINT, DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
533 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
535 g_object_class_install_property (gobject_class, PROP_PROBATION,
536 g_param_spec_uint ("probation", "Number of probations",
537 "Consecutive packet sequence numbers to accept the source",
538 0, G_MAXUINT, DEFAULT_PROBATION,
539 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
541 g_object_class_install_property (gobject_class, PROP_MAX_DROPOUT_TIME,
542 g_param_spec_uint ("max-dropout-time", "Max dropout time",
543 "The maximum time (milliseconds) of missing packets tolerated.",
544 0, G_MAXUINT, DEFAULT_MAX_DROPOUT_TIME,
545 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
547 g_object_class_install_property (gobject_class, PROP_MAX_MISORDER_TIME,
548 g_param_spec_uint ("max-misorder-time", "Max misorder time",
549 "The maximum time (milliseconds) of misordered packets tolerated.",
550 0, G_MAXUINT, DEFAULT_MAX_MISORDER_TIME,
551 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
556 * Various session statistics. This property returns a GstStructure
557 * with name application/x-rtp-session-stats with the following fields:
559 * "rtx-drop-count" G_TYPE_UINT The number of retransmission events
560 * dropped (due to bandwidth constraints)
561 * "sent-nack-count" G_TYPE_UINT Number of NACKs sent
562 * "recv-nack-count" G_TYPE_UINT Number of NACKs received
563 * "source-stats" G_TYPE_BOXED GValueArray of #RTPSource::stats for all
564 * RTP sources (Since 1.8)
568 g_object_class_install_property (gobject_class, PROP_STATS,
569 g_param_spec_boxed ("stats", "Statistics",
570 "Various statistics", GST_TYPE_STRUCTURE,
571 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
573 g_object_class_install_property (gobject_class, PROP_RTP_PROFILE,
574 g_param_spec_enum ("rtp-profile", "RTP Profile",
575 "RTP profile to use for this session", GST_TYPE_RTP_PROFILE,
576 DEFAULT_RTP_PROFILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
578 g_object_class_install_property (gobject_class, PROP_RTCP_REDUCED_SIZE,
579 g_param_spec_boolean ("rtcp-reduced-size", "RTCP Reduced Size",
580 "Use Reduced Size RTCP for feedback packets",
581 DEFAULT_RTCP_REDUCED_SIZE,
582 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
584 klass->get_source_by_ssrc =
585 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
586 klass->send_rtcp = GST_DEBUG_FUNCPTR (rtp_session_send_rtcp);
588 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
592 rtp_session_init (RTPSession * sess)
597 g_mutex_init (&sess->lock);
598 sess->key = g_random_int ();
602 /* TODO: We currently only use the first hash table but this is the
603 * beginning of an implementation for RFC2762
604 for (i = 0; i < 32; i++) {
606 for (i = 0; i < 1; i++) {
608 g_hash_table_new_full (NULL, NULL, NULL,
609 (GDestroyNotify) g_object_unref);
612 rtp_stats_init_defaults (&sess->stats);
613 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
614 rtp_stats_set_min_interval (&sess->stats,
615 (gdouble) DEFAULT_RTCP_MIN_INTERVAL / GST_SECOND);
617 sess->recalc_bandwidth = TRUE;
618 sess->bandwidth = DEFAULT_BANDWIDTH;
619 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
620 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
621 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
623 /* default UDP header length */
624 sess->header_len = 28;
625 sess->mtu = DEFAULT_RTCP_MTU;
627 sess->probation = DEFAULT_PROBATION;
628 sess->max_dropout_time = DEFAULT_MAX_DROPOUT_TIME;
629 sess->max_misorder_time = DEFAULT_MAX_MISORDER_TIME;
631 /* some default SDES entries */
632 sess->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
634 /* we do not want to leak details like the username or hostname here */
635 str = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
636 gst_structure_set (sess->sdes, "cname", G_TYPE_STRING, str, NULL);
640 /* we do not want to leak the user's real name here */
641 str = g_strdup_printf ("Anon%u", g_random_int ());
642 gst_structure_set (sdes, "name", G_TYPE_STRING, str, NULL);
646 gst_structure_set (sess->sdes, "tool", G_TYPE_STRING, "GStreamer", NULL);
648 /* this is the SSRC we suggest */
649 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
650 sess->internal_ssrc_set = FALSE;
652 sess->first_rtcp = TRUE;
653 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
654 sess->last_rtcp_check_time = GST_CLOCK_TIME_NONE;
655 sess->last_rtcp_send_time = GST_CLOCK_TIME_NONE;
656 sess->last_rtcp_interval = GST_CLOCK_TIME_NONE;
658 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
659 sess->rtcp_feedback_retention_window = DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW;
660 sess->rtcp_immediate_feedback_threshold =
661 DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD;
662 sess->rtp_profile = DEFAULT_RTP_PROFILE;
663 sess->reduced_size_rtcp = DEFAULT_RTCP_REDUCED_SIZE;
665 sess->is_doing_ptp = TRUE;
669 rtp_session_finalize (GObject * object)
674 sess = RTP_SESSION_CAST (object);
676 gst_structure_free (sess->sdes);
678 g_list_free_full (sess->conflicting_addresses,
679 (GDestroyNotify) rtp_conflicting_address_free);
681 /* TODO: Change this again when implementing RFC 2762
682 * for (i = 0; i < 32; i++)
684 for (i = 0; i < 1; i++)
685 g_hash_table_destroy (sess->ssrcs[i]);
687 g_mutex_clear (&sess->lock);
689 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
693 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
695 GValue value = { 0 };
697 g_value_init (&value, RTP_TYPE_SOURCE);
698 g_value_take_object (&value, source);
699 /* copies the value */
700 g_value_array_append (arr, &value);
704 rtp_session_create_sources (RTPSession * sess)
709 RTP_SESSION_LOCK (sess);
710 /* get number of elements in the table */
711 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
712 /* create the result value array */
713 res = g_value_array_new (size);
715 /* and copy all values into the array */
716 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
717 RTP_SESSION_UNLOCK (sess);
723 create_source_stats (gpointer key, RTPSource * source, GValueArray * arr)
728 g_object_get (source, "stats", &s, NULL);
730 g_value_array_append (arr, NULL);
731 value = g_value_array_get_nth (arr, arr->n_values - 1);
732 g_value_init (value, GST_TYPE_STRUCTURE);
733 g_value_take_boxed (value, s);
736 static GstStructure *
737 rtp_session_create_stats (RTPSession * sess)
740 GValueArray *source_stats;
741 GValue source_stats_v = G_VALUE_INIT;
744 RTP_SESSION_LOCK (sess);
745 s = gst_structure_new ("application/x-rtp-session-stats",
746 "rtx-drop-count", G_TYPE_UINT, sess->stats.nacks_dropped,
747 "sent-nack-count", G_TYPE_UINT, sess->stats.nacks_sent,
748 "recv-nack-count", G_TYPE_UINT, sess->stats.nacks_received, NULL);
750 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
751 source_stats = g_value_array_new (size);
752 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
753 (GHFunc) create_source_stats, source_stats);
754 RTP_SESSION_UNLOCK (sess);
756 g_value_init (&source_stats_v, G_TYPE_VALUE_ARRAY);
757 g_value_take_boxed (&source_stats_v, source_stats);
758 gst_structure_take_value (s, "source-stats", &source_stats_v);
764 rtp_session_set_property (GObject * object, guint prop_id,
765 const GValue * value, GParamSpec * pspec)
769 sess = RTP_SESSION (object);
772 case PROP_INTERNAL_SSRC:
773 RTP_SESSION_LOCK (sess);
774 sess->suggested_ssrc = g_value_get_uint (value);
775 sess->internal_ssrc_set = TRUE;
776 sess->internal_ssrc_from_caps_or_property = TRUE;
777 RTP_SESSION_UNLOCK (sess);
778 if (sess->callbacks.reconfigure)
779 sess->callbacks.reconfigure (sess, sess->reconfigure_user_data);
782 RTP_SESSION_LOCK (sess);
783 sess->bandwidth = g_value_get_double (value);
784 sess->recalc_bandwidth = TRUE;
785 RTP_SESSION_UNLOCK (sess);
787 case PROP_RTCP_FRACTION:
788 RTP_SESSION_LOCK (sess);
789 sess->rtcp_bandwidth = g_value_get_double (value);
790 sess->recalc_bandwidth = TRUE;
791 RTP_SESSION_UNLOCK (sess);
793 case PROP_RTCP_RR_BANDWIDTH:
794 RTP_SESSION_LOCK (sess);
795 sess->rtcp_rr_bandwidth = g_value_get_int (value);
796 sess->recalc_bandwidth = TRUE;
797 RTP_SESSION_UNLOCK (sess);
799 case PROP_RTCP_RS_BANDWIDTH:
800 RTP_SESSION_LOCK (sess);
801 sess->rtcp_rs_bandwidth = g_value_get_int (value);
802 sess->recalc_bandwidth = TRUE;
803 RTP_SESSION_UNLOCK (sess);
806 sess->mtu = g_value_get_uint (value);
809 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
812 sess->favor_new = g_value_get_boolean (value);
814 case PROP_RTCP_MIN_INTERVAL:
815 rtp_stats_set_min_interval (&sess->stats,
816 (gdouble) g_value_get_uint64 (value) / GST_SECOND);
817 /* trigger reconsideration */
818 RTP_SESSION_LOCK (sess);
819 sess->next_rtcp_check_time = 0;
820 RTP_SESSION_UNLOCK (sess);
821 if (sess->callbacks.reconsider)
822 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
824 case PROP_RTCP_FEEDBACK_RETENTION_WINDOW:
825 sess->rtcp_feedback_retention_window = g_value_get_uint64 (value);
827 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
828 sess->rtcp_immediate_feedback_threshold = g_value_get_uint (value);
831 sess->probation = g_value_get_uint (value);
833 case PROP_MAX_DROPOUT_TIME:
834 sess->max_dropout_time = g_value_get_uint (value);
836 case PROP_MAX_MISORDER_TIME:
837 sess->max_misorder_time = g_value_get_uint (value);
839 case PROP_RTP_PROFILE:
840 sess->rtp_profile = g_value_get_enum (value);
841 /* trigger reconsideration */
842 RTP_SESSION_LOCK (sess);
843 sess->next_rtcp_check_time = 0;
844 RTP_SESSION_UNLOCK (sess);
845 if (sess->callbacks.reconsider)
846 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
848 case PROP_RTCP_REDUCED_SIZE:
849 sess->reduced_size_rtcp = g_value_get_boolean (value);
852 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
858 rtp_session_get_property (GObject * object, guint prop_id,
859 GValue * value, GParamSpec * pspec)
863 sess = RTP_SESSION (object);
866 case PROP_INTERNAL_SSRC:
867 g_value_set_uint (value, rtp_session_suggest_ssrc (sess, NULL));
869 case PROP_INTERNAL_SOURCE:
870 /* FIXME, return a random source */
871 g_value_set_object (value, NULL);
874 g_value_set_double (value, sess->bandwidth);
876 case PROP_RTCP_FRACTION:
877 g_value_set_double (value, sess->rtcp_bandwidth);
879 case PROP_RTCP_RR_BANDWIDTH:
880 g_value_set_int (value, sess->rtcp_rr_bandwidth);
882 case PROP_RTCP_RS_BANDWIDTH:
883 g_value_set_int (value, sess->rtcp_rs_bandwidth);
886 g_value_set_uint (value, sess->mtu);
889 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
891 case PROP_NUM_SOURCES:
892 g_value_set_uint (value, rtp_session_get_num_sources (sess));
894 case PROP_NUM_ACTIVE_SOURCES:
895 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
898 g_value_take_boxed (value, rtp_session_create_sources (sess));
901 g_value_set_boolean (value, sess->favor_new);
903 case PROP_RTCP_MIN_INTERVAL:
904 g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
906 case PROP_RTCP_FEEDBACK_RETENTION_WINDOW:
907 g_value_set_uint64 (value, sess->rtcp_feedback_retention_window);
909 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
910 g_value_set_uint (value, sess->rtcp_immediate_feedback_threshold);
913 g_value_set_uint (value, sess->probation);
915 case PROP_MAX_DROPOUT_TIME:
916 g_value_set_uint (value, sess->max_dropout_time);
918 case PROP_MAX_MISORDER_TIME:
919 g_value_set_uint (value, sess->max_misorder_time);
922 g_value_take_boxed (value, rtp_session_create_stats (sess));
924 case PROP_RTP_PROFILE:
925 g_value_set_enum (value, sess->rtp_profile);
927 case PROP_RTCP_REDUCED_SIZE:
928 g_value_set_boolean (value, sess->reduced_size_rtcp);
931 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
937 on_new_ssrc (RTPSession * sess, RTPSource * source)
939 g_object_ref (source);
940 RTP_SESSION_UNLOCK (sess);
941 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
942 RTP_SESSION_LOCK (sess);
943 g_object_unref (source);
947 on_ssrc_collision (RTPSession * sess, RTPSource * source)
949 g_object_ref (source);
950 RTP_SESSION_UNLOCK (sess);
951 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
953 RTP_SESSION_LOCK (sess);
954 g_object_unref (source);
958 on_ssrc_validated (RTPSession * sess, RTPSource * source)
960 g_object_ref (source);
961 RTP_SESSION_UNLOCK (sess);
962 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
964 RTP_SESSION_LOCK (sess);
965 g_object_unref (source);
969 on_ssrc_active (RTPSession * sess, RTPSource * source)
971 g_object_ref (source);
972 RTP_SESSION_UNLOCK (sess);
973 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
974 RTP_SESSION_LOCK (sess);
975 g_object_unref (source);
979 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
981 g_object_ref (source);
982 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
983 RTP_SESSION_UNLOCK (sess);
984 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
985 RTP_SESSION_LOCK (sess);
986 g_object_unref (source);
990 on_bye_ssrc (RTPSession * sess, RTPSource * source)
992 g_object_ref (source);
993 RTP_SESSION_UNLOCK (sess);
994 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
995 RTP_SESSION_LOCK (sess);
996 g_object_unref (source);
1000 on_bye_timeout (RTPSession * sess, RTPSource * source)
1002 g_object_ref (source);
1003 RTP_SESSION_UNLOCK (sess);
1004 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
1005 RTP_SESSION_LOCK (sess);
1006 g_object_unref (source);
1010 on_timeout (RTPSession * sess, RTPSource * source)
1012 g_object_ref (source);
1013 RTP_SESSION_UNLOCK (sess);
1014 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
1015 RTP_SESSION_LOCK (sess);
1016 g_object_unref (source);
1020 on_sender_timeout (RTPSession * sess, RTPSource * source)
1022 g_object_ref (source);
1023 RTP_SESSION_UNLOCK (sess);
1024 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
1026 RTP_SESSION_LOCK (sess);
1027 g_object_unref (source);
1031 on_new_sender_ssrc (RTPSession * sess, RTPSource * source)
1033 g_object_ref (source);
1034 RTP_SESSION_UNLOCK (sess);
1035 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SENDER_SSRC], 0,
1037 RTP_SESSION_LOCK (sess);
1038 g_object_unref (source);
1042 on_sender_ssrc_active (RTPSession * sess, RTPSource * source)
1044 g_object_ref (source);
1045 RTP_SESSION_UNLOCK (sess);
1046 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE], 0,
1048 RTP_SESSION_LOCK (sess);
1049 g_object_unref (source);
1055 * Create a new session object.
1057 * Returns: a new #RTPSession. g_object_unref() after usage.
1060 rtp_session_new (void)
1064 sess = g_object_new (RTP_TYPE_SESSION, NULL);
1070 * rtp_session_reset:
1071 * @sess: an #RTPSession
1073 * Reset the sources of @sess.
1076 rtp_session_reset (RTPSession * sess)
1078 g_return_if_fail (RTP_IS_SESSION (sess));
1080 /* remove all sources */
1081 g_hash_table_remove_all (sess->ssrcs[sess->mask_idx]);
1082 sess->total_sources = 0;
1083 sess->stats.sender_sources = 0;
1084 sess->stats.internal_sender_sources = 0;
1085 sess->stats.internal_sources = 0;
1086 sess->stats.active_sources = 0;
1088 sess->generation = 0;
1089 sess->first_rtcp = TRUE;
1090 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
1091 sess->last_rtcp_check_time = GST_CLOCK_TIME_NONE;
1092 sess->last_rtcp_send_time = GST_CLOCK_TIME_NONE;
1093 sess->last_rtcp_interval = GST_CLOCK_TIME_NONE;
1094 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
1095 sess->scheduled_bye = FALSE;
1097 /* reset session stats */
1098 sess->stats.bye_members = 0;
1099 sess->stats.nacks_dropped = 0;
1100 sess->stats.nacks_sent = 0;
1101 sess->stats.nacks_received = 0;
1103 sess->is_doing_ptp = TRUE;
1105 g_list_free_full (sess->conflicting_addresses,
1106 (GDestroyNotify) rtp_conflicting_address_free);
1107 sess->conflicting_addresses = NULL;
1111 * rtp_session_set_callbacks:
1112 * @sess: an #RTPSession
1113 * @callbacks: callbacks to configure
1114 * @user_data: user data passed in the callbacks
1116 * Configure a set of callbacks to be notified of actions.
1119 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
1122 g_return_if_fail (RTP_IS_SESSION (sess));
1124 if (callbacks->process_rtp) {
1125 sess->callbacks.process_rtp = callbacks->process_rtp;
1126 sess->process_rtp_user_data = user_data;
1128 if (callbacks->send_rtp) {
1129 sess->callbacks.send_rtp = callbacks->send_rtp;
1130 sess->send_rtp_user_data = user_data;
1132 if (callbacks->send_rtcp) {
1133 sess->callbacks.send_rtcp = callbacks->send_rtcp;
1134 sess->send_rtcp_user_data = user_data;
1136 if (callbacks->sync_rtcp) {
1137 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
1138 sess->sync_rtcp_user_data = user_data;
1140 if (callbacks->clock_rate) {
1141 sess->callbacks.clock_rate = callbacks->clock_rate;
1142 sess->clock_rate_user_data = user_data;
1144 if (callbacks->reconsider) {
1145 sess->callbacks.reconsider = callbacks->reconsider;
1146 sess->reconsider_user_data = user_data;
1148 if (callbacks->request_key_unit) {
1149 sess->callbacks.request_key_unit = callbacks->request_key_unit;
1150 sess->request_key_unit_user_data = user_data;
1152 if (callbacks->request_time) {
1153 sess->callbacks.request_time = callbacks->request_time;
1154 sess->request_time_user_data = user_data;
1156 if (callbacks->notify_nack) {
1157 sess->callbacks.notify_nack = callbacks->notify_nack;
1158 sess->notify_nack_user_data = user_data;
1160 if (callbacks->reconfigure) {
1161 sess->callbacks.reconfigure = callbacks->reconfigure;
1162 sess->reconfigure_user_data = user_data;
1164 if (callbacks->notify_early_rtcp) {
1165 sess->callbacks.notify_early_rtcp = callbacks->notify_early_rtcp;
1166 sess->notify_early_rtcp_user_data = user_data;
1171 * rtp_session_set_process_rtp_callback:
1172 * @sess: an #RTPSession
1173 * @callback: callback to set
1174 * @user_data: user data passed in the callback
1176 * Configure only the process_rtp callback to be notified of the process_rtp action.
1179 rtp_session_set_process_rtp_callback (RTPSession * sess,
1180 RTPSessionProcessRTP callback, gpointer user_data)
1182 g_return_if_fail (RTP_IS_SESSION (sess));
1184 sess->callbacks.process_rtp = callback;
1185 sess->process_rtp_user_data = user_data;
1189 * rtp_session_set_send_rtp_callback:
1190 * @sess: an #RTPSession
1191 * @callback: callback to set
1192 * @user_data: user data passed in the callback
1194 * Configure only the send_rtp callback to be notified of the send_rtp action.
1197 rtp_session_set_send_rtp_callback (RTPSession * sess,
1198 RTPSessionSendRTP callback, gpointer user_data)
1200 g_return_if_fail (RTP_IS_SESSION (sess));
1202 sess->callbacks.send_rtp = callback;
1203 sess->send_rtp_user_data = user_data;
1207 * rtp_session_set_send_rtcp_callback:
1208 * @sess: an #RTPSession
1209 * @callback: callback to set
1210 * @user_data: user data passed in the callback
1212 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
1215 rtp_session_set_send_rtcp_callback (RTPSession * sess,
1216 RTPSessionSendRTCP callback, gpointer user_data)
1218 g_return_if_fail (RTP_IS_SESSION (sess));
1220 sess->callbacks.send_rtcp = callback;
1221 sess->send_rtcp_user_data = user_data;
1225 * rtp_session_set_sync_rtcp_callback:
1226 * @sess: an #RTPSession
1227 * @callback: callback to set
1228 * @user_data: user data passed in the callback
1230 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
1233 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
1234 RTPSessionSyncRTCP callback, gpointer user_data)
1236 g_return_if_fail (RTP_IS_SESSION (sess));
1238 sess->callbacks.sync_rtcp = callback;
1239 sess->sync_rtcp_user_data = user_data;
1243 * rtp_session_set_clock_rate_callback:
1244 * @sess: an #RTPSession
1245 * @callback: callback to set
1246 * @user_data: user data passed in the callback
1248 * Configure only the clock_rate callback to be notified of the clock_rate action.
1251 rtp_session_set_clock_rate_callback (RTPSession * sess,
1252 RTPSessionClockRate callback, gpointer user_data)
1254 g_return_if_fail (RTP_IS_SESSION (sess));
1256 sess->callbacks.clock_rate = callback;
1257 sess->clock_rate_user_data = user_data;
1261 * rtp_session_set_reconsider_callback:
1262 * @sess: an #RTPSession
1263 * @callback: callback to set
1264 * @user_data: user data passed in the callback
1266 * Configure only the reconsider callback to be notified of the reconsider action.
1269 rtp_session_set_reconsider_callback (RTPSession * sess,
1270 RTPSessionReconsider callback, gpointer user_data)
1272 g_return_if_fail (RTP_IS_SESSION (sess));
1274 sess->callbacks.reconsider = callback;
1275 sess->reconsider_user_data = user_data;
1279 * rtp_session_set_request_time_callback:
1280 * @sess: an #RTPSession
1281 * @callback: callback to set
1282 * @user_data: user data passed in the callback
1284 * Configure only the request_time callback
1287 rtp_session_set_request_time_callback (RTPSession * sess,
1288 RTPSessionRequestTime callback, gpointer user_data)
1290 g_return_if_fail (RTP_IS_SESSION (sess));
1292 sess->callbacks.request_time = callback;
1293 sess->request_time_user_data = user_data;
1297 * rtp_session_set_bandwidth:
1298 * @sess: an #RTPSession
1299 * @bandwidth: the bandwidth allocated
1301 * Set the session bandwidth in bits per second.
1304 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
1306 g_return_if_fail (RTP_IS_SESSION (sess));
1308 RTP_SESSION_LOCK (sess);
1309 sess->stats.bandwidth = bandwidth;
1310 RTP_SESSION_UNLOCK (sess);
1314 * rtp_session_get_bandwidth:
1315 * @sess: an #RTPSession
1317 * Get the session bandwidth.
1319 * Returns: the session bandwidth.
1322 rtp_session_get_bandwidth (RTPSession * sess)
1326 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1328 RTP_SESSION_LOCK (sess);
1329 result = sess->stats.bandwidth;
1330 RTP_SESSION_UNLOCK (sess);
1336 * rtp_session_set_rtcp_fraction:
1337 * @sess: an #RTPSession
1338 * @bandwidth: the RTCP bandwidth
1340 * Set the bandwidth in bits per second that should be used for RTCP
1344 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
1346 g_return_if_fail (RTP_IS_SESSION (sess));
1348 RTP_SESSION_LOCK (sess);
1349 sess->stats.rtcp_bandwidth = bandwidth;
1350 RTP_SESSION_UNLOCK (sess);
1354 * rtp_session_get_rtcp_fraction:
1355 * @sess: an #RTPSession
1357 * Get the session bandwidth used for RTCP.
1359 * Returns: The bandwidth used for RTCP messages.
1362 rtp_session_get_rtcp_fraction (RTPSession * sess)
1366 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
1368 RTP_SESSION_LOCK (sess);
1369 result = sess->stats.rtcp_bandwidth;
1370 RTP_SESSION_UNLOCK (sess);
1376 * rtp_session_get_sdes_struct:
1377 * @sess: an #RTSPSession
1379 * Get the SDES data as a #GstStructure
1381 * Returns: a GstStructure with SDES items for @sess. This function returns a
1382 * copy of the SDES structure, use gst_structure_free() after usage.
1385 rtp_session_get_sdes_struct (RTPSession * sess)
1387 GstStructure *result = NULL;
1389 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1391 RTP_SESSION_LOCK (sess);
1393 result = gst_structure_copy (sess->sdes);
1394 RTP_SESSION_UNLOCK (sess);
1400 source_set_sdes (const gchar * key, RTPSource * source, GstStructure * sdes)
1402 rtp_source_set_sdes_struct (source, gst_structure_copy (sdes));
1406 * rtp_session_set_sdes_struct:
1407 * @sess: an #RTSPSession
1408 * @sdes: a #GstStructure
1410 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1413 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
1415 g_return_if_fail (sdes);
1416 g_return_if_fail (RTP_IS_SESSION (sess));
1418 RTP_SESSION_LOCK (sess);
1420 gst_structure_free (sess->sdes);
1421 sess->sdes = gst_structure_copy (sdes);
1423 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
1424 (GHFunc) source_set_sdes, sess->sdes);
1425 RTP_SESSION_UNLOCK (sess);
1428 static GstFlowReturn
1429 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1431 GstFlowReturn result = GST_FLOW_OK;
1433 if (source->internal) {
1434 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1436 RTP_SESSION_UNLOCK (session);
1438 if (session->callbacks.send_rtp)
1440 session->callbacks.send_rtp (session, source, data,
1441 session->send_rtp_user_data);
1443 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1446 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1447 RTP_SESSION_UNLOCK (session);
1449 if (session->callbacks.process_rtp)
1451 session->callbacks.process_rtp (session, source,
1452 GST_BUFFER_CAST (data), session->process_rtp_user_data);
1454 gst_buffer_unref (GST_BUFFER_CAST (data));
1456 RTP_SESSION_LOCK (session);
1462 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1466 RTP_SESSION_UNLOCK (session);
1468 if (session->callbacks.clock_rate)
1470 session->callbacks.clock_rate (session, pt,
1471 session->clock_rate_user_data);
1475 RTP_SESSION_LOCK (session);
1477 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1482 static RTPSourceCallbacks callbacks = {
1483 (RTPSourcePushRTP) source_push_rtp,
1484 (RTPSourceClockRate) source_clock_rate,
1489 * rtp_session_find_conflicting_address:
1490 * @session: The session the packet came in
1491 * @address: address to check for
1492 * @time: The time when the packet that is possibly in conflict arrived
1494 * Checks if an address which has a conflict is already known. If it is
1495 * a known conflict, remember the time
1497 * Returns: TRUE if it was a known conflict, FALSE otherwise
1500 rtp_session_find_conflicting_address (RTPSession * session,
1501 GSocketAddress * address, GstClockTime time)
1503 return find_conflicting_address (session->conflicting_addresses, address,
1508 * rtp_session_add_conflicting_address:
1509 * @session: The session the packet came in
1510 * @address: address to remember
1511 * @time: The time when the packet that is in conflict arrived
1513 * Adds a new conflict address
1516 rtp_session_add_conflicting_address (RTPSession * sess,
1517 GSocketAddress * address, GstClockTime time)
1519 sess->conflicting_addresses =
1520 add_conflicting_address (sess->conflicting_addresses, address, time);
1525 check_collision (RTPSession * sess, RTPSource * source,
1526 RTPPacketInfo * pinfo, gboolean rtp)
1530 /* If we have no pinfo address, we can't do collision checking */
1531 if (!pinfo->address)
1534 ssrc = rtp_source_get_ssrc (source);
1536 if (!source->internal) {
1537 GSocketAddress *from;
1539 /* This is not our local source, but lets check if two remote
1542 from = source->rtp_from;
1544 from = source->rtcp_from;
1548 if (__g_socket_address_equal (from, pinfo->address)) {
1549 /* Address is the same */
1552 GST_LOG ("we have a third-party collision or loop ssrc:%x", ssrc);
1553 if (sess->favor_new) {
1554 if (rtp_source_find_conflicting_address (source,
1555 pinfo->address, pinfo->current_time)) {
1558 buf1 = __g_socket_address_to_string (pinfo->address);
1559 GST_LOG ("Known conflict on %x for %s, dropping packet", ssrc,
1567 /* Current address is not a known conflict, lets assume this is
1568 * a new source. Save old address in possible conflict list
1570 rtp_source_add_conflicting_address (source, from,
1571 pinfo->current_time);
1573 buf1 = __g_socket_address_to_string (from);
1574 buf2 = __g_socket_address_to_string (pinfo->address);
1576 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1577 " saving old as known conflict", ssrc, buf1, buf2);
1580 rtp_source_set_rtp_from (source, pinfo->address);
1582 rtp_source_set_rtcp_from (source, pinfo->address);
1590 /* Don't need to save old addresses, we ignore new sources */
1595 /* We don't already have a from address for RTP, just set it */
1597 rtp_source_set_rtp_from (source, pinfo->address);
1599 rtp_source_set_rtcp_from (source, pinfo->address);
1603 /* FIXME: Log 3rd party collision somehow
1604 * Maybe should be done in upper layer, only the SDES can tell us
1605 * if its a collision or a loop
1608 /* This is sending with our ssrc, is it an address we already know */
1609 if (rtp_session_find_conflicting_address (sess, pinfo->address,
1610 pinfo->current_time)) {
1611 /* Its a known conflict, its probably a loop, not a collision
1612 * lets just drop the incoming packet
1614 GST_DEBUG ("Our packets are being looped back to us, dropping");
1616 /* Its a new collision, lets change our SSRC */
1617 rtp_session_add_conflicting_address (sess, pinfo->address,
1618 pinfo->current_time);
1620 GST_DEBUG ("Collision for SSRC %x", ssrc);
1621 /* mark the source BYE */
1622 rtp_source_mark_bye (source, "SSRC Collision");
1623 /* if we were suggesting this SSRC, change to something else */
1624 if (sess->suggested_ssrc == ssrc) {
1625 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
1626 sess->internal_ssrc_set = TRUE;
1629 on_ssrc_collision (sess, source);
1631 rtp_session_schedule_bye_locked (sess, pinfo->current_time);
1640 gboolean is_doing_ptp;
1641 GSocketAddress *new_addr;
1644 /* check if the two given ip addr are the same (do not care about the port) */
1646 ip_addr_equal (GSocketAddress * a, GSocketAddress * b)
1649 g_inet_address_equal (g_inet_socket_address_get_address
1650 (G_INET_SOCKET_ADDRESS (a)),
1651 g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (b)));
1655 compare_rtp_source_addr (const gchar * key, RTPSource * source,
1656 CompareAddrData * data)
1658 /* only compare ip addr of remote sources which are also not closing */
1659 if (!source->internal && !source->closing && source->rtp_from) {
1660 /* look for the first rtp source */
1661 if (!data->new_addr)
1662 data->new_addr = source->rtp_from;
1663 /* compare current ip addr with the first one */
1665 data->is_doing_ptp &= ip_addr_equal (data->new_addr, source->rtp_from);
1670 compare_rtcp_source_addr (const gchar * key, RTPSource * source,
1671 CompareAddrData * data)
1673 /* only compare ip addr of remote sources which are also not closing */
1674 if (!source->internal && !source->closing && source->rtcp_from) {
1675 /* look for the first rtcp source */
1676 if (!data->new_addr)
1677 data->new_addr = source->rtcp_from;
1679 /* compare current ip addr with the first one */
1680 data->is_doing_ptp &= ip_addr_equal (data->new_addr, source->rtcp_from);
1684 /* loop over our non-internal source to know if the session
1685 * is doing point-to-point */
1687 session_update_ptp (RTPSession * sess)
1689 /* to know if the session is doing point to point, the ip addr
1690 * of each non-internal (=remotes) source have to be compared
1693 gboolean is_doing_rtp_ptp;
1694 gboolean is_doing_rtcp_ptp;
1695 CompareAddrData data;
1697 /* compare the first remote source's ip addr that receive rtp packets
1698 * with other remote rtp source.
1699 * it's enough because the session just needs to know if they are all
1702 data.is_doing_ptp = TRUE;
1703 data.new_addr = NULL;
1704 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
1705 (GHFunc) compare_rtp_source_addr, (gpointer) & data);
1706 is_doing_rtp_ptp = data.is_doing_ptp;
1708 /* same but about rtcp */
1709 data.is_doing_ptp = TRUE;
1710 data.new_addr = NULL;
1711 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
1712 (GHFunc) compare_rtcp_source_addr, (gpointer) & data);
1713 is_doing_rtcp_ptp = data.is_doing_ptp;
1715 /* the session is doing point-to-point if all rtp remote have the same
1716 * ip addr and if all rtcp remote sources have the same ip addr */
1717 sess->is_doing_ptp = is_doing_rtp_ptp && is_doing_rtcp_ptp;
1719 GST_DEBUG ("doing point-to-point: %d", sess->is_doing_ptp);
1723 add_source (RTPSession * sess, RTPSource * src)
1725 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1726 GINT_TO_POINTER (src->ssrc), src);
1727 /* report the new source ASAP */
1728 src->generation = sess->generation;
1729 /* we have one more source now */
1730 sess->total_sources++;
1731 if (RTP_SOURCE_IS_ACTIVE (src))
1732 sess->stats.active_sources++;
1733 if (src->internal) {
1734 sess->stats.internal_sources++;
1735 if (!sess->internal_ssrc_from_caps_or_property
1736 && sess->suggested_ssrc != src->ssrc) {
1737 sess->suggested_ssrc = src->ssrc;
1738 sess->internal_ssrc_set = TRUE;
1742 /* update point-to-point status */
1744 session_update_ptp (sess);
1748 find_source (RTPSession * sess, guint32 ssrc)
1750 return g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1751 GINT_TO_POINTER (ssrc));
1754 /* must be called with the session lock, the returned source needs to be
1755 * unreffed after usage. */
1757 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1758 RTPPacketInfo * pinfo, gboolean rtp)
1762 source = find_source (sess, ssrc);
1763 if (source == NULL) {
1764 /* make new Source in probation and insert */
1765 source = rtp_source_new (ssrc);
1767 GST_DEBUG ("creating new source %08x %p", ssrc, source);
1769 /* for RTP packets we need to set the source in probation. Receiving RTCP
1770 * packets of an SSRC, on the other hand, is a strong indication that we
1771 * are dealing with a valid source. */
1772 g_object_set (source, "probation", rtp ? sess->probation : 0,
1773 "max-dropout-time", sess->max_dropout_time, "max-misorder-time",
1774 sess->max_misorder_time, NULL);
1776 /* store from address, if any */
1777 if (pinfo->address) {
1779 rtp_source_set_rtp_from (source, pinfo->address);
1781 rtp_source_set_rtcp_from (source, pinfo->address);
1784 /* configure a callback on the source */
1785 rtp_source_set_callbacks (source, &callbacks, sess);
1787 add_source (sess, source);
1791 /* check for collision, this updates the address when not previously set */
1792 if (check_collision (sess, source, pinfo, rtp)) {
1795 /* Receiving RTCP packets of an SSRC is a strong indication that we
1796 * are dealing with a valid source. */
1798 g_object_set (source, "probation", 0, NULL);
1800 /* update last activity */
1801 source->last_activity = pinfo->current_time;
1803 source->last_rtp_activity = pinfo->current_time;
1804 g_object_ref (source);
1809 /* must be called with the session lock, the returned source needs to be
1810 * unreffed after usage. */
1812 obtain_internal_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1813 GstClockTime current_time)
1817 source = find_source (sess, ssrc);
1818 if (source == NULL) {
1819 /* make new internal Source and insert */
1820 source = rtp_source_new (ssrc);
1822 GST_DEBUG ("creating new internal source %08x %p", ssrc, source);
1824 source->validated = TRUE;
1825 source->internal = TRUE;
1826 source->probation = FALSE;
1827 rtp_source_set_sdes_struct (source, gst_structure_copy (sess->sdes));
1828 rtp_source_set_callbacks (source, &callbacks, sess);
1830 add_source (sess, source);
1835 /* update last activity */
1836 if (current_time != GST_CLOCK_TIME_NONE) {
1837 source->last_activity = current_time;
1838 source->last_rtp_activity = current_time;
1840 g_object_ref (source);
1846 * rtp_session_suggest_ssrc:
1847 * @sess: a #RTPSession
1848 * @is_random: if the suggested ssrc is random
1850 * Suggest an unused SSRC in @sess.
1852 * Returns: a free unused SSRC
1855 rtp_session_suggest_ssrc (RTPSession * sess, gboolean * is_random)
1859 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1861 RTP_SESSION_LOCK (sess);
1862 result = sess->suggested_ssrc;
1864 *is_random = !sess->internal_ssrc_set;
1865 RTP_SESSION_UNLOCK (sess);
1871 * rtp_session_add_source:
1872 * @sess: a #RTPSession
1873 * @src: #RTPSource to add
1875 * Add @src to @session.
1877 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1878 * existed in the session.
1881 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1883 gboolean result = FALSE;
1886 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1887 g_return_val_if_fail (src != NULL, FALSE);
1889 RTP_SESSION_LOCK (sess);
1890 find = find_source (sess, src->ssrc);
1892 add_source (sess, src);
1895 RTP_SESSION_UNLOCK (sess);
1901 * rtp_session_get_num_sources:
1902 * @sess: an #RTPSession
1904 * Get the number of sources in @sess.
1906 * Returns: The number of sources in @sess.
1909 rtp_session_get_num_sources (RTPSession * sess)
1913 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1915 RTP_SESSION_LOCK (sess);
1916 result = sess->total_sources;
1917 RTP_SESSION_UNLOCK (sess);
1923 * rtp_session_get_num_active_sources:
1924 * @sess: an #RTPSession
1926 * Get the number of active sources in @sess. A source is considered active when
1927 * it has been validated and has not yet received a BYE RTCP message.
1929 * Returns: The number of active sources in @sess.
1932 rtp_session_get_num_active_sources (RTPSession * sess)
1936 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1938 RTP_SESSION_LOCK (sess);
1939 result = sess->stats.active_sources;
1940 RTP_SESSION_UNLOCK (sess);
1946 * rtp_session_get_source_by_ssrc:
1947 * @sess: an #RTPSession
1950 * Find the source with @ssrc in @sess.
1952 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1953 * g_object_unref() after usage.
1956 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1960 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1962 RTP_SESSION_LOCK (sess);
1963 result = find_source (sess, ssrc);
1965 g_object_ref (result);
1966 RTP_SESSION_UNLOCK (sess);
1971 /* should be called with the SESSION lock */
1973 rtp_session_create_new_ssrc (RTPSession * sess)
1978 ssrc = g_random_int ();
1980 /* see if it exists in the session, we're done if it doesn't */
1981 if (find_source (sess, ssrc) == NULL)
1988 update_packet (GstBuffer ** buffer, guint idx, RTPPacketInfo * pinfo)
1990 GstNetAddressMeta *meta;
1992 /* get packet size including header overhead */
1993 pinfo->bytes += gst_buffer_get_size (*buffer) + pinfo->header_len;
1997 GstRTPBuffer rtp = { NULL };
1999 if (!gst_rtp_buffer_map (*buffer, GST_MAP_READ, &rtp))
2000 goto invalid_packet;
2002 pinfo->payload_len += gst_rtp_buffer_get_payload_len (&rtp);
2006 /* only keep info for first buffer */
2007 pinfo->ssrc = gst_rtp_buffer_get_ssrc (&rtp);
2008 pinfo->seqnum = gst_rtp_buffer_get_seq (&rtp);
2009 pinfo->pt = gst_rtp_buffer_get_payload_type (&rtp);
2010 pinfo->rtptime = gst_rtp_buffer_get_timestamp (&rtp);
2011 /* copy available csrc */
2012 pinfo->csrc_count = gst_rtp_buffer_get_csrc_count (&rtp);
2013 for (i = 0; i < pinfo->csrc_count; i++)
2014 pinfo->csrcs[i] = gst_rtp_buffer_get_csrc (&rtp, i);
2016 gst_rtp_buffer_unmap (&rtp);
2020 /* for netbuffer we can store the IP address to check for collisions */
2021 meta = gst_buffer_get_net_address_meta (*buffer);
2023 g_object_unref (pinfo->address);
2025 pinfo->address = G_SOCKET_ADDRESS (g_object_ref (meta->addr));
2027 pinfo->address = NULL;
2035 GST_DEBUG ("invalid RTP packet received");
2040 /* update the RTPPacketInfo structure with the current time and other bits
2041 * about the current buffer we are handling.
2042 * This function is typically called when a validated packet is received.
2043 * This function should be called with the RTP_SESSION_LOCK
2046 update_packet_info (RTPSession * sess, RTPPacketInfo * pinfo,
2047 gboolean send, gboolean rtp, gboolean is_list, gpointer data,
2048 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
2054 pinfo->is_list = is_list;
2056 pinfo->current_time = current_time;
2057 pinfo->running_time = running_time;
2058 pinfo->ntpnstime = ntpnstime;
2059 pinfo->header_len = sess->header_len;
2061 pinfo->payload_len = 0;
2065 GstBufferList *list = GST_BUFFER_LIST_CAST (data);
2067 gst_buffer_list_foreach (list, (GstBufferListFunc) update_packet,
2070 GstBuffer *buffer = GST_BUFFER_CAST (data);
2071 res = update_packet (&buffer, 0, pinfo);
2077 clean_packet_info (RTPPacketInfo * pinfo)
2080 g_object_unref (pinfo->address);
2082 gst_mini_object_unref (pinfo->data);
2088 source_update_active (RTPSession * sess, RTPSource * source,
2089 gboolean prevactive)
2091 gboolean active = RTP_SOURCE_IS_ACTIVE (source);
2092 guint32 ssrc = source->ssrc;
2094 if (prevactive == active)
2098 sess->stats.active_sources++;
2099 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
2100 sess->stats.active_sources);
2102 sess->stats.active_sources--;
2103 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
2104 sess->stats.active_sources);
2110 source_update_sender (RTPSession * sess, RTPSource * source,
2111 gboolean prevsender)
2113 gboolean sender = RTP_SOURCE_IS_SENDER (source);
2114 guint32 ssrc = source->ssrc;
2116 if (prevsender == sender)
2120 sess->stats.sender_sources++;
2121 if (source->internal)
2122 sess->stats.internal_sender_sources++;
2123 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
2124 sess->stats.sender_sources);
2126 sess->stats.sender_sources--;
2127 if (source->internal)
2128 sess->stats.internal_sender_sources--;
2129 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
2130 sess->stats.sender_sources);
2136 * rtp_session_process_rtp:
2137 * @sess: and #RTPSession
2138 * @buffer: an RTP buffer
2139 * @current_time: the current system time
2140 * @running_time: the running_time of @buffer
2142 * Process an RTP buffer in the session manager. This function takes ownership
2145 * Returns: a #GstFlowReturn.
2148 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
2149 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
2151 GstFlowReturn result;
2155 gboolean prevsender, prevactive;
2156 RTPPacketInfo pinfo = { 0, };
2159 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2160 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2162 RTP_SESSION_LOCK (sess);
2164 /* update pinfo stats */
2165 if (!update_packet_info (sess, &pinfo, FALSE, TRUE, FALSE, buffer,
2166 current_time, running_time, ntpnstime)) {
2167 GST_DEBUG ("invalid RTP packet received");
2168 RTP_SESSION_UNLOCK (sess);
2169 return rtp_session_process_rtcp (sess, buffer, current_time, running_time,
2175 source = obtain_source (sess, ssrc, &created, &pinfo, TRUE);
2179 prevsender = RTP_SOURCE_IS_SENDER (source);
2180 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2181 oldrate = source->bitrate;
2183 /* let source process the packet */
2184 result = rtp_source_process_rtp (source, &pinfo);
2186 /* source became active */
2187 if (source_update_active (sess, source, prevactive))
2188 on_ssrc_validated (sess, source);
2190 source_update_sender (sess, source, prevsender);
2192 if (oldrate != source->bitrate)
2193 sess->recalc_bandwidth = TRUE;
2196 on_new_ssrc (sess, source);
2198 if (source->validated) {
2202 /* for validated sources, we add the CSRCs as well */
2203 for (i = 0; i < pinfo.csrc_count; i++) {
2205 RTPSource *csrc_src;
2207 csrc = pinfo.csrcs[i];
2210 csrc_src = obtain_source (sess, csrc, &created, &pinfo, TRUE);
2215 GST_DEBUG ("created new CSRC: %08x", csrc);
2216 rtp_source_set_as_csrc (csrc_src);
2217 source_update_active (sess, csrc_src, FALSE);
2218 on_new_ssrc (sess, csrc_src);
2220 g_object_unref (csrc_src);
2223 g_object_unref (source);
2225 RTP_SESSION_UNLOCK (sess);
2227 clean_packet_info (&pinfo);
2234 RTP_SESSION_UNLOCK (sess);
2235 clean_packet_info (&pinfo);
2236 GST_DEBUG ("ignoring packet because its collisioning");
2242 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
2243 GstRTCPPacket * packet, RTPPacketInfo * pinfo)
2247 count = gst_rtcp_packet_get_rb_count (packet);
2248 for (i = 0; i < count; i++) {
2249 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
2250 guint8 fractionlost;
2254 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
2255 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2257 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
2259 /* find our own source */
2260 src = find_source (sess, ssrc);
2264 if (src->internal && RTP_SOURCE_IS_ACTIVE (src)) {
2265 /* only deal with report blocks for our session, we update the stats of
2266 * the sender of the RTCP message. We could also compare our stats against
2267 * the other sender to see if we are better or worse. */
2268 /* FIXME, need to keep track who the RB block is from */
2269 rtp_source_process_rb (source, pinfo->ntpnstime, fractionlost,
2270 packetslost, exthighestseq, jitter, lsr, dlsr);
2273 on_ssrc_active (sess, source);
2276 /* A Sender report contains statistics about how the sender is doing. This
2277 * includes timing informataion such as the relation between RTP and NTP
2278 * timestamps and the number of packets/bytes it sent to us.
2280 * In this report is also included a set of report blocks related to how this
2281 * sender is receiving data (in case we (or somebody else) is also sending stuff
2282 * to it). This info includes the packet loss, jitter and seqnum. It also
2283 * contains information to calculate the round trip time (LSR/DLSR).
2286 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
2287 RTPPacketInfo * pinfo, gboolean * do_sync)
2289 guint32 senderssrc, rtptime, packet_count, octet_count;
2292 gboolean created, prevsender;
2294 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
2295 &packet_count, &octet_count);
2297 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
2298 senderssrc, GST_TIME_ARGS (pinfo->current_time));
2300 source = obtain_source (sess, senderssrc, &created, pinfo, FALSE);
2304 /* skip non-bye packets for sources that are marked BYE */
2305 if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
2308 /* don't try to do lip-sync for sources that sent a BYE */
2309 if (RTP_SOURCE_IS_MARKED_BYE (source))
2314 prevsender = RTP_SOURCE_IS_SENDER (source);
2316 /* first update the source */
2317 rtp_source_process_sr (source, pinfo->current_time, ntptime, rtptime,
2318 packet_count, octet_count);
2320 source_update_sender (sess, source, prevsender);
2323 on_new_ssrc (sess, source);
2325 rtp_session_process_rb (sess, source, packet, pinfo);
2328 g_object_unref (source);
2331 /* A receiver report contains statistics about how a receiver is doing. It
2332 * includes stuff like packet loss, jitter and the seqnum it received last. It
2333 * also contains info to calculate the round trip time.
2335 * We are only interested in how the sender of this report is doing wrt to us.
2338 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
2339 RTPPacketInfo * pinfo)
2345 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
2347 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
2349 source = obtain_source (sess, senderssrc, &created, pinfo, FALSE);
2353 /* skip non-bye packets for sources that are marked BYE */
2354 if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
2358 on_new_ssrc (sess, source);
2360 rtp_session_process_rb (sess, source, packet, pinfo);
2363 g_object_unref (source);
2366 /* Get SDES items and store them in the SSRC */
2368 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
2369 RTPPacketInfo * pinfo)
2372 gboolean more_items, more_entries;
2374 items = gst_rtcp_packet_sdes_get_item_count (packet);
2375 GST_DEBUG ("got SDES packet with %d items", items);
2377 more_items = gst_rtcp_packet_sdes_first_item (packet);
2379 while (more_items) {
2381 gboolean changed, created, prevactive;
2385 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
2387 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
2391 /* find src, no probation when dealing with RTCP */
2392 source = obtain_source (sess, ssrc, &created, pinfo, FALSE);
2396 /* skip non-bye packets for sources that are marked BYE */
2397 if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
2400 sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
2402 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
2404 while (more_entries) {
2405 GstRTCPSDESType type;
2411 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
2413 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
2416 if (type == GST_RTCP_SDES_PRIV) {
2417 name = g_strndup ((const gchar *) &data[1], data[0]);
2419 data += data[0] + 1;
2421 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
2424 value = g_strndup ((const gchar *) data, len);
2426 if (g_utf8_validate (value, -1, NULL)) {
2427 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
2429 GST_WARNING ("ignore SDES field %s with non-utf8 data %s", name, value);
2435 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
2439 /* takes ownership of sdes */
2440 changed = rtp_source_set_sdes_struct (source, sdes);
2442 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2443 source->validated = TRUE;
2446 on_new_ssrc (sess, source);
2448 /* source became active */
2449 if (source_update_active (sess, source, prevactive))
2450 on_ssrc_validated (sess, source);
2453 on_ssrc_sdes (sess, source);
2456 g_object_unref (source);
2458 more_items = gst_rtcp_packet_sdes_next_item (packet);
2463 /* BYE is sent when a client leaves the session
2466 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
2467 RTPPacketInfo * pinfo)
2471 gboolean reconsider = FALSE;
2473 reason = gst_rtcp_packet_bye_get_reason (packet);
2474 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
2476 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
2477 for (i = 0; i < count; i++) {
2480 gboolean prevactive, prevsender;
2481 guint pmembers, members;
2483 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
2484 GST_DEBUG ("SSRC: %08x", ssrc);
2486 /* find src and mark bye, no probation when dealing with RTCP */
2487 source = find_source (sess, ssrc);
2488 if (!source || source->internal) {
2489 GST_DEBUG ("Ignoring suspicious BYE packet (reason: %s)",
2490 !source ? "can't find source" : "has internal source SSRC");
2494 /* store time for when we need to time out this source */
2495 source->bye_time = pinfo->current_time;
2497 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2498 prevsender = RTP_SOURCE_IS_SENDER (source);
2500 /* mark the source BYE */
2501 rtp_source_mark_bye (source, reason);
2503 pmembers = sess->stats.active_sources;
2505 source_update_active (sess, source, prevactive);
2506 source_update_sender (sess, source, prevsender);
2508 members = sess->stats.active_sources;
2510 if (!sess->scheduled_bye && members < pmembers) {
2511 /* some members went away since the previous timeout estimate.
2512 * Perform reverse reconsideration but only when we are not scheduling a
2514 if (sess->next_rtcp_check_time != GST_CLOCK_TIME_NONE &&
2515 pinfo->current_time < sess->next_rtcp_check_time) {
2516 GstClockTime time_remaining;
2518 /* Scale our next RTCP check time according to the change of numbers
2519 * of members. But only if a) this is the first RTCP, or b) this is not
2520 * a feedback session, or c) this is a feedback session but we schedule
2521 * for every RTCP interval (aka no t-rr-interval set).
2523 * FIXME: a) and b) are not great as we will possibly go below Tmin
2524 * for non-feedback profiles and in case of a) below
2525 * Tmin/t-rr-interval in any case.
2527 if (sess->last_rtcp_send_time == GST_CLOCK_TIME_NONE ||
2528 !(sess->rtp_profile == GST_RTP_PROFILE_AVPF
2529 || sess->rtp_profile == GST_RTP_PROFILE_SAVPF) ||
2530 sess->next_rtcp_check_time - sess->last_rtcp_send_time ==
2531 sess->last_rtcp_interval) {
2532 time_remaining = sess->next_rtcp_check_time - pinfo->current_time;
2533 sess->next_rtcp_check_time =
2534 gst_util_uint64_scale (time_remaining, members, pmembers);
2535 sess->next_rtcp_check_time += pinfo->current_time;
2537 sess->last_rtcp_interval =
2538 gst_util_uint64_scale (sess->last_rtcp_interval, members, pmembers);
2540 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
2541 GST_TIME_ARGS (sess->next_rtcp_check_time));
2543 /* mark pending reconsider. We only want to signal the reconsideration
2544 * once after we handled all the source in the bye packet */
2549 on_bye_ssrc (sess, source);
2552 RTP_SESSION_UNLOCK (sess);
2553 /* notify app of reconsideration */
2554 if (sess->callbacks.reconsider)
2555 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2556 RTP_SESSION_LOCK (sess);
2563 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
2564 RTPPacketInfo * pinfo)
2566 GST_DEBUG ("received APP");
2568 if (g_signal_has_handler_pending (sess,
2569 rtp_session_signals[SIGNAL_ON_APP_RTCP], 0, TRUE)) {
2570 GstBuffer *data_buffer = NULL;
2571 guint16 data_length;
2574 data_length = gst_rtcp_packet_app_get_data_length (packet) * 4;
2575 if (data_length > 0) {
2576 guint8 *data = gst_rtcp_packet_app_get_data (packet);
2577 data_buffer = gst_buffer_copy_region (packet->rtcp->buffer,
2578 GST_BUFFER_COPY_MEMORY, data - packet->rtcp->map.data, data_length);
2579 GST_BUFFER_PTS (data_buffer) = pinfo->running_time;
2582 memcpy (name, gst_rtcp_packet_app_get_name (packet), 4);
2585 RTP_SESSION_UNLOCK (sess);
2586 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_APP_RTCP], 0,
2587 gst_rtcp_packet_app_get_subtype (packet),
2588 gst_rtcp_packet_app_get_ssrc (packet), name, data_buffer);
2589 RTP_SESSION_LOCK (sess);
2592 gst_buffer_unref (data_buffer);
2597 rtp_session_request_local_key_unit (RTPSession * sess, RTPSource * src,
2598 guint32 media_ssrc, gboolean fir, GstClockTime current_time)
2600 guint32 round_trip = 0;
2602 rtp_source_get_last_rb (src, NULL, NULL, NULL, NULL, NULL, NULL, &round_trip);
2604 if (src->last_keyframe_request != GST_CLOCK_TIME_NONE && round_trip) {
2605 GstClockTime round_trip_in_ns = gst_util_uint64_scale (round_trip,
2608 /* Sanity check to avoid always ignoring PLI/FIR if we receive RTCP
2609 * packets with erroneous values resulting in crazy high RTT. */
2610 if (round_trip_in_ns > 5 * GST_SECOND)
2611 round_trip_in_ns = GST_SECOND / 2;
2613 if (current_time - src->last_keyframe_request < 2 * round_trip_in_ns) {
2614 GST_DEBUG ("Ignoring %s request from %X because one was send without one "
2615 "RTT (%" GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")",
2616 fir ? "FIR" : "PLI", rtp_source_get_ssrc (src),
2617 GST_TIME_ARGS (current_time - src->last_keyframe_request),
2618 GST_TIME_ARGS (round_trip_in_ns));
2623 src->last_keyframe_request = current_time;
2625 GST_LOG ("received %s request from %X about %X %p(%p)", fir ? "FIR" : "PLI",
2626 rtp_source_get_ssrc (src), media_ssrc, sess->callbacks.process_rtp,
2627 sess->callbacks.request_key_unit);
2629 RTP_SESSION_UNLOCK (sess);
2630 sess->callbacks.request_key_unit (sess, media_ssrc, fir,
2631 sess->request_key_unit_user_data);
2632 RTP_SESSION_LOCK (sess);
2638 rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
2639 guint32 media_ssrc, GstClockTime current_time)
2643 if (!sess->callbacks.request_key_unit)
2646 src = find_source (sess, sender_ssrc);
2648 /* try to find a src with media_ssrc instead */
2649 src = find_source (sess, media_ssrc);
2654 rtp_session_request_local_key_unit (sess, src, media_ssrc, FALSE,
2659 rtp_session_process_fir (RTPSession * sess, guint32 sender_ssrc,
2660 guint32 media_ssrc, guint8 * fci_data, guint fci_length,
2661 GstClockTime current_time)
2666 gboolean our_request = FALSE;
2668 if (!sess->callbacks.request_key_unit)
2674 src = find_source (sess, sender_ssrc);
2676 /* Hack because Google fails to set the sender_ssrc correctly */
2677 if (!src && sender_ssrc == 1) {
2678 GHashTableIter iter;
2680 /* we can't find the source if there are multiple */
2681 if (sess->stats.sender_sources > sess->stats.internal_sender_sources + 1)
2684 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
2685 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
2686 if (!src->internal && rtp_source_is_sender (src))
2694 for (position = 0; position < fci_length; position += 8) {
2695 guint8 *data = fci_data + position;
2698 ssrc = GST_READ_UINT32_BE (data);
2700 own = find_source (sess, ssrc);
2704 if (own->internal) {
2712 rtp_session_request_local_key_unit (sess, src, media_ssrc, TRUE,
2717 rtp_session_process_nack (RTPSession * sess, guint32 sender_ssrc,
2718 guint32 media_ssrc, guint8 * fci_data, guint fci_length,
2719 GstClockTime current_time)
2721 sess->stats.nacks_received++;
2723 if (!sess->callbacks.notify_nack)
2726 while (fci_length > 0) {
2727 guint16 seqnum, blp;
2729 seqnum = GST_READ_UINT16_BE (fci_data);
2730 blp = GST_READ_UINT16_BE (fci_data + 2);
2732 GST_DEBUG ("NACK #%u, blp %04x, SSRC 0x%08x", seqnum, blp, media_ssrc);
2734 RTP_SESSION_UNLOCK (sess);
2735 sess->callbacks.notify_nack (sess, seqnum, blp, media_ssrc,
2736 sess->notify_nack_user_data);
2737 RTP_SESSION_LOCK (sess);
2745 rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
2746 RTPPacketInfo * pinfo, GstClockTime current_time)
2749 GstRTCPFBType fbtype;
2750 guint32 sender_ssrc, media_ssrc;
2755 /* The feedback packet must include both sender SSRC and media SSRC */
2756 if (packet->length < 2)
2759 type = gst_rtcp_packet_get_type (packet);
2760 fbtype = gst_rtcp_packet_fb_get_type (packet);
2761 sender_ssrc = gst_rtcp_packet_fb_get_sender_ssrc (packet);
2762 media_ssrc = gst_rtcp_packet_fb_get_media_ssrc (packet);
2764 src = find_source (sess, media_ssrc);
2766 /* skip non-bye packets for sources that are marked BYE */
2767 if (sess->scheduled_bye && src && RTP_SOURCE_IS_MARKED_BYE (src))
2773 fci_data = gst_rtcp_packet_fb_get_fci (packet);
2774 fci_length = gst_rtcp_packet_fb_get_fci_length (packet) * sizeof (guint32);
2776 GST_DEBUG ("received feedback %d:%d from %08X about %08X with FCI of "
2777 "length %d", type, fbtype, sender_ssrc, media_ssrc, fci_length);
2779 if (g_signal_has_handler_pending (sess,
2780 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0, TRUE)) {
2781 GstBuffer *fci_buffer = NULL;
2783 if (fci_length > 0) {
2784 fci_buffer = gst_buffer_copy_region (packet->rtcp->buffer,
2785 GST_BUFFER_COPY_MEMORY, fci_data - packet->rtcp->map.data,
2787 GST_BUFFER_PTS (fci_buffer) = pinfo->running_time;
2790 RTP_SESSION_UNLOCK (sess);
2791 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0,
2792 type, fbtype, sender_ssrc, media_ssrc, fci_buffer);
2793 RTP_SESSION_LOCK (sess);
2796 gst_buffer_unref (fci_buffer);
2799 if (src && sess->rtcp_feedback_retention_window != GST_CLOCK_TIME_NONE) {
2800 rtp_source_retain_rtcp_packet (src, packet, pinfo->running_time);
2803 if ((src && src->internal) ||
2804 /* PSFB FIR puts the media ssrc inside the FCI */
2805 (type == GST_RTCP_TYPE_PSFB && fbtype == GST_RTCP_PSFB_TYPE_FIR)) {
2807 case GST_RTCP_TYPE_PSFB:
2809 case GST_RTCP_PSFB_TYPE_PLI:
2811 src->stats.recv_pli_count++;
2812 rtp_session_process_pli (sess, sender_ssrc, media_ssrc,
2815 case GST_RTCP_PSFB_TYPE_FIR:
2817 src->stats.recv_fir_count++;
2818 rtp_session_process_fir (sess, sender_ssrc, media_ssrc, fci_data,
2819 fci_length, current_time);
2825 case GST_RTCP_TYPE_RTPFB:
2827 case GST_RTCP_RTPFB_TYPE_NACK:
2829 src->stats.recv_nack_count++;
2830 rtp_session_process_nack (sess, sender_ssrc, media_ssrc,
2831 fci_data, fci_length, current_time);
2842 g_object_unref (src);
2846 * rtp_session_process_rtcp:
2847 * @sess: and #RTPSession
2848 * @buffer: an RTCP buffer
2849 * @current_time: the current system time
2850 * @ntpnstime: the current NTP time in nanoseconds
2852 * Process an RTCP buffer in the session manager. This function takes ownership
2855 * Returns: a #GstFlowReturn.
2858 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
2859 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
2861 GstRTCPPacket packet;
2862 gboolean more, is_bye = FALSE, do_sync = FALSE;
2863 RTPPacketInfo pinfo = { 0, };
2864 GstFlowReturn result = GST_FLOW_OK;
2865 GstRTCPBuffer rtcp = { NULL, };
2867 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2868 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2870 if (!gst_rtcp_buffer_validate_reduced (buffer))
2871 goto invalid_packet;
2873 GST_DEBUG ("received RTCP packet");
2875 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_RECEIVING_RTCP], 0,
2878 RTP_SESSION_LOCK (sess);
2879 /* update pinfo stats */
2880 update_packet_info (sess, &pinfo, FALSE, FALSE, FALSE, buffer, current_time,
2881 running_time, ntpnstime);
2883 /* start processing the compound packet */
2884 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
2885 more = gst_rtcp_buffer_get_first_packet (&rtcp, &packet);
2889 type = gst_rtcp_packet_get_type (&packet);
2892 case GST_RTCP_TYPE_SR:
2893 rtp_session_process_sr (sess, &packet, &pinfo, &do_sync);
2895 case GST_RTCP_TYPE_RR:
2896 rtp_session_process_rr (sess, &packet, &pinfo);
2898 case GST_RTCP_TYPE_SDES:
2899 rtp_session_process_sdes (sess, &packet, &pinfo);
2901 case GST_RTCP_TYPE_BYE:
2903 /* don't try to attempt lip-sync anymore for streams with a BYE */
2905 rtp_session_process_bye (sess, &packet, &pinfo);
2907 case GST_RTCP_TYPE_APP:
2908 rtp_session_process_app (sess, &packet, &pinfo);
2910 case GST_RTCP_TYPE_RTPFB:
2911 case GST_RTCP_TYPE_PSFB:
2912 rtp_session_process_feedback (sess, &packet, &pinfo, current_time);
2914 case GST_RTCP_TYPE_XR:
2915 /* FIXME: This block is added to downgrade warning level.
2916 * Once the parser is implemented, it should be replaced with
2917 * a proper process function. */
2918 GST_DEBUG ("got RTCP XR packet, but ignored");
2921 GST_WARNING ("got unknown RTCP packet type: %d", type);
2924 more = gst_rtcp_packet_move_to_next (&packet);
2927 gst_rtcp_buffer_unmap (&rtcp);
2929 /* if we are scheduling a BYE, we only want to count bye packets, else we
2930 * count everything */
2931 if (sess->scheduled_bye && is_bye) {
2932 sess->bye_stats.bye_members++;
2933 UPDATE_AVG (sess->bye_stats.avg_rtcp_packet_size, pinfo.bytes);
2936 /* keep track of average packet size */
2937 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, pinfo.bytes);
2939 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
2940 sess->stats.avg_rtcp_packet_size, pinfo.bytes);
2941 RTP_SESSION_UNLOCK (sess);
2944 clean_packet_info (&pinfo);
2946 /* notify caller of sr packets in the callback */
2947 if (do_sync && sess->callbacks.sync_rtcp) {
2948 result = sess->callbacks.sync_rtcp (sess, buffer,
2949 sess->sync_rtcp_user_data);
2951 gst_buffer_unref (buffer);
2958 GST_DEBUG ("invalid RTCP packet received");
2959 gst_buffer_unref (buffer);
2965 * rtp_session_update_send_caps:
2966 * @sess: an #RTPSession
2969 * Update the caps of the sender in the rtp session.
2972 rtp_session_update_send_caps (RTPSession * sess, GstCaps * caps)
2977 g_return_if_fail (RTP_IS_SESSION (sess));
2978 g_return_if_fail (GST_IS_CAPS (caps));
2980 GST_LOG ("received caps %" GST_PTR_FORMAT, caps);
2982 s = gst_caps_get_structure (caps, 0);
2984 if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
2988 RTP_SESSION_LOCK (sess);
2989 source = obtain_internal_source (sess, ssrc, &created, GST_CLOCK_TIME_NONE);
2990 sess->suggested_ssrc = ssrc;
2991 sess->internal_ssrc_set = TRUE;
2992 sess->internal_ssrc_from_caps_or_property = TRUE;
2994 rtp_source_update_caps (source, caps);
2997 on_new_sender_ssrc (sess, source);
2999 g_object_unref (source);
3002 if (gst_structure_get_uint (s, "rtx-ssrc", &ssrc)) {
3004 obtain_internal_source (sess, ssrc, &created, GST_CLOCK_TIME_NONE);
3006 rtp_source_update_caps (source, caps);
3009 on_new_sender_ssrc (sess, source);
3011 g_object_unref (source);
3014 RTP_SESSION_UNLOCK (sess);
3016 sess->internal_ssrc_from_caps_or_property = FALSE;
3021 * rtp_session_send_rtp:
3022 * @sess: an #RTPSession
3023 * @data: pointer to either an RTP buffer or a list of RTP buffers
3024 * @is_list: TRUE when @data is a buffer list
3025 * @current_time: the current system time
3026 * @running_time: the running time of @data
3028 * Send the RTP data (a buffer or buffer list) in the session manager. This
3029 * function takes ownership of @data.
3031 * Returns: a #GstFlowReturn.
3034 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
3035 GstClockTime current_time, GstClockTime running_time)
3037 GstFlowReturn result;
3039 gboolean prevsender;
3041 RTPPacketInfo pinfo = { 0, };
3044 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3045 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
3047 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
3049 RTP_SESSION_LOCK (sess);
3050 if (!update_packet_info (sess, &pinfo, TRUE, TRUE, is_list, data,
3051 current_time, running_time, -1))
3052 goto invalid_packet;
3054 source = obtain_internal_source (sess, pinfo.ssrc, &created, current_time);
3056 on_new_sender_ssrc (sess, source);
3058 if (!source->internal)
3059 /* FIXME: Send GstRTPCollision upstream */
3062 prevsender = RTP_SOURCE_IS_SENDER (source);
3063 oldrate = source->bitrate;
3065 /* we use our own source to send */
3066 result = rtp_source_send_rtp (source, &pinfo);
3068 source_update_sender (sess, source, prevsender);
3070 if (oldrate != source->bitrate)
3071 sess->recalc_bandwidth = TRUE;
3072 RTP_SESSION_UNLOCK (sess);
3074 g_object_unref (source);
3075 clean_packet_info (&pinfo);
3081 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3082 RTP_SESSION_UNLOCK (sess);
3083 GST_DEBUG ("invalid RTP packet received");
3088 g_object_unref (source);
3089 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3090 RTP_SESSION_UNLOCK (sess);
3091 GST_WARNING ("non-internal source with same ssrc %08x, drop packet",
3098 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
3100 *bandwidth += source->bitrate;
3103 /* must be called with session lock */
3105 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
3108 GstClockTime result;
3109 RTPSessionStats *stats;
3111 /* recalculate bandwidth when it changed */
3112 if (sess->recalc_bandwidth) {
3115 if (sess->bandwidth > 0)
3116 bandwidth = sess->bandwidth;
3118 /* If it is <= 0, then try to estimate the actual bandwidth */
3121 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3122 (GHFunc) add_bitrates, &bandwidth);
3124 if (bandwidth < RTP_STATS_BANDWIDTH)
3125 bandwidth = RTP_STATS_BANDWIDTH;
3127 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
3128 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
3130 sess->recalc_bandwidth = FALSE;
3133 if (sess->scheduled_bye) {
3134 stats = &sess->bye_stats;
3135 result = rtp_stats_calculate_bye_interval (stats);
3137 session_update_ptp (sess);
3139 stats = &sess->stats;
3140 result = rtp_stats_calculate_rtcp_interval (stats,
3141 stats->internal_sender_sources > 0, sess->rtp_profile,
3142 sess->is_doing_ptp, first);
3145 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
3146 GST_TIME_ARGS (result), first);
3148 if (!deterministic && result != GST_CLOCK_TIME_NONE)
3149 result = rtp_stats_add_rtcp_jitter (stats, result);
3151 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
3157 source_mark_bye (const gchar * key, RTPSource * source, const gchar * reason)
3159 if (source->internal)
3160 rtp_source_mark_bye (source, reason);
3164 * rtp_session_mark_all_bye:
3165 * @sess: an #RTPSession
3168 * Mark all internal sources of the session as BYE with @reason.
3171 rtp_session_mark_all_bye (RTPSession * sess, const gchar * reason)
3173 g_return_if_fail (RTP_IS_SESSION (sess));
3175 RTP_SESSION_LOCK (sess);
3176 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3177 (GHFunc) source_mark_bye, (gpointer) reason);
3178 RTP_SESSION_UNLOCK (sess);
3181 /* Stop the current @sess and schedule a BYE message for the other members.
3182 * One must have the session lock to call this function
3184 static GstFlowReturn
3185 rtp_session_schedule_bye_locked (RTPSession * sess, GstClockTime current_time)
3187 GstFlowReturn result = GST_FLOW_OK;
3188 GstClockTime interval;
3190 /* nothing to do it we already scheduled bye */
3191 if (sess->scheduled_bye)
3194 /* we schedule BYE now */
3195 sess->scheduled_bye = TRUE;
3196 /* at least one member wants to send a BYE */
3197 memcpy (&sess->bye_stats, &sess->stats, sizeof (RTPSessionStats));
3198 INIT_AVG (sess->bye_stats.avg_rtcp_packet_size, 100);
3199 sess->bye_stats.bye_members = 1;
3200 sess->first_rtcp = TRUE;
3202 /* reschedule transmission */
3203 sess->last_rtcp_send_time = current_time;
3204 sess->last_rtcp_check_time = current_time;
3205 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
3207 if (interval != GST_CLOCK_TIME_NONE)
3208 sess->next_rtcp_check_time = current_time + interval;
3210 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
3211 sess->last_rtcp_interval = interval;
3213 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
3214 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
3216 RTP_SESSION_UNLOCK (sess);
3217 /* notify app of reconsideration */
3218 if (sess->callbacks.reconsider)
3219 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
3220 RTP_SESSION_LOCK (sess);
3227 * rtp_session_schedule_bye:
3228 * @sess: an #RTPSession
3229 * @current_time: the current system time
3231 * Schedule a BYE message for all sources marked as BYE in @sess.
3233 * Returns: a #GstFlowReturn.
3236 rtp_session_schedule_bye (RTPSession * sess, GstClockTime current_time)
3238 GstFlowReturn result;
3240 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3242 RTP_SESSION_LOCK (sess);
3243 result = rtp_session_schedule_bye_locked (sess, current_time);
3244 RTP_SESSION_UNLOCK (sess);
3250 * rtp_session_next_timeout:
3251 * @sess: an #RTPSession
3252 * @current_time: the current system time
3254 * Get the next time we should perform session maintenance tasks.
3256 * Returns: a time when rtp_session_on_timeout() should be called with the
3257 * current system time.
3260 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
3262 GstClockTime result, interval = 0;
3264 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
3266 RTP_SESSION_LOCK (sess);
3268 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
3269 GST_DEBUG ("have early rtcp time");
3270 result = sess->next_early_rtcp_time;
3274 result = sess->next_rtcp_check_time;
3276 GST_DEBUG ("current time: %" GST_TIME_FORMAT
3277 ", next time: %" GST_TIME_FORMAT,
3278 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
3280 if (result == GST_CLOCK_TIME_NONE || result < current_time) {
3281 GST_DEBUG ("take current time as base");
3282 /* our previous check time expired, start counting from the current time
3284 result = current_time;
3287 if (sess->scheduled_bye) {
3288 if (sess->bye_stats.active_sources >= 50) {
3289 GST_DEBUG ("reconsider BYE, more than 50 sources");
3290 /* reconsider BYE if members >= 50 */
3291 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
3292 sess->last_rtcp_interval = interval;
3295 if (sess->first_rtcp) {
3296 GST_DEBUG ("first RTCP packet");
3297 /* we are called for the first time */
3298 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
3299 sess->last_rtcp_interval = interval;
3300 } else if (sess->next_rtcp_check_time < current_time) {
3301 GST_DEBUG ("old check time expired, getting new timeout");
3302 /* get a new timeout when we need to */
3303 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
3304 sess->last_rtcp_interval = interval;
3306 if ((sess->rtp_profile == GST_RTP_PROFILE_AVPF
3307 || sess->rtp_profile == GST_RTP_PROFILE_SAVPF)
3308 && interval != GST_CLOCK_TIME_NONE) {
3309 /* Apply the rules from RFC 4585 section 3.5.3 */
3310 if (sess->stats.min_interval != 0) {
3311 GstClockTime T_rr_current_interval = g_random_double_range (0.5,
3312 1.5) * sess->stats.min_interval * GST_SECOND;
3314 if (T_rr_current_interval > interval) {
3315 GST_DEBUG ("Adjusting interval for t-rr-interval: %" GST_TIME_FORMAT
3316 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (T_rr_current_interval),
3317 GST_TIME_ARGS (interval));
3318 interval = T_rr_current_interval;
3325 if (interval != GST_CLOCK_TIME_NONE)
3328 result = GST_CLOCK_TIME_NONE;
3330 sess->next_rtcp_check_time = result;
3334 GST_DEBUG ("current time: %" GST_TIME_FORMAT
3335 ", next time: %" GST_TIME_FORMAT,
3336 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
3337 RTP_SESSION_UNLOCK (sess);
3351 GstRTCPBuffer rtcpbuf;
3354 guint num_to_report;
3359 GstClockTime current_time;
3361 GstClockTime running_time;
3362 GstClockTime interval;
3363 GstRTCPPacket packet;
3366 gboolean may_suppress;
3368 guint nacked_seqnums;
3372 session_start_rtcp (RTPSession * sess, ReportData * data)
3374 GstRTCPPacket *packet = &data->packet;
3375 RTPSource *own = data->source;
3376 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3378 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
3379 data->has_sdes = FALSE;
3381 gst_rtcp_buffer_map (data->rtcp, GST_MAP_READWRITE, rtcp);
3383 if (data->is_early && sess->reduced_size_rtcp)
3386 if (RTP_SOURCE_IS_SENDER (own)) {
3389 guint32 packet_count, octet_count;
3391 /* we are a sender, create SR */
3392 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
3393 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SR, packet);
3395 /* get latest stats */
3396 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
3397 &ntptime, &rtptime, &packet_count, &octet_count);
3399 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
3400 packet_count, octet_count);
3402 /* fill in sender report info */
3403 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
3404 ntptime, rtptime, packet_count, octet_count);
3406 /* we are only receiver, create RR */
3407 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
3408 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RR, packet);
3409 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
3413 /* construct a Sender or Receiver Report */
3415 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
3417 RTPSession *sess = data->sess;
3418 GstRTCPPacket *packet = &data->packet;
3419 guint8 fractionlost;
3421 guint32 exthighestseq, jitter;
3424 /* don't report for sources in future generations */
3425 if (((gint16) (source->generation - sess->generation)) > 0) {
3426 GST_DEBUG ("source %08x generation %u > %u", source->ssrc,
3427 source->generation, sess->generation);
3431 if (g_hash_table_contains (source->reported_in_sr_of,
3432 GUINT_TO_POINTER (data->source->ssrc))) {
3433 GST_DEBUG ("source %08x already reported in this generation", source->ssrc);
3437 if (gst_rtcp_packet_get_rb_count (packet) == GST_RTCP_MAX_RB_COUNT) {
3438 GST_DEBUG ("max RB count reached");
3442 /* only report about remote sources */
3443 if (source->internal)
3446 if (!RTP_SOURCE_IS_SENDER (source)) {
3447 GST_DEBUG ("source %08x not sender", source->ssrc);
3451 if (source->disable_rtcp) {
3452 GST_DEBUG ("source %08x has RTCP disabled", source->ssrc);
3456 GST_DEBUG ("create RB for SSRC %08x", source->ssrc);
3459 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
3460 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
3462 /* store last generated RR packet */
3463 source->last_rr.is_valid = TRUE;
3464 source->last_rr.fractionlost = fractionlost;
3465 source->last_rr.packetslost = packetslost;
3466 source->last_rr.exthighestseq = exthighestseq;
3467 source->last_rr.jitter = jitter;
3468 source->last_rr.lsr = lsr;
3469 source->last_rr.dlsr = dlsr;
3471 /* packet is not yet filled, add report block for this source. */
3472 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
3473 exthighestseq, jitter, lsr, dlsr);
3476 g_hash_table_add (source->reported_in_sr_of,
3477 GUINT_TO_POINTER (data->source->ssrc));
3482 session_add_fir (const gchar * key, RTPSource * source, ReportData * data)
3484 GstRTCPPacket *packet = &data->packet;
3488 if (!source->send_fir)
3491 len = gst_rtcp_packet_fb_get_fci_length (packet);
3492 if (!gst_rtcp_packet_fb_set_fci_length (packet, len + 2))
3493 /* exit because the packet is full, will put next request in a
3497 fci_data = gst_rtcp_packet_fb_get_fci (packet) + (len * 4);
3499 GST_WRITE_UINT32_BE (fci_data, source->ssrc);
3501 fci_data[0] = source->current_send_fir_seqnum;
3502 fci_data[1] = fci_data[2] = fci_data[3] = 0;
3504 source->send_fir = FALSE;
3505 source->stats.sent_fir_count++;
3509 session_fir (RTPSession * sess, ReportData * data)
3511 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3512 GstRTCPPacket *packet = &data->packet;
3514 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
3517 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_FIR);
3518 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3519 gst_rtcp_packet_fb_set_media_ssrc (packet, 0);
3521 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3522 (GHFunc) session_add_fir, data);
3524 if (gst_rtcp_packet_fb_get_fci_length (packet) == 0)
3525 gst_rtcp_packet_remove (packet);
3527 data->may_suppress = FALSE;
3531 has_pli_compare_func (gconstpointer a, gconstpointer ignored)
3533 GstRTCPPacket packet;
3534 GstRTCPBuffer rtcp = { NULL, };
3535 gboolean ret = FALSE;
3537 gst_rtcp_buffer_map ((GstBuffer *) a, GST_MAP_READ, &rtcp);
3539 if (gst_rtcp_buffer_get_first_packet (&rtcp, &packet)) {
3540 if (gst_rtcp_packet_get_type (&packet) == GST_RTCP_TYPE_PSFB &&
3541 gst_rtcp_packet_fb_get_type (&packet) == GST_RTCP_PSFB_TYPE_PLI)
3545 gst_rtcp_buffer_unmap (&rtcp);
3552 session_pli (const gchar * key, RTPSource * source, ReportData * data)
3554 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3555 GstRTCPPacket *packet = &data->packet;
3557 if (!source->send_pli)
3560 if (rtp_source_has_retained (source, has_pli_compare_func, NULL))
3563 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
3564 /* exit because the packet is full, will put next request in a
3568 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_PLI);
3569 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3570 gst_rtcp_packet_fb_set_media_ssrc (packet, source->ssrc);
3572 source->send_pli = FALSE;
3573 data->may_suppress = FALSE;
3575 source->stats.sent_pli_count++;
3578 /* construct NACK */
3580 session_nack (const gchar * key, RTPSource * source, ReportData * data)
3582 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3583 GstRTCPPacket *packet = &data->packet;
3588 if (!source->send_nack)
3591 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RTPFB, packet))
3592 /* exit because the packet is full, will put next request in a
3596 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_RTPFB_TYPE_NACK);
3597 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3598 gst_rtcp_packet_fb_set_media_ssrc (packet, source->ssrc);
3600 nacks = rtp_source_get_nacks (source, &n_nacks);
3601 GST_DEBUG ("%u NACKs", n_nacks);
3602 if (!gst_rtcp_packet_fb_set_fci_length (packet, n_nacks))
3605 fci_data = gst_rtcp_packet_fb_get_fci (packet);
3606 for (i = 0; i < n_nacks; i++) {
3607 GST_WRITE_UINT32_BE (fci_data, nacks[i]);
3609 data->nacked_seqnums++;
3612 rtp_source_clear_nacks (source);
3613 data->may_suppress = FALSE;
3614 source->stats.sent_nack_count += n_nacks;
3617 /* perform cleanup of sources that timed out */
3619 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
3621 gboolean remove = FALSE;
3622 gboolean byetimeout = FALSE;
3623 gboolean sendertimeout = FALSE;
3624 gboolean is_sender, is_active;
3625 RTPSession *sess = data->sess;
3626 GstClockTime interval, binterval;
3629 GST_DEBUG ("look at %08x, generation %u", source->ssrc, source->generation);
3631 /* check for outdated collisions */
3632 if (source->internal) {
3633 GST_DEBUG ("Timing out collisions for %x", source->ssrc);
3634 rtp_source_timeout (source, data->current_time, data->running_time,
3635 sess->rtcp_feedback_retention_window);
3638 /* nothing else to do when without RTCP */
3639 if (data->interval == GST_CLOCK_TIME_NONE)
3642 is_sender = RTP_SOURCE_IS_SENDER (source);
3643 is_active = RTP_SOURCE_IS_ACTIVE (source);
3645 /* our own rtcp interval may have been forced low by secondary configuration,
3646 * while sender side may still operate with higher interval,
3647 * so do not just take our interval to decide on timing out sender,
3648 * but take (if data->interval <= 5 * GST_SECOND):
3649 * interval = CLAMP (sender_interval, data->interval, 5 * GST_SECOND)
3650 * where sender_interval is difference between last 2 received RTCP reports
3652 if (data->interval >= 5 * GST_SECOND || source->internal) {
3653 binterval = data->interval;
3655 GST_LOG ("prev_rtcp %" GST_TIME_FORMAT ", last_rtcp %" GST_TIME_FORMAT,
3656 GST_TIME_ARGS (source->stats.prev_rtcptime),
3657 GST_TIME_ARGS (source->stats.last_rtcptime));
3658 /* if not received enough yet, fallback to larger default */
3659 if (source->stats.last_rtcptime > source->stats.prev_rtcptime)
3660 binterval = source->stats.last_rtcptime - source->stats.prev_rtcptime;
3662 binterval = 5 * GST_SECOND;
3663 binterval = CLAMP (binterval, data->interval, 5 * GST_SECOND);
3665 GST_LOG ("timeout base interval %" GST_TIME_FORMAT,
3666 GST_TIME_ARGS (binterval));
3668 if (!source->internal && source->marked_bye) {
3669 /* if we received a BYE from the source, remove the source after some
3671 if (data->current_time > source->bye_time &&
3672 data->current_time - source->bye_time > sess->stats.bye_timeout) {
3673 GST_DEBUG ("removing BYE source %08x", source->ssrc);
3679 if (source->internal && source->sent_bye) {
3680 GST_DEBUG ("removing internal source that has sent BYE %08x", source->ssrc);
3684 /* sources that were inactive for more than 5 times the deterministic reporting
3685 * interval get timed out. the min timeout is 5 seconds. */
3686 /* mind old time that might pre-date last time going to PLAYING */
3687 btime = MAX (source->last_activity, sess->start_time);
3688 if (data->current_time > btime) {
3689 interval = MAX (binterval * 5, 5 * GST_SECOND);
3690 if (data->current_time - btime > interval) {
3691 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
3692 source->ssrc, GST_TIME_ARGS (btime));
3693 if (source->internal) {
3694 /* this is an internal source that is not using our suggested ssrc.
3695 * since there must be another source using this ssrc, we can remove
3696 * this one instead of making it a receiver forever */
3697 if (source->ssrc != sess->suggested_ssrc) {
3698 rtp_source_mark_bye (source, "timed out");
3699 /* do not schedule bye here, since we are inside the RTCP timeout
3700 * processing and scheduling bye will interfere with SR/RR sending */
3708 /* senders that did not send for a long time become a receiver, this also
3709 * holds for our own sources. */
3711 /* mind old time that might pre-date last time going to PLAYING */
3712 btime = MAX (source->last_rtp_activity, sess->start_time);
3713 if (data->current_time > btime) {
3714 interval = MAX (binterval * 2, 5 * GST_SECOND);
3715 if (data->current_time - btime > interval) {
3716 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
3717 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
3718 sendertimeout = TRUE;
3724 sess->total_sources--;
3726 sess->stats.sender_sources--;
3727 if (source->internal)
3728 sess->stats.internal_sender_sources--;
3731 sess->stats.active_sources--;
3733 if (source->internal)
3734 sess->stats.internal_sources--;
3737 on_bye_timeout (sess, source);
3739 on_timeout (sess, source);
3741 if (sendertimeout) {
3742 source->is_sender = FALSE;
3743 sess->stats.sender_sources--;
3744 if (source->internal)
3745 sess->stats.internal_sender_sources--;
3747 on_sender_timeout (sess, source);
3749 /* count how many source to report in this generation */
3750 if (((gint16) (source->generation - sess->generation)) <= 0)
3751 data->num_to_report++;
3753 source->closing = remove;
3757 session_sdes (RTPSession * sess, ReportData * data)
3759 GstRTCPPacket *packet = &data->packet;
3760 const GstStructure *sdes;
3762 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3764 /* add SDES packet */
3765 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SDES, packet);
3767 gst_rtcp_packet_sdes_add_item (packet, data->source->ssrc);
3769 sdes = rtp_source_get_sdes_struct (data->source);
3771 /* add all fields in the structure, the order is not important. */
3772 n_fields = gst_structure_n_fields (sdes);
3773 for (i = 0; i < n_fields; ++i) {
3776 GstRTCPSDESType type;
3778 field = gst_structure_nth_field_name (sdes, i);
3781 value = gst_structure_get_string (sdes, field);
3784 type = gst_rtcp_sdes_name_to_type (field);
3786 /* Early packets are minimal and only include the CNAME */
3787 if (data->is_early && type != GST_RTCP_SDES_CNAME)
3790 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
3791 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
3792 (const guint8 *) value);
3793 } else if (type == GST_RTCP_SDES_PRIV) {
3799 /* don't accept entries that are too big */
3800 prefix_len = strlen (field);
3801 if (prefix_len > 255)
3803 value_len = strlen (value);
3804 if (value_len > 255)
3806 data_len = 1 + prefix_len + value_len;
3810 data[0] = prefix_len;
3811 memcpy (&data[1], field, prefix_len);
3812 memcpy (&data[1 + prefix_len], value, value_len);
3814 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
3818 data->has_sdes = TRUE;
3821 /* schedule a BYE packet */
3823 make_source_bye (RTPSession * sess, RTPSource * source, ReportData * data)
3825 GstRTCPPacket *packet = &data->packet;
3826 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3829 session_sdes (sess, data);
3830 /* add a BYE packet */
3831 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_BYE, packet);
3832 gst_rtcp_packet_bye_add_ssrc (packet, source->ssrc);
3833 if (source->bye_reason)
3834 gst_rtcp_packet_bye_set_reason (packet, source->bye_reason);
3836 /* we have a BYE packet now */
3837 source->sent_bye = TRUE;
3841 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
3843 GstClockTime new_send_time;
3844 GstClockTime interval;
3845 RTPSessionStats *stats;
3847 if (sess->scheduled_bye)
3848 stats = &sess->bye_stats;
3850 stats = &sess->stats;
3852 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3853 data->is_early = TRUE;
3855 data->is_early = FALSE;
3857 if (data->is_early && sess->next_early_rtcp_time <= current_time) {
3858 GST_DEBUG ("early feedback %" GST_TIME_FORMAT " <= now %"
3859 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_early_rtcp_time),
3860 GST_TIME_ARGS (current_time));
3861 } else if (sess->next_rtcp_check_time == GST_CLOCK_TIME_NONE ||
3862 sess->next_rtcp_check_time > current_time) {
3863 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
3864 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
3865 GST_TIME_ARGS (current_time));
3869 /* take interval and add jitter */
3870 interval = data->interval;
3871 if (interval != GST_CLOCK_TIME_NONE)
3872 interval = rtp_stats_add_rtcp_jitter (stats, interval);
3874 if (sess->last_rtcp_check_time != GST_CLOCK_TIME_NONE) {
3875 /* perform forward reconsideration */
3876 if (interval != GST_CLOCK_TIME_NONE) {
3877 GstClockTime elapsed;
3879 /* get elapsed time since we last reported */
3880 elapsed = current_time - sess->last_rtcp_check_time;
3882 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
3883 GST_TIME_FORMAT, GST_TIME_ARGS (interval), GST_TIME_ARGS (elapsed));
3884 new_send_time = interval + sess->last_rtcp_check_time;
3886 new_send_time = sess->last_rtcp_check_time;
3889 /* If this is the first RTCP packet, we can reconsider anything based
3890 * on the last RTCP send time because there was none.
3892 g_warn_if_fail (!data->is_early);
3893 data->is_early = FALSE;
3894 new_send_time = current_time;
3897 if (!data->is_early) {
3898 /* check if reconsideration */
3899 if (new_send_time == GST_CLOCK_TIME_NONE || current_time < new_send_time) {
3900 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
3901 GST_TIME_ARGS (new_send_time));
3902 /* store new check time */
3903 sess->next_rtcp_check_time = new_send_time;
3904 sess->last_rtcp_interval = interval;
3908 sess->last_rtcp_interval = interval;
3909 if ((sess->rtp_profile == GST_RTP_PROFILE_AVPF
3910 || sess->rtp_profile == GST_RTP_PROFILE_SAVPF)
3911 && interval != GST_CLOCK_TIME_NONE) {
3912 /* Apply the rules from RFC 4585 section 3.5.3 */
3913 if (stats->min_interval != 0 && !sess->first_rtcp) {
3914 GstClockTime T_rr_current_interval =
3915 g_random_double_range (0.5, 1.5) * stats->min_interval * GST_SECOND;
3917 if (T_rr_current_interval > interval) {
3918 GST_DEBUG ("Adjusting interval for t-rr-interval: %" GST_TIME_FORMAT
3919 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (T_rr_current_interval),
3920 GST_TIME_ARGS (interval));
3921 interval = T_rr_current_interval;
3925 sess->next_rtcp_check_time = current_time + interval;
3929 GST_DEBUG ("can send RTCP now, next %" GST_TIME_FORMAT,
3930 GST_TIME_ARGS (sess->next_rtcp_check_time));
3936 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
3938 g_hash_table_insert (hash_table, key, g_object_ref (source));
3942 remove_closing_sources (const gchar * key, RTPSource * source,
3945 if (source->closing)
3948 if (source->send_fir)
3949 data->have_fir = TRUE;
3950 if (source->send_pli)
3951 data->have_pli = TRUE;
3952 if (source->send_nack)
3953 data->have_nack = TRUE;
3959 generate_rtcp (const gchar * key, RTPSource * source, ReportData * data)
3961 RTPSession *sess = data->sess;
3962 gboolean is_bye = FALSE;
3963 ReportOutput *output;
3965 /* only generate RTCP for active internal sources */
3966 if (!source->internal || source->sent_bye)
3969 /* ignore other sources when we do the timeout after a scheduled BYE */
3970 if (sess->scheduled_bye && !source->marked_bye)
3973 /* skip if RTCP is disabled */
3974 if (source->disable_rtcp) {
3975 GST_DEBUG ("source %08x has RTCP disabled", source->ssrc);
3979 data->source = source;
3982 session_start_rtcp (sess, data);
3984 if (source->marked_bye) {
3986 make_source_bye (sess, source, data);
3988 } else if (!data->is_early) {
3989 /* loop over all known sources and add report blocks. If we are early, we
3990 * just make a minimal RTCP packet and skip this step */
3991 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3992 (GHFunc) session_report_blocks, data);
3994 if (!data->has_sdes && (!data->is_early || !sess->reduced_size_rtcp))
3995 session_sdes (sess, data);
3998 session_fir (sess, data);
4001 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4002 (GHFunc) session_pli, data);
4004 if (data->have_nack)
4005 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4006 (GHFunc) session_nack, data);
4008 gst_rtcp_buffer_unmap (&data->rtcpbuf);
4010 output = g_slice_new (ReportOutput);
4011 output->source = g_object_ref (source);
4012 output->is_bye = is_bye;
4013 output->buffer = data->rtcp;
4014 /* queue the RTCP packet to push later */
4015 g_queue_push_tail (&data->output, output);
4019 update_generation (const gchar * key, RTPSource * source, ReportData * data)
4021 RTPSession *sess = data->sess;
4023 if (g_hash_table_size (source->reported_in_sr_of) >=
4024 sess->stats.internal_sources) {
4025 /* source is reported, move to next generation */
4026 source->generation = sess->generation + 1;
4027 g_hash_table_remove_all (source->reported_in_sr_of);
4029 GST_LOG ("reported source %x, new generation: %d", source->ssrc,
4030 source->generation);
4032 /* if we reported all sources in this generation, move to next */
4033 if (--data->num_to_report == 0) {
4035 GST_DEBUG ("all reported, generation now %u", sess->generation);
4041 rtp_session_are_all_sources_bye (RTPSession * sess)
4043 GHashTableIter iter;
4046 RTP_SESSION_LOCK (sess);
4047 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
4048 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
4049 if (src->internal && !src->sent_bye) {
4050 RTP_SESSION_UNLOCK (sess);
4054 RTP_SESSION_UNLOCK (sess);
4060 * rtp_session_on_timeout:
4061 * @sess: an #RTPSession
4062 * @current_time: the current system time
4063 * @ntpnstime: the current NTP time in nanoseconds
4064 * @running_time: the current running_time of the pipeline
4066 * Perform maintenance actions after the timeout obtained with
4067 * rtp_session_next_timeout() expired.
4069 * This function will perform timeouts of receivers and senders, send a BYE
4070 * packet or generate RTCP packets with current session stats.
4072 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
4073 * times, for each packet that should be processed.
4075 * Returns: a #GstFlowReturn.
4078 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
4079 guint64 ntpnstime, GstClockTime running_time)
4081 GstFlowReturn result = GST_FLOW_OK;
4082 ReportData data = { GST_RTCP_BUFFER_INIT };
4083 GHashTable *table_copy;
4084 ReportOutput *output;
4085 gboolean all_empty = FALSE;
4087 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
4089 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT
4090 ", running-time %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
4091 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (running_time));
4094 data.current_time = current_time;
4095 data.ntpnstime = ntpnstime;
4096 data.running_time = running_time;
4097 data.num_to_report = 0;
4098 data.may_suppress = FALSE;
4099 data.nacked_seqnums = 0;
4100 g_queue_init (&data.output);
4102 RTP_SESSION_LOCK (sess);
4103 /* get a new interval, we need this for various cleanups etc */
4104 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
4106 GST_DEBUG ("interval %" GST_TIME_FORMAT, GST_TIME_ARGS (data.interval));
4108 /* we need an internal source now */
4109 if (sess->stats.internal_sources == 0) {
4113 source = obtain_internal_source (sess, sess->suggested_ssrc, &created,
4115 sess->internal_ssrc_set = TRUE;
4118 on_new_sender_ssrc (sess, source);
4120 g_object_unref (source);
4123 sess->conflicting_addresses =
4124 timeout_conflicting_addresses (sess->conflicting_addresses, current_time);
4126 /* Make a local copy of the hashtable. We need to do this because the
4127 * cleanup stage below releases the session lock. */
4128 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
4129 (GDestroyNotify) g_object_unref);
4130 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4131 (GHFunc) clone_ssrcs_hashtable, table_copy);
4133 /* Clean up the session, mark the source for removing, this might release the
4135 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
4136 g_hash_table_destroy (table_copy);
4138 /* Now remove the marked sources */
4139 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
4140 (GHRFunc) remove_closing_sources, &data);
4142 /* update point-to-point status */
4143 session_update_ptp (sess);
4145 /* see if we need to generate SR or RR packets */
4146 if (!is_rtcp_time (sess, current_time, &data))
4149 /* check if all the buffers are empty afer generation */
4153 ("doing RTCP generation %u for %u sources, early %d, may suppress %d",
4154 sess->generation, data.num_to_report, data.is_early, data.may_suppress);
4156 /* generate RTCP for all internal sources */
4157 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4158 (GHFunc) generate_rtcp, &data);
4160 /* update the generation for all the sources that have been reported */
4161 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4162 (GHFunc) update_generation, &data);
4164 /* we keep track of the last report time in order to timeout inactive
4165 * receivers or senders */
4166 if (!data.is_early) {
4167 GST_DEBUG ("Time since last regular RTCP: %" GST_TIME_FORMAT " - %"
4168 GST_TIME_FORMAT " = %" GST_TIME_FORMAT,
4169 GST_TIME_ARGS (data.current_time),
4170 GST_TIME_ARGS (sess->last_rtcp_send_time),
4171 GST_TIME_ARGS (data.current_time - sess->last_rtcp_send_time));
4172 sess->last_rtcp_send_time = data.current_time;
4175 GST_DEBUG ("Time since last RTCP: %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT
4176 " = %" GST_TIME_FORMAT, GST_TIME_ARGS (data.current_time),
4177 GST_TIME_ARGS (sess->last_rtcp_check_time),
4178 GST_TIME_ARGS (data.current_time - sess->last_rtcp_check_time));
4179 sess->last_rtcp_check_time = data.current_time;
4180 sess->first_rtcp = FALSE;
4181 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
4182 sess->scheduled_bye = FALSE;
4185 RTP_SESSION_UNLOCK (sess);
4187 /* notify about updated statistics */
4188 g_object_notify (G_OBJECT (sess), "stats");
4190 /* push out the RTCP packets */
4191 while ((output = g_queue_pop_head (&data.output))) {
4192 gboolean do_not_suppress, empty_buffer;
4193 GstBuffer *buffer = output->buffer;
4194 RTPSource *source = output->source;
4196 /* Give the user a change to add its own packet */
4197 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
4198 buffer, data.is_early, &do_not_suppress);
4200 empty_buffer = gst_buffer_get_size (buffer) == 0;
4205 if (sess->callbacks.send_rtcp &&
4206 !empty_buffer && (do_not_suppress || !data.may_suppress)) {
4209 packet_size = gst_buffer_get_size (buffer) + sess->header_len;
4211 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
4212 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
4213 sess->stats.avg_rtcp_packet_size, packet_size);
4215 sess->callbacks.send_rtcp (sess, source, buffer,
4216 rtp_session_are_all_sources_bye (sess), sess->send_rtcp_user_data);
4218 RTP_SESSION_LOCK (sess);
4219 sess->stats.nacks_sent += data.nacked_seqnums;
4220 on_sender_ssrc_active (sess, source);
4221 RTP_SESSION_UNLOCK (sess);
4223 GST_DEBUG ("freeing packet callback: %p"
4224 " empty_buffer: %d, "
4225 " do_not_suppress: %d may_suppress: %d", sess->callbacks.send_rtcp,
4226 empty_buffer, do_not_suppress, data.may_suppress);
4227 if (!empty_buffer) {
4228 RTP_SESSION_LOCK (sess);
4229 sess->stats.nacks_dropped += data.nacked_seqnums;
4230 RTP_SESSION_UNLOCK (sess);
4232 gst_buffer_unref (buffer);
4234 g_object_unref (source);
4235 g_slice_free (ReportOutput, output);
4239 GST_ERROR ("generated empty RTCP messages for all the sources");
4245 * rtp_session_request_early_rtcp:
4246 * @sess: an #RTPSession
4247 * @current_time: the current system time
4248 * @max_delay: maximum delay
4250 * Request transmission of early RTCP
4252 * Returns: %TRUE if the related RTCP can be scheduled.
4255 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
4256 GstClockTime max_delay)
4258 GstClockTime T_dither_max, T_rr, offset = 0;
4260 gboolean allow_early;
4262 /* Implements the algorithm described in RFC 4585 section 3.5.2 */
4264 RTP_SESSION_LOCK (sess);
4266 /* We assume a feedback profile if something is requesting RTCP
4268 sess->rtp_profile = GST_RTP_PROFILE_AVPF;
4270 /* Check if already requested */
4271 /* RFC 4585 section 3.5.2 step 2 */
4272 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
4273 GST_LOG_OBJECT (sess, "already have next early rtcp time");
4274 ret = (current_time + max_delay > sess->next_early_rtcp_time);
4278 if (!GST_CLOCK_TIME_IS_VALID (sess->next_rtcp_check_time)) {
4279 GST_LOG_OBJECT (sess, "no next RTCP check time");
4284 /* RFC 4585 section 3.5.3 step 1
4285 * If no regular RTCP packet has been sent before, then a regular
4286 * RTCP packet has to be scheduled first and FB messages might be
4289 if (!GST_CLOCK_TIME_IS_VALID (sess->last_rtcp_send_time)) {
4290 GST_LOG_OBJECT (sess, "no RTCP sent yet");
4292 if (current_time + max_delay > sess->next_rtcp_check_time) {
4293 GST_LOG_OBJECT (sess,
4294 "next scheduled time is soon %" GST_TIME_FORMAT " + %" GST_TIME_FORMAT
4295 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
4296 GST_TIME_ARGS (max_delay),
4297 GST_TIME_ARGS (sess->next_rtcp_check_time));
4300 GST_LOG_OBJECT (sess,
4301 "can't allow early feedback, next scheduled time is too late %"
4302 GST_TIME_FORMAT " + %" GST_TIME_FORMAT " < %" GST_TIME_FORMAT,
4303 GST_TIME_ARGS (current_time), GST_TIME_ARGS (max_delay),
4304 GST_TIME_ARGS (sess->next_rtcp_check_time));
4310 T_rr = sess->last_rtcp_interval;
4312 /* RFC 4585 section 3.5.2 step 2b */
4313 /* If the total sources is <=2, then there is only us and one peer */
4314 /* When there is one auxiliary stream the session can still do point
4317 if (sess->is_doing_ptp) {
4320 /* Divide by 2 because l = 0.5 */
4321 T_dither_max = T_rr;
4325 /* RFC 4585 section 3.5.2 step 3 */
4326 if (current_time + T_dither_max > sess->next_rtcp_check_time) {
4327 GST_LOG_OBJECT (sess,
4328 "don't send because of dither, next scheduled time is too soon %"
4329 GST_TIME_FORMAT " + %" GST_TIME_FORMAT " > %" GST_TIME_FORMAT,
4330 GST_TIME_ARGS (current_time), GST_TIME_ARGS (T_dither_max),
4331 GST_TIME_ARGS (sess->next_rtcp_check_time));
4332 ret = T_dither_max <= max_delay;
4336 /* RFC 4585 section 3.5.2 step 4a and
4337 * RFC 4585 section 3.5.2 step 6 */
4338 allow_early = FALSE;
4339 if (sess->last_rtcp_check_time == sess->last_rtcp_send_time) {
4340 /* Last time we sent a full RTCP packet, we can now immediately
4341 * send an early one as allow_early was reset to TRUE */
4343 } else if (sess->last_rtcp_check_time + T_rr <= current_time + max_delay) {
4344 /* Last packet we sent was an early RTCP packet and more than
4345 * T_rr has passed since then, meaning we would have suppressed
4346 * a regular RTCP packet already and reset allow_early to TRUE */
4349 /* We have to offset a bit as T_rr has not passed yet, but will before
4351 if (sess->last_rtcp_check_time + T_rr > current_time)
4352 offset = (sess->last_rtcp_check_time + T_rr) - current_time;
4354 GST_DEBUG_OBJECT (sess,
4355 "can't allow early RTCP yet: last regular %" GST_TIME_FORMAT ", %"
4356 GST_TIME_FORMAT " + %" GST_TIME_FORMAT " > %" GST_TIME_FORMAT " + %"
4357 GST_TIME_FORMAT, GST_TIME_ARGS (sess->last_rtcp_send_time),
4358 GST_TIME_ARGS (sess->last_rtcp_check_time), GST_TIME_ARGS (T_rr),
4359 GST_TIME_ARGS (current_time), GST_TIME_ARGS (max_delay));
4363 /* Ignore the request a scheduled packet will be in time anyway */
4364 if (current_time + max_delay > sess->next_rtcp_check_time) {
4365 GST_LOG_OBJECT (sess,
4366 "next scheduled time is soon %" GST_TIME_FORMAT " + %" GST_TIME_FORMAT
4367 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
4368 GST_TIME_ARGS (max_delay),
4369 GST_TIME_ARGS (sess->next_rtcp_check_time));
4372 GST_LOG_OBJECT (sess,
4373 "can't allow early feedback and next scheduled time is too late %"
4374 GST_TIME_FORMAT " + %" GST_TIME_FORMAT " < %" GST_TIME_FORMAT,
4375 GST_TIME_ARGS (current_time), GST_TIME_ARGS (max_delay),
4376 GST_TIME_ARGS (sess->next_rtcp_check_time));
4382 /* RFC 4585 section 3.5.2 step 4b */
4384 /* Schedule an early transmission later */
4385 sess->next_early_rtcp_time = g_random_double () * T_dither_max +
4386 current_time + offset;
4388 /* If no dithering, schedule it for NOW */
4389 sess->next_early_rtcp_time = current_time + offset;
4392 GST_LOG_OBJECT (sess, "next early RTCP time %" GST_TIME_FORMAT
4393 ", next regular RTCP time %" GST_TIME_FORMAT,
4394 GST_TIME_ARGS (sess->next_early_rtcp_time),
4395 GST_TIME_ARGS (sess->next_rtcp_check_time));
4396 RTP_SESSION_UNLOCK (sess);
4398 /* notify app of need to send packet early
4399 * and therefore of timeout change */
4400 if (sess->callbacks.reconsider)
4401 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
4407 RTP_SESSION_UNLOCK (sess);
4413 rtp_session_send_rtcp (RTPSession * sess, GstClockTime max_delay)
4417 if (!sess->callbacks.send_rtcp)
4420 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
4422 /* notify the application that we intend to send early RTCP */
4423 if (sess->callbacks.notify_early_rtcp)
4424 sess->callbacks.notify_early_rtcp (sess, sess->notify_early_rtcp_user_data);
4426 return rtp_session_request_early_rtcp (sess, now, max_delay);
4430 rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc,
4431 gboolean fir, gint count)
4435 RTP_SESSION_LOCK (sess);
4436 src = find_source (sess, ssrc);
4441 src->send_pli = FALSE;
4442 src->send_fir = TRUE;
4444 if (count == -1 || count != src->last_fir_count)
4445 src->current_send_fir_seqnum++;
4446 src->last_fir_count = count;
4447 } else if (!src->send_fir) {
4448 src->send_pli = TRUE;
4450 RTP_SESSION_UNLOCK (sess);
4452 if (!rtp_session_send_rtcp (sess, 5 * GST_SECOND)) {
4453 GST_DEBUG ("FIR/PLI not sent early, sending with next regular RTCP");
4461 RTP_SESSION_UNLOCK (sess);
4467 * rtp_session_request_nack:
4468 * @sess: a #RTPSession
4470 * @seqnum: the missing seqnum
4471 * @max_delay: max delay to request NACK
4473 * Request scheduling of a NACK feedback packet for @seqnum in @ssrc.
4475 * Returns: %TRUE if the NACK feedback could be scheduled
4478 rtp_session_request_nack (RTPSession * sess, guint32 ssrc, guint16 seqnum,
4479 GstClockTime max_delay)
4483 RTP_SESSION_LOCK (sess);
4484 source = find_source (sess, ssrc);
4488 GST_DEBUG ("request NACK for %08x, #%u", ssrc, seqnum);
4489 rtp_source_register_nack (source, seqnum);
4490 RTP_SESSION_UNLOCK (sess);
4492 if (!rtp_session_send_rtcp (sess, max_delay)) {
4493 GST_DEBUG ("NACK not sent early, sending with next regular RTCP");
4501 RTP_SESSION_UNLOCK (sess);