2 * Copyright (C) <2007> Wim Taymans <wim@fluendo.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include <gst/rtp/gstrtpbuffer.h>
23 #include <gst/rtp/gstrtcpbuffer.h>
24 #include <gst/netbuffer/gstnetbuffer.h>
26 #include "rtpsession.h"
28 GST_DEBUG_CATEGORY_STATIC (rtp_session_debug);
29 #define GST_CAT_DEFAULT rtp_session_debug
31 /* signals and args */
35 SIGNAL_ON_SSRC_COLLISION,
36 SIGNAL_ON_SSRC_VALIDATED,
38 SIGNAL_ON_BYE_TIMEOUT,
43 #define RTP_DEFAULT_BANDWIDTH 64000.0
44 #define RTP_DEFAULT_RTCP_BANDWIDTH 1000
51 /* update average packet size, we keep this scaled by 16 to keep enough
53 #define UPDATE_AVG(avg, val) \
57 (avg) = ((val) + (15 * (avg))) >> 4;
59 /* GObject vmethods */
60 static void rtp_session_finalize (GObject * object);
61 static void rtp_session_set_property (GObject * object, guint prop_id,
62 const GValue * value, GParamSpec * pspec);
63 static void rtp_session_get_property (GObject * object, guint prop_id,
64 GValue * value, GParamSpec * pspec);
66 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
68 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
70 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
71 gboolean * created, RTPArrivalStats * arrival, gboolean rtp);
74 rtp_session_class_init (RTPSessionClass * klass)
76 GObjectClass *gobject_class;
78 gobject_class = (GObjectClass *) klass;
80 gobject_class->finalize = rtp_session_finalize;
81 gobject_class->set_property = rtp_session_set_property;
82 gobject_class->get_property = rtp_session_get_property;
85 * RTPSession::on-new-ssrc:
86 * @session: the object which received the signal
87 * @src: the new RTPSource
89 * Notify of a new SSRC that entered @session.
91 rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
92 g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
93 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
94 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
97 * RTPSession::on-ssrc_collision:
98 * @session: the object which received the signal
99 * @src: the #RTPSource that caused a collision
101 * Notify when we have an SSRC collision
103 rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
104 g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
105 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
106 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
109 * RTPSession::on-ssrc_validated:
110 * @session: the object which received the signal
111 * @src: the new validated RTPSource
113 * Notify of a new SSRC that became validated.
115 rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
116 g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
117 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
118 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
121 * RTPSession::on-bye-ssrc:
122 * @session: the object which received the signal
123 * @src: the RTPSource that went away
125 * Notify of an SSRC that became inactive because of a BYE packet.
127 rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
128 g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
129 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
130 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
133 * RTPSession::on-bye-timeout:
134 * @session: the object which received the signal
135 * @src: the RTPSource that timed out
137 * Notify of an SSRC that has timed out because of BYE
139 rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
140 g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
141 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
142 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
145 * RTPSession::on-timeout:
146 * @session: the object which received the signal
147 * @src: the RTPSource that timed out
149 * Notify of an SSRC that has timed out
151 rtp_session_signals[SIGNAL_ON_TIMEOUT] =
152 g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
153 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
154 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
157 GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
161 rtp_session_init (RTPSession * sess)
165 sess->lock = g_mutex_new ();
166 sess->key = g_random_int ();
170 for (i = 0; i < 32; i++) {
172 g_hash_table_new_full (NULL, NULL, NULL,
173 (GDestroyNotify) g_object_unref);
175 sess->cnames = g_hash_table_new_full (NULL, NULL, g_free, NULL);
177 rtp_stats_init_defaults (&sess->stats);
179 /* create an active SSRC for this session manager */
180 sess->source = rtp_session_create_source (sess);
181 sess->source->validated = TRUE;
182 sess->stats.active_sources++;
184 /* default UDP header length */
185 sess->header_len = 28;
188 /* some default SDES entries */
189 //sess->cname = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
190 sess->cname = g_strdup_printf ("foo@%s", g_get_host_name ());
191 sess->name = g_strdup (g_get_real_name ());
192 sess->tool = g_strdup ("GStreamer");
194 sess->first_rtcp = TRUE;
196 GST_DEBUG ("%p: session using SSRC: %08x", sess, sess->source->ssrc);
200 rtp_session_finalize (GObject * object)
205 sess = RTP_SESSION_CAST (object);
207 g_mutex_free (sess->lock);
208 for (i = 0; i < 32; i++)
209 g_hash_table_destroy (sess->ssrcs[i]);
211 g_hash_table_destroy (sess->cnames);
212 g_object_unref (sess->source);
214 g_free (sess->cname);
216 g_free (sess->bye_reason);
218 G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
222 rtp_session_set_property (GObject * object, guint prop_id,
223 const GValue * value, GParamSpec * pspec)
227 sess = RTP_SESSION (object);
231 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
237 rtp_session_get_property (GObject * object, guint prop_id,
238 GValue * value, GParamSpec * pspec)
242 sess = RTP_SESSION (object);
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252 on_new_ssrc (RTPSession * sess, RTPSource * source)
254 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
258 on_ssrc_collision (RTPSession * sess, RTPSource * source)
260 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
265 on_ssrc_validated (RTPSession * sess, RTPSource * source)
267 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
272 on_bye_ssrc (RTPSession * sess, RTPSource * source)
274 /* notify app that reconsideration should be performed */
275 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
279 on_bye_timeout (RTPSession * sess, RTPSource * source)
281 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
285 on_timeout (RTPSession * sess, RTPSource * source)
287 g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
293 * Create a new session object.
295 * Returns: a new #RTPSession. g_object_unref() after usage.
298 rtp_session_new (void)
302 sess = g_object_new (RTP_TYPE_SESSION, NULL);
308 * rtp_session_set_callbacks:
309 * @sess: an #RTPSession
310 * @callbacks: callbacks to configure
311 * @user_data: user data passed in the callbacks
313 * Configure a set of callbacks to be notified of actions.
316 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
319 g_return_if_fail (RTP_IS_SESSION (sess));
321 sess->callbacks.process_rtp = callbacks->process_rtp;
322 sess->callbacks.send_rtp = callbacks->send_rtp;
323 sess->callbacks.send_rtcp = callbacks->send_rtcp;
324 sess->callbacks.clock_rate = callbacks->clock_rate;
325 sess->callbacks.get_time = callbacks->get_time;
326 sess->callbacks.reconsider = callbacks->reconsider;
327 sess->user_data = user_data;
331 * rtp_session_set_bandwidth:
332 * @sess: an #RTPSession
333 * @bandwidth: the bandwidth allocated
335 * Set the session bandwidth in bytes per second.
338 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
340 g_return_if_fail (RTP_IS_SESSION (sess));
342 sess->stats.bandwidth = bandwidth;
346 * rtp_session_get_bandwidth:
347 * @sess: an #RTPSession
349 * Get the session bandwidth.
351 * Returns: the session bandwidth.
354 rtp_session_get_bandwidth (RTPSession * sess)
356 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
358 return sess->stats.bandwidth;
362 * rtp_session_set_rtcp_bandwidth:
363 * @sess: an #RTPSession
364 * @bandwidth: the RTCP bandwidth
366 * Set the bandwidth that should be used for RTCP
370 rtp_session_set_rtcp_bandwidth (RTPSession * sess, gdouble bandwidth)
372 g_return_if_fail (RTP_IS_SESSION (sess));
374 sess->stats.rtcp_bandwidth = bandwidth;
378 * rtp_session_get_rtcp_bandwidth:
379 * @sess: an #RTPSession
381 * Get the session bandwidth used for RTCP.
383 * Returns: The bandwidth used for RTCP messages.
386 rtp_session_get_rtcp_bandwidth (RTPSession * sess)
388 g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
390 return sess->stats.rtcp_bandwidth;
394 * rtp_session_set_cname:
395 * @sess: an #RTPSession
396 * @cname: a CNAME for the session
398 * Set the CNAME for the session.
401 rtp_session_set_cname (RTPSession * sess, const gchar * cname)
403 g_return_if_fail (RTP_IS_SESSION (sess));
405 g_free (sess->cname);
406 sess->cname = g_strdup (cname);
410 * rtp_session_get_cname:
411 * @sess: an #RTPSession
413 * Get the currently configured CNAME for the session.
415 * Returns: The CNAME. g_free after usage.
418 rtp_session_get_cname (RTPSession * sess)
420 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
422 return g_strdup (sess->cname);
426 * rtp_session_set_name:
427 * @sess: an #RTPSession
428 * @name: a NAME for the session
430 * Set the NAME for the session.
433 rtp_session_set_name (RTPSession * sess, const gchar * name)
435 g_return_if_fail (RTP_IS_SESSION (sess));
438 sess->name = g_strdup (name);
442 * rtp_session_get_name:
443 * @sess: an #RTPSession
445 * Get the currently configured NAME for the session.
447 * Returns: The NAME. g_free after usage.
450 rtp_session_get_name (RTPSession * sess)
452 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
454 return g_strdup (sess->name);
458 * rtp_session_set_email:
459 * @sess: an #RTPSession
460 * @email: an EMAIL for the session
462 * Set the EMAIL the session.
465 rtp_session_set_email (RTPSession * sess, const gchar * email)
467 g_return_if_fail (RTP_IS_SESSION (sess));
469 g_free (sess->email);
470 sess->email = g_strdup (email);
474 * rtp_session_get_email:
475 * @sess: an #RTPSession
477 * Get the currently configured EMAIL of the session.
479 * Returns: The EMAIL. g_free after usage.
482 rtp_session_get_email (RTPSession * sess)
484 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
486 return g_strdup (sess->email);
490 * rtp_session_set_phone:
491 * @sess: an #RTPSession
492 * @phone: a PHONE for the session
494 * Set the PHONE the session.
497 rtp_session_set_phone (RTPSession * sess, const gchar * phone)
499 g_return_if_fail (RTP_IS_SESSION (sess));
501 g_free (sess->phone);
502 sess->phone = g_strdup (phone);
506 * rtp_session_get_location:
507 * @sess: an #RTPSession
509 * Get the currently configured PHONE of the session.
511 * Returns: The PHONE. g_free after usage.
514 rtp_session_get_phone (RTPSession * sess)
516 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
518 return g_strdup (sess->phone);
522 * rtp_session_set_location:
523 * @sess: an #RTPSession
524 * @location: a LOCATION for the session
526 * Set the LOCATION the session.
529 rtp_session_set_location (RTPSession * sess, const gchar * location)
531 g_return_if_fail (RTP_IS_SESSION (sess));
533 g_free (sess->location);
534 sess->location = g_strdup (location);
538 * rtp_session_get_location:
539 * @sess: an #RTPSession
541 * Get the currently configured LOCATION of the session.
543 * Returns: The LOCATION. g_free after usage.
546 rtp_session_get_location (RTPSession * sess)
548 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
550 return g_strdup (sess->location);
554 * rtp_session_set_tool:
555 * @sess: an #RTPSession
556 * @tool: a TOOL for the session
558 * Set the TOOL the session.
561 rtp_session_set_tool (RTPSession * sess, const gchar * tool)
563 g_return_if_fail (RTP_IS_SESSION (sess));
566 sess->tool = g_strdup (tool);
570 * rtp_session_get_tool:
571 * @sess: an #RTPSession
573 * Get the currently configured TOOL of the session.
575 * Returns: The TOOL. g_free after usage.
578 rtp_session_get_tool (RTPSession * sess)
580 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
582 return g_strdup (sess->tool);
586 * rtp_session_set_note:
587 * @sess: an #RTPSession
588 * @note: a NOTE for the session
590 * Set the NOTE the session.
593 rtp_session_set_note (RTPSession * sess, const gchar * note)
595 g_return_if_fail (RTP_IS_SESSION (sess));
598 sess->note = g_strdup (note);
602 * rtp_session_get_note:
603 * @sess: an #RTPSession
605 * Get the currently configured NOTE of the session.
607 * Returns: The NOTE. g_free after usage.
610 rtp_session_get_note (RTPSession * sess)
612 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
614 return g_strdup (sess->note);
618 source_push_rtp (RTPSource * source, GstBuffer * buffer, RTPSession * session)
620 GstFlowReturn result = GST_FLOW_OK;
622 if (source == session->source) {
623 GST_DEBUG ("source %08x pushed sender RTP packet", source->ssrc);
626 if (session->callbacks.send_rtp)
628 session->callbacks.send_rtp (session, source, buffer,
631 gst_buffer_unref (buffer);
633 GST_DEBUG ("source %08x pushed receiver RTP packet", source->ssrc);
634 if (session->callbacks.process_rtp)
636 session->callbacks.process_rtp (session, source, buffer,
639 gst_buffer_unref (buffer);
645 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
649 if (session->callbacks.clock_rate)
650 result = session->callbacks.clock_rate (session, pt, session->user_data);
654 GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
659 static RTPSourceCallbacks callbacks = {
660 (RTPSourcePushRTP) source_push_rtp,
661 (RTPSourceClockRate) source_clock_rate,
665 check_collision (RTPSession * sess, RTPSource * source,
666 RTPArrivalStats * arrival)
668 /* FIXME, do collision check */
673 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
674 RTPArrivalStats * arrival, gboolean rtp)
679 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
680 if (source == NULL) {
681 /* make new Source in probation and insert */
682 source = rtp_source_new (ssrc);
685 source->probation = RTP_DEFAULT_PROBATION;
687 source->probation = 0;
689 /* store from address, if any */
690 if (arrival->have_address) {
692 rtp_source_set_rtp_from (source, &arrival->address);
694 rtp_source_set_rtcp_from (source, &arrival->address);
697 /* configure a callback on the source */
698 rtp_source_set_callbacks (source, &callbacks, sess);
700 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
703 /* we have one more source now */
704 sess->total_sources++;
708 /* check for collision, this updates the address when not previously set */
709 if (check_collision (sess, source, arrival))
710 on_ssrc_collision (sess, source);
712 /* update last activity */
713 source->last_activity = arrival->time;
715 source->last_rtp_activity = arrival->time;
721 * rtp_session_add_source:
722 * @sess: a #RTPSession
723 * @src: #RTPSource to add
725 * Add @src to @session.
727 * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
728 * existed in the session.
731 rtp_session_add_source (RTPSession * sess, RTPSource * src)
733 gboolean result = FALSE;
736 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
737 g_return_val_if_fail (src != NULL, FALSE);
739 RTP_SESSION_LOCK (sess);
741 g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
742 GINT_TO_POINTER (src->ssrc));
744 g_hash_table_insert (sess->ssrcs[sess->mask_idx],
745 GINT_TO_POINTER (src->ssrc), src);
746 /* we have one more source now */
747 sess->total_sources++;
750 RTP_SESSION_UNLOCK (sess);
756 * rtp_session_get_num_sources:
757 * @sess: an #RTPSession
759 * Get the number of sources in @sess.
761 * Returns: The number of sources in @sess.
764 rtp_session_get_num_sources (RTPSession * sess)
768 g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
770 RTP_SESSION_LOCK (sess);
771 result = sess->total_sources;
772 RTP_SESSION_UNLOCK (sess);
778 * rtp_session_get_num_active_sources:
779 * @sess: an #RTPSession
781 * Get the number of active sources in @sess. A source is considered active when
782 * it has been validated and has not yet received a BYE RTCP message.
784 * Returns: The number of active sources in @sess.
787 rtp_session_get_num_active_sources (RTPSession * sess)
791 g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
793 RTP_SESSION_LOCK (sess);
794 result = sess->stats.active_sources;
795 RTP_SESSION_UNLOCK (sess);
801 * rtp_session_get_source_by_ssrc:
802 * @sess: an #RTPSession
805 * Find the source with @ssrc in @sess.
807 * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
808 * g_object_unref() after usage.
811 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
815 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
817 RTP_SESSION_LOCK (sess);
819 g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
821 g_object_ref (result);
822 RTP_SESSION_UNLOCK (sess);
828 * rtp_session_get_source_by_cname:
829 * @sess: a #RTPSession
832 * Find the source with @cname in @sess.
834 * Returns: a #RTPSource with CNAME @cname or NULL if the source was not found.
835 * g_object_unref() after usage.
838 rtp_session_get_source_by_cname (RTPSession * sess, const gchar * cname)
842 g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
843 g_return_val_if_fail (cname != NULL, NULL);
845 RTP_SESSION_LOCK (sess);
846 result = g_hash_table_lookup (sess->cnames, cname);
848 g_object_ref (result);
849 RTP_SESSION_UNLOCK (sess);
855 * rtp_session_create_source:
856 * @sess: an #RTPSession
858 * Create an #RTPSource for use in @sess. This function will create a source
859 * with an ssrc that is currently not used by any participants in the session.
861 * Returns: an #RTPSource.
864 rtp_session_create_source (RTPSession * sess)
869 RTP_SESSION_LOCK (sess);
871 ssrc = g_random_int ();
873 /* see if it exists in the session, we're done if it doesn't */
874 if (g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
875 GINT_TO_POINTER (ssrc)) == NULL)
878 source = rtp_source_new (ssrc);
879 g_object_ref (source);
880 g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
882 /* we have one more source now */
883 sess->total_sources++;
884 RTP_SESSION_UNLOCK (sess);
889 /* update the RTPArrivalStats structure with the current time and other bits
890 * about the current buffer we are handling.
891 * This function is typically called when a validated packet is received.
892 * This function should be called with the SESSION_LOCK
895 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
896 gboolean rtp, GstBuffer * buffer)
898 /* get time or arrival */
899 if (sess->callbacks.get_time)
900 arrival->time = sess->callbacks.get_time (sess, sess->user_data);
902 arrival->time = GST_CLOCK_TIME_NONE;
904 /* get packet size including header overhead */
905 arrival->bytes = GST_BUFFER_SIZE (buffer) + sess->header_len;
908 arrival->payload_len = gst_rtp_buffer_get_payload_len (buffer);
910 arrival->payload_len = 0;
913 /* for netbuffer we can store the IP address to check for collisions */
914 arrival->have_address = GST_IS_NETBUFFER (buffer);
915 if (arrival->have_address) {
916 GstNetBuffer *netbuf = (GstNetBuffer *) buffer;
918 memcpy (&arrival->address, &netbuf->from, sizeof (GstNetAddress));
923 * rtp_session_process_rtp:
924 * @sess: and #RTPSession
925 * @buffer: an RTP buffer
927 * Process an RTP buffer in the session manager. This function takes ownership
930 * Returns: a #GstFlowReturn.
933 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer)
935 GstFlowReturn result;
939 gboolean prevsender, prevactive;
940 RTPArrivalStats arrival;
942 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
943 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
945 if (!gst_rtp_buffer_validate (buffer))
948 RTP_SESSION_LOCK (sess);
949 /* update arrival stats */
950 update_arrival_stats (sess, &arrival, TRUE, buffer);
952 /* ignore more RTP packets when we left the session */
953 if (sess->source->received_bye)
956 /* get SSRC and look up in session database */
957 ssrc = gst_rtp_buffer_get_ssrc (buffer);
958 source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
960 prevsender = RTP_SOURCE_IS_SENDER (source);
961 prevactive = RTP_SOURCE_IS_ACTIVE (source);
963 /* we need to ref so that we can process the CSRCs later */
964 gst_buffer_ref (buffer);
966 /* let source process the packet */
967 result = rtp_source_process_rtp (source, buffer, &arrival);
969 /* source became active */
970 if (prevactive != RTP_SOURCE_IS_ACTIVE (source)) {
971 sess->stats.active_sources++;
972 GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
973 sess->stats.active_sources);
974 on_ssrc_validated (sess, source);
976 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
977 sess->stats.sender_sources++;
978 GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
979 sess->stats.sender_sources);
983 on_new_ssrc (sess, source);
985 if (source->validated) {
989 /* for validated sources, we add the CSRCs as well */
990 count = gst_rtp_buffer_get_csrc_count (buffer);
992 for (i = 0; i < count; i++) {
996 csrc = gst_rtp_buffer_get_csrc (buffer, i);
999 csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1002 GST_DEBUG ("created new CSRC: %08x", csrc);
1003 rtp_source_set_as_csrc (csrc_src);
1004 if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1005 sess->stats.active_sources++;
1006 on_new_ssrc (sess, source);
1010 gst_buffer_unref (buffer);
1012 RTP_SESSION_UNLOCK (sess);
1019 gst_buffer_unref (buffer);
1020 GST_DEBUG ("invalid RTP packet received");
1025 gst_buffer_unref (buffer);
1026 RTP_SESSION_UNLOCK (sess);
1027 GST_DEBUG ("ignoring RTP packet because we are leaving");
1032 /* A Sender report contains statistics about how the sender is doing. This
1033 * includes timing informataion about the relation between RTP and NTP
1034 * timestamps is it using and the number of packets/bytes it sent to us.
1036 * In this report is also included a set of report blocks related to how this
1037 * sender is receiving data (in case we (or somebody else) is also sending stuff
1038 * to it). This info includes the packet loss, jitter and seqnum. It also
1039 * contains information to calculate the round trip time (LSR/DLSR).
1042 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1043 RTPArrivalStats * arrival)
1045 guint32 senderssrc, rtptime, packet_count, octet_count;
1049 gboolean created, prevsender;
1051 gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1052 &packet_count, &octet_count);
1054 GST_DEBUG ("got SR packet: SSRC %08x", senderssrc);
1056 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1058 prevsender = RTP_SOURCE_IS_SENDER (source);
1060 /* first update the source */
1061 rtp_source_process_sr (source, ntptime, rtptime, packet_count, octet_count,
1064 if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1065 sess->stats.sender_sources++;
1066 GST_DEBUG ("source: %08x became sender, %d sender sources", senderssrc,
1067 sess->stats.sender_sources);
1071 on_new_ssrc (sess, source);
1073 count = gst_rtcp_packet_get_rb_count (packet);
1074 for (i = 0; i < count; i++) {
1075 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1076 guint8 fractionlost;
1079 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1080 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1082 if (ssrc == sess->source->ssrc) {
1083 /* only deal with report blocks for our session, we update the stats of
1084 * the sender of the RTCP message. We could also compare our stats against
1085 * the other sender to see if we are better or worse. */
1086 rtp_source_process_rb (source, fractionlost, packetslost,
1087 exthighestseq, jitter, lsr, dlsr);
1092 /* A receiver report contains statistics about how a receiver is doing. It
1093 * includes stuff like packet loss, jitter and the seqnum it received last. It
1094 * also contains info to calculate the round trip time.
1096 * We are only interested in how the sender of this report is doing wrt to us.
1099 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1100 RTPArrivalStats * arrival)
1107 senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1109 GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1111 source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1114 on_new_ssrc (sess, source);
1116 count = gst_rtcp_packet_get_rb_count (packet);
1117 for (i = 0; i < count; i++) {
1118 guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1119 guint8 fractionlost;
1122 gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1123 &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1125 if (ssrc == sess->source->ssrc) {
1126 rtp_source_process_rb (source, fractionlost, packetslost,
1127 exthighestseq, jitter, lsr, dlsr);
1132 /* FIXME, we're just printing this for now... */
1134 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1135 RTPArrivalStats * arrival)
1138 gboolean more_items, more_entries;
1140 items = gst_rtcp_packet_sdes_get_item_count (packet);
1141 GST_DEBUG ("got SDES packet with %d items", items);
1143 more_items = gst_rtcp_packet_sdes_first_item (packet);
1145 while (more_items) {
1148 ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1150 GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1152 more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1154 while (more_entries) {
1155 GstRTCPSDESType type;
1159 gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1161 GST_DEBUG ("entry %d, type %d, len %d, data %s", j, type, len, data);
1163 more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1166 more_items = gst_rtcp_packet_sdes_next_item (packet);
1171 /* BYE is sent when a client leaves the session
1174 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
1175 RTPArrivalStats * arrival)
1180 reason = gst_rtcp_packet_bye_get_reason (packet);
1181 GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
1183 count = gst_rtcp_packet_bye_get_ssrc_count (packet);
1184 for (i = 0; i < count; i++) {
1187 gboolean created, prevactive, prevsender;
1188 guint pmembers, members;
1190 ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
1191 GST_DEBUG ("SSRC: %08x", ssrc);
1193 /* find src and mark bye, no probation when dealing with RTCP */
1194 source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1196 /* store time for when we need to time out this source */
1197 source->bye_time = arrival->time;
1199 prevactive = RTP_SOURCE_IS_ACTIVE (source);
1200 prevsender = RTP_SOURCE_IS_SENDER (source);
1202 /* let the source handle the rest */
1203 rtp_source_process_bye (source, reason);
1205 pmembers = sess->stats.active_sources;
1207 if (prevactive && !RTP_SOURCE_IS_ACTIVE (source)) {
1208 sess->stats.active_sources--;
1209 GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
1210 sess->stats.active_sources);
1212 if (prevsender && !RTP_SOURCE_IS_SENDER (source)) {
1213 sess->stats.sender_sources--;
1214 GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
1215 sess->stats.sender_sources);
1217 members = sess->stats.active_sources;
1219 if (!sess->source->received_bye && members < pmembers) {
1220 /* some members went away since the previous timeout estimate.
1221 * Perform reverse reconsideration but only when we are not scheduling a
1223 if (arrival->time < sess->next_rtcp_check_time) {
1224 GstClockTime time_remaining;
1226 time_remaining = sess->next_rtcp_check_time - arrival->time;
1227 sess->next_rtcp_check_time =
1228 gst_util_uint64_scale (time_remaining, members, pmembers);
1230 GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
1231 GST_TIME_ARGS (sess->next_rtcp_check_time));
1233 sess->next_rtcp_check_time += arrival->time;
1235 /* notify app of reconsideration */
1236 if (sess->callbacks.reconsider)
1237 sess->callbacks.reconsider (sess, sess->user_data);
1242 on_new_ssrc (sess, source);
1244 on_bye_ssrc (sess, source);
1250 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
1251 RTPArrivalStats * arrival)
1253 GST_DEBUG ("received APP");
1257 * rtp_session_process_rtcp:
1258 * @sess: and #RTPSession
1259 * @buffer: an RTCP buffer
1261 * Process an RTCP buffer in the session manager.
1263 * Returns: a #GstFlowReturn.
1266 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer)
1268 GstRTCPPacket packet;
1269 gboolean more, is_bye = FALSE;
1270 RTPArrivalStats arrival;
1272 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1273 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1275 if (!gst_rtcp_buffer_validate (buffer))
1276 goto invalid_packet;
1278 GST_DEBUG ("received RTCP packet");
1280 RTP_SESSION_LOCK (sess);
1281 /* update arrival stats */
1282 update_arrival_stats (sess, &arrival, FALSE, buffer);
1287 /* start processing the compound packet */
1288 more = gst_rtcp_buffer_get_first_packet (buffer, &packet);
1292 type = gst_rtcp_packet_get_type (&packet);
1294 /* when we are leaving the session, we should ignore all non-BYE messages */
1295 if (sess->source->received_bye && type != GST_RTCP_TYPE_BYE) {
1296 GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
1301 case GST_RTCP_TYPE_SR:
1302 rtp_session_process_sr (sess, &packet, &arrival);
1304 case GST_RTCP_TYPE_RR:
1305 rtp_session_process_rr (sess, &packet, &arrival);
1307 case GST_RTCP_TYPE_SDES:
1308 rtp_session_process_sdes (sess, &packet, &arrival);
1310 case GST_RTCP_TYPE_BYE:
1312 rtp_session_process_bye (sess, &packet, &arrival);
1314 case GST_RTCP_TYPE_APP:
1315 rtp_session_process_app (sess, &packet, &arrival);
1318 GST_WARNING ("got unknown RTCP packet");
1322 more = gst_rtcp_packet_move_to_next (&packet);
1325 /* if we are scheduling a BYE, we only want to count bye packets, else we
1326 * count everything */
1327 if (sess->source->received_bye) {
1329 sess->stats.bye_members++;
1330 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
1333 /* keep track of average packet size */
1334 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
1336 RTP_SESSION_UNLOCK (sess);
1338 gst_buffer_unref (buffer);
1345 GST_DEBUG ("invalid RTCP packet received");
1350 gst_buffer_unref (buffer);
1351 RTP_SESSION_UNLOCK (sess);
1352 GST_DEBUG ("ignoring RTP packet because we left");
1358 * rtp_session_send_rtp:
1359 * @sess: an #RTPSession
1360 * @buffer: an RTP buffer
1362 * Send the RTP buffer in the session manager.
1364 * Returns: a #GstFlowReturn.
1367 rtp_session_send_rtp (RTPSession * sess, GstBuffer * buffer)
1369 GstFlowReturn result;
1371 gboolean prevsender;
1373 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1374 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1376 RTP_SESSION_LOCK (sess);
1377 source = sess->source;
1379 prevsender = RTP_SOURCE_IS_SENDER (source);
1381 /* we use our own source to send */
1382 result = rtp_source_send_rtp (sess->source, buffer);
1384 if (RTP_SOURCE_IS_SENDER (source) && !prevsender)
1385 sess->stats.sender_sources++;
1386 RTP_SESSION_UNLOCK (sess);
1392 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
1395 GstClockTime result;
1397 if (sess->source->received_bye) {
1398 result = rtp_stats_calculate_rtcp_interval (&sess->stats,
1399 RTP_SOURCE_IS_SENDER (sess->source), first);
1401 result = rtp_stats_calculate_bye_interval (&sess->stats);
1404 GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT,
1405 GST_TIME_ARGS (result));
1408 result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
1410 GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
1416 * rtp_session_send_bye:
1417 * @sess: an #RTPSession
1418 * @reason: a reason or NULL
1420 * Stop the current @sess and schedule a BYE message for the other members.
1422 * Returns: a #GstFlowReturn.
1425 rtp_session_send_bye (RTPSession * sess, const gchar * reason)
1427 GstFlowReturn result = GST_FLOW_OK;
1429 GstClockTime current, interval;
1431 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1433 RTP_SESSION_LOCK (sess);
1434 source = sess->source;
1436 /* ignore more BYEs */
1437 if (source->received_bye)
1440 /* we have BYE now */
1441 source->received_bye = TRUE;
1442 /* at least one member wants to send a BYE */
1443 sess->bye_reason = g_strdup (reason);
1444 sess->stats.avg_rtcp_packet_size = 100;
1445 sess->stats.bye_members = 1;
1446 sess->first_rtcp = TRUE;
1447 sess->sent_bye = FALSE;
1449 /* get current time */
1450 if (sess->callbacks.get_time)
1451 current = sess->callbacks.get_time (sess, sess->user_data);
1455 /* reschedule transmission */
1456 sess->last_rtcp_send_time = current;
1457 interval = calculate_rtcp_interval (sess, FALSE, TRUE);
1458 sess->next_rtcp_check_time = current + interval;
1460 GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
1461 GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
1463 /* notify app of reconsideration */
1464 if (sess->callbacks.reconsider)
1465 sess->callbacks.reconsider (sess, sess->user_data);
1467 RTP_SESSION_UNLOCK (sess);
1473 * rtp_session_next_timeout:
1474 * @sess: an #RTPSession
1475 * @time: the current time
1477 * Get the next time we should perform session maintenance tasks.
1479 * Returns: a time when rtp_session_on_timeout() should be called with the
1483 rtp_session_next_timeout (RTPSession * sess, GstClockTime time)
1485 GstClockTime result;
1487 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1489 RTP_SESSION_LOCK (sess);
1491 result = sess->next_rtcp_check_time;
1493 if (sess->source->received_bye) {
1495 result = GST_CLOCK_TIME_NONE;
1496 else if (sess->stats.active_sources >= 50)
1497 /* reconsider BYE if members >= 50 */
1498 result = time + calculate_rtcp_interval (sess, FALSE, TRUE);;
1500 if (sess->first_rtcp)
1501 /* we are called for the first time */
1502 result = time + calculate_rtcp_interval (sess, FALSE, TRUE);
1503 else if (sess->next_rtcp_check_time < time)
1504 /* get a new timeout when we need to */
1505 result = time + calculate_rtcp_interval (sess, FALSE, FALSE);
1507 sess->next_rtcp_check_time = result;
1509 GST_DEBUG ("next timeout: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
1510 RTP_SESSION_UNLOCK (sess);
1520 GstClockTime interval;
1521 GstRTCPPacket packet;
1527 session_start_rtcp (RTPSession * sess, ReportData * data)
1529 GstRTCPPacket *packet = &data->packet;
1530 RTPSource *own = sess->source;
1532 data->rtcp = gst_rtcp_buffer_new (sess->mtu);
1534 if (RTP_SOURCE_IS_SENDER (own)) {
1535 /* we are a sender, create SR */
1536 GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
1537 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SR, packet);
1539 /* fill in sender report info, FIXME NTP and RTP timestamps missing */
1540 gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
1541 0, 0, own->stats.packets_sent, own->stats.octets_sent);
1543 /* we are only receiver, create RR */
1544 GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
1545 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_RR, packet);
1546 gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
1550 /* construct a Sender or Receiver Report */
1552 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
1554 RTPSession *sess = data->sess;
1555 GstRTCPPacket *packet = &data->packet;
1557 /* create a new buffer if needed */
1558 if (data->rtcp == NULL) {
1559 session_start_rtcp (sess, data);
1561 if (gst_rtcp_packet_get_rb_count (packet) < GST_RTCP_MAX_RB_COUNT) {
1562 /* only report about other sender sources */
1563 if (source != sess->source && RTP_SOURCE_IS_SENDER (source)) {
1564 RTPSourceStats *stats;
1565 guint64 extended_max, expected;
1566 guint64 expected_interval, received_interval, ntptime;
1567 gint64 lost, lost_interval;
1568 guint32 fraction, LSR, DLSR;
1571 stats = &source->stats;
1573 extended_max = stats->cycles + stats->max_seq;
1574 expected = extended_max - stats->base_seq + 1;
1576 GST_DEBUG ("ext_max %d, expected %d, received %d, base_seq %d",
1577 extended_max, expected, stats->packets_received, stats->base_seq);
1579 lost = expected - stats->packets_received;
1580 lost = CLAMP (lost, -0x800000, 0x7fffff);
1582 expected_interval = expected - stats->prev_expected;
1583 stats->prev_expected = expected;
1584 received_interval = stats->packets_received - stats->prev_received;
1585 stats->prev_received = stats->packets_received;
1587 lost_interval = expected_interval - received_interval;
1589 if (expected_interval == 0 || lost_interval <= 0)
1592 fraction = (lost_interval << 8) / expected_interval;
1594 GST_DEBUG ("add RR for SSRC %08x", source->ssrc);
1595 /* we scaled the jitter up for additional precision */
1596 GST_DEBUG ("fraction %d, lost %d, extseq %u, jitter %d", fraction, lost,
1597 extended_max, stats->jitter >> 4);
1599 if (rtp_source_get_last_sr (source, &ntptime, NULL, NULL, NULL, &time)) {
1600 /* LSR is middle bits of the last ntptime */
1601 LSR = (ntptime >> 16) & 0xffffffff;
1602 /* DLSR, delay since last SR is expressed in 1/65536 second units */
1603 DLSR = gst_util_uint64_scale_int (data->time - time, 65536, GST_SECOND);
1605 /* No valid SR received, LSR/DLSR are set to 0 then */
1609 GST_DEBUG ("LSR %08x, DLSR %08x", LSR, DLSR);
1611 /* packet is not yet filled, add report block for this source. */
1612 gst_rtcp_packet_add_rb (packet, source->ssrc, fraction, lost,
1613 extended_max, stats->jitter >> 4, LSR, DLSR);
1618 /* perform cleanup of sources that timed out */
1620 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
1622 gboolean remove = FALSE;
1623 gboolean byetimeout = FALSE;
1624 gboolean is_sender, is_active;
1625 RTPSession *sess = data->sess;
1626 GstClockTime interval;
1628 is_sender = RTP_SOURCE_IS_SENDER (source);
1629 is_active = RTP_SOURCE_IS_ACTIVE (source);
1631 /* check for our own source, we don't want to delete our own source. */
1632 if (!(source == sess->source)) {
1633 if (source->received_bye) {
1634 /* if we received a BYE from the source, remove the source after some
1636 if (data->time > source->bye_time &&
1637 data->time - source->bye_time > sess->stats.bye_timeout) {
1638 GST_DEBUG ("removing BYE source %08x", source->ssrc);
1643 /* sources that were inactive for more than 5 times the deterministic reporting
1644 * interval get timed out. the min timeout is 5 seconds. */
1645 if (data->time > source->last_activity) {
1646 interval = MAX (data->interval * 5, 5 * GST_SECOND);
1647 if (data->time - source->last_activity > interval) {
1648 GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
1649 source->ssrc, GST_TIME_ARGS (source->last_activity));
1655 /* senders that did not send for a long time become a receiver, this also
1656 * holds for our own source. */
1658 if (data->time > source->last_rtp_activity) {
1659 interval = MAX (data->interval * 2, 5 * GST_SECOND);
1661 if (data->time - source->last_rtp_activity > interval) {
1662 GST_DEBUG ("sender source %08x timed out and became receiver, last %"
1663 GST_TIME_FORMAT, source->ssrc,
1664 GST_TIME_ARGS (source->last_rtp_activity));
1665 source->is_sender = FALSE;
1666 sess->stats.sender_sources--;
1672 sess->total_sources--;
1674 sess->stats.sender_sources--;
1676 sess->stats.active_sources--;
1679 on_bye_timeout (sess, source);
1681 on_timeout (sess, source);
1688 session_sdes (RTPSession * sess, ReportData * data)
1690 GstRTCPPacket *packet = &data->packet;
1692 /* add SDES packet */
1693 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SDES, packet);
1695 gst_rtcp_packet_sdes_add_item (packet, sess->source->ssrc);
1696 gst_rtcp_packet_sdes_add_entry (packet, GST_RTCP_SDES_CNAME,
1697 strlen (sess->cname), (guint8 *) sess->cname);
1699 /* other SDES items must only be added at regular intervals and only when the
1700 * user requests to since it might be a privacy problem */
1702 gst_rtcp_packet_sdes_add_entry (&packet, GST_RTCP_SDES_NAME,
1703 strlen (sess->name), (guint8 *) sess->name);
1704 gst_rtcp_packet_sdes_add_entry (&packet, GST_RTCP_SDES_TOOL,
1705 strlen (sess->tool), (guint8 *) sess->tool);
1708 data->has_sdes = TRUE;
1711 /* schedule a BYE packet */
1713 session_bye (RTPSession * sess, ReportData * data)
1715 GstRTCPPacket *packet = &data->packet;
1718 session_start_rtcp (sess, data);
1721 session_sdes (sess, data);
1723 /* add a BYE packet */
1724 gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_BYE, packet);
1725 gst_rtcp_packet_bye_add_ssrc (packet, sess->source->ssrc);
1726 if (sess->bye_reason)
1727 gst_rtcp_packet_bye_set_reason (packet, sess->bye_reason);
1729 /* we have a BYE packet now */
1730 data->is_bye = TRUE;
1734 is_rtcp_time (RTPSession * sess, GstClockTime time, ReportData * data)
1736 GstClockTime new_send_time;
1739 /* no need to check yet */
1740 if (sess->next_rtcp_check_time > time) {
1741 GST_DEBUG ("no check time yet");
1745 /* perform forward reconsideration */
1746 new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, data->interval);
1748 GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT,
1749 GST_TIME_ARGS (new_send_time));
1751 new_send_time += sess->last_rtcp_send_time;
1753 /* check if reconsideration */
1754 if (time < new_send_time) {
1755 GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
1756 GST_TIME_ARGS (new_send_time));
1758 /* store new check time */
1759 sess->next_rtcp_check_time = new_send_time;
1762 new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
1764 GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
1765 GST_TIME_ARGS (new_send_time));
1766 sess->next_rtcp_check_time = time + new_send_time;
1772 * rtp_session_on_timeout:
1773 * @sess: an #RTPSession
1775 * Perform maintenance actions after the timeout obtained with
1776 * rtp_session_next_timeout() expired.
1778 * This function will perform timeouts of receivers and senders, send a BYE
1779 * packet or generate RTCP packets with current session stats.
1781 * This function can call the #RTPSessionSendRTCP callback, possibly multiple
1782 * times, for each packet that should be processed.
1784 * Returns: a #GstFlowReturn.
1787 rtp_session_on_timeout (RTPSession * sess, GstClockTime time)
1789 GstFlowReturn result = GST_FLOW_OK;
1792 g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1797 data.is_bye = FALSE;
1798 data.has_sdes = FALSE;
1800 GST_DEBUG ("reporting at %" GST_TIME_FORMAT, GST_TIME_ARGS (time));
1802 RTP_SESSION_LOCK (sess);
1803 /* get a new interval, we need this for various cleanups etc */
1804 data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
1806 /* first perform cleanups */
1807 g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
1808 (GHRFunc) session_cleanup, &data);
1810 /* see if we need to generate SR or RR packets */
1811 if (is_rtcp_time (sess, time, &data)) {
1812 if (sess->source->received_bye) {
1813 /* generate BYE instead */
1814 session_bye (sess, &data);
1815 sess->sent_bye = TRUE;
1817 /* loop over all known sources and do something */
1818 g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
1819 (GHFunc) session_report_blocks, &data);
1826 /* we keep track of the last report time in order to timeout inactive
1827 * receivers or senders */
1828 sess->last_rtcp_send_time = data.time;
1829 sess->first_rtcp = FALSE;
1831 /* add SDES for this source when not already added */
1833 session_sdes (sess, &data);
1835 /* update average RTCP size before sending */
1836 size = GST_BUFFER_SIZE (data.rtcp) + sess->header_len;
1837 UPDATE_AVG (sess->stats.avg_rtcp_packet_size, size);
1839 RTP_SESSION_UNLOCK (sess);
1841 /* push out the RTCP packet */
1843 /* close the RTCP packet */
1844 gst_rtcp_buffer_end (data.rtcp);
1846 if (sess->callbacks.send_rtcp)
1847 result = sess->callbacks.send_rtcp (sess, sess->source, data.rtcp,
1850 gst_buffer_unref (data.rtcp);