configure.ac: Disable rtpmanager for now because it depends on CVS -base.
[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 }
37
38 /**
39  * rtp_stats_calculate_rtcp_interval:
40  * @stats: an #RTPSessionStats struct
41  * 
42  * Calculate the RTCP interval. The result of this function is the amount of
43  * time to wait (in seconds) before sender a new RTCP message.
44  *
45  * Returns: the RTCP interval.
46  */
47 gdouble
48 rtp_stats_calculate_rtcp_interval (RTPSessionStats * stats, gboolean sender)
49 {
50   gdouble active, senders, receivers, sfraction;
51   gboolean avg_rtcp;
52   gdouble interval;
53
54   active = stats->active_sources;
55   /* Try to avoid division by zero */
56   if (stats->active_sources == 0)
57     active += 1.0;
58
59   senders = (gdouble) stats->sender_sources;
60   receivers = (gdouble) (active - senders);
61   avg_rtcp = (gdouble) stats->avg_rtcp_packet_size;
62
63   sfraction = senders / active;
64
65   GST_DEBUG ("senders: %f, receivers %f, avg_rtcp %f, sfraction %f",
66       senders, receivers, avg_rtcp, sfraction);
67
68   if (sfraction <= stats->sender_fraction) {
69     if (sender) {
70       interval =
71           (avg_rtcp * senders) / (stats->sender_fraction *
72           stats->rtcp_bandwidth);
73     } else {
74       interval =
75           (avg_rtcp * receivers) / ((1.0 -
76               stats->sender_fraction) * stats->rtcp_bandwidth);
77     }
78   } else {
79     interval = (avg_rtcp * active) / stats->rtcp_bandwidth;
80   }
81
82   if (interval < stats->min_interval)
83     interval = stats->min_interval;
84
85   if (!stats->sent_rtcp)
86     interval /= 2.0;
87
88   return interval;
89 }
90
91 /**
92  * rtp_stats_calculate_rtcp_interval:
93  * @stats: an #RTPSessionStats struct
94  * @interval: an RTCP interval
95  * 
96  * Apply a random jitter to the @interval. @interval is typically obtained with
97  * rtp_stats_calculate_rtcp_interval().
98  *
99  * Returns: the new RTCP interval.
100  */
101 gdouble
102 rtp_stats_add_rtcp_jitter (RTPSessionStats * stats, gdouble interval)
103 {
104   /* see RFC 3550 p 30 
105    * To compensate for "unconditional reconsideration" converging to a
106    * value below the intended average.
107    */
108 #define COMPENSATION  (2.71828 - 1.5);
109
110   return (interval * g_random_double_range (0.5, 1.5)) / COMPENSATION;
111 }