rtp: Add property to disable RTCP reports per internal rtpsource
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / rtpstats.h
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  * Copyright (C)  2015 Kurento (http://kurento.org/)
4  *   @author: Miguel ParĂ­s <mparisdiaz@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #ifndef __RTP_STATS_H__
23 #define __RTP_STATS_H__
24
25 #include <gst/gst.h>
26 #include <gst/net/gstnetaddressmeta.h>
27 #include <gst/rtp/rtp.h>
28 #include <gio/gio.h>
29
30 /**
31  * RTPSenderReport:
32  *
33  * A sender report structure.
34  */
35 typedef struct {
36   gboolean is_valid;
37   guint64 ntptime;
38   guint32 rtptime;
39   guint32 packet_count;
40   guint32 octet_count;
41   GstClockTime time;
42 } RTPSenderReport;
43
44 /**
45  * RTPReceiverReport:
46  *
47  * A receiver report structure.
48  */
49 typedef struct {
50   gboolean is_valid;
51   guint32 ssrc; /* who the report is from */
52   guint8  fractionlost;
53   guint32 packetslost;
54   guint32 exthighestseq;
55   guint32 jitter;
56   guint32 lsr;
57   guint32 dlsr;
58   guint32 round_trip;
59 } RTPReceiverReport;
60
61 /**
62  * RTPPacketInfo:
63  * @send: if this is a packet for sending
64  * @rtp: if this info is about an RTP packet
65  * @is_list: if this is a bufferlist
66  * @data: a #GstBuffer or #GstBufferList
67  * @address: address of the sender of the packet
68  * @current_time: current time according to the system clock
69  * @running_time: time of a packet as buffer running_time
70  * @ntpnstime: time of a packet NTP time in nanoseconds
71  * @header_len: number of overhead bytes per packet
72  * @bytes: bytes of the packet including lowlevel overhead
73  * @payload_len: bytes of the RTP payload
74  * @seqnum: the seqnum of the packet
75  * @pt: the payload type of the packet
76  * @rtptime: the RTP time of the packet
77  *
78  * Structure holding information about the packet.
79  */
80 typedef struct {
81   gboolean      send;
82   gboolean      rtp;
83   gboolean      is_list;
84   gpointer      data;
85   GSocketAddress *address;
86   GstClockTime  current_time;
87   GstClockTime  running_time;
88   guint64       ntpnstime;
89   guint         header_len;
90   guint         bytes;
91   guint         packets;
92   guint         payload_len;
93   guint32       ssrc;
94   guint16       seqnum;
95   guint8        pt;
96   guint32       rtptime;
97   guint32       csrc_count;
98   guint32       csrcs[16];
99 } RTPPacketInfo;
100
101 /**
102  * RTPSourceStats:
103  * @packetsreceived: number of received packets in total
104  * @prevpacketsreceived: number of packets received in previous reporting
105  *                       interval
106  * @octetsreceived: number of payload bytes received
107  * @bytesreceived: number of total bytes received including headers and lower
108  *                 protocol level overhead
109  * @max_seqnr: highest sequence number received
110  * @transit: previous transit time used for calculating @jitter
111  * @jitter: current jitter (in clock rate units scaled by 16 for precision)
112  * @prev_rtptime: previous time when an RTP packet was received
113  * @prev_rtcptime: previous time when an RTCP packet was received
114  * @last_rtptime: time when last RTP packet received
115  * @last_rtcptime: time when last RTCP packet received
116  * @curr_rr: index of current @rr block
117  * @rr: previous and current receiver report block
118  * @curr_sr: index of current @sr block
119  * @sr: previous and current sender report block
120  *
121  * Stats about a source.
122  */
123 typedef struct {
124   guint64      packets_received;
125   guint64      octets_received;
126   guint64      bytes_received;
127
128   guint32      prev_expected;
129   guint32      prev_received;
130
131   guint16      max_seq;
132   guint64      cycles;
133   guint32      base_seq;
134   guint32      bad_seq;
135   guint32      transit;
136   guint32      jitter;
137
138   guint64      packets_sent;
139   guint64      octets_sent;
140
141   guint        sent_pli_count;
142   guint        recv_pli_count;
143   guint        sent_fir_count;
144   guint        recv_fir_count;
145   guint        sent_nack_count;
146   guint        recv_nack_count;
147
148   /* when we received stuff */
149   GstClockTime prev_rtptime;
150   GstClockTime prev_rtcptime;
151   GstClockTime last_rtptime;
152   GstClockTime last_rtcptime;
153
154   /* sender and receiver reports */
155   gint              curr_rr;
156   RTPReceiverReport rr[2];
157   gint              curr_sr;
158   RTPSenderReport   sr[2];
159 } RTPSourceStats;
160
161 #define RTP_STATS_BANDWIDTH           64000
162 #define RTP_STATS_RTCP_FRACTION       0.05
163 /*
164  * Minimum average time between RTCP packets from this site (in
165  * seconds).  This time prevents the reports from `clumping' when
166  * sessions are small and the law of large numbers isn't helping
167  * to smooth out the traffic.  It also keeps the report interval
168  * from becoming ridiculously small during transient outages like
169  * a network partition.
170  */
171 #define RTP_STATS_MIN_INTERVAL      5.0
172 /*
173  * Fraction of the RTCP bandwidth to be shared among active
174  * senders.  (This fraction was chosen so that in a typical
175  * session with one or two active senders, the computed report
176  * time would be roughly equal to the minimum report time so that
177  * we don't unnecessarily slow down receiver reports.) The
178  * receiver fraction must be 1 - the sender fraction.
179  */
180 #define RTP_STATS_SENDER_FRACTION       (0.25)
181 #define RTP_STATS_RECEIVER_FRACTION     (1.0 - RTP_STATS_SENDER_FRACTION)
182
183 /*
184  * When receiving a BYE from a source, remove the source from the database
185  * after this timeout.
186  */
187 #define RTP_STATS_BYE_TIMEOUT           (2 * GST_SECOND)
188
189 /*
190  * The default and minimum values of the maximum number of missing packets we tolerate.
191  * These are packets with asequence number bigger than the last seen packet.
192  */
193 #define RTP_DEF_DROPOUT      3000
194 #define RTP_MIN_DROPOUT      30
195
196 /*
197  * The default and minimum values of the maximum number of misordered packets we tolerate.
198  * These are packets with a sequence number smaller than the last seen packet.
199  */
200 #define RTP_DEF_MISORDER     100
201 #define RTP_MIN_MISORDER     10
202
203 /**
204  * RTPPacketRateCtx:
205  *
206  * Context to calculate the pseudo-average packet rate.
207  */
208 typedef struct {
209   gboolean probed;
210   gint32 clock_rate;
211   guint16 last_seqnum;
212   guint64 last_ts;
213   guint32 avg_packet_rate;
214 } RTPPacketRateCtx;
215
216 void gst_rtp_packet_rate_ctx_reset (RTPPacketRateCtx * ctx, gint32 clock_rate);
217 guint32 gst_rtp_packet_rate_ctx_update (RTPPacketRateCtx *ctx, guint16 seqnum, guint32 ts);
218 guint32 gst_rtp_packet_rate_ctx_get (RTPPacketRateCtx *ctx);
219 guint32 gst_rtp_packet_rate_ctx_get_max_dropout (RTPPacketRateCtx *ctx, gint32 time_ms);
220 guint32 gst_rtp_packet_rate_ctx_get_max_misorder (RTPPacketRateCtx *ctx, gint32 time_ms);
221
222 /**
223  * RTPSessionStats:
224  *
225  * Stats kept for a session and used to produce RTCP packet timeouts.
226  */
227 typedef struct {
228   guint         bandwidth;
229   guint         rtcp_bandwidth;
230   gdouble       sender_fraction;
231   gdouble       receiver_fraction;
232   gdouble       min_interval;
233   GstClockTime  bye_timeout;
234   guint         internal_sources;
235   guint         sender_sources;
236   guint         internal_sender_sources;
237   guint         active_sources;
238   guint         avg_rtcp_packet_size;
239   guint         bye_members;
240   guint         nacks_dropped;
241   guint         nacks_sent;
242   guint         nacks_received;
243 } RTPSessionStats;
244
245 void           rtp_stats_init_defaults              (RTPSessionStats *stats);
246
247 void           rtp_stats_set_bandwidths             (RTPSessionStats *stats,
248                                                      guint rtp_bw,
249                                                      gdouble rtcp_bw,
250                                                      guint rs, guint rr);
251
252 GstClockTime   rtp_stats_calculate_rtcp_interval    (RTPSessionStats *stats, gboolean sender, GstRTPProfile profile, gboolean ptp, gboolean first);
253 GstClockTime   rtp_stats_add_rtcp_jitter            (RTPSessionStats *stats, GstClockTime interval);
254 GstClockTime   rtp_stats_calculate_bye_interval     (RTPSessionStats *stats);
255 gint64         rtp_stats_get_packets_lost           (const RTPSourceStats *stats);
256
257 void           rtp_stats_set_min_interval           (RTPSessionStats *stats,
258                                                      gdouble min_interval);
259
260
261 gboolean __g_socket_address_equal (GSocketAddress *a, GSocketAddress *b);
262 gchar * __g_socket_address_to_string (GSocketAddress * addr);
263
264 #endif /* __RTP_STATS_H__ */