xref: /aosp_15_r20/external/webrtc/video/report_block_stats.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2014 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 #include "video/report_block_stats.h"
12 
13 #include <algorithm>
14 
15 namespace webrtc {
16 
17 namespace {
FractionLost(uint32_t num_lost_sequence_numbers,uint32_t num_sequence_numbers)18 int FractionLost(uint32_t num_lost_sequence_numbers,
19                  uint32_t num_sequence_numbers) {
20   if (num_sequence_numbers == 0) {
21     return 0;
22   }
23   return ((num_lost_sequence_numbers * 255) + (num_sequence_numbers / 2)) /
24          num_sequence_numbers;
25 }
26 }  // namespace
27 
28 // Helper class for rtcp statistics.
ReportBlockStats()29 ReportBlockStats::ReportBlockStats()
30     : num_sequence_numbers_(0), num_lost_sequence_numbers_(0) {}
31 
~ReportBlockStats()32 ReportBlockStats::~ReportBlockStats() {}
33 
Store(uint32_t ssrc,int packets_lost,uint32_t extended_highest_sequence_number)34 void ReportBlockStats::Store(uint32_t ssrc,
35                              int packets_lost,
36                              uint32_t extended_highest_sequence_number) {
37   Report report;
38   report.packets_lost = packets_lost;
39   report.extended_highest_sequence_number = extended_highest_sequence_number;
40 
41   // Get diff with previous report block.
42   const auto prev_report = prev_reports_.find(ssrc);
43   if (prev_report != prev_reports_.end()) {
44     int seq_num_diff = report.extended_highest_sequence_number -
45                        prev_report->second.extended_highest_sequence_number;
46     int cum_loss_diff = report.packets_lost - prev_report->second.packets_lost;
47     if (seq_num_diff >= 0 && cum_loss_diff >= 0) {
48       // Update total number of packets/lost packets.
49       num_sequence_numbers_ += seq_num_diff;
50       num_lost_sequence_numbers_ += cum_loss_diff;
51     }
52   }
53   // Store current report block.
54   prev_reports_[ssrc] = report;
55 }
56 
FractionLostInPercent() const57 int ReportBlockStats::FractionLostInPercent() const {
58   if (num_sequence_numbers_ == 0) {
59     return -1;
60   }
61   return FractionLost(num_lost_sequence_numbers_, num_sequence_numbers_) * 100 /
62          255;
63 }
64 
65 }  // namespace webrtc
66