xref: /aosp_15_r20/external/webrtc/video/report_block_stats.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #include "video/report_block_stats.h"
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include <algorithm>
14*d9f75844SAndroid Build Coastguard Worker 
15*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
16*d9f75844SAndroid Build Coastguard Worker 
17*d9f75844SAndroid Build Coastguard Worker namespace {
FractionLost(uint32_t num_lost_sequence_numbers,uint32_t num_sequence_numbers)18*d9f75844SAndroid Build Coastguard Worker int FractionLost(uint32_t num_lost_sequence_numbers,
19*d9f75844SAndroid Build Coastguard Worker                  uint32_t num_sequence_numbers) {
20*d9f75844SAndroid Build Coastguard Worker   if (num_sequence_numbers == 0) {
21*d9f75844SAndroid Build Coastguard Worker     return 0;
22*d9f75844SAndroid Build Coastguard Worker   }
23*d9f75844SAndroid Build Coastguard Worker   return ((num_lost_sequence_numbers * 255) + (num_sequence_numbers / 2)) /
24*d9f75844SAndroid Build Coastguard Worker          num_sequence_numbers;
25*d9f75844SAndroid Build Coastguard Worker }
26*d9f75844SAndroid Build Coastguard Worker }  // namespace
27*d9f75844SAndroid Build Coastguard Worker 
28*d9f75844SAndroid Build Coastguard Worker // Helper class for rtcp statistics.
ReportBlockStats()29*d9f75844SAndroid Build Coastguard Worker ReportBlockStats::ReportBlockStats()
30*d9f75844SAndroid Build Coastguard Worker     : num_sequence_numbers_(0), num_lost_sequence_numbers_(0) {}
31*d9f75844SAndroid Build Coastguard Worker 
~ReportBlockStats()32*d9f75844SAndroid Build Coastguard Worker ReportBlockStats::~ReportBlockStats() {}
33*d9f75844SAndroid Build Coastguard Worker 
Store(uint32_t ssrc,int packets_lost,uint32_t extended_highest_sequence_number)34*d9f75844SAndroid Build Coastguard Worker void ReportBlockStats::Store(uint32_t ssrc,
35*d9f75844SAndroid Build Coastguard Worker                              int packets_lost,
36*d9f75844SAndroid Build Coastguard Worker                              uint32_t extended_highest_sequence_number) {
37*d9f75844SAndroid Build Coastguard Worker   Report report;
38*d9f75844SAndroid Build Coastguard Worker   report.packets_lost = packets_lost;
39*d9f75844SAndroid Build Coastguard Worker   report.extended_highest_sequence_number = extended_highest_sequence_number;
40*d9f75844SAndroid Build Coastguard Worker 
41*d9f75844SAndroid Build Coastguard Worker   // Get diff with previous report block.
42*d9f75844SAndroid Build Coastguard Worker   const auto prev_report = prev_reports_.find(ssrc);
43*d9f75844SAndroid Build Coastguard Worker   if (prev_report != prev_reports_.end()) {
44*d9f75844SAndroid Build Coastguard Worker     int seq_num_diff = report.extended_highest_sequence_number -
45*d9f75844SAndroid Build Coastguard Worker                        prev_report->second.extended_highest_sequence_number;
46*d9f75844SAndroid Build Coastguard Worker     int cum_loss_diff = report.packets_lost - prev_report->second.packets_lost;
47*d9f75844SAndroid Build Coastguard Worker     if (seq_num_diff >= 0 && cum_loss_diff >= 0) {
48*d9f75844SAndroid Build Coastguard Worker       // Update total number of packets/lost packets.
49*d9f75844SAndroid Build Coastguard Worker       num_sequence_numbers_ += seq_num_diff;
50*d9f75844SAndroid Build Coastguard Worker       num_lost_sequence_numbers_ += cum_loss_diff;
51*d9f75844SAndroid Build Coastguard Worker     }
52*d9f75844SAndroid Build Coastguard Worker   }
53*d9f75844SAndroid Build Coastguard Worker   // Store current report block.
54*d9f75844SAndroid Build Coastguard Worker   prev_reports_[ssrc] = report;
55*d9f75844SAndroid Build Coastguard Worker }
56*d9f75844SAndroid Build Coastguard Worker 
FractionLostInPercent() const57*d9f75844SAndroid Build Coastguard Worker int ReportBlockStats::FractionLostInPercent() const {
58*d9f75844SAndroid Build Coastguard Worker   if (num_sequence_numbers_ == 0) {
59*d9f75844SAndroid Build Coastguard Worker     return -1;
60*d9f75844SAndroid Build Coastguard Worker   }
61*d9f75844SAndroid Build Coastguard Worker   return FractionLost(num_lost_sequence_numbers_, num_sequence_numbers_) * 100 /
62*d9f75844SAndroid Build Coastguard Worker          255;
63*d9f75844SAndroid Build Coastguard Worker }
64*d9f75844SAndroid Build Coastguard Worker 
65*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
66