MillicastSDK 2.5.0
Loading...
Searching...
No Matches
stats.h
Go to the documentation of this file.
1#ifndef MILLICAST_API_STATS_H
2#define MILLICAST_API_STATS_H
3
11#ifdef __cplusplus
12
13#include <map>
14#include <memory>
15#include <optional>
16#include <string>
17#include <vector>
18
19#include "diagnostics.h"
20#include "exports.h"
21
22namespace millicast {
23namespace rtcstats {
24
25enum class Type {
26 CODEC,
27 OUTBOUND_RTP,
28 INBOUND_RTP,
29 REMOTE_INBOUND_RTP,
30 REMOTE_OUTBOUND_RTP,
31 MEDIA_SOURCE,
32 AUDIO_SOURCE,
33 VIDEO_SOURCE,
34 RTC_ICE_CANDIDATE
35};
36
37struct Stats {
42 int64_t timestamp;
43
47 std::string id;
48
52 Type type;
53
58 template <typename T>
59 const T* cast_to() const {
60 return static_cast<const T*>(this);
61 }
62
63 virtual ~Stats() = default;
64};
65
66struct Codecs : public Stats {
67 enum class Type { ENCODE, DECODE };
68 static constexpr auto STYPE = ::millicast::rtcstats::Type::CODEC;
69
70 unsigned long payload_type;
71 std::string transport_id;
72 std::string mime_type;
73
74 std::optional<Type> codec_type;
75 std::optional<unsigned long> clock_rate;
76 std::optional<unsigned long> channels;
77 std::optional<std::string> sdp_fmtp_line;
78};
79
80struct RTCIceCandidatePair : public Stats {
81 static constexpr auto STYPE = Type::RTC_ICE_CANDIDATE;
82 std::optional<double> total_round_trip_time;
83 std::optional<double> current_round_trip_time;
84 std::optional<double> available_outgoing_bitrate;
85 std::optional<double> available_incoming_bitrate;
86};
87
88struct RtpStream : public Stats {
89 unsigned long ssrc;
90 std::string kind;
91 std::optional<std::string> transport_id;
92 std::optional<std::string> codec_id;
93};
94
95struct ReceivedRtpStream : public RtpStream {
96 static constexpr auto STYPE = Type::INBOUND_RTP;
97
98 std::optional<unsigned long long> packets_received;
99 std::optional<double> jitter;
100 std::optional<long long> packets_lost;
101 std::optional<unsigned long long> frames_dropped;
102};
103
104struct InboundRtpStream : public ReceivedRtpStream {
105 static constexpr auto STYPE = Type::INBOUND_RTP;
106
107 std::optional<std::string> remote_id;
108 std::optional<std::string> mid;
109 std::optional<std::string> track_identifier;
110 std::optional<unsigned long> frames_decoded;
111 std::optional<unsigned long> key_frames_decoded;
112 std::optional<unsigned long> nack_count;
113 std::optional<unsigned long long> fir_count;
114 std::optional<unsigned long long> pli_count;
115 std::optional<unsigned long> frames_received;
116 std::optional<unsigned long> frame_width;
117 std::optional<unsigned long> frame_height;
118 std::optional<double> frames_per_second;
119 std::optional<unsigned long long> bytes_received;
120 std::optional<unsigned long long> header_bytes_received;
121 std::optional<unsigned long long> last_packet_received_timestamp;
122 std::optional<double> audio_level;
123 std::optional<double> total_audio_energy;
124 std::optional<unsigned long long> fec_packets_received;
125 std::optional<unsigned long long> fec_packets_discarded;
126 std::optional<unsigned long long> total_samples_received;
127 std::optional<unsigned long long> concealed_samples;
128 std::optional<unsigned long long> silent_concealed_samples;
129 std::optional<unsigned long long> concealment_events;
130 std::optional<unsigned long long> inserted_samples_for_deceleration;
131 std::optional<unsigned long long> removed_samples_for_acceleration;
132 std::optional<unsigned long long> frames_assembled_from_multiple_packets;
133 std::optional<double> total_samples_duration;
134 std::optional<std::string> decoder_implementation;
135
136 std::optional<unsigned long long> retransmitted_packets_received;
137 std::optional<unsigned long long> retransmitted_bytes_received;
138
139 std::optional<double> jitter_buffer_delay;
140 std::optional<double> jitter_buffer_target_delay;
141 std::optional<double> jitter_buffer_minimum_delay;
142 std::optional<double> total_processing_delay;
143 std::optional<double> total_decode_time;
144 std::optional<double> total_assembly_time;
145 std::optional<double> total_inter_frame_delay;
146 std::optional<double> total_squared_inter_frame_delay;
147
148 std::optional<uint32_t> freeze_count;
149 std::optional<double> total_freezes_duration;
150
151 std::optional<uint32_t> pause_count;
152 std::optional<double> total_pauses_duration;
153
154 std::optional<uint64_t> jitter_buffer_emitted_count;
155 std::optional<uint64_t> estimated_playout_timestamp;
156 std::optional<double> min_playout_delay;
157};
158
159struct SentRtpStream : public RtpStream {
160 static constexpr auto STYPE = Type::OUTBOUND_RTP;
161 unsigned long packets_sent;
162 unsigned long retransmitted_packets_sent;
163 unsigned long long bytes_sent;
164 unsigned long retransmitted_bytes_sent;
165 unsigned long long header_bytes_sent;
166};
167
168struct OutboundRtpStream : public SentRtpStream {
169 static constexpr auto STYPE = Type::OUTBOUND_RTP;
170 std::optional<std::string> sender_id;
171 std::optional<std::string> remote_id;
172 std::optional<double> target_bitrate;
173 std::optional<unsigned long> frame_width;
174 std::optional<unsigned long> frame_height;
175 std::optional<double> frames_per_second;
176 std::optional<unsigned long> frames_sent;
177 std::optional<unsigned long> huge_frames_sent;
178 std::optional<unsigned long> total_packet_send_delay;
179 std::optional<unsigned long> frames_encoded;
180 std::optional<unsigned long> key_frames_encoded;
181 std::optional<unsigned long> total_encoded_bytes_target;
182 std::optional<unsigned long> nack_count;
183 std::optional<unsigned long> fir_count;
184 std::optional<unsigned long> pli_count;
185 std::optional<unsigned long> qp_sum;
186 std::optional<std::string> quality_limitation_durations;
187 std::optional<unsigned int> quality_limitation_resolution_changes;
188 std::optional<std::string> quality_limitation_reason;
189 std::optional<std::string> encoder_implementation;
190};
191
192struct RemoteOutboundRtpStream : public SentRtpStream {
193 static constexpr auto STYPE = Type::REMOTE_OUTBOUND_RTP;
194 std::string local_id;
195 double remote_timestamp;
196 unsigned long long reports_sent;
197 double round_trip_time;
198 unsigned long long round_trip_time_measurements;
199 double total_round_trip_time;
200};
201
202struct RemoteInboundRtpStream : public ReceivedRtpStream {
203 static constexpr auto STYPE = Type::REMOTE_INBOUND_RTP;
204 std::string local_id;
205 double round_trip_time;
206 double total_round_trip_time;
207 unsigned long round_trip_time_measurements;
208 double fraction_lost; // fraction packet loss
209};
210
211struct MediaSource : public Stats {
212 static constexpr auto STYPE = Type::MEDIA_SOURCE;
213 std::string track_identifier;
214 std::string kind;
215};
216
217struct VideoSource : public MediaSource {
218 static constexpr auto STYPE = Type::VIDEO_SOURCE;
219 std::optional<unsigned long> width;
220 std::optional<unsigned long> height;
221 std::optional<unsigned long> frames;
222 std::optional<double> frames_per_second;
223};
224
225struct AudioSource : public MediaSource {
226 static constexpr auto STYPE = Type::AUDIO_SOURCE;
227 std::optional<double> audio_level;
228 std::optional<double> total_audio_energy;
229 std::optional<double> total_samples_duration;
230 std::optional<double> echo_return_loss;
231 std::optional<double> echo_return_loss_enhancement;
232};
233} // namespace rtcstats
234
235using StatObject = std::unique_ptr<const rtcstats::Stats>;
236
237class MILLICAST_API StatsReport {
238 public:
239 class const_iterator { // NOLINT
240 std::map<std::string, StatObject>::const_iterator _it;
241
242 public:
243 const_iterator(std::map<std::string, StatObject>::const_iterator it)
244 : _it(it) {}
245 const_iterator(const_iterator&& other) = default;
246 ~const_iterator() = default;
247
248 const_iterator& operator++() {
249 ++_it;
250 return *this;
251 }
252 const_iterator& operator++(int) { return ++(*this); }
253 const rtcstats::Stats& operator*() const { return *_it->second.get(); }
254 const rtcstats::Stats* operator->() const { return _it->second.get(); }
255 bool operator==(const const_iterator& other) const {
256 return _it == other._it;
257 }
258 bool operator!=(const const_iterator& other) const {
259 return !(*this == other);
260 }
261 };
262
263 StatsReport();
264 virtual ~StatsReport() = default;
265
266 virtual void add_stats(StatObject stats) = 0;
267 [[nodiscard]] virtual const rtcstats::Stats* get(
268 const std::string& id) const = 0;
269 template <typename T>
270 const T* get_as(const std::string& id) const {
271 const rtcstats::Stats* stats = get(id);
272 if (!stats || stats->type != T::STYPE) {
273 return nullptr;
274 }
275
276 return stats->cast_to<const T>();
277 }
278
279 [[nodiscard]] virtual const_iterator begin() const = 0;
280 [[nodiscard]] virtual const_iterator end() const = 0;
281
282 template <typename T>
283 std::vector<const T*> get_stats_of_type() const {
284 std::vector<const T*> stats_of_type;
285 for (const auto& stats : *this) {
286 if (stats.type == T::STYPE) {
287 stats_of_type.push_back(stats.cast_to<const T>());
288 }
289 }
290 return stats_of_type;
291 }
296 virtual StreamDetails stream_details() const = 0;
297
298 // Removes the stats object from the report, returning ownership of it or null
299 // if there is no object with `id`.
300 virtual StatObject take(const std::string& id) = 0;
301
302 [[nodiscard]] virtual size_t size() const = 0;
303};
304
305} // namespace millicast
306
307#endif // __cplusplus
308#endif /* MILLICAST_API_STATS_H */
#define MILLICAST_API
Definition exports.h:51