gst/rtpmanager/gstrtpsession.c: Remove debug.
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / rtpstats.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim@fluendo.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "rtpstats.h"
21
22 /**
23  * rtp_stats_init_defaults:
24  * @stats: an #RTPSessionStats struct
25  *
26  * Initialize @stats with its default values.
27  */
28 void
29 rtp_stats_init_defaults (RTPSessionStats * stats)
30 {
31   stats->bandwidth = RTP_STATS_BANDWIDTH;
32   stats->sender_fraction = RTP_STATS_SENDER_FRACTION;
33   stats->receiver_fraction = RTP_STATS_RECEIVER_FRACTION;
34   stats->rtcp_bandwidth = RTP_STATS_RTCP_BANDWIDTH;
35   stats->min_interval = RTP_STATS_MIN_INTERVAL;
36   stats->bye_timeout = RTP_STATS_BYE_TIMEOUT;
37 }
38
39 /**
40  * rtp_stats_calculate_rtcp_interval:
41  * @stats: an #RTPSessionStats struct
42  * @sender: if we are a sender
43  * @first: if this is the first time
44  * 
45  * Calculate the RTCP interval. The result of this function is the amount of
46  * time to wait (in nanoseconds) before sending a new RTCP message.
47  *
48  * Returns: the RTCP interval.
49  */
50 GstClockTime
51 rtp_stats_calculate_rtcp_interval (RTPSessionStats * stats, gboolean we_send,
52     gboolean first)
53 {
54   gdouble members, senders, n;
55   gdouble avg_rtcp_size, rtcp_bw;
56   gdouble interval;
57   gdouble rtcp_min_time;
58
59   /* Very first call at application start-up uses half the min
60    * delay for quicker notification while still allowing some time
61    * before reporting for randomization and to learn about other
62    * sources so the report interval will converge to the correct
63    * interval more quickly.
64    */
65   rtcp_min_time = stats->min_interval;
66   if (first)
67     rtcp_min_time /= 2.0;
68
69   /* Dedicate a fraction of the RTCP bandwidth to senders unless
70    * the number of senders is large enough that their share is
71    * more than that fraction.
72    */
73   n = members = stats->active_sources;
74   senders = (gdouble) stats->sender_sources;
75   rtcp_bw = stats->rtcp_bandwidth;
76
77   if (senders <= members * RTP_STATS_SENDER_FRACTION) {
78     if (we_send) {
79       rtcp_bw *= RTP_STATS_SENDER_FRACTION;
80       n = senders;
81     } else {
82       rtcp_bw *= RTP_STATS_RECEIVER_FRACTION;
83       n -= senders;
84     }
85   }
86
87   avg_rtcp_size = stats->avg_rtcp_packet_size / 16.0;
88   /*
89    * The effective number of sites times the average packet size is
90    * the total number of octets sent when each site sends a report.
91    * Dividing this by the effective bandwidth gives the time
92    * interval over which those packets must be sent in order to
93    * meet the bandwidth target, with a minimum enforced.  In that
94    * time interval we send one report so this time is also our
95    * average time between reports.
96    */
97   interval = avg_rtcp_size * n / rtcp_bw;
98   if (interval < rtcp_min_time)
99     interval = rtcp_min_time;
100
101   return interval * GST_SECOND;
102 }
103
104 /**
105  * rtp_stats_add_rtcp_jitter:
106  * @stats: an #RTPSessionStats struct
107  * @interval: an RTCP interval
108  * 
109  * Apply a random jitter to the @interval. @interval is typically obtained with
110  * rtp_stats_calculate_rtcp_interval().
111  *
112  * Returns: the new RTCP interval.
113  */
114 GstClockTime
115 rtp_stats_add_rtcp_jitter (RTPSessionStats * stats, GstClockTime interval)
116 {
117   gdouble temp;
118
119   /* see RFC 3550 p 30 
120    * To compensate for "unconditional reconsideration" converging to a
121    * value below the intended average.
122    */
123 #define COMPENSATION  (2.71828 - 1.5);
124
125   temp = (interval * g_random_double_range (0.5, 1.5)) / COMPENSATION;
126
127   return (GstClockTime) temp;
128 }
129
130
131 /**
132  * rtp_stats_calculate_bye_interval:
133  * @stats: an #RTPSessionStats struct
134  * 
135  * Calculate the BYE interval. The result of this function is the amount of
136  * time to wait (in nanoseconds) before sending a BYE message.
137  *
138  * Returns: the BYE interval.
139  */
140 GstClockTime
141 rtp_stats_calculate_bye_interval (RTPSessionStats * stats)
142 {
143   gdouble members;
144   gdouble avg_rtcp_size, rtcp_bw;
145   gdouble interval;
146   gdouble rtcp_min_time;
147
148   rtcp_min_time = (stats->min_interval) / 2.0;
149
150   /* Dedicate a fraction of the RTCP bandwidth to senders unless
151    * the number of senders is large enough that their share is
152    * more than that fraction.
153    */
154   members = stats->bye_members;
155   rtcp_bw = stats->rtcp_bandwidth * RTP_STATS_RECEIVER_FRACTION;
156
157   avg_rtcp_size = stats->avg_rtcp_packet_size / 16.0;
158   /*
159    * The effective number of sites times the average packet size is
160    * the total number of octets sent when each site sends a report.
161    * Dividing this by the effective bandwidth gives the time
162    * interval over which those packets must be sent in order to
163    * meet the bandwidth target, with a minimum enforced.  In that
164    * time interval we send one report so this time is also our
165    * average time between reports.
166    */
167   interval = avg_rtcp_size * members / rtcp_bw;
168   if (interval < rtcp_min_time)
169     interval = rtcp_min_time;
170
171   return interval * GST_SECOND;
172 }