blob: e3e95f9aed8a9e52bb120af92023dddad02f7264 [file] [log] [blame]
asapersson@webrtc.org823c9b82015-01-08 07:50:56 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "video/report_block_stats.h"
asapersson@webrtc.org823c9b82015-01-08 07:50:56 +000012
Sebastian Jansson5ca90f52018-08-07 16:01:42 +020013#include <algorithm>
14
asapersson@webrtc.org823c9b82015-01-08 07:50:56 +000015namespace webrtc {
16
17namespace {
18int 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)) /
Yves Gerey665174f2018-06-19 15:03:05 +020024 num_sequence_numbers;
asapersson@webrtc.org823c9b82015-01-08 07:50:56 +000025}
26} // namespace
27
asapersson@webrtc.org823c9b82015-01-08 07:50:56 +000028// Helper class for rtcp statistics.
29ReportBlockStats::ReportBlockStats()
Yves Gerey665174f2018-06-19 15:03:05 +020030 : num_sequence_numbers_(0), num_lost_sequence_numbers_(0) {}
asapersson@webrtc.org823c9b82015-01-08 07:50:56 +000031
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020032ReportBlockStats::~ReportBlockStats() {}
33
Niels Möller77d3efc2019-08-01 15:09:51 +020034void ReportBlockStats::Store(uint32_t ssrc, const RtcpStatistics& rtcp_stats) {
35 Report report;
36 report.packets_lost = rtcp_stats.packets_lost;
37 report.extended_highest_sequence_number =
srte3e69e5c2017-08-09 06:13:45 -070038 rtcp_stats.extended_highest_sequence_number;
Niels Möller77d3efc2019-08-01 15:09:51 +020039 StoreAndAddPacketIncrement(ssrc, report);
asapersson@webrtc.org823c9b82015-01-08 07:50:56 +000040}
41
Niels Möller77d3efc2019-08-01 15:09:51 +020042void ReportBlockStats::StoreAndAddPacketIncrement(uint32_t ssrc,
43 const Report& report) {
asapersson@webrtc.org823c9b82015-01-08 07:50:56 +000044 // Get diff with previous report block.
Niels Möller77d3efc2019-08-01 15:09:51 +020045 const auto prev_report = prev_reports_.find(ssrc);
46 if (prev_report != prev_reports_.end()) {
47 int seq_num_diff = report.extended_highest_sequence_number -
48 prev_report->second.extended_highest_sequence_number;
49 int cum_loss_diff = report.packets_lost - prev_report->second.packets_lost;
asapersson@webrtc.org823c9b82015-01-08 07:50:56 +000050 if (seq_num_diff >= 0 && cum_loss_diff >= 0) {
asapersson@webrtc.org823c9b82015-01-08 07:50:56 +000051 // Update total number of packets/lost packets.
52 num_sequence_numbers_ += seq_num_diff;
53 num_lost_sequence_numbers_ += cum_loss_diff;
54 }
55 }
56 // Store current report block.
Niels Möller77d3efc2019-08-01 15:09:51 +020057 prev_reports_[ssrc] = report;
asapersson@webrtc.org823c9b82015-01-08 07:50:56 +000058}
59
60int ReportBlockStats::FractionLostInPercent() const {
asapersson@webrtc.orge7358ea2015-01-21 09:00:19 +000061 if (num_sequence_numbers_ == 0) {
62 return -1;
63 }
Yves Gerey665174f2018-06-19 15:03:05 +020064 return FractionLost(num_lost_sequence_numbers_, num_sequence_numbers_) * 100 /
65 255;
asapersson@webrtc.org823c9b82015-01-08 07:50:56 +000066}
67
68} // namespace webrtc