session: store more in the PacketInfo structure
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / rtpstats.h
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifndef __RTP_STATS_H__
21 #define __RTP_STATS_H__
22
23 #include <gst/gst.h>
24 #include <gst/net/gstnetaddressmeta.h>
25 #include <gio/gio.h>
26
27 /**
28  * RTPSenderReport:
29  *
30  * A sender report structure.
31  */
32 typedef struct {
33   gboolean is_valid;
34   guint64 ntptime;
35   guint32 rtptime;
36   guint32 packet_count;
37   guint32 octet_count;
38   GstClockTime time;
39 } RTPSenderReport;
40
41 /**
42  * RTPReceiverReport:
43  *
44  * A receiver report structure.
45  */
46 typedef struct {
47   gboolean is_valid;
48   guint32 ssrc; /* who the report is from */
49   guint8  fractionlost;
50   guint32 packetslost;
51   guint32 exthighestseq;
52   guint32 jitter;
53   guint32 lsr;
54   guint32 dlsr;
55   guint32 round_trip;
56 } RTPReceiverReport;
57
58 /**
59  * RTPPacketInfo:
60  * @send: if this is a packet for sending
61  * @rtp: if this info is about an RTP packet
62  * @is_list: if this is a bufferlist
63  * @data: a #GstBuffer or #GstBufferList
64  * @address: address of the sender of the packet
65  * @current_time: current time according to the system clock
66  * @running_time: time of a packet as buffer running_time
67  * @ntpnstime: time of a packet NTP time in nanoseconds
68  * @header_len: number of overhead bytes per packet
69  * @bytes: bytes of the packet including lowlevel overhead
70  * @payload_len: bytes of the RTP payload
71  *
72  * Structure holding information about the packet.
73  */
74 typedef struct {
75   gboolean      send;
76   gboolean      rtp;
77   gboolean      is_list;
78   gpointer      data;
79   GSocketAddress *address;
80   GstClockTime  current_time;
81   GstClockTime  running_time;
82   guint64       ntpnstime;
83   guint         header_len;
84   guint         bytes;
85   guint         payload_len;
86 } RTPPacketInfo;
87
88 /**
89  * RTPSourceStats:
90  * @packetsreceived: number of received packets in total
91  * @prevpacketsreceived: number of packets received in previous reporting
92  *                       interval
93  * @octetsreceived: number of payload bytes received
94  * @bytesreceived: number of total bytes received including headers and lower
95  *                 protocol level overhead
96  * @max_seqnr: highest sequence number received
97  * @transit: previous transit time used for calculating @jitter
98  * @jitter: current jitter
99  * @prev_rtptime: previous time when an RTP packet was received
100  * @prev_rtcptime: previous time when an RTCP packet was received
101  * @last_rtptime: time when last RTP packet received
102  * @last_rtcptime: time when last RTCP packet received
103  * @curr_rr: index of current @rr block
104  * @rr: previous and current receiver report block
105  * @curr_sr: index of current @sr block
106  * @sr: previous and current sender report block
107  *
108  * Stats about a source.
109  */
110 typedef struct {
111   guint64      packets_received;
112   guint64      octets_received;
113   guint64      bytes_received;
114
115   guint32      prev_expected;
116   guint32      prev_received;
117
118   guint16      max_seq;
119   guint64      cycles;
120   guint32      base_seq;
121   guint32      bad_seq;
122   guint32      transit;
123   guint32      jitter;
124
125   guint64      packets_sent;
126   guint64      octets_sent;
127
128   /* when we received stuff */
129   GstClockTime prev_rtptime;
130   GstClockTime prev_rtcptime;
131   GstClockTime last_rtptime;
132   GstClockTime last_rtcptime;
133
134   /* sender and receiver reports */
135   gint              curr_rr;
136   RTPReceiverReport rr[2];
137   gint              curr_sr;
138   RTPSenderReport   sr[2];
139 } RTPSourceStats;
140
141 #define RTP_STATS_BANDWIDTH           64000
142 #define RTP_STATS_RTCP_FRACTION       0.05
143 /*
144  * Minimum average time between RTCP packets from this site (in
145  * seconds).  This time prevents the reports from `clumping' when
146  * sessions are small and the law of large numbers isn't helping
147  * to smooth out the traffic.  It also keeps the report interval
148  * from becoming ridiculously small during transient outages like
149  * a network partition.
150  */
151 #define RTP_STATS_MIN_INTERVAL      5.0
152 /*
153  * Fraction of the RTCP bandwidth to be shared among active
154  * senders.  (This fraction was chosen so that in a typical
155  * session with one or two active senders, the computed report
156  * time would be roughly equal to the minimum report time so that
157  * we don't unnecessarily slow down receiver reports.) The
158  * receiver fraction must be 1 - the sender fraction.
159  */
160 #define RTP_STATS_SENDER_FRACTION       (0.25)
161 #define RTP_STATS_RECEIVER_FRACTION     (1.0 - RTP_STATS_SENDER_FRACTION)
162
163 /*
164  * When receiving a BYE from a source, remove the source from the database
165  * after this timeout.
166  */
167 #define RTP_STATS_BYE_TIMEOUT           (2 * GST_SECOND)
168
169 /*
170  * The maximum number of missing packets we tollerate. These are packets with a
171  * sequence number bigger than the last seen packet.
172  */
173 #define RTP_MAX_DROPOUT      3000
174 /*
175  * The maximum number of misordered packets we tollerate. These are packets with
176  * a sequence number smaller than the last seen packet.
177  */
178 #define RTP_MAX_MISORDER     100
179
180 /**
181  * RTPSessionStats:
182  *
183  * Stats kept for a session and used to produce RTCP packet timeouts.
184  */
185 typedef struct {
186   guint         bandwidth;
187   guint         rtcp_bandwidth;
188   gdouble       sender_fraction;
189   gdouble       receiver_fraction;
190   gdouble       min_interval;
191   GstClockTime  bye_timeout;
192   guint         internal_sources;
193   guint         sender_sources;
194   guint         internal_sender_sources;
195   guint         active_sources;
196   guint         avg_rtcp_packet_size;
197   guint         bye_members;
198 } RTPSessionStats;
199
200 void           rtp_stats_init_defaults              (RTPSessionStats *stats);
201
202 void           rtp_stats_set_bandwidths             (RTPSessionStats *stats,
203                                                      guint rtp_bw,
204                                                      gdouble rtcp_bw,
205                                                      guint rs, guint rr);
206
207 GstClockTime   rtp_stats_calculate_rtcp_interval    (RTPSessionStats *stats, gboolean sender, gboolean first);
208 GstClockTime   rtp_stats_add_rtcp_jitter            (RTPSessionStats *stats, GstClockTime interval);
209 GstClockTime   rtp_stats_calculate_bye_interval     (RTPSessionStats *stats);
210 gint64         rtp_stats_get_packets_lost           (const RTPSourceStats *stats);
211
212 void           rtp_stats_set_min_interval           (RTPSessionStats *stats,
213                                                      gdouble min_interval);
214
215
216 gboolean __g_socket_address_equal (GSocketAddress *a, GSocketAddress *b);
217 gchar * __g_socket_address_to_string (GSocketAddress * addr);
218
219 #endif /* __RTP_STATS_H__ */