1 /*
2  *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef MODULES_RTP_RTCP_INCLUDE_RTCP_STATISTICS_H_
12 #define MODULES_RTP_RTCP_INCLUDE_RTCP_STATISTICS_H_
13 
14 #include <stdint.h>
15 
16 #include "absl/strings/string_view.h"
17 
18 namespace webrtc {
19 
20 // Statistics for an RTCP channel
21 struct RtcpStatistics {
22   uint8_t fraction_lost = 0;
23   int32_t packets_lost = 0;  // Defined as a 24 bit signed integer in RTCP
24   uint32_t extended_highest_sequence_number = 0;
25   uint32_t jitter = 0;
26 };
27 
28 class RtcpStatisticsCallback {
29  public:
~RtcpStatisticsCallback()30   virtual ~RtcpStatisticsCallback() {}
31 
32   virtual void StatisticsUpdated(const RtcpStatistics& statistics,
33                                  uint32_t ssrc) = 0;
34 };
35 
36 // Statistics for RTCP packet types.
37 struct RtcpPacketTypeCounter {
RtcpPacketTypeCounterRtcpPacketTypeCounter38   RtcpPacketTypeCounter()
39       : first_packet_time_ms(-1),
40         nack_packets(0),
41         fir_packets(0),
42         pli_packets(0),
43         nack_requests(0),
44         unique_nack_requests(0) {}
45 
AddRtcpPacketTypeCounter46   void Add(const RtcpPacketTypeCounter& other) {
47     nack_packets += other.nack_packets;
48     fir_packets += other.fir_packets;
49     pli_packets += other.pli_packets;
50     nack_requests += other.nack_requests;
51     unique_nack_requests += other.unique_nack_requests;
52     if (other.first_packet_time_ms != -1 &&
53         (other.first_packet_time_ms < first_packet_time_ms ||
54          first_packet_time_ms == -1)) {
55       // Use oldest time.
56       first_packet_time_ms = other.first_packet_time_ms;
57     }
58   }
59 
SubtractRtcpPacketTypeCounter60   void Subtract(const RtcpPacketTypeCounter& other) {
61     nack_packets -= other.nack_packets;
62     fir_packets -= other.fir_packets;
63     pli_packets -= other.pli_packets;
64     nack_requests -= other.nack_requests;
65     unique_nack_requests -= other.unique_nack_requests;
66     if (other.first_packet_time_ms != -1 &&
67         (other.first_packet_time_ms > first_packet_time_ms ||
68          first_packet_time_ms == -1)) {
69       // Use youngest time.
70       first_packet_time_ms = other.first_packet_time_ms;
71     }
72   }
73 
TimeSinceFirstPacketInMsRtcpPacketTypeCounter74   int64_t TimeSinceFirstPacketInMs(int64_t now_ms) const {
75     return (first_packet_time_ms == -1) ? -1 : (now_ms - first_packet_time_ms);
76   }
77 
UniqueNackRequestsInPercentRtcpPacketTypeCounter78   int UniqueNackRequestsInPercent() const {
79     if (nack_requests == 0) {
80       return 0;
81     }
82     return static_cast<int>((unique_nack_requests * 100.0f / nack_requests) +
83                             0.5f);
84   }
85 
86   int64_t first_packet_time_ms;   // Time when first packet is sent/received.
87   uint32_t nack_packets;          // Number of RTCP NACK packets.
88   uint32_t fir_packets;           // Number of RTCP FIR packets.
89   uint32_t pli_packets;           // Number of RTCP PLI packets.
90   uint32_t nack_requests;         // Number of NACKed RTP packets.
91   uint32_t unique_nack_requests;  // Number of unique NACKed RTP packets.
92 };
93 
94 class RtcpPacketTypeCounterObserver {
95  public:
~RtcpPacketTypeCounterObserver()96   virtual ~RtcpPacketTypeCounterObserver() {}
97   virtual void RtcpPacketTypesCounterUpdated(
98       uint32_t ssrc,
99       const RtcpPacketTypeCounter& packet_counter) = 0;
100 };
101 
102 // Invoked for each cname passed in RTCP SDES blocks.
103 class RtcpCnameCallback {
104  public:
105   virtual ~RtcpCnameCallback() = default;
106 
107   virtual void OnCname(uint32_t ssrc, absl::string_view cname) = 0;
108 };
109 
110 }  // namespace webrtc
111 #endif  // MODULES_RTP_RTCP_INCLUDE_RTCP_STATISTICS_H_
112