• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* UDP/IP is assumed for bandwidth calculation */
31 #define UDP_IP_HEADER_OVERHEAD 28
32 
33 /**
34  * RTPSenderReport:
35  *
36  * A sender report structure.
37  */
38 typedef struct {
39   gboolean is_valid;
40   guint64 ntptime;
41   guint32 rtptime;
42   guint32 packet_count;
43   guint32 octet_count;
44   GstClockTime time;
45 } RTPSenderReport;
46 
47 /**
48  * RTPReceiverReport:
49  *
50  * A receiver report structure.
51  */
52 typedef struct {
53   gboolean is_valid;
54   guint32 ssrc; /* which source is the report about */
55   guint8  fractionlost;
56   guint32 packetslost;
57   guint32 exthighestseq;
58   guint32 jitter;
59   guint32 lsr;
60   guint32 dlsr;
61   guint32 round_trip;
62 } RTPReceiverReport;
63 
64 /**
65  * RTPPacketInfo:
66  * @send: if this is a packet for sending
67  * @rtp: if this info is about an RTP packet
68  * @is_list: if this is a bufferlist
69  * @data: a #GstBuffer or #GstBufferList
70  * @address: address of the sender of the packet
71  * @current_time: current time according to the system clock
72  * @running_time: time of a packet as buffer running_time
73  * @arrival_time: time of arrival of a packet
74  * @ntpnstime: time of a packet NTP time in nanoseconds
75  * @header_len: number of overhead bytes per packet
76  * @bytes: bytes of the packet including lowlevel overhead
77  * @payload_len: bytes of the RTP payload
78  * @seqnum: the seqnum of the packet
79  * @pt: the payload type of the packet
80  * @rtptime: the RTP time of the packet
81  * @marker: the marker bit
82  *
83  * @tw_seqnum_ext_id: the extension-header ID for transport-wide seqnums
84  * @tw_seqnum: the transport-wide seqnum of the packet
85  *
86  * Structure holding information about the packet.
87  */
88 typedef struct {
89   gboolean      send;
90   gboolean      rtp;
91   gboolean      is_list;
92   gpointer      data;
93   GSocketAddress *address;
94   GstClockTime  current_time;
95   GstClockTime  running_time;
96   GstClockTime  arrival_time;
97   guint64       ntpnstime;
98   guint         header_len;
99   guint         bytes;
100   guint         packets;
101   guint         payload_len;
102   guint32       ssrc;
103   guint16       seqnum;
104   guint8        pt;
105   guint32       rtptime;
106   gboolean      marker;
107   guint32       csrc_count;
108   guint32       csrcs[16];
109   GBytes        *header_ext;
110   guint16       header_ext_bit_pattern;
111 } RTPPacketInfo;
112 
113 /**
114  * RTPSourceStats:
115  * @packets_received: number of received packets in total
116  * @prev_received: number of packets received in previous reporting
117  *                       interval
118  * @octets_received: number of payload bytes received
119  * @bytes_received: number of total bytes received including headers and lower
120  *                 protocol level overhead
121  * @max_seqnr: highest sequence number received
122  * @transit: previous transit time used for calculating @jitter
123  * @jitter: current jitter (in clock rate units scaled by 16 for precision)
124  * @prev_rtptime: previous time when an RTP packet was received
125  * @prev_rtcptime: previous time when an RTCP packet was received
126  * @last_rtptime: time when last RTP packet received
127  * @last_rtcptime: time when last RTCP packet received
128  * @curr_rr: index of current @rr block
129  * @rr: previous and current receiver report block
130  * @curr_sr: index of current @sr block
131  * @sr: previous and current sender report block
132  *
133  * Stats about a source.
134  */
135 typedef struct {
136   guint64      packets_received;
137   guint64      octets_received;
138   guint64      bytes_received;
139 
140   guint32      prev_expected;
141   guint32      prev_received;
142 
143   guint16      max_seq;
144   guint64      cycles;
145   guint32      base_seq;
146   guint32      bad_seq;
147   guint32      transit;
148   guint32      jitter;
149 
150   guint64      packets_sent;
151   guint64      octets_sent;
152 
153   guint        sent_pli_count;
154   guint        recv_pli_count;
155   guint        sent_fir_count;
156   guint        recv_fir_count;
157   guint        sent_nack_count;
158   guint        recv_nack_count;
159 
160   /* when we received stuff */
161   GstClockTime prev_rtptime;
162   GstClockTime prev_rtcptime;
163   GstClockTime last_rtptime;
164   GstClockTime last_rtcptime;
165 
166   /* sender and receiver reports */
167   gint              curr_rr;
168   RTPReceiverReport rr[2];
169   gint              curr_sr;
170   RTPSenderReport   sr[2];
171 } RTPSourceStats;
172 
173 #define RTP_STATS_BANDWIDTH           64000
174 #define RTP_STATS_RTCP_FRACTION       0.05
175 /*
176  * Minimum average time between RTCP packets from this site (in
177  * seconds).  This time prevents the reports from `clumping' when
178  * sessions are small and the law of large numbers isn't helping
179  * to smooth out the traffic.  It also keeps the report interval
180  * from becoming ridiculously small during transient outages like
181  * a network partition.
182  */
183 #define RTP_STATS_MIN_INTERVAL      5.0
184 /*
185  * Fraction of the RTCP bandwidth to be shared among active
186  * senders.  (This fraction was chosen so that in a typical
187  * session with one or two active senders, the computed report
188  * time would be roughly equal to the minimum report time so that
189  * we don't unnecessarily slow down receiver reports.) The
190  * receiver fraction must be 1 - the sender fraction.
191  */
192 #define RTP_STATS_SENDER_FRACTION       (0.25)
193 #define RTP_STATS_RECEIVER_FRACTION     (1.0 - RTP_STATS_SENDER_FRACTION)
194 
195 /*
196  * When receiving a BYE from a source, remove the source from the database
197  * after this timeout.
198  */
199 #define RTP_STATS_BYE_TIMEOUT           (2 * GST_SECOND)
200 
201 /*
202  * The default and minimum values of the maximum number of missing packets we tolerate.
203  * These are packets with asequence number bigger than the last seen packet.
204  */
205 #define RTP_DEF_DROPOUT      3000
206 #define RTP_MIN_DROPOUT      30
207 
208 /*
209  * The default and minimum values of the maximum number of misordered packets we tolerate.
210  * These are packets with a sequence number smaller than the last seen packet.
211  */
212 #define RTP_DEF_MISORDER     100
213 #define RTP_MIN_MISORDER     10
214 
215 /**
216  * RTPPacketRateCtx:
217  *
218  * Context to calculate the pseudo-average packet rate.
219  */
220 typedef struct {
221   gboolean probed;
222   gint32 clock_rate;
223   guint16 last_seqnum;
224   guint64 last_ts;
225   guint32 avg_packet_rate;
226 } RTPPacketRateCtx;
227 
228 void gst_rtp_packet_rate_ctx_reset (RTPPacketRateCtx * ctx, gint32 clock_rate);
229 guint32 gst_rtp_packet_rate_ctx_update (RTPPacketRateCtx *ctx, guint16 seqnum, guint32 ts);
230 guint32 gst_rtp_packet_rate_ctx_get (RTPPacketRateCtx *ctx);
231 guint32 gst_rtp_packet_rate_ctx_get_max_dropout (RTPPacketRateCtx *ctx, gint32 time_ms);
232 guint32 gst_rtp_packet_rate_ctx_get_max_misorder (RTPPacketRateCtx *ctx, gint32 time_ms);
233 
234 /**
235  * RTPSessionStats:
236  *
237  * Stats kept for a session and used to produce RTCP packet timeouts.
238  */
239 typedef struct {
240   guint         bandwidth;
241   guint         rtcp_bandwidth;
242   gdouble       sender_fraction;
243   gdouble       receiver_fraction;
244   gdouble       min_interval;
245   GstClockTime  bye_timeout;
246   guint         internal_sources;
247   guint         sender_sources;
248   guint         internal_sender_sources;
249   guint         active_sources;
250   guint         avg_rtcp_packet_size;
251   guint         bye_members;
252   guint         nacks_dropped;
253   guint         nacks_sent;
254   guint         nacks_received;
255 } RTPSessionStats;
256 
257 /**
258  * RTPTWCCStats:
259  *
260  * Stats kept for a session and used to produce TWCC stats.
261  */
262 typedef struct {
263   GArray       *packets;
264   GstClockTime window_size;
265   GstClockTime  last_local_ts;
266   GstClockTime  last_remote_ts;
267 
268   guint bitrate_sent;
269   guint bitrate_recv;
270   guint packets_sent;
271   guint packets_recv;
272   gfloat packet_loss_pct;
273   GstClockTimeDiff avg_delta_of_delta;
274   gfloat avg_delta_of_delta_change;
275 } RTPTWCCStats;
276 
277 
278 void           rtp_stats_init_defaults              (RTPSessionStats *stats);
279 
280 void           rtp_stats_set_bandwidths             (RTPSessionStats *stats,
281                                                      guint rtp_bw,
282                                                      gdouble rtcp_bw,
283                                                      guint rs, guint rr);
284 
285 GstClockTime   rtp_stats_calculate_rtcp_interval    (RTPSessionStats *stats, gboolean sender, GstRTPProfile profile, gboolean ptp, gboolean first);
286 GstClockTime   rtp_stats_add_rtcp_jitter            (RTPSessionStats *stats, GstClockTime interval);
287 GstClockTime   rtp_stats_calculate_bye_interval     (RTPSessionStats *stats);
288 gint64         rtp_stats_get_packets_lost           (const RTPSourceStats *stats);
289 
290 void           rtp_stats_set_min_interval           (RTPSessionStats *stats,
291                                                      gdouble min_interval);
292 
293 
294 gboolean __g_socket_address_equal (GSocketAddress *a, GSocketAddress *b);
295 gchar * __g_socket_address_to_string (GSocketAddress * addr);
296 
297 RTPTWCCStats * rtp_twcc_stats_new (void);
298 void rtp_twcc_stats_free (RTPTWCCStats * stats);
299 GstStructure * rtp_twcc_stats_process_packets (RTPTWCCStats * stats,
300     GArray * twcc_packets);
301 GstStructure * rtp_twcc_stats_get_packets_structure (GArray * twcc_packets);
302 
303 #endif /* __RTP_STATS_H__ */
304