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,
57 SIGNAL_ON_SENDING_NACKS,
61 #define DEFAULT_INTERNAL_SOURCE NULL
62 #define DEFAULT_BANDWIDTH 0.0
63 #define DEFAULT_RTCP_FRACTION RTP_STATS_RTCP_FRACTION
64 #define DEFAULT_RTCP_RR_BANDWIDTH -1
65 #define DEFAULT_RTCP_RS_BANDWIDTH -1
66 #define DEFAULT_RTCP_MTU 1400
67 #define DEFAULT_SDES NULL
68 #define DEFAULT_NUM_SOURCES 0
69 #define DEFAULT_NUM_ACTIVE_SOURCES 0
70 #define DEFAULT_SOURCES NULL
71 #define DEFAULT_RTCP_MIN_INTERVAL (RTP_STATS_MIN_INTERVAL * GST_SECOND)
72 #define DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW (2 * GST_SECOND)
73 #define DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD (3)
74 #define DEFAULT_PROBATION RTP_DEFAULT_PROBATION
75 #define DEFAULT_MAX_DROPOUT_TIME 60000
76 #define DEFAULT_MAX_MISORDER_TIME 2000
77 #define DEFAULT_RTP_PROFILE GST_RTP_PROFILE_AVP
78 #define DEFAULT_RTCP_REDUCED_SIZE FALSE
79 #define DEFAULT_RTCP_DISABLE_SR_TIMESTAMP FALSE
88 PROP_RTCP_RR_BANDWIDTH,
89 PROP_RTCP_RS_BANDWIDTH,
93 PROP_NUM_ACTIVE_SOURCES,
96 PROP_RTCP_MIN_INTERVAL,
97 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
98 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
100 PROP_MAX_DROPOUT_TIME,
101 PROP_MAX_MISORDER_TIME,
104 PROP_RTCP_REDUCED_SIZE,
105 PROP_RTCP_DISABLE_SR_TIMESTAMP
108 /* update average packet size */
109 #define INIT_AVG(avg, val) \
111 #define UPDATE_AVG(avg, val) \
115 (avg) = ((val) + (15 * (avg))) >> 4;
118 /* GObject vmethods */
119 static void rtp_session_finalize (GObject * object);
120 static void rtp_session_set_property (GObject * object, guint prop_id,
121 const GValue * value, GParamSpec * pspec);
122 static void rtp_session_get_property (GObject * object, guint prop_id,
123 GValue * value, GParamSpec * pspec);
125 static gboolean rtp_session_send_rtcp (RTPSession * sess,
126 GstClockTime max_delay);
127 static gboolean rtp_session_send_rtcp_with_deadline (RTPSession * sess,
128 GstClockTime deadline);
130 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
132 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
134 static guint32 rtp_session_create_new_ssrc (RTPSession * sess);
135 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
136 gboolean * created, RTPPacketInfo * pinfo, gboolean rtp);
137 static RTPSource *obtain_internal_source (RTPSession * sess,
138 guint32 ssrc, gboolean * created, GstClockTime current_time);
139 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
140 GstClockTime current_time);
141 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
142 gboolean deterministic, gboolean first);
145 accumulate_trues (GSignalInvocationHint * ihint, GValue * return_accu,
146 const GValue * handler_return, gpointer data)
148 if (g_value_get_boolean (handler_return))
149 g_value_set_boolean (return_accu, TRUE);
155 rtp_session_class_init (RTPSessionClass * klass)
157 GObjectClass *gobject_class;
159 gobject_class = (GObjectClass *) klass;
161 gobject_class->finalize = rtp_session_finalize;
162 gobject_class->set_property = rtp_session_set_property;
163 gobject_class->get_property = rtp_session_get_property;
166 * RTPSession::get-source-by-ssrc:
167 * @session: the object which received the signal
168 * @ssrc: the SSRC of the RTPSource
170 * Request the #RTPSource object with SSRC @ssrc in @session.
172 rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
173 g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
174 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
175 get_source_by_ssrc), NULL, NULL, g_cclosure_marshal_generic,
176 RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
179 * RTPSession::on-new-ssrc:
180 * @session: the object which received the signal
181 * @src: the new RTPSource
183 * Notify of a new SSRC that entered @session.
185 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
186 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
187 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
188 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
191 * RTPSession::on-ssrc-collision:
192 * @session: the object which received the signal
193 * @src: the #RTPSource that caused a collision
195 * Notify when we have an SSRC collision
197 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
198 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
199 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
200 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
203 * RTPSession::on-ssrc-validated:
204 * @session: the object which received the signal
205 * @src: the new validated RTPSource
207 * Notify of a new SSRC that became validated.
209 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
210 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
211 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
212 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
215 * RTPSession::on-ssrc-active:
216 * @session: the object which received the signal
217 * @src: the active RTPSource
219 * Notify of a SSRC that is active, i.e., sending RTCP.
221 rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
222 g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
223 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
224 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
227 * RTPSession::on-ssrc-sdes:
228 * @session: the object which received the signal
229 * @src: the RTPSource
231 * Notify that a new SDES was received for SSRC.
233 rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
234 g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
235 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
236 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
239 * RTPSession::on-bye-ssrc:
240 * @session: the object which received the signal
241 * @src: the RTPSource that went away
243 * Notify of an SSRC that became inactive because of a BYE packet.
245 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
246 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
247 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
248 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
251 * RTPSession::on-bye-timeout:
252 * @session: the object which received the signal
253 * @src: the RTPSource that timed out
255 * Notify of an SSRC that has timed out because of BYE
257 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
258 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
259 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
260 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
263 * RTPSession::on-timeout:
264 * @session: the object which received the signal
265 * @src: the RTPSource that timed out
267 * Notify of an SSRC that has timed out
269 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
270 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
271 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
272 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
275 * RTPSession::on-sender-timeout:
276 * @session: the object which received the signal
277 * @src: the RTPSource that timed out
279 * Notify of an SSRC that was a sender but timed out and became a receiver.
281 rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
282 g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
283 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
284 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
288 * RTPSession::on-sending-rtcp
289 * @session: the object which received the signal
290 * @buffer: the #GstBuffer containing the RTCP packet about to be sent
291 * @early: %TRUE if the packet is early, %FALSE if it is regular
293 * This signal is emitted before sending an RTCP packet, it can be used
294 * to add extra RTCP Packets.
296 * Returns: %TRUE if the RTCP buffer should NOT be suppressed, %FALSE
297 * if suppressing it is acceptable
299 rtp_session_signals[SIGNAL_ON_SENDING_RTCP] =
300 g_signal_new ("on-sending-rtcp", G_TYPE_FROM_CLASS (klass),
301 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_rtcp),
302 accumulate_trues, NULL, g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2,
303 GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_BOOLEAN);
306 * RTPSession::on-app-rtcp:
307 * @session: the object which received the signal
308 * @subtype: The subtype of the packet
309 * @ssrc: The SSRC/CSRC of the packet
310 * @name: The name of the packet
311 * @data: a #GstBuffer with the application-dependant data or %NULL if
314 * Notify that a RTCP APP packet has been received
316 rtp_session_signals[SIGNAL_ON_APP_RTCP] =
317 g_signal_new ("on-app-rtcp", G_TYPE_FROM_CLASS (klass),
318 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_app_rtcp),
319 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 4,
320 G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING, GST_TYPE_BUFFER);
323 * RTPSession::on-feedback-rtcp:
324 * @session: the object which received the signal
325 * @type: Type of RTCP packet, will be %GST_RTCP_TYPE_RTPFB or
326 * %GST_RTCP_TYPE_RTPFB
327 * @fbtype: The type of RTCP FB packet, probably part of #GstRTCPFBType
328 * @sender_ssrc: The SSRC of the sender
329 * @media_ssrc: The SSRC of the media this refers to
330 * @fci: a #GstBuffer with the FCI data from the FB packet or %NULL if
333 * Notify that a RTCP feedback packet has been received
335 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP] =
336 g_signal_new ("on-feedback-rtcp", G_TYPE_FROM_CLASS (klass),
337 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_feedback_rtcp),
338 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 5, G_TYPE_UINT,
339 G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, GST_TYPE_BUFFER);
342 * RTPSession::send-rtcp:
343 * @session: the object which received the signal
344 * @max_delay: The maximum delay after which the feedback will not be useful
347 * Requests that the #RTPSession initiate a new RTCP packet as soon as
348 * possible within the requested delay.
350 * This sets feedback to %TRUE if not already done before.
352 rtp_session_signals[SIGNAL_SEND_RTCP] =
353 g_signal_new ("send-rtcp", G_TYPE_FROM_CLASS (klass),
354 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
355 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
356 g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_UINT64);
359 * RTPSession::send-rtcp-full:
360 * @session: the object which received the signal
361 * @max_delay: The maximum delay after which the feedback will not be useful
364 * Requests that the #RTPSession initiate a new RTCP packet as soon as
365 * possible within the requested delay.
367 * This sets feedback to %TRUE if not already done before.
369 * Returns: TRUE if the new RTCP packet could be scheduled within the
370 * requested delay, FALSE otherwise.
374 rtp_session_signals[SIGNAL_SEND_RTCP_FULL] =
375 g_signal_new ("send-rtcp-full", G_TYPE_FROM_CLASS (klass),
376 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
377 G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
378 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 1, G_TYPE_UINT64);
381 * RTPSession::on-receiving-rtcp
382 * @session: the object which received the signal
383 * @buffer: the #GstBuffer containing the RTCP packet that was received
385 * This signal is emitted when receiving an RTCP packet before it is handled
386 * by the session. It can be used to extract custom information from RTCP packets.
390 rtp_session_signals[SIGNAL_ON_RECEIVING_RTCP] =
391 g_signal_new ("on-receiving-rtcp", G_TYPE_FROM_CLASS (klass),
392 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_receiving_rtcp),
393 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
394 GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE);
397 * RTPSession::on-new-sender-ssrc:
398 * @session: the object which received the signal
399 * @src: the new sender RTPSource
401 * Notify of a new sender SSRC that entered @session.
405 rtp_session_signals[SIGNAL_ON_NEW_SENDER_SSRC] =
406 g_signal_new ("on-new-sender-ssrc", G_TYPE_FROM_CLASS (klass),
407 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_sender_ssrc),
408 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
412 * RTPSession::on-sender-ssrc-active:
413 * @session: the object which received the signal
414 * @src: the active sender RTPSource
416 * Notify of a sender SSRC that is active, i.e., sending RTCP.
420 rtp_session_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE] =
421 g_signal_new ("on-sender-ssrc-active", G_TYPE_FROM_CLASS (klass),
422 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass,
423 on_sender_ssrc_active), NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
424 G_TYPE_NONE, 1, RTP_TYPE_SOURCE);
427 * RTPSession::on-sending-nack
428 * @session: the object which received the signal
429 * @sender_ssrc: the sender ssrc
430 * @media_ssrc: the media ssrc
431 * @nacks: (element-type guint16): the list of seqnum to be nacked
432 * @buffer: the #GstBuffer containing the RTCP packet about to be sent
434 * This signal is emitted before NACK packets are added into the RTCP
435 * packet. This signal can be used to override the conversion of the NACK
436 * seqnum array into packets. This can be used if your protocol uses
437 * different type of NACK (e.g. based on RTCP APP).
439 * The handler should transform the seqnum from @nacks array into packets.
440 * @nacks seqnum must be consumed from the start. The remaining will be
441 * rescheduled for later base on bandwidth. Only one handler will be
444 * A handler may return 0 to signal that generic NACKs should be created
445 * for this set. This can be useful if the signal is used for other purpose
446 * or if the other type of NACK would use more space.
448 * Returns: the number of NACK seqnum that was consumed from @nacks.
452 rtp_session_signals[SIGNAL_ON_SENDING_NACKS] =
453 g_signal_new ("on-sending-nacks", G_TYPE_FROM_CLASS (klass),
454 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_nacks),
455 g_signal_accumulator_first_wins, NULL, g_cclosure_marshal_generic,
456 G_TYPE_UINT, 4, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_ARRAY,
457 GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE);
459 g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
460 g_param_spec_uint ("internal-ssrc", "Internal SSRC",
461 "The internal SSRC used for the session (deprecated)",
462 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
464 g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
465 g_param_spec_object ("internal-source", "Internal Source",
466 "The internal source element of the session (deprecated)",
467 RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
469 g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
470 g_param_spec_double ("bandwidth", "Bandwidth",
471 "The bandwidth of the session in bits per second (0 for auto-discover)",
472 0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
473 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
475 g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
476 g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
477 "The fraction of the bandwidth used for RTCP in bits per second (or as a real fraction of the RTP bandwidth if < 1)",
478 0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
479 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
481 g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
482 g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
483 "The RTCP bandwidth used for receivers in bits per second (-1 = default)",
484 -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
485 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
487 g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
488 g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
489 "The RTCP bandwidth used for senders in bits per second (-1 = default)",
490 -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
491 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
493 g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
494 g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
495 "The maximum size of the RTCP packets",
496 16, G_MAXINT16, DEFAULT_RTCP_MTU,
497 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
499 g_object_class_install_property (gobject_class, PROP_SDES,
500 g_param_spec_boxed ("sdes", "SDES",
501 "The SDES items of this session",
502 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
504 g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
505 g_param_spec_uint ("num-sources", "Num Sources",
506 "The number of sources in the session", 0, G_MAXUINT,
507 DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
509 g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
510 g_param_spec_uint ("num-active-sources", "Num Active Sources",
511 "The number of active sources in the session", 0, G_MAXUINT,
512 DEFAULT_NUM_ACTIVE_SOURCES,
513 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
517 * Get a GValue Array of all sources in the session.
520 * <title>Getting the #RTPSources of a session
527 * g_object_get (sess, "sources", &arr, NULL);
529 * for (i = 0; i < arr->n_values; i++) {
532 * val = g_value_array_get_nth (arr, i);
533 * source = g_value_get_object (val);
535 * g_value_array_free (arr);
540 g_object_class_install_property (gobject_class, PROP_SOURCES,
541 g_param_spec_boxed ("sources", "Sources",
542 "An array of all known sources in the session",
543 G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
545 g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
546 g_param_spec_boolean ("favor-new", "Favor new sources",
547 "Resolve SSRC conflict in favor of new sources", FALSE,
548 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
550 g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
551 g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
552 "Minimum interval between Regular RTCP packet (in ns)",
553 0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
554 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
556 g_object_class_install_property (gobject_class,
557 PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
558 g_param_spec_uint64 ("rtcp-feedback-retention-window",
559 "RTCP Feedback retention window",
560 "Duration during which RTCP Feedback packets are retained (in ns)",
561 0, G_MAXUINT64, DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW,
562 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
564 g_object_class_install_property (gobject_class,
565 PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
566 g_param_spec_uint ("rtcp-immediate-feedback-threshold",
567 "RTCP Immediate Feedback threshold",
568 "The maximum number of members of a RTP session for which immediate"
569 " feedback is used (DEPRECATED: has no effect and is not needed)",
570 0, G_MAXUINT, DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
571 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
573 g_object_class_install_property (gobject_class, PROP_PROBATION,
574 g_param_spec_uint ("probation", "Number of probations",
575 "Consecutive packet sequence numbers to accept the source",
576 0, G_MAXUINT, DEFAULT_PROBATION,
577 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
579 g_object_class_install_property (gobject_class, PROP_MAX_DROPOUT_TIME,
580 g_param_spec_uint ("max-dropout-time", "Max dropout time",
581 "The maximum time (milliseconds) of missing packets tolerated.",
582 0, G_MAXUINT, DEFAULT_MAX_DROPOUT_TIME,
583 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
585 g_object_class_install_property (gobject_class, PROP_MAX_MISORDER_TIME,
586 g_param_spec_uint ("max-misorder-time", "Max misorder time",
587 "The maximum time (milliseconds) of misordered packets tolerated.",
588 0, G_MAXUINT, DEFAULT_MAX_MISORDER_TIME,
589 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
594 * Various session statistics. This property returns a GstStructure
595 * with name application/x-rtp-session-stats with the following fields:
597 * "rtx-drop-count" G_TYPE_UINT The number of retransmission events
598 * dropped (due to bandwidth constraints)
599 * "sent-nack-count" G_TYPE_UINT Number of NACKs sent
600 * "recv-nack-count" G_TYPE_UINT Number of NACKs received
601 * "source-stats" G_TYPE_BOXED GValueArray of #RTPSource::stats for all
602 * RTP sources (Since 1.8)
606 g_object_class_install_property (gobject_class, PROP_STATS,
607 g_param_spec_boxed ("stats", "Statistics",
608 "Various statistics", GST_TYPE_STRUCTURE,
609 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
611 g_object_class_install_property (gobject_class, PROP_RTP_PROFILE,
612 g_param_spec_enum ("rtp-profile", "RTP Profile",
613 "RTP profile to use for this session", GST_TYPE_RTP_PROFILE,
614 DEFAULT_RTP_PROFILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
616 g_object_class_install_property (gobject_class, PROP_RTCP_REDUCED_SIZE,
617 g_param_spec_boolean ("rtcp-reduced-size", "RTCP Reduced Size",
618 "Use Reduced Size RTCP for feedback packets",
619 DEFAULT_RTCP_REDUCED_SIZE,
620 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
623 * RTPSession::disable-sr-timestamp:
625 * Whether sender reports should be timestamped.
629 g_object_class_install_property (gobject_class,
630 PROP_RTCP_DISABLE_SR_TIMESTAMP,
631 g_param_spec_boolean ("disable-sr-timestamp",
632 "Disable Sender Report Timestamp",
633 "Whether sender reports should be timestamped",
634 DEFAULT_RTCP_DISABLE_SR_TIMESTAMP,
635 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
637 klass->get_source_by_ssrc =
638 GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
639 klass->send_rtcp = GST_DEBUG_FUNCPTR (rtp_session_send_rtcp);
641 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
645 rtp_session_init (RTPSession * sess)
650 g_mutex_init (&sess->lock);
651 sess->key = g_random_int ();
655 /* TODO: We currently only use the first hash table but this is the
656 * beginning of an implementation for RFC2762
657 for (i = 0; i < 32; i++) {
659 for (i = 0; i < 1; i++) {
661 g_hash_table_new_full (NULL, NULL, NULL,
662 (GDestroyNotify) g_object_unref);
665 rtp_stats_init_defaults (&sess->stats);
666 INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
667 rtp_stats_set_min_interval (&sess->stats,
668 (gdouble) DEFAULT_RTCP_MIN_INTERVAL / GST_SECOND);
670 sess->recalc_bandwidth = TRUE;
671 sess->bandwidth = DEFAULT_BANDWIDTH;
672 sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
673 sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
674 sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
676 /* default UDP header length */
677 sess->header_len = 28;
678 sess->mtu = DEFAULT_RTCP_MTU;
680 sess->probation = DEFAULT_PROBATION;
681 sess->max_dropout_time = DEFAULT_MAX_DROPOUT_TIME;
682 sess->max_misorder_time = DEFAULT_MAX_MISORDER_TIME;
684 /* some default SDES entries */
685 sess->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
687 /* we do not want to leak details like the username or hostname here */
688 str = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
689 gst_structure_set (sess->sdes, "cname", G_TYPE_STRING, str, NULL);
693 /* we do not want to leak the user's real name here */
694 str = g_strdup_printf ("Anon%u", g_random_int ());
695 gst_structure_set (sdes, "name", G_TYPE_STRING, str, NULL);
699 gst_structure_set (sess->sdes, "tool", G_TYPE_STRING, "GStreamer", NULL);
701 /* this is the SSRC we suggest */
702 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
703 sess->internal_ssrc_set = FALSE;
705 sess->first_rtcp = TRUE;
706 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
707 sess->last_rtcp_check_time = GST_CLOCK_TIME_NONE;
708 sess->last_rtcp_send_time = GST_CLOCK_TIME_NONE;
709 sess->last_rtcp_interval = GST_CLOCK_TIME_NONE;
711 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
712 sess->rtcp_feedback_retention_window = DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW;
713 sess->rtcp_immediate_feedback_threshold =
714 DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD;
715 sess->rtp_profile = DEFAULT_RTP_PROFILE;
716 sess->reduced_size_rtcp = DEFAULT_RTCP_REDUCED_SIZE;
717 sess->timestamp_sender_reports = !DEFAULT_RTCP_DISABLE_SR_TIMESTAMP;
719 sess->is_doing_ptp = TRUE;
723 rtp_session_finalize (GObject * object)
728 sess = RTP_SESSION_CAST (object);
730 gst_structure_free (sess->sdes);
732 g_list_free_full (sess->conflicting_addresses,
733 (GDestroyNotify) rtp_conflicting_address_free);
735 /* TODO: Change this again when implementing RFC 2762
736 * for (i = 0; i < 32; i++)
738 for (i = 0; i < 1; i++)
739 g_hash_table_destroy (sess->ssrcs[i]);
741 g_mutex_clear (&sess->lock);
743 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
747 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
749 GValue value = { 0 };
751 g_value_init (&value, RTP_TYPE_SOURCE);
752 g_value_take_object (&value, source);
753 /* copies the value */
754 g_value_array_append (arr, &value);
758 rtp_session_create_sources (RTPSession * sess)
763 RTP_SESSION_LOCK (sess);
764 /* get number of elements in the table */
765 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
766 /* create the result value array */
767 res = g_value_array_new (size);
769 /* and copy all values into the array */
770 g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
771 RTP_SESSION_UNLOCK (sess);
777 create_source_stats (gpointer key, RTPSource * source, GValueArray * arr)
782 g_object_get (source, "stats", &s, NULL);
784 g_value_array_append (arr, NULL);
785 value = g_value_array_get_nth (arr, arr->n_values - 1);
786 g_value_init (value, GST_TYPE_STRUCTURE);
787 g_value_take_boxed (value, s);
790 static GstStructure *
791 rtp_session_create_stats (RTPSession * sess)
794 GValueArray *source_stats;
795 GValue source_stats_v = G_VALUE_INIT;
798 RTP_SESSION_LOCK (sess);
799 s = gst_structure_new ("application/x-rtp-session-stats",
800 "rtx-drop-count", G_TYPE_UINT, sess->stats.nacks_dropped,
801 "sent-nack-count", G_TYPE_UINT, sess->stats.nacks_sent,
802 "recv-nack-count", G_TYPE_UINT, sess->stats.nacks_received, NULL);
804 size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
805 source_stats = g_value_array_new (size);
806 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
807 (GHFunc) create_source_stats, source_stats);
808 RTP_SESSION_UNLOCK (sess);
810 g_value_init (&source_stats_v, G_TYPE_VALUE_ARRAY);
811 g_value_take_boxed (&source_stats_v, source_stats);
812 gst_structure_take_value (s, "source-stats", &source_stats_v);
818 rtp_session_set_property (GObject * object, guint prop_id,
819 const GValue * value, GParamSpec * pspec)
823 sess = RTP_SESSION (object);
826 case PROP_INTERNAL_SSRC:
827 RTP_SESSION_LOCK (sess);
828 sess->suggested_ssrc = g_value_get_uint (value);
829 sess->internal_ssrc_set = TRUE;
830 sess->internal_ssrc_from_caps_or_property = TRUE;
831 RTP_SESSION_UNLOCK (sess);
832 if (sess->callbacks.reconfigure)
833 sess->callbacks.reconfigure (sess, sess->reconfigure_user_data);
836 RTP_SESSION_LOCK (sess);
837 sess->bandwidth = g_value_get_double (value);
838 sess->recalc_bandwidth = TRUE;
839 RTP_SESSION_UNLOCK (sess);
841 case PROP_RTCP_FRACTION:
842 RTP_SESSION_LOCK (sess);
843 sess->rtcp_bandwidth = g_value_get_double (value);
844 sess->recalc_bandwidth = TRUE;
845 RTP_SESSION_UNLOCK (sess);
847 case PROP_RTCP_RR_BANDWIDTH:
848 RTP_SESSION_LOCK (sess);
849 sess->rtcp_rr_bandwidth = g_value_get_int (value);
850 sess->recalc_bandwidth = TRUE;
851 RTP_SESSION_UNLOCK (sess);
853 case PROP_RTCP_RS_BANDWIDTH:
854 RTP_SESSION_LOCK (sess);
855 sess->rtcp_rs_bandwidth = g_value_get_int (value);
856 sess->recalc_bandwidth = TRUE;
857 RTP_SESSION_UNLOCK (sess);
860 sess->mtu = g_value_get_uint (value);
863 rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
866 sess->favor_new = g_value_get_boolean (value);
868 case PROP_RTCP_MIN_INTERVAL:
869 rtp_stats_set_min_interval (&sess->stats,
870 (gdouble) g_value_get_uint64 (value) / GST_SECOND);
871 /* trigger reconsideration */
872 RTP_SESSION_LOCK (sess);
873 sess->next_rtcp_check_time = 0;
874 RTP_SESSION_UNLOCK (sess);
875 if (sess->callbacks.reconsider)
876 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
878 case PROP_RTCP_FEEDBACK_RETENTION_WINDOW:
879 sess->rtcp_feedback_retention_window = g_value_get_uint64 (value);
881 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
882 sess->rtcp_immediate_feedback_threshold = g_value_get_uint (value);
885 sess->probation = g_value_get_uint (value);
887 case PROP_MAX_DROPOUT_TIME:
888 sess->max_dropout_time = g_value_get_uint (value);
890 case PROP_MAX_MISORDER_TIME:
891 sess->max_misorder_time = g_value_get_uint (value);
893 case PROP_RTP_PROFILE:
894 sess->rtp_profile = g_value_get_enum (value);
895 /* trigger reconsideration */
896 RTP_SESSION_LOCK (sess);
897 sess->next_rtcp_check_time = 0;
898 RTP_SESSION_UNLOCK (sess);
899 if (sess->callbacks.reconsider)
900 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
902 case PROP_RTCP_REDUCED_SIZE:
903 sess->reduced_size_rtcp = g_value_get_boolean (value);
905 case PROP_RTCP_DISABLE_SR_TIMESTAMP:
906 sess->timestamp_sender_reports = !g_value_get_boolean (value);
909 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
915 rtp_session_get_property (GObject * object, guint prop_id,
916 GValue * value, GParamSpec * pspec)
920 sess = RTP_SESSION (object);
923 case PROP_INTERNAL_SSRC:
924 g_value_set_uint (value, rtp_session_suggest_ssrc (sess, NULL));
926 case PROP_INTERNAL_SOURCE:
927 /* FIXME, return a random source */
928 g_value_set_object (value, NULL);
931 g_value_set_double (value, sess->bandwidth);
933 case PROP_RTCP_FRACTION:
934 g_value_set_double (value, sess->rtcp_bandwidth);
936 case PROP_RTCP_RR_BANDWIDTH:
937 g_value_set_int (value, sess->rtcp_rr_bandwidth);
939 case PROP_RTCP_RS_BANDWIDTH:
940 g_value_set_int (value, sess->rtcp_rs_bandwidth);
943 g_value_set_uint (value, sess->mtu);
946 g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
948 case PROP_NUM_SOURCES:
949 g_value_set_uint (value, rtp_session_get_num_sources (sess));
951 case PROP_NUM_ACTIVE_SOURCES:
952 g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
955 g_value_take_boxed (value, rtp_session_create_sources (sess));
958 g_value_set_boolean (value, sess->favor_new);
960 case PROP_RTCP_MIN_INTERVAL:
961 g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
963 case PROP_RTCP_FEEDBACK_RETENTION_WINDOW:
964 g_value_set_uint64 (value, sess->rtcp_feedback_retention_window);
966 case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
967 g_value_set_uint (value, sess->rtcp_immediate_feedback_threshold);
970 g_value_set_uint (value, sess->probation);
972 case PROP_MAX_DROPOUT_TIME:
973 g_value_set_uint (value, sess->max_dropout_time);
975 case PROP_MAX_MISORDER_TIME:
976 g_value_set_uint (value, sess->max_misorder_time);
979 g_value_take_boxed (value, rtp_session_create_stats (sess));
981 case PROP_RTP_PROFILE:
982 g_value_set_enum (value, sess->rtp_profile);
984 case PROP_RTCP_REDUCED_SIZE:
985 g_value_set_boolean (value, sess->reduced_size_rtcp);
987 case PROP_RTCP_DISABLE_SR_TIMESTAMP:
988 g_value_set_boolean (value, !sess->timestamp_sender_reports);
991 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
997 on_new_ssrc (RTPSession * sess, RTPSource * source)
999 g_object_ref (source);
1000 RTP_SESSION_UNLOCK (sess);
1001 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
1002 RTP_SESSION_LOCK (sess);
1003 g_object_unref (source);
1007 on_ssrc_collision (RTPSession * sess, RTPSource * source)
1009 g_object_ref (source);
1010 RTP_SESSION_UNLOCK (sess);
1011 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
1013 RTP_SESSION_LOCK (sess);
1014 g_object_unref (source);
1018 on_ssrc_validated (RTPSession * sess, RTPSource * source)
1020 g_object_ref (source);
1021 RTP_SESSION_UNLOCK (sess);
1022 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
1024 RTP_SESSION_LOCK (sess);
1025 g_object_unref (source);
1029 on_ssrc_active (RTPSession * sess, RTPSource * source)
1031 g_object_ref (source);
1032 RTP_SESSION_UNLOCK (sess);
1033 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
1034 RTP_SESSION_LOCK (sess);
1035 g_object_unref (source);
1039 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
1041 g_object_ref (source);
1042 GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
1043 RTP_SESSION_UNLOCK (sess);
1044 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
1045 RTP_SESSION_LOCK (sess);
1046 g_object_unref (source);
1050 on_bye_ssrc (RTPSession * sess, RTPSource * source)
1052 g_object_ref (source);
1053 RTP_SESSION_UNLOCK (sess);
1054 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
1055 RTP_SESSION_LOCK (sess);
1056 g_object_unref (source);
1060 on_bye_timeout (RTPSession * sess, RTPSource * source)
1062 g_object_ref (source);
1063 RTP_SESSION_UNLOCK (sess);
1064 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
1065 RTP_SESSION_LOCK (sess);
1066 g_object_unref (source);
1070 on_timeout (RTPSession * sess, RTPSource * source)
1072 g_object_ref (source);
1073 RTP_SESSION_UNLOCK (sess);
1074 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
1075 RTP_SESSION_LOCK (sess);
1076 g_object_unref (source);
1080 on_sender_timeout (RTPSession * sess, RTPSource * source)
1082 g_object_ref (source);
1083 RTP_SESSION_UNLOCK (sess);
1084 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
1086 RTP_SESSION_LOCK (sess);
1087 g_object_unref (source);
1091 on_new_sender_ssrc (RTPSession * sess, RTPSource * source)
1093 g_object_ref (source);
1094 RTP_SESSION_UNLOCK (sess);
1095 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SENDER_SSRC], 0,
1097 RTP_SESSION_LOCK (sess);
1098 g_object_unref (source);
1102 on_sender_ssrc_active (RTPSession * sess, RTPSource * source)
1104 g_object_ref (source);
1105 RTP_SESSION_UNLOCK (sess);
1106 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE], 0,
1108 RTP_SESSION_LOCK (sess);
1109 g_object_unref (source);
1115 * Create a new session object.
1117 * Returns: a new #RTPSession. g_object_unref() after usage.
1120 rtp_session_new (void)
1124 sess = g_object_new (RTP_TYPE_SESSION, NULL);
1130 * rtp_session_reset:
1131 * @sess: an #RTPSession
1133 * Reset the sources of @sess.
1136 rtp_session_reset (RTPSession * sess)
1138 g_return_if_fail (RTP_IS_SESSION (sess));
1140 /* remove all sources */
1141 g_hash_table_remove_all (sess->ssrcs[sess->mask_idx]);
1142 sess->total_sources = 0;
1143 sess->stats.sender_sources = 0;
1144 sess->stats.internal_sender_sources = 0;
1145 sess->stats.internal_sources = 0;
1146 sess->stats.active_sources = 0;
1148 sess->generation = 0;
1149 sess->first_rtcp = TRUE;
1150 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
1151 sess->last_rtcp_check_time = GST_CLOCK_TIME_NONE;
1152 sess->last_rtcp_send_time = GST_CLOCK_TIME_NONE;
1153 sess->last_rtcp_interval = GST_CLOCK_TIME_NONE;
1154 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
1155 sess->scheduled_bye = FALSE;
1157 /* reset session stats */
1158 sess->stats.bye_members = 0;
1159 sess->stats.nacks_dropped = 0;
1160 sess->stats.nacks_sent = 0;
1161 sess->stats.nacks_received = 0;
1163 sess->is_doing_ptp = TRUE;
1165 g_list_free_full (sess->conflicting_addresses,
1166 (GDestroyNotify) rtp_conflicting_address_free);
1167 sess->conflicting_addresses = NULL;
1171 * rtp_session_set_callbacks:
1172 * @sess: an #RTPSession
1173 * @callbacks: callbacks to configure
1174 * @user_data: user data passed in the callbacks
1176 * Configure a set of callbacks to be notified of actions.
1179 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
1182 g_return_if_fail (RTP_IS_SESSION (sess));
1184 if (callbacks->process_rtp) {
1185 sess->callbacks.process_rtp = callbacks->process_rtp;
1186 sess->process_rtp_user_data = user_data;
1188 if (callbacks->send_rtp) {
1189 sess->callbacks.send_rtp = callbacks->send_rtp;
1190 sess->send_rtp_user_data = user_data;
1192 if (callbacks->send_rtcp) {
1193 sess->callbacks.send_rtcp = callbacks->send_rtcp;
1194 sess->send_rtcp_user_data = user_data;
1196 if (callbacks->sync_rtcp) {
1197 sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
1198 sess->sync_rtcp_user_data = user_data;
1200 if (callbacks->clock_rate) {
1201 sess->callbacks.clock_rate = callbacks->clock_rate;
1202 sess->clock_rate_user_data = user_data;
1204 if (callbacks->reconsider) {
1205 sess->callbacks.reconsider = callbacks->reconsider;
1206 sess->reconsider_user_data = user_data;
1208 if (callbacks->request_key_unit) {
1209 sess->callbacks.request_key_unit = callbacks->request_key_unit;
1210 sess->request_key_unit_user_data = user_data;
1212 if (callbacks->request_time) {
1213 sess->callbacks.request_time = callbacks->request_time;
1214 sess->request_time_user_data = user_data;
1216 if (callbacks->notify_nack) {
1217 sess->callbacks.notify_nack = callbacks->notify_nack;
1218 sess->notify_nack_user_data = user_data;
1220 if (callbacks->reconfigure) {
1221 sess->callbacks.reconfigure = callbacks->reconfigure;
1222 sess->reconfigure_user_data = user_data;
1224 if (callbacks->notify_early_rtcp) {
1225 sess->callbacks.notify_early_rtcp = callbacks->notify_early_rtcp;
1226 sess->notify_early_rtcp_user_data = user_data;
1231 * rtp_session_set_process_rtp_callback:
1232 * @sess: an #RTPSession
1233 * @callback: callback to set
1234 * @user_data: user data passed in the callback
1236 * Configure only the process_rtp callback to be notified of the process_rtp action.
1239 rtp_session_set_process_rtp_callback (RTPSession * sess,
1240 RTPSessionProcessRTP callback, gpointer user_data)
1242 g_return_if_fail (RTP_IS_SESSION (sess));
1244 sess->callbacks.process_rtp = callback;
1245 sess->process_rtp_user_data = user_data;
1249 * rtp_session_set_send_rtp_callback:
1250 * @sess: an #RTPSession
1251 * @callback: callback to set
1252 * @user_data: user data passed in the callback
1254 * Configure only the send_rtp callback to be notified of the send_rtp action.
1257 rtp_session_set_send_rtp_callback (RTPSession * sess,
1258 RTPSessionSendRTP callback, gpointer user_data)
1260 g_return_if_fail (RTP_IS_SESSION (sess));
1262 sess->callbacks.send_rtp = callback;
1263 sess->send_rtp_user_data = user_data;
1267 * rtp_session_set_send_rtcp_callback:
1268 * @sess: an #RTPSession
1269 * @callback: callback to set
1270 * @user_data: user data passed in the callback
1272 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
1275 rtp_session_set_send_rtcp_callback (RTPSession * sess,
1276 RTPSessionSendRTCP callback, gpointer user_data)
1278 g_return_if_fail (RTP_IS_SESSION (sess));
1280 sess->callbacks.send_rtcp = callback;
1281 sess->send_rtcp_user_data = user_data;
1285 * rtp_session_set_sync_rtcp_callback:
1286 * @sess: an #RTPSession
1287 * @callback: callback to set
1288 * @user_data: user data passed in the callback
1290 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
1293 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
1294 RTPSessionSyncRTCP callback, gpointer user_data)
1296 g_return_if_fail (RTP_IS_SESSION (sess));
1298 sess->callbacks.sync_rtcp = callback;
1299 sess->sync_rtcp_user_data = user_data;
1303 * rtp_session_set_clock_rate_callback:
1304 * @sess: an #RTPSession
1305 * @callback: callback to set
1306 * @user_data: user data passed in the callback
1308 * Configure only the clock_rate callback to be notified of the clock_rate action.
1311 rtp_session_set_clock_rate_callback (RTPSession * sess,
1312 RTPSessionClockRate callback, gpointer user_data)
1314 g_return_if_fail (RTP_IS_SESSION (sess));
1316 sess->callbacks.clock_rate = callback;
1317 sess->clock_rate_user_data = user_data;
1321 * rtp_session_set_reconsider_callback:
1322 * @sess: an #RTPSession
1323 * @callback: callback to set
1324 * @user_data: user data passed in the callback
1326 * Configure only the reconsider callback to be notified of the reconsider action.
1329 rtp_session_set_reconsider_callback (RTPSession * sess,
1330 RTPSessionReconsider callback, gpointer user_data)
1332 g_return_if_fail (RTP_IS_SESSION (sess));
1334 sess->callbacks.reconsider = callback;
1335 sess->reconsider_user_data = user_data;
1339 * rtp_session_set_request_time_callback:
1340 * @sess: an #RTPSession
1341 * @callback: callback to set
1342 * @user_data: user data passed in the callback
1344 * Configure only the request_time callback
1347 rtp_session_set_request_time_callback (RTPSession * sess,
1348 RTPSessionRequestTime callback, gpointer user_data)
1350 g_return_if_fail (RTP_IS_SESSION (sess));
1352 sess->callbacks.request_time = callback;
1353 sess->request_time_user_data = user_data;
1357 * rtp_session_set_bandwidth:
1358 * @sess: an #RTPSession
1359 * @bandwidth: the bandwidth allocated
1361 * Set the session bandwidth in bits per second.
1364 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
1366 g_return_if_fail (RTP_IS_SESSION (sess));
1368 RTP_SESSION_LOCK (sess);
1369 sess->stats.bandwidth = bandwidth;
1370 RTP_SESSION_UNLOCK (sess);
1374 * rtp_session_get_bandwidth:
1375 * @sess: an #RTPSession
1377 * Get the session bandwidth.
1379 * Returns: the session bandwidth.
1382 rtp_session_get_bandwidth (RTPSession * sess)
1386 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1388 RTP_SESSION_LOCK (sess);
1389 result = sess->stats.bandwidth;
1390 RTP_SESSION_UNLOCK (sess);
1396 * rtp_session_set_rtcp_fraction:
1397 * @sess: an #RTPSession
1398 * @bandwidth: the RTCP bandwidth
1400 * Set the bandwidth in bits per second that should be used for RTCP
1404 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
1406 g_return_if_fail (RTP_IS_SESSION (sess));
1408 RTP_SESSION_LOCK (sess);
1409 sess->stats.rtcp_bandwidth = bandwidth;
1410 RTP_SESSION_UNLOCK (sess);
1414 * rtp_session_get_rtcp_fraction:
1415 * @sess: an #RTPSession
1417 * Get the session bandwidth used for RTCP.
1419 * Returns: The bandwidth used for RTCP messages.
1422 rtp_session_get_rtcp_fraction (RTPSession * sess)
1426 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
1428 RTP_SESSION_LOCK (sess);
1429 result = sess->stats.rtcp_bandwidth;
1430 RTP_SESSION_UNLOCK (sess);
1436 * rtp_session_get_sdes_struct:
1437 * @sess: an #RTSPSession
1439 * Get the SDES data as a #GstStructure
1441 * Returns: a GstStructure with SDES items for @sess. This function returns a
1442 * copy of the SDES structure, use gst_structure_free() after usage.
1445 rtp_session_get_sdes_struct (RTPSession * sess)
1447 GstStructure *result = NULL;
1449 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1451 RTP_SESSION_LOCK (sess);
1453 result = gst_structure_copy (sess->sdes);
1454 RTP_SESSION_UNLOCK (sess);
1460 source_set_sdes (const gchar * key, RTPSource * source, GstStructure * sdes)
1462 rtp_source_set_sdes_struct (source, gst_structure_copy (sdes));
1466 * rtp_session_set_sdes_struct:
1467 * @sess: an #RTSPSession
1468 * @sdes: a #GstStructure
1470 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1473 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
1475 g_return_if_fail (sdes);
1476 g_return_if_fail (RTP_IS_SESSION (sess));
1478 RTP_SESSION_LOCK (sess);
1480 gst_structure_free (sess->sdes);
1481 sess->sdes = gst_structure_copy (sdes);
1483 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
1484 (GHFunc) source_set_sdes, sess->sdes);
1485 RTP_SESSION_UNLOCK (sess);
1488 static GstFlowReturn
1489 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1491 GstFlowReturn result = GST_FLOW_OK;
1493 if (source->internal) {
1494 GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1496 RTP_SESSION_UNLOCK (session);
1498 if (session->callbacks.send_rtp)
1500 session->callbacks.send_rtp (session, source, data,
1501 session->send_rtp_user_data);
1503 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1506 GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1507 RTP_SESSION_UNLOCK (session);
1509 if (session->callbacks.process_rtp)
1511 session->callbacks.process_rtp (session, source,
1512 GST_BUFFER_CAST (data), session->process_rtp_user_data);
1514 gst_buffer_unref (GST_BUFFER_CAST (data));
1516 RTP_SESSION_LOCK (session);
1522 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1526 RTP_SESSION_UNLOCK (session);
1528 if (session->callbacks.clock_rate)
1530 session->callbacks.clock_rate (session, pt,
1531 session->clock_rate_user_data);
1535 RTP_SESSION_LOCK (session);
1537 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1542 static RTPSourceCallbacks callbacks = {
1543 (RTPSourcePushRTP) source_push_rtp,
1544 (RTPSourceClockRate) source_clock_rate,
1549 * rtp_session_find_conflicting_address:
1550 * @session: The session the packet came in
1551 * @address: address to check for
1552 * @time: The time when the packet that is possibly in conflict arrived
1554 * Checks if an address which has a conflict is already known. If it is
1555 * a known conflict, remember the time
1557 * Returns: TRUE if it was a known conflict, FALSE otherwise
1560 rtp_session_find_conflicting_address (RTPSession * session,
1561 GSocketAddress * address, GstClockTime time)
1563 return find_conflicting_address (session->conflicting_addresses, address,
1568 * rtp_session_add_conflicting_address:
1569 * @session: The session the packet came in
1570 * @address: address to remember
1571 * @time: The time when the packet that is in conflict arrived
1573 * Adds a new conflict address
1576 rtp_session_add_conflicting_address (RTPSession * sess,
1577 GSocketAddress * address, GstClockTime time)
1579 sess->conflicting_addresses =
1580 add_conflicting_address (sess->conflicting_addresses, address, time);
1585 check_collision (RTPSession * sess, RTPSource * source,
1586 RTPPacketInfo * pinfo, gboolean rtp)
1590 /* If we have no pinfo address, we can't do collision checking */
1591 if (!pinfo->address)
1594 ssrc = rtp_source_get_ssrc (source);
1596 if (!source->internal) {
1597 GSocketAddress *from;
1599 /* This is not our local source, but lets check if two remote
1602 from = source->rtp_from;
1604 from = source->rtcp_from;
1608 if (__g_socket_address_equal (from, pinfo->address)) {
1609 /* Address is the same */
1612 GST_LOG ("we have a third-party collision or loop ssrc:%x", ssrc);
1613 if (sess->favor_new) {
1614 if (rtp_source_find_conflicting_address (source,
1615 pinfo->address, pinfo->current_time)) {
1618 buf1 = __g_socket_address_to_string (pinfo->address);
1619 GST_LOG ("Known conflict on %x for %s, dropping packet", ssrc,
1627 /* Current address is not a known conflict, lets assume this is
1628 * a new source. Save old address in possible conflict list
1630 rtp_source_add_conflicting_address (source, from,
1631 pinfo->current_time);
1633 buf1 = __g_socket_address_to_string (from);
1634 buf2 = __g_socket_address_to_string (pinfo->address);
1636 GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1637 " saving old as known conflict", ssrc, buf1, buf2);
1640 rtp_source_set_rtp_from (source, pinfo->address);
1642 rtp_source_set_rtcp_from (source, pinfo->address);
1650 /* Don't need to save old addresses, we ignore new sources */
1655 /* We don't already have a from address for RTP, just set it */
1657 rtp_source_set_rtp_from (source, pinfo->address);
1659 rtp_source_set_rtcp_from (source, pinfo->address);
1663 /* FIXME: Log 3rd party collision somehow
1664 * Maybe should be done in upper layer, only the SDES can tell us
1665 * if its a collision or a loop
1668 /* This is sending with our ssrc, is it an address we already know */
1669 if (rtp_session_find_conflicting_address (sess, pinfo->address,
1670 pinfo->current_time)) {
1671 /* Its a known conflict, its probably a loop, not a collision
1672 * lets just drop the incoming packet
1674 GST_DEBUG ("Our packets are being looped back to us, dropping");
1676 /* Its a new collision, lets change our SSRC */
1677 rtp_session_add_conflicting_address (sess, pinfo->address,
1678 pinfo->current_time);
1680 GST_DEBUG ("Collision for SSRC %x", ssrc);
1681 /* mark the source BYE */
1682 rtp_source_mark_bye (source, "SSRC Collision");
1683 /* if we were suggesting this SSRC, change to something else */
1684 if (sess->suggested_ssrc == ssrc) {
1685 sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
1686 sess->internal_ssrc_set = TRUE;
1689 on_ssrc_collision (sess, source);
1691 rtp_session_schedule_bye_locked (sess, pinfo->current_time);
1700 gboolean is_doing_ptp;
1701 GSocketAddress *new_addr;
1704 /* check if the two given ip addr are the same (do not care about the port) */
1706 ip_addr_equal (GSocketAddress * a, GSocketAddress * b)
1709 g_inet_address_equal (g_inet_socket_address_get_address
1710 (G_INET_SOCKET_ADDRESS (a)),
1711 g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (b)));
1715 compare_rtp_source_addr (const gchar * key, RTPSource * source,
1716 CompareAddrData * data)
1718 /* only compare ip addr of remote sources which are also not closing */
1719 if (!source->internal && !source->closing && source->rtp_from) {
1720 /* look for the first rtp source */
1721 if (!data->new_addr)
1722 data->new_addr = source->rtp_from;
1723 /* compare current ip addr with the first one */
1725 data->is_doing_ptp &= ip_addr_equal (data->new_addr, source->rtp_from);
1730 compare_rtcp_source_addr (const gchar * key, RTPSource * source,
1731 CompareAddrData * data)
1733 /* only compare ip addr of remote sources which are also not closing */
1734 if (!source->internal && !source->closing && source->rtcp_from) {
1735 /* look for the first rtcp source */
1736 if (!data->new_addr)
1737 data->new_addr = source->rtcp_from;
1739 /* compare current ip addr with the first one */
1740 data->is_doing_ptp &= ip_addr_equal (data->new_addr, source->rtcp_from);
1744 /* loop over our non-internal source to know if the session
1745 * is doing point-to-point */
1747 session_update_ptp (RTPSession * sess)
1749 /* to know if the session is doing point to point, the ip addr
1750 * of each non-internal (=remotes) source have to be compared
1753 gboolean is_doing_rtp_ptp;
1754 gboolean is_doing_rtcp_ptp;
1755 CompareAddrData data;
1757 /* compare the first remote source's ip addr that receive rtp packets
1758 * with other remote rtp source.
1759 * it's enough because the session just needs to know if they are all
1762 data.is_doing_ptp = TRUE;
1763 data.new_addr = NULL;
1764 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
1765 (GHFunc) compare_rtp_source_addr, (gpointer) & data);
1766 is_doing_rtp_ptp = data.is_doing_ptp;
1768 /* same but about rtcp */
1769 data.is_doing_ptp = TRUE;
1770 data.new_addr = NULL;
1771 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
1772 (GHFunc) compare_rtcp_source_addr, (gpointer) & data);
1773 is_doing_rtcp_ptp = data.is_doing_ptp;
1775 /* the session is doing point-to-point if all rtp remote have the same
1776 * ip addr and if all rtcp remote sources have the same ip addr */
1777 sess->is_doing_ptp = is_doing_rtp_ptp && is_doing_rtcp_ptp;
1779 GST_DEBUG ("doing point-to-point: %d", sess->is_doing_ptp);
1783 add_source (RTPSession * sess, RTPSource * src)
1785 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1786 GINT_TO_POINTER (src->ssrc), src);
1787 /* report the new source ASAP */
1788 src->generation = sess->generation;
1789 /* we have one more source now */
1790 sess->total_sources++;
1791 if (RTP_SOURCE_IS_ACTIVE (src))
1792 sess->stats.active_sources++;
1793 if (src->internal) {
1794 sess->stats.internal_sources++;
1795 if (!sess->internal_ssrc_from_caps_or_property
1796 && sess->suggested_ssrc != src->ssrc) {
1797 sess->suggested_ssrc = src->ssrc;
1798 sess->internal_ssrc_set = TRUE;
1802 /* update point-to-point status */
1804 session_update_ptp (sess);
1808 find_source (RTPSession * sess, guint32 ssrc)
1810 return g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1811 GINT_TO_POINTER (ssrc));
1814 /* must be called with the session lock, the returned source needs to be
1815 * unreffed after usage. */
1817 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1818 RTPPacketInfo * pinfo, gboolean rtp)
1822 source = find_source (sess, ssrc);
1823 if (source == NULL) {
1824 /* make new Source in probation and insert */
1825 source = rtp_source_new (ssrc);
1827 GST_DEBUG ("creating new source %08x %p", ssrc, source);
1829 /* for RTP packets we need to set the source in probation. Receiving RTCP
1830 * packets of an SSRC, on the other hand, is a strong indication that we
1831 * are dealing with a valid source. */
1832 g_object_set (source, "probation", rtp ? sess->probation : 0,
1833 "max-dropout-time", sess->max_dropout_time, "max-misorder-time",
1834 sess->max_misorder_time, NULL);
1836 /* store from address, if any */
1837 if (pinfo->address) {
1839 rtp_source_set_rtp_from (source, pinfo->address);
1841 rtp_source_set_rtcp_from (source, pinfo->address);
1844 /* configure a callback on the source */
1845 rtp_source_set_callbacks (source, &callbacks, sess);
1847 add_source (sess, source);
1851 /* check for collision, this updates the address when not previously set */
1852 if (check_collision (sess, source, pinfo, rtp)) {
1855 /* Receiving RTCP packets of an SSRC is a strong indication that we
1856 * are dealing with a valid source. */
1858 g_object_set (source, "probation", 0, NULL);
1860 /* update last activity */
1861 source->last_activity = pinfo->current_time;
1863 source->last_rtp_activity = pinfo->current_time;
1864 g_object_ref (source);
1869 /* must be called with the session lock, the returned source needs to be
1870 * unreffed after usage. */
1872 obtain_internal_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1873 GstClockTime current_time)
1877 source = find_source (sess, ssrc);
1878 if (source == NULL) {
1879 /* make new internal Source and insert */
1880 source = rtp_source_new (ssrc);
1882 GST_DEBUG ("creating new internal source %08x %p", ssrc, source);
1884 source->validated = TRUE;
1885 source->internal = TRUE;
1886 source->probation = FALSE;
1887 rtp_source_set_sdes_struct (source, gst_structure_copy (sess->sdes));
1888 rtp_source_set_callbacks (source, &callbacks, sess);
1890 add_source (sess, source);
1895 /* update last activity */
1896 if (current_time != GST_CLOCK_TIME_NONE) {
1897 source->last_activity = current_time;
1898 source->last_rtp_activity = current_time;
1900 g_object_ref (source);
1906 * rtp_session_suggest_ssrc:
1907 * @sess: a #RTPSession
1908 * @is_random: if the suggested ssrc is random
1910 * Suggest an unused SSRC in @sess.
1912 * Returns: a free unused SSRC
1915 rtp_session_suggest_ssrc (RTPSession * sess, gboolean * is_random)
1919 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1921 RTP_SESSION_LOCK (sess);
1922 result = sess->suggested_ssrc;
1924 *is_random = !sess->internal_ssrc_set;
1925 RTP_SESSION_UNLOCK (sess);
1931 * rtp_session_add_source:
1932 * @sess: a #RTPSession
1933 * @src: #RTPSource to add
1935 * Add @src to @session.
1937 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1938 * existed in the session.
1941 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1943 gboolean result = FALSE;
1946 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1947 g_return_val_if_fail (src != NULL, FALSE);
1949 RTP_SESSION_LOCK (sess);
1950 find = find_source (sess, src->ssrc);
1952 add_source (sess, src);
1955 RTP_SESSION_UNLOCK (sess);
1961 * rtp_session_get_num_sources:
1962 * @sess: an #RTPSession
1964 * Get the number of sources in @sess.
1966 * Returns: The number of sources in @sess.
1969 rtp_session_get_num_sources (RTPSession * sess)
1973 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1975 RTP_SESSION_LOCK (sess);
1976 result = sess->total_sources;
1977 RTP_SESSION_UNLOCK (sess);
1983 * rtp_session_get_num_active_sources:
1984 * @sess: an #RTPSession
1986 * Get the number of active sources in @sess. A source is considered active when
1987 * it has been validated and has not yet received a BYE RTCP message.
1989 * Returns: The number of active sources in @sess.
1992 rtp_session_get_num_active_sources (RTPSession * sess)
1996 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1998 RTP_SESSION_LOCK (sess);
1999 result = sess->stats.active_sources;
2000 RTP_SESSION_UNLOCK (sess);
2006 * rtp_session_get_source_by_ssrc:
2007 * @sess: an #RTPSession
2010 * Find the source with @ssrc in @sess.
2012 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
2013 * g_object_unref() after usage.
2016 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
2020 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
2022 RTP_SESSION_LOCK (sess);
2023 result = find_source (sess, ssrc);
2025 g_object_ref (result);
2026 RTP_SESSION_UNLOCK (sess);
2031 /* should be called with the SESSION lock */
2033 rtp_session_create_new_ssrc (RTPSession * sess)
2038 ssrc = g_random_int ();
2040 /* see if it exists in the session, we're done if it doesn't */
2041 if (find_source (sess, ssrc) == NULL)
2048 update_packet (GstBuffer ** buffer, guint idx, RTPPacketInfo * pinfo)
2050 GstNetAddressMeta *meta;
2052 /* get packet size including header overhead */
2053 pinfo->bytes += gst_buffer_get_size (*buffer) + pinfo->header_len;
2057 GstRTPBuffer rtp = { NULL };
2059 if (!gst_rtp_buffer_map (*buffer, GST_MAP_READ, &rtp))
2060 goto invalid_packet;
2062 pinfo->payload_len += gst_rtp_buffer_get_payload_len (&rtp);
2066 /* only keep info for first buffer */
2067 pinfo->ssrc = gst_rtp_buffer_get_ssrc (&rtp);
2068 pinfo->seqnum = gst_rtp_buffer_get_seq (&rtp);
2069 pinfo->pt = gst_rtp_buffer_get_payload_type (&rtp);
2070 pinfo->rtptime = gst_rtp_buffer_get_timestamp (&rtp);
2071 /* copy available csrc */
2072 pinfo->csrc_count = gst_rtp_buffer_get_csrc_count (&rtp);
2073 for (i = 0; i < pinfo->csrc_count; i++)
2074 pinfo->csrcs[i] = gst_rtp_buffer_get_csrc (&rtp, i);
2076 gst_rtp_buffer_unmap (&rtp);
2080 /* for netbuffer we can store the IP address to check for collisions */
2081 meta = gst_buffer_get_net_address_meta (*buffer);
2083 g_object_unref (pinfo->address);
2085 pinfo->address = G_SOCKET_ADDRESS (g_object_ref (meta->addr));
2087 pinfo->address = NULL;
2095 GST_DEBUG ("invalid RTP packet received");
2100 /* update the RTPPacketInfo structure with the current time and other bits
2101 * about the current buffer we are handling.
2102 * This function is typically called when a validated packet is received.
2103 * This function should be called with the RTP_SESSION_LOCK
2106 update_packet_info (RTPSession * sess, RTPPacketInfo * pinfo,
2107 gboolean send, gboolean rtp, gboolean is_list, gpointer data,
2108 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
2114 pinfo->is_list = is_list;
2116 pinfo->current_time = current_time;
2117 pinfo->running_time = running_time;
2118 pinfo->ntpnstime = ntpnstime;
2119 pinfo->header_len = sess->header_len;
2121 pinfo->payload_len = 0;
2125 GstBufferList *list = GST_BUFFER_LIST_CAST (data);
2127 gst_buffer_list_foreach (list, (GstBufferListFunc) update_packet,
2130 GstBuffer *buffer = GST_BUFFER_CAST (data);
2131 res = update_packet (&buffer, 0, pinfo);
2137 clean_packet_info (RTPPacketInfo * pinfo)
2140 g_object_unref (pinfo->address);
2142 gst_mini_object_unref (pinfo->data);
2148 source_update_active (RTPSession * sess, RTPSource * source,
2149 gboolean prevactive)
2151 gboolean active = RTP_SOURCE_IS_ACTIVE (source);
2152 guint32 ssrc = source->ssrc;
2154 if (prevactive == active)
2158 sess->stats.active_sources++;
2159 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
2160 sess->stats.active_sources);
2162 sess->stats.active_sources--;
2163 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
2164 sess->stats.active_sources);
2170 source_update_sender (RTPSession * sess, RTPSource * source,
2171 gboolean prevsender)
2173 gboolean sender = RTP_SOURCE_IS_SENDER (source);
2174 guint32 ssrc = source->ssrc;
2176 if (prevsender == sender)
2180 sess->stats.sender_sources++;
2181 if (source->internal)
2182 sess->stats.internal_sender_sources++;
2183 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
2184 sess->stats.sender_sources);
2186 sess->stats.sender_sources--;
2187 if (source->internal)
2188 sess->stats.internal_sender_sources--;
2189 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
2190 sess->stats.sender_sources);
2196 * rtp_session_process_rtp:
2197 * @sess: and #RTPSession
2198 * @buffer: an RTP buffer
2199 * @current_time: the current system time
2200 * @running_time: the running_time of @buffer
2202 * Process an RTP buffer in the session manager. This function takes ownership
2205 * Returns: a #GstFlowReturn.
2208 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
2209 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
2211 GstFlowReturn result;
2215 gboolean prevsender, prevactive;
2216 RTPPacketInfo pinfo = { 0, };
2219 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2220 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2222 RTP_SESSION_LOCK (sess);
2224 /* update pinfo stats */
2225 if (!update_packet_info (sess, &pinfo, FALSE, TRUE, FALSE, buffer,
2226 current_time, running_time, ntpnstime)) {
2227 GST_DEBUG ("invalid RTP packet received");
2228 RTP_SESSION_UNLOCK (sess);
2229 return rtp_session_process_rtcp (sess, buffer, current_time, running_time,
2235 source = obtain_source (sess, ssrc, &created, &pinfo, TRUE);
2239 prevsender = RTP_SOURCE_IS_SENDER (source);
2240 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2241 oldrate = source->bitrate;
2244 on_new_ssrc (sess, source);
2246 /* let source process the packet */
2247 result = rtp_source_process_rtp (source, &pinfo);
2249 /* source became active */
2250 if (source_update_active (sess, source, prevactive))
2251 on_ssrc_validated (sess, source);
2253 source_update_sender (sess, source, prevsender);
2255 if (oldrate != source->bitrate)
2256 sess->recalc_bandwidth = TRUE;
2259 if (source->validated) {
2263 /* for validated sources, we add the CSRCs as well */
2264 for (i = 0; i < pinfo.csrc_count; i++) {
2266 RTPSource *csrc_src;
2268 csrc = pinfo.csrcs[i];
2271 csrc_src = obtain_source (sess, csrc, &created, &pinfo, TRUE);
2276 GST_DEBUG ("created new CSRC: %08x", csrc);
2277 rtp_source_set_as_csrc (csrc_src);
2278 source_update_active (sess, csrc_src, FALSE);
2279 on_new_ssrc (sess, csrc_src);
2281 g_object_unref (csrc_src);
2284 g_object_unref (source);
2286 RTP_SESSION_UNLOCK (sess);
2288 clean_packet_info (&pinfo);
2295 RTP_SESSION_UNLOCK (sess);
2296 clean_packet_info (&pinfo);
2297 GST_DEBUG ("ignoring packet because its collisioning");
2303 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
2304 GstRTCPPacket * packet, RTPPacketInfo * pinfo)
2308 count = gst_rtcp_packet_get_rb_count (packet);
2309 for (i = 0; i < count; i++) {
2310 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
2311 guint8 fractionlost;
2315 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
2316 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2318 GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
2320 /* find our own source */
2321 src = find_source (sess, ssrc);
2325 if (src->internal && RTP_SOURCE_IS_ACTIVE (src)) {
2326 /* only deal with report blocks for our session, we update the stats of
2327 * the sender of the RTCP message. We could also compare our stats against
2328 * the other sender to see if we are better or worse. */
2329 /* FIXME, need to keep track who the RB block is from */
2330 rtp_source_process_rb (source, pinfo->ntpnstime, fractionlost,
2331 packetslost, exthighestseq, jitter, lsr, dlsr);
2334 on_ssrc_active (sess, source);
2337 /* A Sender report contains statistics about how the sender is doing. This
2338 * includes timing informataion such as the relation between RTP and NTP
2339 * timestamps and the number of packets/bytes it sent to us.
2341 * In this report is also included a set of report blocks related to how this
2342 * sender is receiving data (in case we (or somebody else) is also sending stuff
2343 * to it). This info includes the packet loss, jitter and seqnum. It also
2344 * contains information to calculate the round trip time (LSR/DLSR).
2347 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
2348 RTPPacketInfo * pinfo, gboolean * do_sync)
2350 guint32 senderssrc, rtptime, packet_count, octet_count;
2353 gboolean created, prevsender;
2355 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
2356 &packet_count, &octet_count);
2358 GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
2359 senderssrc, GST_TIME_ARGS (pinfo->current_time));
2361 source = obtain_source (sess, senderssrc, &created, pinfo, FALSE);
2365 /* skip non-bye packets for sources that are marked BYE */
2366 if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
2369 /* don't try to do lip-sync for sources that sent a BYE */
2370 if (RTP_SOURCE_IS_MARKED_BYE (source))
2375 prevsender = RTP_SOURCE_IS_SENDER (source);
2377 /* first update the source */
2378 rtp_source_process_sr (source, pinfo->current_time, ntptime, rtptime,
2379 packet_count, octet_count);
2381 source_update_sender (sess, source, prevsender);
2384 on_new_ssrc (sess, source);
2386 rtp_session_process_rb (sess, source, packet, pinfo);
2389 g_object_unref (source);
2392 /* A receiver report contains statistics about how a receiver is doing. It
2393 * includes stuff like packet loss, jitter and the seqnum it received last. It
2394 * also contains info to calculate the round trip time.
2396 * We are only interested in how the sender of this report is doing wrt to us.
2399 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
2400 RTPPacketInfo * pinfo)
2406 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
2408 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
2410 source = obtain_source (sess, senderssrc, &created, pinfo, FALSE);
2414 /* skip non-bye packets for sources that are marked BYE */
2415 if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
2419 on_new_ssrc (sess, source);
2421 rtp_session_process_rb (sess, source, packet, pinfo);
2424 g_object_unref (source);
2427 /* Get SDES items and store them in the SSRC */
2429 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
2430 RTPPacketInfo * pinfo)
2433 gboolean more_items, more_entries;
2435 items = gst_rtcp_packet_sdes_get_item_count (packet);
2436 GST_DEBUG ("got SDES packet with %d items", items);
2438 more_items = gst_rtcp_packet_sdes_first_item (packet);
2440 while (more_items) {
2442 gboolean changed, created, prevactive;
2446 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
2448 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
2452 /* find src, no probation when dealing with RTCP */
2453 source = obtain_source (sess, ssrc, &created, pinfo, FALSE);
2457 /* skip non-bye packets for sources that are marked BYE */
2458 if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
2461 sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
2463 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
2465 while (more_entries) {
2466 GstRTCPSDESType type;
2472 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
2474 GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
2477 if (type == GST_RTCP_SDES_PRIV) {
2478 name = g_strndup ((const gchar *) &data[1], data[0]);
2480 data += data[0] + 1;
2482 name = g_strdup (gst_rtcp_sdes_type_to_name (type));
2485 value = g_strndup ((const gchar *) data, len);
2487 if (g_utf8_validate (value, -1, NULL)) {
2488 gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
2490 GST_WARNING ("ignore SDES field %s with non-utf8 data %s", name, value);
2496 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
2500 /* takes ownership of sdes */
2501 changed = rtp_source_set_sdes_struct (source, sdes);
2503 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2504 source->validated = TRUE;
2507 on_new_ssrc (sess, source);
2509 /* source became active */
2510 if (source_update_active (sess, source, prevactive))
2511 on_ssrc_validated (sess, source);
2514 on_ssrc_sdes (sess, source);
2517 g_object_unref (source);
2519 more_items = gst_rtcp_packet_sdes_next_item (packet);
2524 /* BYE is sent when a client leaves the session
2527 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
2528 RTPPacketInfo * pinfo)
2532 gboolean reconsider = FALSE;
2534 reason = gst_rtcp_packet_bye_get_reason (packet);
2535 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
2537 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
2538 for (i = 0; i < count; i++) {
2541 gboolean prevactive, prevsender;
2542 guint pmembers, members;
2544 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
2545 GST_DEBUG ("SSRC: %08x", ssrc);
2547 /* find src and mark bye, no probation when dealing with RTCP */
2548 source = find_source (sess, ssrc);
2549 if (!source || source->internal) {
2550 GST_DEBUG ("Ignoring suspicious BYE packet (reason: %s)",
2551 !source ? "can't find source" : "has internal source SSRC");
2555 /* store time for when we need to time out this source */
2556 source->bye_time = pinfo->current_time;
2558 prevactive = RTP_SOURCE_IS_ACTIVE (source);
2559 prevsender = RTP_SOURCE_IS_SENDER (source);
2561 /* mark the source BYE */
2562 rtp_source_mark_bye (source, reason);
2564 pmembers = sess->stats.active_sources;
2566 source_update_active (sess, source, prevactive);
2567 source_update_sender (sess, source, prevsender);
2569 members = sess->stats.active_sources;
2571 if (!sess->scheduled_bye && members < pmembers) {
2572 /* some members went away since the previous timeout estimate.
2573 * Perform reverse reconsideration but only when we are not scheduling a
2575 if (sess->next_rtcp_check_time != GST_CLOCK_TIME_NONE &&
2576 pinfo->current_time < sess->next_rtcp_check_time) {
2577 GstClockTime time_remaining;
2579 /* Scale our next RTCP check time according to the change of numbers
2580 * of members. But only if a) this is the first RTCP, or b) this is not
2581 * a feedback session, or c) this is a feedback session but we schedule
2582 * for every RTCP interval (aka no t-rr-interval set).
2584 * FIXME: a) and b) are not great as we will possibly go below Tmin
2585 * for non-feedback profiles and in case of a) below
2586 * Tmin/t-rr-interval in any case.
2588 if (sess->last_rtcp_send_time == GST_CLOCK_TIME_NONE ||
2589 !(sess->rtp_profile == GST_RTP_PROFILE_AVPF
2590 || sess->rtp_profile == GST_RTP_PROFILE_SAVPF) ||
2591 sess->next_rtcp_check_time - sess->last_rtcp_send_time ==
2592 sess->last_rtcp_interval) {
2593 time_remaining = sess->next_rtcp_check_time - pinfo->current_time;
2594 sess->next_rtcp_check_time =
2595 gst_util_uint64_scale (time_remaining, members, pmembers);
2596 sess->next_rtcp_check_time += pinfo->current_time;
2598 sess->last_rtcp_interval =
2599 gst_util_uint64_scale (sess->last_rtcp_interval, members, pmembers);
2601 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
2602 GST_TIME_ARGS (sess->next_rtcp_check_time));
2604 /* mark pending reconsider. We only want to signal the reconsideration
2605 * once after we handled all the source in the bye packet */
2610 on_bye_ssrc (sess, source);
2613 RTP_SESSION_UNLOCK (sess);
2614 /* notify app of reconsideration */
2615 if (sess->callbacks.reconsider)
2616 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2617 RTP_SESSION_LOCK (sess);
2624 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
2625 RTPPacketInfo * pinfo)
2627 GST_DEBUG ("received APP");
2629 if (g_signal_has_handler_pending (sess,
2630 rtp_session_signals[SIGNAL_ON_APP_RTCP], 0, TRUE)) {
2631 GstBuffer *data_buffer = NULL;
2632 guint16 data_length;
2635 data_length = gst_rtcp_packet_app_get_data_length (packet) * 4;
2636 if (data_length > 0) {
2637 guint8 *data = gst_rtcp_packet_app_get_data (packet);
2638 data_buffer = gst_buffer_copy_region (packet->rtcp->buffer,
2639 GST_BUFFER_COPY_MEMORY, data - packet->rtcp->map.data, data_length);
2640 GST_BUFFER_PTS (data_buffer) = pinfo->running_time;
2643 memcpy (name, gst_rtcp_packet_app_get_name (packet), 4);
2646 RTP_SESSION_UNLOCK (sess);
2647 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_APP_RTCP], 0,
2648 gst_rtcp_packet_app_get_subtype (packet),
2649 gst_rtcp_packet_app_get_ssrc (packet), name, data_buffer);
2650 RTP_SESSION_LOCK (sess);
2653 gst_buffer_unref (data_buffer);
2658 rtp_session_request_local_key_unit (RTPSession * sess, RTPSource * src,
2659 guint32 media_ssrc, gboolean fir, GstClockTime current_time)
2661 guint32 round_trip = 0;
2663 rtp_source_get_last_rb (src, NULL, NULL, NULL, NULL, NULL, NULL, &round_trip);
2665 if (src->last_keyframe_request != GST_CLOCK_TIME_NONE && round_trip) {
2666 GstClockTime round_trip_in_ns = gst_util_uint64_scale (round_trip,
2669 /* Sanity check to avoid always ignoring PLI/FIR if we receive RTCP
2670 * packets with erroneous values resulting in crazy high RTT. */
2671 if (round_trip_in_ns > 5 * GST_SECOND)
2672 round_trip_in_ns = GST_SECOND / 2;
2674 if (current_time - src->last_keyframe_request < 2 * round_trip_in_ns) {
2675 GST_DEBUG ("Ignoring %s request from %X because one was send without one "
2676 "RTT (%" GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")",
2677 fir ? "FIR" : "PLI", rtp_source_get_ssrc (src),
2678 GST_TIME_ARGS (current_time - src->last_keyframe_request),
2679 GST_TIME_ARGS (round_trip_in_ns));
2684 src->last_keyframe_request = current_time;
2686 GST_LOG ("received %s request from %X about %X %p(%p)", fir ? "FIR" : "PLI",
2687 rtp_source_get_ssrc (src), media_ssrc, sess->callbacks.process_rtp,
2688 sess->callbacks.request_key_unit);
2690 RTP_SESSION_UNLOCK (sess);
2691 sess->callbacks.request_key_unit (sess, media_ssrc, fir,
2692 sess->request_key_unit_user_data);
2693 RTP_SESSION_LOCK (sess);
2699 rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
2700 guint32 media_ssrc, GstClockTime current_time)
2704 if (!sess->callbacks.request_key_unit)
2707 src = find_source (sess, sender_ssrc);
2709 /* try to find a src with media_ssrc instead */
2710 src = find_source (sess, media_ssrc);
2715 rtp_session_request_local_key_unit (sess, src, media_ssrc, FALSE,
2720 rtp_session_process_fir (RTPSession * sess, guint32 sender_ssrc,
2721 guint32 media_ssrc, guint8 * fci_data, guint fci_length,
2722 GstClockTime current_time)
2727 gboolean our_request = FALSE;
2729 if (!sess->callbacks.request_key_unit)
2735 src = find_source (sess, sender_ssrc);
2737 /* Hack because Google fails to set the sender_ssrc correctly */
2738 if (!src && sender_ssrc == 1) {
2739 GHashTableIter iter;
2741 /* we can't find the source if there are multiple */
2742 if (sess->stats.sender_sources > sess->stats.internal_sender_sources + 1)
2745 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
2746 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
2747 if (!src->internal && rtp_source_is_sender (src))
2755 for (position = 0; position < fci_length; position += 8) {
2756 guint8 *data = fci_data + position;
2759 ssrc = GST_READ_UINT32_BE (data);
2761 own = find_source (sess, ssrc);
2765 if (own->internal) {
2773 rtp_session_request_local_key_unit (sess, src, media_ssrc, TRUE,
2778 rtp_session_process_nack (RTPSession * sess, guint32 sender_ssrc,
2779 guint32 media_ssrc, guint8 * fci_data, guint fci_length,
2780 GstClockTime current_time)
2782 sess->stats.nacks_received++;
2784 if (!sess->callbacks.notify_nack)
2787 while (fci_length > 0) {
2788 guint16 seqnum, blp;
2790 seqnum = GST_READ_UINT16_BE (fci_data);
2791 blp = GST_READ_UINT16_BE (fci_data + 2);
2793 GST_DEBUG ("NACK #%u, blp %04x, SSRC 0x%08x", seqnum, blp, media_ssrc);
2795 RTP_SESSION_UNLOCK (sess);
2796 sess->callbacks.notify_nack (sess, seqnum, blp, media_ssrc,
2797 sess->notify_nack_user_data);
2798 RTP_SESSION_LOCK (sess);
2806 rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
2807 RTPPacketInfo * pinfo, GstClockTime current_time)
2810 GstRTCPFBType fbtype;
2811 guint32 sender_ssrc, media_ssrc;
2816 /* The feedback packet must include both sender SSRC and media SSRC */
2817 if (packet->length < 2)
2820 type = gst_rtcp_packet_get_type (packet);
2821 fbtype = gst_rtcp_packet_fb_get_type (packet);
2822 sender_ssrc = gst_rtcp_packet_fb_get_sender_ssrc (packet);
2823 media_ssrc = gst_rtcp_packet_fb_get_media_ssrc (packet);
2825 src = find_source (sess, media_ssrc);
2827 /* skip non-bye packets for sources that are marked BYE */
2828 if (sess->scheduled_bye && src && RTP_SOURCE_IS_MARKED_BYE (src))
2834 fci_data = gst_rtcp_packet_fb_get_fci (packet);
2835 fci_length = gst_rtcp_packet_fb_get_fci_length (packet) * sizeof (guint32);
2837 GST_DEBUG ("received feedback %d:%d from %08X about %08X with FCI of "
2838 "length %d", type, fbtype, sender_ssrc, media_ssrc, fci_length);
2840 if (g_signal_has_handler_pending (sess,
2841 rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0, TRUE)) {
2842 GstBuffer *fci_buffer = NULL;
2844 if (fci_length > 0) {
2845 fci_buffer = gst_buffer_copy_region (packet->rtcp->buffer,
2846 GST_BUFFER_COPY_MEMORY, fci_data - packet->rtcp->map.data,
2848 GST_BUFFER_PTS (fci_buffer) = pinfo->running_time;
2851 RTP_SESSION_UNLOCK (sess);
2852 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0,
2853 type, fbtype, sender_ssrc, media_ssrc, fci_buffer);
2854 RTP_SESSION_LOCK (sess);
2857 gst_buffer_unref (fci_buffer);
2860 if (src && sess->rtcp_feedback_retention_window != GST_CLOCK_TIME_NONE) {
2861 rtp_source_retain_rtcp_packet (src, packet, pinfo->running_time);
2864 if ((src && src->internal) ||
2865 /* PSFB FIR puts the media ssrc inside the FCI */
2866 (type == GST_RTCP_TYPE_PSFB && fbtype == GST_RTCP_PSFB_TYPE_FIR)) {
2868 case GST_RTCP_TYPE_PSFB:
2870 case GST_RTCP_PSFB_TYPE_PLI:
2872 src->stats.recv_pli_count++;
2873 rtp_session_process_pli (sess, sender_ssrc, media_ssrc,
2876 case GST_RTCP_PSFB_TYPE_FIR:
2878 src->stats.recv_fir_count++;
2879 rtp_session_process_fir (sess, sender_ssrc, media_ssrc, fci_data,
2880 fci_length, current_time);
2886 case GST_RTCP_TYPE_RTPFB:
2888 case GST_RTCP_RTPFB_TYPE_NACK:
2890 src->stats.recv_nack_count++;
2891 rtp_session_process_nack (sess, sender_ssrc, media_ssrc,
2892 fci_data, fci_length, current_time);
2903 g_object_unref (src);
2907 * rtp_session_process_rtcp:
2908 * @sess: and #RTPSession
2909 * @buffer: an RTCP buffer
2910 * @current_time: the current system time
2911 * @ntpnstime: the current NTP time in nanoseconds
2913 * Process an RTCP buffer in the session manager. This function takes ownership
2916 * Returns: a #GstFlowReturn.
2919 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
2920 GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
2922 GstRTCPPacket packet;
2923 gboolean more, is_bye = FALSE, do_sync = FALSE;
2924 RTPPacketInfo pinfo = { 0, };
2925 GstFlowReturn result = GST_FLOW_OK;
2926 GstRTCPBuffer rtcp = { NULL, };
2928 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2929 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2931 if (!gst_rtcp_buffer_validate_reduced (buffer))
2932 goto invalid_packet;
2934 GST_DEBUG ("received RTCP packet");
2936 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_RECEIVING_RTCP], 0,
2939 RTP_SESSION_LOCK (sess);
2940 /* update pinfo stats */
2941 update_packet_info (sess, &pinfo, FALSE, FALSE, FALSE, buffer, current_time,
2942 running_time, ntpnstime);
2944 /* start processing the compound packet */
2945 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
2946 more = gst_rtcp_buffer_get_first_packet (&rtcp, &packet);
2950 type = gst_rtcp_packet_get_type (&packet);
2953 case GST_RTCP_TYPE_SR:
2954 rtp_session_process_sr (sess, &packet, &pinfo, &do_sync);
2956 case GST_RTCP_TYPE_RR:
2957 rtp_session_process_rr (sess, &packet, &pinfo);
2959 case GST_RTCP_TYPE_SDES:
2960 rtp_session_process_sdes (sess, &packet, &pinfo);
2962 case GST_RTCP_TYPE_BYE:
2964 /* don't try to attempt lip-sync anymore for streams with a BYE */
2966 rtp_session_process_bye (sess, &packet, &pinfo);
2968 case GST_RTCP_TYPE_APP:
2969 rtp_session_process_app (sess, &packet, &pinfo);
2971 case GST_RTCP_TYPE_RTPFB:
2972 case GST_RTCP_TYPE_PSFB:
2973 rtp_session_process_feedback (sess, &packet, &pinfo, current_time);
2975 case GST_RTCP_TYPE_XR:
2976 /* FIXME: This block is added to downgrade warning level.
2977 * Once the parser is implemented, it should be replaced with
2978 * a proper process function. */
2979 GST_DEBUG ("got RTCP XR packet, but ignored");
2982 GST_WARNING ("got unknown RTCP packet type: %d", type);
2985 more = gst_rtcp_packet_move_to_next (&packet);
2988 gst_rtcp_buffer_unmap (&rtcp);
2990 /* if we are scheduling a BYE, we only want to count bye packets, else we
2991 * count everything */
2992 if (sess->scheduled_bye && is_bye) {
2993 sess->bye_stats.bye_members++;
2994 UPDATE_AVG (sess->bye_stats.avg_rtcp_packet_size, pinfo.bytes);
2997 /* keep track of average packet size */
2998 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, pinfo.bytes);
3000 GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
3001 sess->stats.avg_rtcp_packet_size, pinfo.bytes);
3002 RTP_SESSION_UNLOCK (sess);
3005 clean_packet_info (&pinfo);
3007 /* notify caller of sr packets in the callback */
3008 if (do_sync && sess->callbacks.sync_rtcp) {
3009 result = sess->callbacks.sync_rtcp (sess, buffer,
3010 sess->sync_rtcp_user_data);
3012 gst_buffer_unref (buffer);
3019 GST_DEBUG ("invalid RTCP packet received");
3020 gst_buffer_unref (buffer);
3026 * rtp_session_update_send_caps:
3027 * @sess: an #RTPSession
3030 * Update the caps of the sender in the rtp session.
3033 rtp_session_update_send_caps (RTPSession * sess, GstCaps * caps)
3038 g_return_if_fail (RTP_IS_SESSION (sess));
3039 g_return_if_fail (GST_IS_CAPS (caps));
3041 GST_LOG ("received caps %" GST_PTR_FORMAT, caps);
3043 s = gst_caps_get_structure (caps, 0);
3045 if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
3049 RTP_SESSION_LOCK (sess);
3050 source = obtain_internal_source (sess, ssrc, &created, GST_CLOCK_TIME_NONE);
3051 sess->suggested_ssrc = ssrc;
3052 sess->internal_ssrc_set = TRUE;
3053 sess->internal_ssrc_from_caps_or_property = TRUE;
3055 rtp_source_update_caps (source, caps);
3058 on_new_sender_ssrc (sess, source);
3060 g_object_unref (source);
3063 if (gst_structure_get_uint (s, "rtx-ssrc", &ssrc)) {
3065 obtain_internal_source (sess, ssrc, &created, GST_CLOCK_TIME_NONE);
3067 rtp_source_update_caps (source, caps);
3070 on_new_sender_ssrc (sess, source);
3072 g_object_unref (source);
3075 RTP_SESSION_UNLOCK (sess);
3077 sess->internal_ssrc_from_caps_or_property = FALSE;
3082 * rtp_session_send_rtp:
3083 * @sess: an #RTPSession
3084 * @data: pointer to either an RTP buffer or a list of RTP buffers
3085 * @is_list: TRUE when @data is a buffer list
3086 * @current_time: the current system time
3087 * @running_time: the running time of @data
3089 * Send the RTP data (a buffer or buffer list) in the session manager. This
3090 * function takes ownership of @data.
3092 * Returns: a #GstFlowReturn.
3095 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
3096 GstClockTime current_time, GstClockTime running_time)
3098 GstFlowReturn result;
3100 gboolean prevsender;
3102 RTPPacketInfo pinfo = { 0, };
3105 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3106 g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
3108 GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
3110 RTP_SESSION_LOCK (sess);
3111 if (!update_packet_info (sess, &pinfo, TRUE, TRUE, is_list, data,
3112 current_time, running_time, -1))
3113 goto invalid_packet;
3115 source = obtain_internal_source (sess, pinfo.ssrc, &created, current_time);
3117 on_new_sender_ssrc (sess, source);
3119 if (!source->internal)
3120 /* FIXME: Send GstRTPCollision upstream */
3123 prevsender = RTP_SOURCE_IS_SENDER (source);
3124 oldrate = source->bitrate;
3126 /* we use our own source to send */
3127 result = rtp_source_send_rtp (source, &pinfo);
3129 source_update_sender (sess, source, prevsender);
3131 if (oldrate != source->bitrate)
3132 sess->recalc_bandwidth = TRUE;
3133 RTP_SESSION_UNLOCK (sess);
3135 g_object_unref (source);
3136 clean_packet_info (&pinfo);
3142 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3143 RTP_SESSION_UNLOCK (sess);
3144 GST_DEBUG ("invalid RTP packet received");
3149 g_object_unref (source);
3150 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3151 RTP_SESSION_UNLOCK (sess);
3152 GST_WARNING ("non-internal source with same ssrc %08x, drop packet",
3159 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
3161 *bandwidth += source->bitrate;
3164 /* must be called with session lock */
3166 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
3169 GstClockTime result;
3170 RTPSessionStats *stats;
3172 /* recalculate bandwidth when it changed */
3173 if (sess->recalc_bandwidth) {
3176 if (sess->bandwidth > 0)
3177 bandwidth = sess->bandwidth;
3179 /* If it is <= 0, then try to estimate the actual bandwidth */
3182 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3183 (GHFunc) add_bitrates, &bandwidth);
3185 if (bandwidth < RTP_STATS_BANDWIDTH)
3186 bandwidth = RTP_STATS_BANDWIDTH;
3188 rtp_stats_set_bandwidths (&sess->stats, bandwidth,
3189 sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
3191 sess->recalc_bandwidth = FALSE;
3194 if (sess->scheduled_bye) {
3195 stats = &sess->bye_stats;
3196 result = rtp_stats_calculate_bye_interval (stats);
3198 session_update_ptp (sess);
3200 stats = &sess->stats;
3201 result = rtp_stats_calculate_rtcp_interval (stats,
3202 stats->internal_sender_sources > 0, sess->rtp_profile,
3203 sess->is_doing_ptp, first);
3206 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
3207 GST_TIME_ARGS (result), first);
3209 if (!deterministic && result != GST_CLOCK_TIME_NONE)
3210 result = rtp_stats_add_rtcp_jitter (stats, result);
3212 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
3218 source_mark_bye (const gchar * key, RTPSource * source, const gchar * reason)
3220 if (source->internal)
3221 rtp_source_mark_bye (source, reason);
3225 * rtp_session_mark_all_bye:
3226 * @sess: an #RTPSession
3229 * Mark all internal sources of the session as BYE with @reason.
3232 rtp_session_mark_all_bye (RTPSession * sess, const gchar * reason)
3234 g_return_if_fail (RTP_IS_SESSION (sess));
3236 RTP_SESSION_LOCK (sess);
3237 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3238 (GHFunc) source_mark_bye, (gpointer) reason);
3239 RTP_SESSION_UNLOCK (sess);
3242 /* Stop the current @sess and schedule a BYE message for the other members.
3243 * One must have the session lock to call this function
3245 static GstFlowReturn
3246 rtp_session_schedule_bye_locked (RTPSession * sess, GstClockTime current_time)
3248 GstFlowReturn result = GST_FLOW_OK;
3249 GstClockTime interval;
3251 /* nothing to do it we already scheduled bye */
3252 if (sess->scheduled_bye)
3255 /* we schedule BYE now */
3256 sess->scheduled_bye = TRUE;
3257 /* at least one member wants to send a BYE */
3258 memcpy (&sess->bye_stats, &sess->stats, sizeof (RTPSessionStats));
3259 INIT_AVG (sess->bye_stats.avg_rtcp_packet_size, 100);
3260 sess->bye_stats.bye_members = 1;
3261 sess->first_rtcp = TRUE;
3263 /* reschedule transmission */
3264 sess->last_rtcp_send_time = current_time;
3265 sess->last_rtcp_check_time = current_time;
3266 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
3268 if (interval != GST_CLOCK_TIME_NONE)
3269 sess->next_rtcp_check_time = current_time + interval;
3271 sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
3272 sess->last_rtcp_interval = interval;
3274 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
3275 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
3277 RTP_SESSION_UNLOCK (sess);
3278 /* notify app of reconsideration */
3279 if (sess->callbacks.reconsider)
3280 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
3281 RTP_SESSION_LOCK (sess);
3288 * rtp_session_schedule_bye:
3289 * @sess: an #RTPSession
3290 * @current_time: the current system time
3292 * Schedule a BYE message for all sources marked as BYE in @sess.
3294 * Returns: a #GstFlowReturn.
3297 rtp_session_schedule_bye (RTPSession * sess, GstClockTime current_time)
3299 GstFlowReturn result;
3301 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3303 RTP_SESSION_LOCK (sess);
3304 result = rtp_session_schedule_bye_locked (sess, current_time);
3305 RTP_SESSION_UNLOCK (sess);
3311 * rtp_session_next_timeout:
3312 * @sess: an #RTPSession
3313 * @current_time: the current system time
3315 * Get the next time we should perform session maintenance tasks.
3317 * Returns: a time when rtp_session_on_timeout() should be called with the
3318 * current system time.
3321 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
3323 GstClockTime result, interval = 0;
3325 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
3327 RTP_SESSION_LOCK (sess);
3329 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
3330 GST_DEBUG ("have early rtcp time");
3331 result = sess->next_early_rtcp_time;
3335 result = sess->next_rtcp_check_time;
3337 GST_DEBUG ("current time: %" GST_TIME_FORMAT
3338 ", next time: %" GST_TIME_FORMAT,
3339 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
3341 if (result == GST_CLOCK_TIME_NONE || result < current_time) {
3342 GST_DEBUG ("take current time as base");
3343 /* our previous check time expired, start counting from the current time
3345 result = current_time;
3348 if (sess->scheduled_bye) {
3349 if (sess->bye_stats.active_sources >= 50) {
3350 GST_DEBUG ("reconsider BYE, more than 50 sources");
3351 /* reconsider BYE if members >= 50 */
3352 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
3353 sess->last_rtcp_interval = interval;
3356 if (sess->first_rtcp) {
3357 GST_DEBUG ("first RTCP packet");
3358 /* we are called for the first time */
3359 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
3360 sess->last_rtcp_interval = interval;
3361 } else if (sess->next_rtcp_check_time < current_time) {
3362 GST_DEBUG ("old check time expired, getting new timeout");
3363 /* get a new timeout when we need to */
3364 interval = calculate_rtcp_interval (sess, FALSE, FALSE);
3365 sess->last_rtcp_interval = interval;
3367 if ((sess->rtp_profile == GST_RTP_PROFILE_AVPF
3368 || sess->rtp_profile == GST_RTP_PROFILE_SAVPF)
3369 && interval != GST_CLOCK_TIME_NONE) {
3370 /* Apply the rules from RFC 4585 section 3.5.3 */
3371 if (sess->stats.min_interval != 0) {
3372 GstClockTime T_rr_current_interval = g_random_double_range (0.5,
3373 1.5) * sess->stats.min_interval * GST_SECOND;
3375 if (T_rr_current_interval > interval) {
3376 GST_DEBUG ("Adjusting interval for t-rr-interval: %" GST_TIME_FORMAT
3377 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (T_rr_current_interval),
3378 GST_TIME_ARGS (interval));
3379 interval = T_rr_current_interval;
3386 if (interval != GST_CLOCK_TIME_NONE)
3389 result = GST_CLOCK_TIME_NONE;
3391 sess->next_rtcp_check_time = result;
3395 GST_DEBUG ("current time: %" GST_TIME_FORMAT
3396 ", next time: %" GST_TIME_FORMAT,
3397 GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
3398 RTP_SESSION_UNLOCK (sess);
3412 GstRTCPBuffer rtcpbuf;
3415 guint num_to_report;
3420 GstClockTime current_time;
3422 GstClockTime running_time;
3423 GstClockTime interval;
3424 GstRTCPPacket packet;
3427 gboolean may_suppress;
3429 guint nacked_seqnums;
3433 session_start_rtcp (RTPSession * sess, ReportData * data)
3435 GstRTCPPacket *packet = &data->packet;
3436 RTPSource *own = data->source;
3437 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3439 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
3440 data->has_sdes = FALSE;
3442 gst_rtcp_buffer_map (data->rtcp, GST_MAP_READWRITE, rtcp);
3444 if (data->is_early && sess->reduced_size_rtcp)
3447 if (RTP_SOURCE_IS_SENDER (own)) {
3450 guint32 packet_count, octet_count;
3452 /* we are a sender, create SR */
3453 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
3454 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SR, packet);
3456 /* get latest stats */
3457 rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
3458 &ntptime, &rtptime, &packet_count, &octet_count);
3460 rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
3461 packet_count, octet_count);
3463 /* fill in sender report info */
3464 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
3465 sess->timestamp_sender_reports ? ntptime : 0,
3466 sess->timestamp_sender_reports ? rtptime : 0,
3467 packet_count, octet_count);
3469 /* we are only receiver, create RR */
3470 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
3471 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RR, packet);
3472 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
3476 /* construct a Sender or Receiver Report */
3478 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
3480 RTPSession *sess = data->sess;
3481 GstRTCPPacket *packet = &data->packet;
3482 guint8 fractionlost;
3484 guint32 exthighestseq, jitter;
3487 /* don't report for sources in future generations */
3488 if (((gint16) (source->generation - sess->generation)) > 0) {
3489 GST_DEBUG ("source %08x generation %u > %u", source->ssrc,
3490 source->generation, sess->generation);
3494 if (g_hash_table_contains (source->reported_in_sr_of,
3495 GUINT_TO_POINTER (data->source->ssrc))) {
3496 GST_DEBUG ("source %08x already reported in this generation", source->ssrc);
3500 if (gst_rtcp_packet_get_rb_count (packet) == GST_RTCP_MAX_RB_COUNT) {
3501 GST_DEBUG ("max RB count reached");
3505 /* only report about remote sources */
3506 if (source->internal)
3509 if (!RTP_SOURCE_IS_SENDER (source)) {
3510 GST_DEBUG ("source %08x not sender", source->ssrc);
3514 if (source->disable_rtcp) {
3515 GST_DEBUG ("source %08x has RTCP disabled", source->ssrc);
3519 GST_DEBUG ("create RB for SSRC %08x", source->ssrc);
3522 rtp_source_get_new_rb (source, data->current_time, &fractionlost,
3523 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
3525 /* store last generated RR packet */
3526 source->last_rr.is_valid = TRUE;
3527 source->last_rr.fractionlost = fractionlost;
3528 source->last_rr.packetslost = packetslost;
3529 source->last_rr.exthighestseq = exthighestseq;
3530 source->last_rr.jitter = jitter;
3531 source->last_rr.lsr = lsr;
3532 source->last_rr.dlsr = dlsr;
3534 /* packet is not yet filled, add report block for this source. */
3535 gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
3536 exthighestseq, jitter, lsr, dlsr);
3539 g_hash_table_add (source->reported_in_sr_of,
3540 GUINT_TO_POINTER (data->source->ssrc));
3545 session_add_fir (const gchar * key, RTPSource * source, ReportData * data)
3547 GstRTCPPacket *packet = &data->packet;
3551 if (!source->send_fir)
3554 len = gst_rtcp_packet_fb_get_fci_length (packet);
3555 if (!gst_rtcp_packet_fb_set_fci_length (packet, len + 2))
3556 /* exit because the packet is full, will put next request in a
3560 fci_data = gst_rtcp_packet_fb_get_fci (packet) + (len * 4);
3562 GST_WRITE_UINT32_BE (fci_data, source->ssrc);
3564 fci_data[0] = source->current_send_fir_seqnum;
3565 fci_data[1] = fci_data[2] = fci_data[3] = 0;
3567 source->send_fir = FALSE;
3568 source->stats.sent_fir_count++;
3572 session_fir (RTPSession * sess, ReportData * data)
3574 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3575 GstRTCPPacket *packet = &data->packet;
3577 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
3580 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_FIR);
3581 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3582 gst_rtcp_packet_fb_set_media_ssrc (packet, 0);
3584 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3585 (GHFunc) session_add_fir, data);
3587 if (gst_rtcp_packet_fb_get_fci_length (packet) == 0)
3588 gst_rtcp_packet_remove (packet);
3590 data->may_suppress = FALSE;
3594 has_pli_compare_func (gconstpointer a, gconstpointer ignored)
3596 GstRTCPPacket packet;
3597 GstRTCPBuffer rtcp = { NULL, };
3598 gboolean ret = FALSE;
3600 gst_rtcp_buffer_map ((GstBuffer *) a, GST_MAP_READ, &rtcp);
3602 if (gst_rtcp_buffer_get_first_packet (&rtcp, &packet)) {
3603 if (gst_rtcp_packet_get_type (&packet) == GST_RTCP_TYPE_PSFB &&
3604 gst_rtcp_packet_fb_get_type (&packet) == GST_RTCP_PSFB_TYPE_PLI)
3608 gst_rtcp_buffer_unmap (&rtcp);
3615 session_pli (const gchar * key, RTPSource * source, ReportData * data)
3617 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3618 GstRTCPPacket *packet = &data->packet;
3620 if (!source->send_pli)
3623 if (rtp_source_has_retained (source, has_pli_compare_func, NULL))
3626 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
3627 /* exit because the packet is full, will put next request in a
3631 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_PLI);
3632 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3633 gst_rtcp_packet_fb_set_media_ssrc (packet, source->ssrc);
3635 source->send_pli = FALSE;
3636 data->may_suppress = FALSE;
3638 source->stats.sent_pli_count++;
3641 /* construct NACK */
3643 session_nack (const gchar * key, RTPSource * source, ReportData * data)
3645 RTPSession *sess = data->sess;
3646 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3647 GstRTCPPacket *packet = &data->packet;
3649 GstClockTime *nack_deadlines;
3650 guint n_nacks, i = 0;
3651 guint nacked_seqnums = 0;
3652 guint16 n_fb_nacks = 0;
3655 if (!source->send_nack)
3658 nacks = rtp_source_get_nacks (source, &n_nacks);
3659 nack_deadlines = rtp_source_get_nack_deadlines (source, NULL);
3660 GST_DEBUG ("%u NACKs current time %" GST_TIME_FORMAT, n_nacks,
3661 GST_TIME_ARGS (data->current_time));
3663 /* cleanup expired nacks */
3664 for (i = 0; i < n_nacks; i++) {
3665 GST_DEBUG ("#%u deadline %" GST_TIME_FORMAT, nacks[i],
3666 GST_TIME_ARGS (nack_deadlines[i]));
3667 if (nack_deadlines[i] >= data->current_time)
3671 if (data->is_early) {
3672 /* don't remove them all if this is an early RTCP packet. It may happen
3673 * that the NACKs are late due to high RTT, not sending NACKs at all would
3674 * keep the RTX RTT stats high and maintain a dropping state. */
3675 i = MIN (n_nacks - 1, i);
3679 GST_WARNING ("Removing %u expired NACKS", i);
3680 rtp_source_clear_nacks (source, i);
3686 /* allow overriding NACK to packet conversion */
3687 if (g_signal_has_handler_pending (sess,
3688 rtp_session_signals[SIGNAL_ON_SENDING_NACKS], 0, TRUE)) {
3689 /* this is needed as it will actually resize the buffer */
3690 gst_rtcp_buffer_unmap (rtcp);
3692 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_NACKS], 0,
3693 data->source->ssrc, source->ssrc, source->nacks, data->rtcp,
3696 /* and now remap for the remaining work */
3697 gst_rtcp_buffer_map (data->rtcp, GST_MAP_READWRITE, rtcp);
3699 if (nacked_seqnums > 0)
3703 if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RTPFB, packet))
3704 /* exit because the packet is full, will put next request in a
3708 gst_rtcp_packet_fb_set_type (packet, GST_RTCP_RTPFB_TYPE_NACK);
3709 gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3710 gst_rtcp_packet_fb_set_media_ssrc (packet, source->ssrc);
3712 if (!gst_rtcp_packet_fb_set_fci_length (packet, 1)) {
3713 gst_rtcp_packet_remove (packet);
3714 GST_WARNING ("no nacks fit in the packet");
3718 fci_data = gst_rtcp_packet_fb_get_fci (packet);
3719 for (i = 0; i < n_nacks; i = nacked_seqnums) {
3720 guint16 seqnum = nacks[i];
3724 if (!gst_rtcp_packet_fb_set_fci_length (packet, n_fb_nacks + 1))
3730 for (j = i + 1; j < n_nacks; j++) {
3733 diff = gst_rtp_buffer_compare_seqnum (seqnum, nacks[j]);
3734 GST_TRACE ("[%u][%u] %u %u diff %i", i, j, seqnum, nacks[j], diff);
3738 blp |= 1 << (diff - 1);
3742 GST_WRITE_UINT32_BE (fci_data, seqnum << 16 | blp);
3746 GST_DEBUG ("Sent %u seqnums into %u FB NACKs", nacked_seqnums, n_fb_nacks);
3747 source->stats.sent_nack_count += n_fb_nacks;
3750 data->nacked_seqnums += nacked_seqnums;
3751 rtp_source_clear_nacks (source, nacked_seqnums);
3752 data->may_suppress = FALSE;
3755 /* perform cleanup of sources that timed out */
3757 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
3759 gboolean remove = FALSE;
3760 gboolean byetimeout = FALSE;
3761 gboolean sendertimeout = FALSE;
3762 gboolean is_sender, is_active;
3763 RTPSession *sess = data->sess;
3764 GstClockTime interval, binterval;
3767 GST_DEBUG ("look at %08x, generation %u", source->ssrc, source->generation);
3769 /* check for outdated collisions */
3770 if (source->internal) {
3771 GST_DEBUG ("Timing out collisions for %x", source->ssrc);
3772 rtp_source_timeout (source, data->current_time, data->running_time,
3773 sess->rtcp_feedback_retention_window);
3776 /* nothing else to do when without RTCP */
3777 if (data->interval == GST_CLOCK_TIME_NONE)
3780 is_sender = RTP_SOURCE_IS_SENDER (source);
3781 is_active = RTP_SOURCE_IS_ACTIVE (source);
3783 /* our own rtcp interval may have been forced low by secondary configuration,
3784 * while sender side may still operate with higher interval,
3785 * so do not just take our interval to decide on timing out sender,
3786 * but take (if data->interval <= 5 * GST_SECOND):
3787 * interval = CLAMP (sender_interval, data->interval, 5 * GST_SECOND)
3788 * where sender_interval is difference between last 2 received RTCP reports
3790 if (data->interval >= 5 * GST_SECOND || source->internal) {
3791 binterval = data->interval;
3793 GST_LOG ("prev_rtcp %" GST_TIME_FORMAT ", last_rtcp %" GST_TIME_FORMAT,
3794 GST_TIME_ARGS (source->stats.prev_rtcptime),
3795 GST_TIME_ARGS (source->stats.last_rtcptime));
3796 /* if not received enough yet, fallback to larger default */
3797 if (source->stats.last_rtcptime > source->stats.prev_rtcptime)
3798 binterval = source->stats.last_rtcptime - source->stats.prev_rtcptime;
3800 binterval = 5 * GST_SECOND;
3801 binterval = CLAMP (binterval, data->interval, 5 * GST_SECOND);
3803 GST_LOG ("timeout base interval %" GST_TIME_FORMAT,
3804 GST_TIME_ARGS (binterval));
3806 if (!source->internal && source->marked_bye) {
3807 /* if we received a BYE from the source, remove the source after some
3809 if (data->current_time > source->bye_time &&
3810 data->current_time - source->bye_time > sess->stats.bye_timeout) {
3811 GST_DEBUG ("removing BYE source %08x", source->ssrc);
3817 if (source->internal && source->sent_bye) {
3818 GST_DEBUG ("removing internal source that has sent BYE %08x", source->ssrc);
3822 /* sources that were inactive for more than 5 times the deterministic reporting
3823 * interval get timed out. the min timeout is 5 seconds. */
3824 /* mind old time that might pre-date last time going to PLAYING */
3825 btime = MAX (source->last_activity, sess->start_time);
3826 if (data->current_time > btime) {
3827 interval = MAX (binterval * 5, 5 * GST_SECOND);
3828 if (data->current_time - btime > interval) {
3829 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
3830 source->ssrc, GST_TIME_ARGS (btime));
3831 if (source->internal) {
3832 /* this is an internal source that is not using our suggested ssrc.
3833 * since there must be another source using this ssrc, we can remove
3834 * this one instead of making it a receiver forever */
3835 if (source->ssrc != sess->suggested_ssrc) {
3836 rtp_source_mark_bye (source, "timed out");
3837 /* do not schedule bye here, since we are inside the RTCP timeout
3838 * processing and scheduling bye will interfere with SR/RR sending */
3846 /* senders that did not send for a long time become a receiver, this also
3847 * holds for our own sources. */
3849 /* mind old time that might pre-date last time going to PLAYING */
3850 btime = MAX (source->last_rtp_activity, sess->start_time);
3851 if (data->current_time > btime) {
3852 interval = MAX (binterval * 2, 5 * GST_SECOND);
3853 if (data->current_time - btime > interval) {
3854 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
3855 GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
3856 sendertimeout = TRUE;
3862 sess->total_sources--;
3864 sess->stats.sender_sources--;
3865 if (source->internal)
3866 sess->stats.internal_sender_sources--;
3869 sess->stats.active_sources--;
3871 if (source->internal)
3872 sess->stats.internal_sources--;
3875 on_bye_timeout (sess, source);
3877 on_timeout (sess, source);
3879 if (sendertimeout) {
3880 source->is_sender = FALSE;
3881 sess->stats.sender_sources--;
3882 if (source->internal)
3883 sess->stats.internal_sender_sources--;
3885 on_sender_timeout (sess, source);
3887 /* count how many source to report in this generation */
3888 if (((gint16) (source->generation - sess->generation)) <= 0)
3889 data->num_to_report++;
3891 source->closing = remove;
3895 session_sdes (RTPSession * sess, ReportData * data)
3897 GstRTCPPacket *packet = &data->packet;
3898 const GstStructure *sdes;
3900 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3902 /* add SDES packet */
3903 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SDES, packet);
3905 gst_rtcp_packet_sdes_add_item (packet, data->source->ssrc);
3907 sdes = rtp_source_get_sdes_struct (data->source);
3909 /* add all fields in the structure, the order is not important. */
3910 n_fields = gst_structure_n_fields (sdes);
3911 for (i = 0; i < n_fields; ++i) {
3914 GstRTCPSDESType type;
3916 field = gst_structure_nth_field_name (sdes, i);
3919 value = gst_structure_get_string (sdes, field);
3922 type = gst_rtcp_sdes_name_to_type (field);
3924 /* Early packets are minimal and only include the CNAME */
3925 if (data->is_early && type != GST_RTCP_SDES_CNAME)
3928 if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
3929 gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
3930 (const guint8 *) value);
3931 } else if (type == GST_RTCP_SDES_PRIV) {
3937 /* don't accept entries that are too big */
3938 prefix_len = strlen (field);
3939 if (prefix_len > 255)
3941 value_len = strlen (value);
3942 if (value_len > 255)
3944 data_len = 1 + prefix_len + value_len;
3948 data[0] = prefix_len;
3949 memcpy (&data[1], field, prefix_len);
3950 memcpy (&data[1 + prefix_len], value, value_len);
3952 gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
3956 data->has_sdes = TRUE;
3959 /* schedule a BYE packet */
3961 make_source_bye (RTPSession * sess, RTPSource * source, ReportData * data)
3963 GstRTCPPacket *packet = &data->packet;
3964 GstRTCPBuffer *rtcp = &data->rtcpbuf;
3967 session_sdes (sess, data);
3968 /* add a BYE packet */
3969 gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_BYE, packet);
3970 gst_rtcp_packet_bye_add_ssrc (packet, source->ssrc);
3971 if (source->bye_reason)
3972 gst_rtcp_packet_bye_set_reason (packet, source->bye_reason);
3974 /* we have a BYE packet now */
3975 source->sent_bye = TRUE;
3979 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
3981 GstClockTime new_send_time;
3982 GstClockTime interval;
3983 RTPSessionStats *stats;
3985 if (sess->scheduled_bye)
3986 stats = &sess->bye_stats;
3988 stats = &sess->stats;
3990 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3991 data->is_early = TRUE;
3993 data->is_early = FALSE;
3995 if (data->is_early && sess->next_early_rtcp_time <= current_time) {
3996 GST_DEBUG ("early feedback %" GST_TIME_FORMAT " <= now %"
3997 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_early_rtcp_time),
3998 GST_TIME_ARGS (current_time));
3999 } else if (sess->next_rtcp_check_time == GST_CLOCK_TIME_NONE ||
4000 sess->next_rtcp_check_time > current_time) {
4001 GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
4002 GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
4003 GST_TIME_ARGS (current_time));
4007 /* take interval and add jitter */
4008 interval = data->interval;
4009 if (interval != GST_CLOCK_TIME_NONE)
4010 interval = rtp_stats_add_rtcp_jitter (stats, interval);
4012 if (sess->last_rtcp_check_time != GST_CLOCK_TIME_NONE) {
4013 /* perform forward reconsideration */
4014 if (interval != GST_CLOCK_TIME_NONE) {
4015 GstClockTime elapsed;
4017 /* get elapsed time since we last reported */
4018 elapsed = current_time - sess->last_rtcp_check_time;
4020 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
4021 GST_TIME_FORMAT, GST_TIME_ARGS (interval), GST_TIME_ARGS (elapsed));
4022 new_send_time = interval + sess->last_rtcp_check_time;
4024 new_send_time = sess->last_rtcp_check_time;
4027 /* If this is the first RTCP packet, we can reconsider anything based
4028 * on the last RTCP send time because there was none.
4030 g_warn_if_fail (!data->is_early);
4031 data->is_early = FALSE;
4032 new_send_time = current_time;
4035 if (!data->is_early) {
4036 /* check if reconsideration */
4037 if (new_send_time == GST_CLOCK_TIME_NONE || current_time < new_send_time) {
4038 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
4039 GST_TIME_ARGS (new_send_time));
4040 /* store new check time */
4041 sess->next_rtcp_check_time = new_send_time;
4042 sess->last_rtcp_interval = interval;
4046 sess->last_rtcp_interval = interval;
4047 if ((sess->rtp_profile == GST_RTP_PROFILE_AVPF
4048 || sess->rtp_profile == GST_RTP_PROFILE_SAVPF)
4049 && interval != GST_CLOCK_TIME_NONE) {
4050 /* Apply the rules from RFC 4585 section 3.5.3 */
4051 if (stats->min_interval != 0 && !sess->first_rtcp) {
4052 GstClockTime T_rr_current_interval =
4053 g_random_double_range (0.5, 1.5) * stats->min_interval * GST_SECOND;
4055 if (T_rr_current_interval > interval) {
4056 GST_DEBUG ("Adjusting interval for t-rr-interval: %" GST_TIME_FORMAT
4057 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (T_rr_current_interval),
4058 GST_TIME_ARGS (interval));
4059 interval = T_rr_current_interval;
4063 sess->next_rtcp_check_time = current_time + interval;
4067 GST_DEBUG ("can send RTCP now, next %" GST_TIME_FORMAT,
4068 GST_TIME_ARGS (sess->next_rtcp_check_time));
4074 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
4076 g_hash_table_insert (hash_table, key, g_object_ref (source));
4080 remove_closing_sources (const gchar * key, RTPSource * source,
4083 if (source->closing)
4086 if (source->send_fir)
4087 data->have_fir = TRUE;
4088 if (source->send_pli)
4089 data->have_pli = TRUE;
4090 if (source->send_nack)
4091 data->have_nack = TRUE;
4097 generate_rtcp (const gchar * key, RTPSource * source, ReportData * data)
4099 RTPSession *sess = data->sess;
4100 gboolean is_bye = FALSE;
4101 ReportOutput *output;
4103 /* only generate RTCP for active internal sources */
4104 if (!source->internal || source->sent_bye)
4107 /* ignore other sources when we do the timeout after a scheduled BYE */
4108 if (sess->scheduled_bye && !source->marked_bye)
4111 /* skip if RTCP is disabled */
4112 if (source->disable_rtcp) {
4113 GST_DEBUG ("source %08x has RTCP disabled", source->ssrc);
4117 data->source = source;
4120 session_start_rtcp (sess, data);
4122 if (source->marked_bye) {
4124 make_source_bye (sess, source, data);
4126 } else if (!data->is_early) {
4127 /* loop over all known sources and add report blocks. If we are early, we
4128 * just make a minimal RTCP packet and skip this step */
4129 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4130 (GHFunc) session_report_blocks, data);
4132 if (!data->has_sdes && (!data->is_early || !sess->reduced_size_rtcp))
4133 session_sdes (sess, data);
4136 session_fir (sess, data);
4139 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4140 (GHFunc) session_pli, data);
4142 if (data->have_nack)
4143 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4144 (GHFunc) session_nack, data);
4146 gst_rtcp_buffer_unmap (&data->rtcpbuf);
4148 output = g_slice_new (ReportOutput);
4149 output->source = g_object_ref (source);
4150 output->is_bye = is_bye;
4151 output->buffer = data->rtcp;
4152 /* queue the RTCP packet to push later */
4153 g_queue_push_tail (&data->output, output);
4157 update_generation (const gchar * key, RTPSource * source, ReportData * data)
4159 RTPSession *sess = data->sess;
4161 if (g_hash_table_size (source->reported_in_sr_of) >=
4162 sess->stats.internal_sources) {
4163 /* source is reported, move to next generation */
4164 source->generation = sess->generation + 1;
4165 g_hash_table_remove_all (source->reported_in_sr_of);
4167 GST_LOG ("reported source %x, new generation: %d", source->ssrc,
4168 source->generation);
4170 /* if we reported all sources in this generation, move to next */
4171 if (--data->num_to_report == 0) {
4173 GST_DEBUG ("all reported, generation now %u", sess->generation);
4179 schedule_remaining_nacks (const gchar * key, RTPSource * source,
4182 RTPSession *sess = data->sess;
4183 GstClockTime *nack_deadlines;
4184 GstClockTime deadline;
4187 if (!source->send_nack)
4190 /* the scheduling is entirely based on available bandwidth, just take the
4191 * biggest seqnum, which will have the largest deadline to request early
4193 nack_deadlines = rtp_source_get_nack_deadlines (source, &n_nacks);
4194 deadline = nack_deadlines[n_nacks - 1];
4195 RTP_SESSION_UNLOCK (sess);
4196 rtp_session_send_rtcp_with_deadline (sess, deadline);
4197 RTP_SESSION_LOCK (sess);
4201 rtp_session_are_all_sources_bye (RTPSession * sess)
4203 GHashTableIter iter;
4206 RTP_SESSION_LOCK (sess);
4207 g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
4208 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
4209 if (src->internal && !src->sent_bye) {
4210 RTP_SESSION_UNLOCK (sess);
4214 RTP_SESSION_UNLOCK (sess);
4220 * rtp_session_on_timeout:
4221 * @sess: an #RTPSession
4222 * @current_time: the current system time
4223 * @ntpnstime: the current NTP time in nanoseconds
4224 * @running_time: the current running_time of the pipeline
4226 * Perform maintenance actions after the timeout obtained with
4227 * rtp_session_next_timeout() expired.
4229 * This function will perform timeouts of receivers and senders, send a BYE
4230 * packet or generate RTCP packets with current session stats.
4232 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
4233 * times, for each packet that should be processed.
4235 * Returns: a #GstFlowReturn.
4238 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
4239 guint64 ntpnstime, GstClockTime running_time)
4241 GstFlowReturn result = GST_FLOW_OK;
4242 ReportData data = { GST_RTCP_BUFFER_INIT };
4243 GHashTable *table_copy;
4244 ReportOutput *output;
4245 gboolean all_empty = FALSE;
4247 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
4249 GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT
4250 ", running-time %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
4251 GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (running_time));
4254 data.current_time = current_time;
4255 data.ntpnstime = ntpnstime;
4256 data.running_time = running_time;
4257 data.num_to_report = 0;
4258 data.may_suppress = FALSE;
4259 data.nacked_seqnums = 0;
4260 g_queue_init (&data.output);
4262 RTP_SESSION_LOCK (sess);
4263 /* get a new interval, we need this for various cleanups etc */
4264 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
4266 GST_DEBUG ("interval %" GST_TIME_FORMAT, GST_TIME_ARGS (data.interval));
4268 /* we need an internal source now */
4269 if (sess->stats.internal_sources == 0) {
4273 source = obtain_internal_source (sess, sess->suggested_ssrc, &created,
4275 sess->internal_ssrc_set = TRUE;
4278 on_new_sender_ssrc (sess, source);
4280 g_object_unref (source);
4283 sess->conflicting_addresses =
4284 timeout_conflicting_addresses (sess->conflicting_addresses, current_time);
4286 /* Make a local copy of the hashtable. We need to do this because the
4287 * cleanup stage below releases the session lock. */
4288 table_copy = g_hash_table_new_full (NULL, NULL, NULL,
4289 (GDestroyNotify) g_object_unref);
4290 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4291 (GHFunc) clone_ssrcs_hashtable, table_copy);
4293 /* Clean up the session, mark the source for removing, this might release the
4295 g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
4296 g_hash_table_destroy (table_copy);
4298 /* Now remove the marked sources */
4299 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
4300 (GHRFunc) remove_closing_sources, &data);
4302 /* update point-to-point status */
4303 session_update_ptp (sess);
4305 /* see if we need to generate SR or RR packets */
4306 if (!is_rtcp_time (sess, current_time, &data))
4309 /* check if all the buffers are empty afer generation */
4313 ("doing RTCP generation %u for %u sources, early %d, may suppress %d",
4314 sess->generation, data.num_to_report, data.is_early, data.may_suppress);
4316 /* generate RTCP for all internal sources */
4317 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4318 (GHFunc) generate_rtcp, &data);
4320 /* update the generation for all the sources that have been reported */
4321 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4322 (GHFunc) update_generation, &data);
4324 /* we keep track of the last report time in order to timeout inactive
4325 * receivers or senders */
4326 if (!data.is_early) {
4327 GST_DEBUG ("Time since last regular RTCP: %" GST_TIME_FORMAT " - %"
4328 GST_TIME_FORMAT " = %" GST_TIME_FORMAT,
4329 GST_TIME_ARGS (data.current_time),
4330 GST_TIME_ARGS (sess->last_rtcp_send_time),
4331 GST_TIME_ARGS (data.current_time - sess->last_rtcp_send_time));
4332 sess->last_rtcp_send_time = data.current_time;
4335 GST_DEBUG ("Time since last RTCP: %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT
4336 " = %" GST_TIME_FORMAT, GST_TIME_ARGS (data.current_time),
4337 GST_TIME_ARGS (sess->last_rtcp_check_time),
4338 GST_TIME_ARGS (data.current_time - sess->last_rtcp_check_time));
4339 sess->last_rtcp_check_time = data.current_time;
4340 sess->first_rtcp = FALSE;
4341 sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
4342 sess->scheduled_bye = FALSE;
4345 RTP_SESSION_UNLOCK (sess);
4347 /* notify about updated statistics */
4348 g_object_notify (G_OBJECT (sess), "stats");
4350 /* push out the RTCP packets */
4351 while ((output = g_queue_pop_head (&data.output))) {
4352 gboolean do_not_suppress, empty_buffer;
4353 GstBuffer *buffer = output->buffer;
4354 RTPSource *source = output->source;
4356 /* Give the user a change to add its own packet */
4357 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
4358 buffer, data.is_early, &do_not_suppress);
4360 empty_buffer = gst_buffer_get_size (buffer) == 0;
4365 if (sess->callbacks.send_rtcp &&
4366 !empty_buffer && (do_not_suppress || !data.may_suppress)) {
4369 packet_size = gst_buffer_get_size (buffer) + sess->header_len;
4371 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
4372 GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
4373 sess->stats.avg_rtcp_packet_size, packet_size);
4375 sess->callbacks.send_rtcp (sess, source, buffer,
4376 rtp_session_are_all_sources_bye (sess), sess->send_rtcp_user_data);
4378 RTP_SESSION_LOCK (sess);
4379 sess->stats.nacks_sent += data.nacked_seqnums;
4380 on_sender_ssrc_active (sess, source);
4381 RTP_SESSION_UNLOCK (sess);
4383 GST_DEBUG ("freeing packet callback: %p"
4384 " empty_buffer: %d, "
4385 " do_not_suppress: %d may_suppress: %d", sess->callbacks.send_rtcp,
4386 empty_buffer, do_not_suppress, data.may_suppress);
4387 if (!empty_buffer) {
4388 RTP_SESSION_LOCK (sess);
4389 sess->stats.nacks_dropped += data.nacked_seqnums;
4390 RTP_SESSION_UNLOCK (sess);
4392 gst_buffer_unref (buffer);
4394 g_object_unref (source);
4395 g_slice_free (ReportOutput, output);
4399 GST_ERROR ("generated empty RTCP messages for all the sources");
4401 /* schedule remaining nacks */
4402 RTP_SESSION_LOCK (sess);
4403 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4404 (GHFunc) schedule_remaining_nacks, &data);
4405 RTP_SESSION_UNLOCK (sess);
4411 * rtp_session_request_early_rtcp:
4412 * @sess: an #RTPSession
4413 * @current_time: the current system time
4414 * @max_delay: maximum delay
4416 * Request transmission of early RTCP
4418 * Returns: %TRUE if the related RTCP can be scheduled.
4421 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
4422 GstClockTime max_delay)
4424 GstClockTime T_dither_max, T_rr, offset = 0;
4426 gboolean allow_early;
4428 /* Implements the algorithm described in RFC 4585 section 3.5.2 */
4430 RTP_SESSION_LOCK (sess);
4432 /* We assume a feedback profile if something is requesting RTCP
4434 sess->rtp_profile = GST_RTP_PROFILE_AVPF;
4436 /* Check if already requested */
4437 /* RFC 4585 section 3.5.2 step 2 */
4438 if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
4439 GST_LOG_OBJECT (sess, "already have next early rtcp time");
4440 ret = (current_time + max_delay > sess->next_early_rtcp_time);
4444 if (!GST_CLOCK_TIME_IS_VALID (sess->next_rtcp_check_time)) {
4445 GST_LOG_OBJECT (sess, "no next RTCP check time");
4450 /* RFC 4585 section 3.5.3 step 1
4451 * If no regular RTCP packet has been sent before, then a regular
4452 * RTCP packet has to be scheduled first and FB messages might be
4455 if (!GST_CLOCK_TIME_IS_VALID (sess->last_rtcp_send_time)) {
4456 GST_LOG_OBJECT (sess, "no RTCP sent yet");
4458 if (current_time + max_delay > sess->next_rtcp_check_time) {
4459 GST_LOG_OBJECT (sess,
4460 "next scheduled time is soon %" GST_TIME_FORMAT " + %" GST_TIME_FORMAT
4461 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
4462 GST_TIME_ARGS (max_delay),
4463 GST_TIME_ARGS (sess->next_rtcp_check_time));
4466 GST_LOG_OBJECT (sess,
4467 "can't allow early feedback, next scheduled time is too late %"
4468 GST_TIME_FORMAT " + %" GST_TIME_FORMAT " < %" GST_TIME_FORMAT,
4469 GST_TIME_ARGS (current_time), GST_TIME_ARGS (max_delay),
4470 GST_TIME_ARGS (sess->next_rtcp_check_time));
4476 T_rr = sess->last_rtcp_interval;
4478 /* RFC 4585 section 3.5.2 step 2b */
4479 /* If the total sources is <=2, then there is only us and one peer */
4480 /* When there is one auxiliary stream the session can still do point
4483 if (sess->is_doing_ptp) {
4486 /* Divide by 2 because l = 0.5 */
4487 T_dither_max = T_rr;
4491 /* RFC 4585 section 3.5.2 step 3 */
4492 if (current_time + T_dither_max > sess->next_rtcp_check_time) {
4493 GST_LOG_OBJECT (sess,
4494 "don't send because of dither, next scheduled time is too soon %"
4495 GST_TIME_FORMAT " + %" GST_TIME_FORMAT " > %" GST_TIME_FORMAT,
4496 GST_TIME_ARGS (current_time), GST_TIME_ARGS (T_dither_max),
4497 GST_TIME_ARGS (sess->next_rtcp_check_time));
4498 ret = T_dither_max <= max_delay;
4502 /* RFC 4585 section 3.5.2 step 4a and
4503 * RFC 4585 section 3.5.2 step 6 */
4504 allow_early = FALSE;
4505 if (sess->last_rtcp_check_time == sess->last_rtcp_send_time) {
4506 /* Last time we sent a full RTCP packet, we can now immediately
4507 * send an early one as allow_early was reset to TRUE */
4509 } else if (sess->last_rtcp_check_time + T_rr <= current_time + max_delay) {
4510 /* Last packet we sent was an early RTCP packet and more than
4511 * T_rr has passed since then, meaning we would have suppressed
4512 * a regular RTCP packet already and reset allow_early to TRUE */
4515 /* We have to offset a bit as T_rr has not passed yet, but will before
4517 if (sess->last_rtcp_check_time + T_rr > current_time)
4518 offset = (sess->last_rtcp_check_time + T_rr) - current_time;
4520 GST_DEBUG_OBJECT (sess,
4521 "can't allow early RTCP yet: last regular %" GST_TIME_FORMAT ", %"
4522 GST_TIME_FORMAT " + %" GST_TIME_FORMAT " > %" GST_TIME_FORMAT " + %"
4523 GST_TIME_FORMAT, GST_TIME_ARGS (sess->last_rtcp_send_time),
4524 GST_TIME_ARGS (sess->last_rtcp_check_time), GST_TIME_ARGS (T_rr),
4525 GST_TIME_ARGS (current_time), GST_TIME_ARGS (max_delay));
4529 /* Ignore the request a scheduled packet will be in time anyway */
4530 if (current_time + max_delay > sess->next_rtcp_check_time) {
4531 GST_LOG_OBJECT (sess,
4532 "next scheduled time is soon %" GST_TIME_FORMAT " + %" GST_TIME_FORMAT
4533 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
4534 GST_TIME_ARGS (max_delay),
4535 GST_TIME_ARGS (sess->next_rtcp_check_time));
4538 GST_LOG_OBJECT (sess,
4539 "can't allow early feedback and next scheduled time is too late %"
4540 GST_TIME_FORMAT " + %" GST_TIME_FORMAT " < %" GST_TIME_FORMAT,
4541 GST_TIME_ARGS (current_time), GST_TIME_ARGS (max_delay),
4542 GST_TIME_ARGS (sess->next_rtcp_check_time));
4548 /* RFC 4585 section 3.5.2 step 4b */
4550 /* Schedule an early transmission later */
4551 sess->next_early_rtcp_time = g_random_double () * T_dither_max +
4552 current_time + offset;
4554 /* If no dithering, schedule it for NOW */
4555 sess->next_early_rtcp_time = current_time + offset;
4558 GST_LOG_OBJECT (sess, "next early RTCP time %" GST_TIME_FORMAT
4559 ", next regular RTCP time %" GST_TIME_FORMAT,
4560 GST_TIME_ARGS (sess->next_early_rtcp_time),
4561 GST_TIME_ARGS (sess->next_rtcp_check_time));
4562 RTP_SESSION_UNLOCK (sess);
4564 /* notify app of need to send packet early
4565 * and therefore of timeout change */
4566 if (sess->callbacks.reconsider)
4567 sess->callbacks.reconsider (sess, sess->reconsider_user_data);
4573 RTP_SESSION_UNLOCK (sess);
4579 rtp_session_send_rtcp_internal (RTPSession * sess, GstClockTime now,
4580 GstClockTime max_delay)
4582 /* notify the application that we intend to send early RTCP */
4583 if (sess->callbacks.notify_early_rtcp)
4584 sess->callbacks.notify_early_rtcp (sess, sess->notify_early_rtcp_user_data);
4586 return rtp_session_request_early_rtcp (sess, now, max_delay);
4590 rtp_session_send_rtcp_with_deadline (RTPSession * sess, GstClockTime deadline)
4592 GstClockTime now, max_delay;
4594 if (!sess->callbacks.send_rtcp)
4597 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
4602 max_delay = deadline - now;
4604 return rtp_session_send_rtcp_internal (sess, now, max_delay);
4608 rtp_session_send_rtcp (RTPSession * sess, GstClockTime max_delay)
4612 if (!sess->callbacks.send_rtcp)
4615 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
4617 return rtp_session_send_rtcp_internal (sess, now, max_delay);
4621 rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc,
4622 gboolean fir, gint count)
4626 RTP_SESSION_LOCK (sess);
4627 src = find_source (sess, ssrc);
4632 src->send_pli = FALSE;
4633 src->send_fir = TRUE;
4635 if (count == -1 || count != src->last_fir_count)
4636 src->current_send_fir_seqnum++;
4637 src->last_fir_count = count;
4638 } else if (!src->send_fir) {
4639 src->send_pli = TRUE;
4641 RTP_SESSION_UNLOCK (sess);
4643 if (!rtp_session_send_rtcp (sess, 5 * GST_SECOND)) {
4644 GST_DEBUG ("FIR/PLI not sent early, sending with next regular RTCP");
4652 RTP_SESSION_UNLOCK (sess);
4658 * rtp_session_request_nack:
4659 * @sess: a #RTPSession
4661 * @seqnum: the missing seqnum
4662 * @max_delay: max delay to request NACK
4664 * Request scheduling of a NACK feedback packet for @seqnum in @ssrc.
4666 * Returns: %TRUE if the NACK feedback could be scheduled
4669 rtp_session_request_nack (RTPSession * sess, guint32 ssrc, guint16 seqnum,
4670 GstClockTime max_delay)
4675 if (!sess->callbacks.send_rtcp)
4678 now = sess->callbacks.request_time (sess, sess->request_time_user_data);
4680 RTP_SESSION_LOCK (sess);
4681 source = find_source (sess, ssrc);
4685 GST_DEBUG ("request NACK for SSRC %08x, #%u, deadline %" GST_TIME_FORMAT,
4686 ssrc, seqnum, GST_TIME_ARGS (now + max_delay));
4687 rtp_source_register_nack (source, seqnum, now + max_delay);
4688 RTP_SESSION_UNLOCK (sess);
4690 if (!rtp_session_send_rtcp_internal (sess, now, max_delay)) {
4691 GST_DEBUG ("NACK not sent early, sending with next regular RTCP");
4699 RTP_SESSION_UNLOCK (sess);