Debian Codegraph @

src/stats.c

raw symbols SCIP
1 /* librist. Copyright © 2019-2020 SipRadius LLC. All right reserved.
2 * Author: Daniele Lacamera <[email protected]>
3 * Author: Kuldeep Singh Dhaka <[email protected]>
4 * Author: Sergio Ammirata <[email protected]>
5 * Author: Gijs Peskens <[email protected]>
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 */
9
10 #include "rist-private.h"
11 #include "log-private.h"
12 #include "udp-private.h"
13 #include "proto/rist_time.h"
14 #include <string.h>
15 #include "cjson/cJSON.h"
16
17 /* Bump on any incompatible shape change to the stats JSON payloads. */
18 #define RIST_STATS_JSON_SCHEMA_VERSION 5
19
20 /* How often to repeat the no-retransmission-budget warning. */
21 #define RIST_BANDWIDTH_WARN_INTERVAL (30000ULL * RIST_CLOCK)
22
23 static double round_two_digits(double number)
24 {
25 long new_number = (long)(number * 100);
26 return (double)(new_number) / 100;
27 }
28
29 static const char *rist_profile_name(int profile)
30 {
31 switch (profile) {
32 case RIST_PROFILE_SIMPLE: return "simple";
33 case RIST_PROFILE_MAIN: return "main";
34 case RIST_PROFILE_ADVANCED: return "advanced";
35 default: return "unknown";
36 }
37 }
38
39 void rist_sender_flow_statistics(struct rist_sender *ctx)
40 {
41 cJSON *stats = cJSON_CreateObject();
42 cJSON_AddNumberToObject(stats, "schema_version", RIST_STATS_JSON_SCHEMA_VERSION);
43 cJSON *rist_sender_stats = cJSON_AddObjectToObject(stats, "sender-stats");
44 cJSON *peers = cJSON_AddArrayToObject(rist_sender_stats, "peers");
45
46 pthread_mutex_lock(&ctx->common.peerlist_lock);
47 for (size_t j = 0; j < ctx->peer_lst_len; j++) {
48 struct rist_peer *peer = ctx->peer_lst[j];
49 if (!peer->dead && peer->authenticated) {
50 cJSON *peer_obj = rist_sender_peer_statistics(peer);
51 cJSON_AddItemToArray(peers, peer_obj);
52 }
53 }
54 pthread_mutex_unlock(&ctx->common.peerlist_lock);
55
56 cJSON *udp_queue_obj = cJSON_AddObjectToObject(rist_sender_stats, "incoming_queue");
57 cJSON_AddNumberToObject(udp_queue_obj, "size", ctx->sender_queue_size);
58 cJSON_AddNumberToObject(udp_queue_obj, "bytesize", ctx->sender_queue_bytesize);
59 cJSON_AddNumberToObject(udp_queue_obj, "time_length", ctx->sender_queue_timelength);
60 if (ctx->sender_queue_timelength > 0)
61 cJSON_AddNumberToObject(udp_queue_obj, "packets_per_second", 1000 * ctx->sender_queue_size / ctx->sender_queue_timelength);
62 else
63 cJSON_AddNumberToObject(udp_queue_obj, "packets_per_second", 0);
64
65 if (ctx->split_mode != LIBRIST_SPLIT_MODE_OFF) {
66 cJSON *split_obj = cJSON_AddObjectToObject(rist_sender_stats, "split");
67 const char *mode_str = ctx->split_mode == LIBRIST_SPLIT_MODE_AUTO ? "auto" : "half";
68 cJSON_AddStringToObject(split_obj, "mode", mode_str);
69 cJSON_AddNumberToObject(split_obj, "pairs_emitted", (double)ctx->stats_pairs_emitted);
70 cJSON_AddNumberToObject(split_obj, "fallback_not_ts", (double)ctx->stats_split_fallback_not_ts);
71 }
72
73 char *stats_json = cJSON_PrintUnformatted(stats);
74 cJSON_Delete(stats);
75
76 if (ctx->sender_stats_callback != NULL)
77 ctx->sender_stats_callback(ctx->sender_stats_callback_argument, RIST_SENDER_STATS_VERSION, stats_json, (uint32_t)strlen(stats_json));
78
79 free(stats_json);
80 }
81
82 cJSON *rist_sender_peer_statistics(struct rist_peer *peer)
83 {
84 pthread_mutex_lock(&(get_cctx(peer)->stats_lock));
85 struct rist_stats *stats_container = malloc(sizeof(struct rist_stats));
86 stats_container->stats_type = RIST_STATS_SENDER_PEER;
87 stats_container->version = RIST_STATS_VERSION;
88
89 peer->stats_sender_total.received += peer->stats_sender_instant.received;
90
91 size_t retry_buf_size = rist_get_sender_retry_queue_size(peer->sender_ctx);
92
93 struct rist_bandwidth_estimation *cli_bw = &peer->bw;
94 struct rist_bandwidth_estimation *retry_bw = &peer->retry_bw;
95 struct rist_bandwidth_estimation *ts_nulls_bw = &peer->ts_nulls_bw;
96
97 // Refresh stats value just in case
98 rist_calculate_bitrate(0, cli_bw);
99 rist_calculate_bitrate(0, retry_bw);
100 rist_calculate_bitrate(0, ts_nulls_bw);
101
102 double Q = 100;
103 if (peer->stats_sender_instant.sent > 0)
104 {
105 Q = (double)((peer->stats_sender_instant.sent) * 100.0) /
106 (double)(peer->stats_sender_instant.sent + peer->stats_sender_instant.bloat_skip + peer->stats_sender_instant.bandwidth_skip + peer->stats_sender_instant.retrans_skip + peer->stats_sender_instant.retrans);
107 Q = round_two_digits(Q);
108 }
109
110 uint32_t time_left = 0;
111 if (peer->sender_ctx->cooldown_time > 0)
112 {
113 time_left = (uint32_t)(timestampNTP_u64() - peer->sender_ctx->cooldown_time) / 1000;
114 }
115
116 size_t bitrate = cli_bw->eight_times_bitrate_fast / 8;
117 size_t retry_bitrate = retry_bw->eight_times_bitrate_fast / 8;
118 size_t ts_nulls_bitrate = ts_nulls_bw->eight_times_bitrate_fast / 8;
119 double avg_rtt = ((double)peer->eight_times_rtt / 8);
120
121 struct rist_common_ctx *cctx = get_cctx(peer);
122
123 if (peer->is_data
124 && rist_retransmit_budget_starved(bitrate,
125 (size_t)peer->config.recovery_maxbitrate * 1000)) {
126 uint64_t now = timestampNTP_u64();
127 if (rist_bandwidth_warn_due(peer->bandwidth_warn_ts,
128 RIST_BANDWIDTH_WARN_INTERVAL, now)) {
129 peer->bandwidth_warn_ts = now;
130 rist_log_priv(cctx, RIST_LOG_WARN,
131 "Peer #%"PRIu32": no retransmission budget. Payload alone is %zu kbps "
132 "against a %u kbps bandwidth ceiling, and that ceiling covers payload "
133 "plus retransmissions, so every NACK is refused (%"PRIu32" this interval) "
134 "and lost packets are never recovered. Raise ?bandwidth= above the "
135 "payload rate, leaving headroom for recovery.\n",
136 peer->adv_peer_id, bitrate / 1000, peer->config.recovery_maxbitrate,
137 peer->stats_sender_instant.bandwidth_skip);
138 }
139 }
140
141 cJSON *peer_obj = cJSON_CreateObject();
142 cJSON_AddNumberToObject(peer_obj, "flow_id", peer->adv_flow_id);
143 cJSON_AddNumberToObject(peer_obj, "id", peer->adv_peer_id);
144 cJSON_AddStringToObject(peer_obj, "cname", peer->receiver_name);
145 cJSON_AddStringToObject(peer_obj, "type", peer->is_data ? "data" : "rtcp");
146 cJSON_AddNumberToObject(peer_obj, "profile", cctx->profile);
147 cJSON_AddStringToObject(peer_obj, "profile_name", rist_profile_name(cctx->profile));
148 cJSON_AddBoolToObject(peer_obj, "advanced_active", peer->is_advanced ? 1 : 0);
149 if (peer->miface[0])
150 cJSON_AddStringToObject(peer_obj, "miface", peer->miface);
151 cJSON *json_stats = cJSON_AddObjectToObject(peer_obj, "stats");
152 cJSON_AddNumberToObject(json_stats, "quality", Q);
153 cJSON_AddNumberToObject(json_stats, "sent", (double)peer->stats_sender_instant.sent);
154 cJSON_AddNumberToObject(json_stats, "ts_null", (double)peer->stats_sender_instant.ts_null);
155 cJSON_AddNumberToObject(json_stats, "received", (double)peer->stats_sender_instant.received);
156 cJSON_AddNumberToObject(json_stats, "retransmitted", (double)peer->stats_sender_instant.retrans);
157 cJSON_AddNumberToObject(json_stats, "sent_bytes", (double)peer->stats_sender_instant.sent_bytes);
158 cJSON_AddNumberToObject(json_stats, "retransmitted_bytes", (double)peer->stats_sender_instant.retransmitted_bytes);
159 cJSON_AddNumberToObject(json_stats, "bandwidth", (double)bitrate);
160 cJSON_AddNumberToObject(json_stats, "retry_bandwidth", (double)retry_bitrate);
161 cJSON_AddNumberToObject(json_stats, "ts_nulls_bandwidth", (double)ts_nulls_bitrate);
162 cJSON_AddNumberToObject(json_stats, "bandwidth_skipped", (double)peer->stats_sender_instant.bandwidth_skip);
163 cJSON_AddNumberToObject(json_stats, "bloat_skipped", (double)peer->stats_sender_instant.bloat_skip);
164 cJSON_AddNumberToObject(json_stats, "retransmit_skipped", (double)peer->stats_sender_instant.retrans_skip);
165 cJSON_AddNumberToObject(json_stats, "rtt", (double)peer->last_rtt / RIST_CLOCK);
166 cJSON_AddNumberToObject(json_stats, "avg_rtt", (double)avg_rtt / RIST_CLOCK);
167 cJSON_AddNumberToObject(json_stats, "retry_buffer_size", (double)retry_buf_size);
168 cJSON_AddNumberToObject(json_stats, "cooldown_time", (double)time_left);
169 cJSON_AddBoolToObject(json_stats, "rtt_muted", peer->rtt_muted ? 1 : 0);
170 cJSON_AddNumberToObject(json_stats, "rtt_mute_events", (double)peer->rtt_mute_count);
171 cJSON *stats = cJSON_CreateObject();
172 cJSON_AddNumberToObject(stats, "schema_version", RIST_STATS_JSON_SCHEMA_VERSION);
173 cJSON *rist_sender_stats = cJSON_AddObjectToObject(stats, "sender-stats");
174 cJSON *peers = cJSON_AddArrayToObject(rist_sender_stats, "peers");
175 cJSON_AddItemToArray(peers, cJSON_Duplicate(peer_obj, true));
176 char *stats_string = cJSON_PrintUnformatted(stats);
177 cJSON_Delete(stats);
178
179 stats_container->stats_json = stats_string;
180 stats_container->json_size = (uint32_t)strlen(stats_string);
181 strncpy(stats_container->stats.sender_peer.cname,
182 peer->receiver_name,
183 sizeof(stats_container->stats.sender_peer.cname) - 1);
184 stats_container->stats.sender_peer.cname[sizeof(stats_container->stats.sender_peer.cname) - 1] = '\0';
185 stats_container->stats.sender_peer.peer_id = peer->adv_peer_id;
186 stats_container->stats.sender_peer.bandwidth = bitrate;
187 stats_container->stats.sender_peer.retry_bandwidth = retry_bitrate;
188 //stats_container->stats.sender_peer.ts_nulls_bandwidth = ts_nulls_bitrate;
189 stats_container->stats.sender_peer.sent = peer->stats_sender_instant.sent;
190 stats_container->stats.sender_peer.received = peer->stats_sender_instant.received;
191 stats_container->stats.sender_peer.retransmitted = peer->stats_sender_instant.retrans;
192 stats_container->stats.sender_peer.quality = Q;
193 stats_container->stats.sender_peer.rtt = avg_rtt / RIST_CLOCK;
194 stats_container->stats.sender_peer.sent_bytes = peer->stats_sender_instant.sent_bytes;
195 stats_container->stats.sender_peer.retransmitted_bytes = peer->stats_sender_instant.retransmitted_bytes;
196 stats_container->stats.sender_peer.profile = (uint8_t)cctx->profile;
197 stats_container->stats.sender_peer.advanced_active = peer->is_advanced ? 1 : 0;
198 stats_container->stats.sender_peer.rtt_muted = peer->rtt_muted ? 1 : 0;
199 stats_container->stats.sender_peer.rtt_mute_events = peer->rtt_mute_count;
200
201 if (cctx->stats_callback != NULL)
202 cctx->stats_callback(cctx->stats_callback_argument, stats_container);
203 else
204 rist_stats_free(stats_container);
205
206 memset(&peer->stats_sender_instant, 0, sizeof(peer->stats_sender_instant));
207 pthread_mutex_unlock(&(get_cctx(peer)->stats_lock));
208
209 return peer_obj;
210 }
211
212 void rist_receiver_flow_statistics(struct rist_receiver *ctx, struct rist_flow *flow)
213 {
214 if (!flow)
215 return;
216 pthread_mutex_lock(&ctx->common.stats_lock);
217 //Log errors that used to be packet
218 if (flow->stats_instant.dropped_full)
219 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Dropped %u packets due to buffers being full\n", flow->stats_instant.dropped_full );
220 if (flow->stats_instant.dropped_late)
221 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Dropped %u late packets\n", flow->stats_instant.dropped_late);
222 if (flow->stats_instant.lost)
223 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Lost %u packets\n", flow->stats_instant.lost);
224
225 struct rist_stats *stats_container = malloc(sizeof(struct rist_stats));
226 stats_container->stats_type = RIST_STATS_RECEIVER_FLOW;
227 stats_container->version = RIST_STATS_VERSION;
228
229 if (flow->stats_instant.avg_count)
230 {
231 flow->stats_instant.cur_ips = (flow->stats_instant.total_ips / flow->stats_instant.avg_count);
232 }
233
234 stats_container->stats.receiver_flow.peers = calloc(flow->peer_lst_len, sizeof(struct rist_stats_receiver_peer));
235
236 cJSON *stats = cJSON_CreateObject();
237 cJSON_AddNumberToObject(stats, "schema_version", RIST_STATS_JSON_SCHEMA_VERSION);
238 cJSON *stats_obj = cJSON_AddObjectToObject(stats, "receiver-stats");
239 cJSON *flow_obj = cJSON_AddObjectToObject(stats_obj, "flowinstant");
240 cJSON_AddNumberToObject(flow_obj, "flow_id", flow->flow_id);
241 cJSON_AddNumberToObject(flow_obj, "dead", flow->dead);
242 cJSON_AddNumberToObject(flow_obj, "profile", ctx->common.profile);
243 cJSON_AddStringToObject(flow_obj, "profile_name", rist_profile_name(ctx->common.profile));
244 cJSON_AddNumberToObject(flow_obj, "seq_bits", flow->short_seq ? 16 : 32);
245 cJSON_AddBoolToObject(flow_obj, "advanced_active",
246 (ctx->common.profile >= RIST_PROFILE_ADVANCED && !flow->short_seq) ? 1 : 0);
247 cJSON *json_stats = cJSON_AddObjectToObject(flow_obj, "stats");
248 cJSON *peers = cJSON_AddArrayToObject(flow_obj, "peers");
249 uint32_t flow_rtt = 0;
250 uint32_t flow_sent_instant = 0;
251 for (size_t i = 0; i < flow->peer_lst_len; i++)
252 {
253 struct rist_peer *peer = flow->peer_lst[i];
254 if (!peer->is_data && peer->peer_data)
255 peer = peer->peer_data;
256 double avg_rtt = ((double)peer->eight_times_rtt / 8);
257
258 size_t bitrate = peer->bw.eight_times_bitrate_fast / 8;
259 size_t avg_bitrate = peer->bw.eight_times_bitrate / 8;
260 flow_sent_instant += peer->stats_receiver_instant.sent_rtcp;
261 flow_rtt += (uint32_t)(peer->eight_times_rtt / 8);
262
263 cJSON *peer_obj = cJSON_CreateObject();
264 cJSON_AddNumberToObject(peer_obj, "id", peer->adv_peer_id);
265 cJSON_AddNumberToObject(peer_obj, "dead", peer->dead);
266 /* Listener-mode children have url=NULL; the configured URL
267 * lives on the parent peer. */
268 const char *peer_url_str = peer->url;
269 if ((peer_url_str == NULL || peer_url_str[0] == '\0') && peer->parent && peer->parent->url) {
270 peer_url_str = peer->parent->url;
271 }
272 cJSON_AddStringToObject(peer_obj, "url", peer_url_str ? peer_url_str : "");
273 cJSON *peer_stats = cJSON_AddObjectToObject(peer_obj, "stats");
274 cJSON_AddNumberToObject(peer_stats, "received_data", (double)peer->stats_receiver_instant.received);
275 cJSON_AddNumberToObject(peer_stats, "received_bytes", (double)peer->stats_receiver_instant.received_bytes);
276 cJSON_AddNumberToObject(peer_stats, "ts_null", (double)peer->stats_receiver_instant.ts_null);
277 cJSON_AddNumberToObject(peer_stats, "received_rtcp", (double)peer->stats_receiver_instant.received_rtcp);
278 cJSON_AddNumberToObject(peer_stats, "sent_rtcp", (double)peer->stats_receiver_instant.sent_rtcp);
279 cJSON_AddNumberToObject(peer_stats, "rtt", (double)peer->last_rtt / RIST_CLOCK);
280 cJSON_AddNumberToObject(peer_stats, "avg_rtt", (double)avg_rtt / RIST_CLOCK);
281 cJSON_AddNumberToObject(peer_stats, "bitrate", (double)bitrate);
282 cJSON_AddNumberToObject(peer_stats, "avg_bitrate", (double)avg_bitrate);
283 cJSON_AddItemToArray(peers, peer_obj);
284
285 stats_container->stats.receiver_flow.peers[i].peer_id = peer->adv_peer_id;
286 stats_container->stats.receiver_flow.peers[i].received_data = peer->stats_receiver_instant.received;
287 stats_container->stats.receiver_flow.peers[i].received_bytes = peer->stats_receiver_instant.received_bytes;
288 stats_container->stats.receiver_flow.peers[i].received_rtcp = peer->stats_receiver_instant.received_rtcp;
289 stats_container->stats.receiver_flow.peers[i].sent_rtcp = peer->stats_receiver_instant.sent_rtcp;
290 stats_container->stats.receiver_flow.peers[i].rtt = peer->last_rtt / RIST_CLOCK;
291 stats_container->stats.receiver_flow.peers[i].avg_rtt = avg_rtt / RIST_CLOCK;
292 stats_container->stats.receiver_flow.peers[i].bandwidth = bitrate;
293 stats_container->stats.receiver_flow.peers[i].avg_bandwidth = avg_bitrate;
294
295 // Clear peer instant stats
296 memset(&peer->stats_receiver_instant, 0, sizeof(peer->stats_receiver_instant));
297 }
298
299 flow->stats_instant.recovered_average = (flow->stats_instant.recovered_sum * 100) - flow->stats_instant.recovered;
300 flow->stats_instant.recovered_slope = flow->stats_instant.recovered_3nack - flow->stats_instant.recovered_0nack;
301 if ((int32_t)(flow->stats_instant.recovered_1nack - flow->stats_instant.recovered_0nack) > 0 &&
302 flow->stats_instant.recovered_1nack != 0 && flow->stats_instant.recovered_0nack != 0)
303 {
304 flow->stats_instant.recovered_slope_inverted++;
305 }
306
307 if ((int32_t)(flow->stats_instant.recovered_2nack - flow->stats_instant.recovered_1nack) > 0 &&
308 flow->stats_instant.recovered_2nack != 0 && flow->stats_instant.recovered_1nack != 0)
309 {
310 flow->stats_instant.recovered_slope_inverted++;
311 }
312
313 if ((int32_t)(flow->stats_instant.recovered_3nack - flow->stats_instant.recovered_2nack) > 0 &&
314 flow->stats_instant.recovered_3nack != 0 && flow->stats_instant.recovered_2nack != 0)
315 {
316 flow->stats_instant.recovered_slope_inverted++;
317 }
318
319 double Q = 100;
320 if (flow->stats_instant.received > 0)
321 {
322 Q = (double)((flow->stats_instant.received)*100.0) /
323 (double)(flow->stats_instant.received + flow->stats_instant.missing);
324 Q = round_two_digits(Q);
325 }
326
327 // This last one should trigger buffer protection immediately
328 if ((flow->missing_counter == 0 || flow->stats_instant.recovered == 0 ||
329 (flow->stats_instant.recovered * 10) < flow->stats_instant.missing) &&
330 flow->stats_instant.received > 10 &&
331 flow->stats_instant.received < flow->stats_instant.missing)
332 {
333 rist_log_priv(&ctx->common, RIST_LOG_INFO, "\tThe flow link is dead %" PRIu32 " > %" PRIu64 ", deleting all missing queue elements!\n",
334 flow->stats_instant.missing, flow->stats_instant.received);
335 /* Delete all missing queue elements (if any) */
336 rist_flush_missing_flow_queue(flow);
337 }
338
339 uint64_t avg_buffer_duration = 0;
340 if (flow->stats_instant.buffer_duration_count > 0)
341 {
342
343 for (size_t i = 0; i < flow->stats_instant.buffer_duration_count; i++)
344 {
345 avg_buffer_duration += flow->stats_instant.buffer_duration[i];
346 }
347 avg_buffer_duration /= flow->stats_instant.buffer_duration_count;
348 flow->stats_instant.buffer_duration_count = 0;
349 }
350 cJSON_AddNumberToObject(json_stats, "quality", Q);
351 cJSON_AddNumberToObject(json_stats, "received", (double)flow->stats_instant.received);
352 cJSON_AddNumberToObject(json_stats, "received_bytes", (double)flow->stats_instant.received_bytes);
353 cJSON_AddNumberToObject(json_stats, "dropped_late", (double)flow->stats_instant.dropped_late);
354 cJSON_AddNumberToObject(json_stats, "dropped_full", (double)flow->stats_instant.dropped_full);
355 cJSON_AddNumberToObject(json_stats, "missing", (double)flow->stats_instant.missing);
356 cJSON_AddNumberToObject(json_stats, "recovered_total", (double)flow->stats_instant.recovered);
357 cJSON_AddNumberToObject(json_stats, "reordered", (double)flow->stats_instant.reordered);
358 cJSON_AddNumberToObject(json_stats, "retries", (double)flow->stats_instant.retries);
359 cJSON_AddNumberToObject(json_stats, "recovered_one_nack", (double)flow->stats_instant.recovered_0nack);
360 cJSON_AddNumberToObject(json_stats, "recovered_two_nacks", (double)flow->stats_instant.recovered_1nack);
361 cJSON_AddNumberToObject(json_stats, "recovered_three_nacks", (double)flow->stats_instant.recovered_2nack);
362 cJSON_AddNumberToObject(json_stats, "recovered_four_nacks", (double)flow->stats_instant.recovered_3nack);
363 cJSON_AddNumberToObject(json_stats, "recovered_more_nacks", (double)flow->stats_instant.recovered_morenack);
364 cJSON_AddNumberToObject(json_stats, "lost", (double)flow->stats_instant.lost);
365 cJSON_AddNumberToObject(json_stats, "avg_buffer_time", (double)avg_buffer_duration);
366 cJSON_AddNumberToObject(json_stats, "duplicates", (double)flow->stats_instant.dupe);
367 cJSON_AddNumberToObject(json_stats, "missing_queue", (double)flow->missing_counter);
368 cJSON_AddNumberToObject(json_stats, "missing_queue_max", (double)flow->missing_counter_max);
369 cJSON_AddNumberToObject(json_stats, "min_inter_packet_spacing", (double)flow->stats_instant.min_ips);
370 cJSON_AddNumberToObject(json_stats, "cur_inter_packet_spacing", (double)flow->stats_instant.cur_ips);
371 cJSON_AddNumberToObject(json_stats, "max_inter_packet_spacing", (double)flow->stats_instant.max_ips);
372 /* Decay flow bitrate counters when no traffic has arrived since the last tick. */
373 rist_refresh_flow_bitrate(&flow->bw);
374 rist_refresh_flow_bitrate(&flow->bw_retries);
375 rist_refresh_flow_bitrate(&flow->bw_rejected);
376 rist_refresh_flow_bitrate(&flow->bw_tsnull);
377 cJSON_AddNumberToObject(json_stats, "bitrate", (double)flow->bw.bitrate);
378 cJSON_AddNumberToObject(json_stats, "bitrate_retries", (double)flow->bw_retries.bitrate);
379 cJSON_AddNumberToObject(json_stats, "bitrate_rejected", (double)flow->bw_rejected.bitrate);
380 cJSON_AddNumberToObject(json_stats, "bitrate_ts_nulls", (double)flow->bw_tsnull.bitrate);
381 cJSON_AddNumberToObject(json_stats, "bitrate_payload", (double)flow->bw.bitrate + (double)flow->bw_tsnull.bitrate);
382
383 if (ctx->merge_mode != LIBRIST_MERGE_MODE_OFF) {
384 cJSON *merge_obj = cJSON_AddObjectToObject(flow_obj, "merge");
385 const char *mode_str = ctx->merge_mode == LIBRIST_MERGE_MODE_AUTO ? "auto" : "pairs";
386 cJSON_AddStringToObject(merge_obj, "mode", mode_str);
387 cJSON_AddNumberToObject(merge_obj, "pairs_merged", (double)ctx->stats_pairs_merged);
388 cJSON_AddNumberToObject(merge_obj, "orphan_first_delivered", (double)ctx->stats_orphan_first_delivered);
389 cJSON_AddNumberToObject(merge_obj, "orphan_last_delivered", (double)ctx->stats_orphan_last_delivered);
390 }
391
392 char *stats_string = cJSON_PrintUnformatted(stats);
393 cJSON_Delete(stats);
394
395 stats_container->stats_json = stats_string;
396 stats_container->json_size = (uint32_t)strlen(stats_string);
397
398 stats_container->stats.receiver_flow.peer_count = (uint32_t)flow->peer_lst_len;
399 // TODO: populate stats_receiver_flow->cname
400 stats_container->stats.receiver_flow.cname[0] = '\0';
401 stats_container->stats.receiver_flow.flow_id = flow->flow_id;
402 stats_container->stats.receiver_flow.status = flow->dead;
403 stats_container->stats.receiver_flow.bandwidth = flow->bw.bitrate;
404 stats_container->stats.receiver_flow.retry_bandwidth = flow->bw_retries.bitrate;
405 //stats_container->stats.receiver_flow.ts_nulls_bandwidth = flow->bw_tsnull.bitrate;
406 stats_container->stats.receiver_flow.sent = flow->peer_lst_len ? flow_sent_instant / flow->peer_lst_len : 0;
407 stats_container->stats.receiver_flow.received = flow->stats_instant.received;
408 stats_container->stats.receiver_flow.received_bytes = flow->stats_instant.received_bytes;
409 stats_container->stats.receiver_flow.missing = flow->stats_instant.missing;
410 stats_container->stats.receiver_flow.reordered = flow->stats_instant.reordered;
411 stats_container->stats.receiver_flow.recovered = flow->stats_instant.recovered;
412 stats_container->stats.receiver_flow.recovered_one_retry = flow->stats_instant.recovered_0nack;
413 stats_container->stats.receiver_flow.lost = flow->stats_instant.lost;
414 stats_container->stats.receiver_flow.quality = Q;
415 stats_container->stats.receiver_flow.min_inter_packet_spacing = flow->stats_instant.min_ips;
416 stats_container->stats.receiver_flow.cur_inter_packet_spacing = flow->stats_instant.cur_ips;
417 stats_container->stats.receiver_flow.max_inter_packet_spacing = flow->stats_instant.max_ips;
418 stats_container->stats.receiver_flow.rtt = flow->peer_lst_len ? (flow_rtt / flow->peer_lst_len)/RIST_CLOCK : 0;
419 stats_container->stats.receiver_flow.avg_buffer_time = avg_buffer_duration;
420 stats_container->stats.receiver_flow.profile = (uint8_t)ctx->common.profile;
421 stats_container->stats.receiver_flow.seq_bits = flow->short_seq ? 16 : 32;
422 stats_container->stats.receiver_flow.advanced_active =
423 (ctx->common.profile >= RIST_PROFILE_ADVANCED && !flow->short_seq) ? 1 : 0;
424
425 /* CALLBACK CALL */
426 if (ctx->common.stats_callback != NULL)
427 ctx->common.stats_callback(ctx->common.stats_callback_argument, stats_container);
428 else
429 rist_stats_free(stats_container);
430
431 memset(&flow->stats_instant, 0, sizeof(flow->stats_instant));
432 flow->stats_instant.min_ips = 0xFFFFFFFFFFFFFFFFULL;
433 pthread_mutex_unlock(&ctx->common.stats_lock);
434 }