asapersson@webrtc.org | 823c9b8 | 2015-01-08 07:50:56 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "video/report_block_stats.h" |
asapersson@webrtc.org | 823c9b8 | 2015-01-08 07:50:56 +0000 | [diff] [blame] | 12 | |
Sebastian Jansson | 5ca90f5 | 2018-08-07 16:01:42 +0200 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
asapersson@webrtc.org | 823c9b8 | 2015-01-08 07:50:56 +0000 | [diff] [blame] | 15 | namespace webrtc { |
| 16 | |
| 17 | namespace { |
| 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)) / |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 24 | num_sequence_numbers; |
asapersson@webrtc.org | 823c9b8 | 2015-01-08 07:50:56 +0000 | [diff] [blame] | 25 | } |
| 26 | } // namespace |
| 27 | |
asapersson@webrtc.org | 823c9b8 | 2015-01-08 07:50:56 +0000 | [diff] [blame] | 28 | // Helper class for rtcp statistics. |
| 29 | ReportBlockStats::ReportBlockStats() |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 30 | : num_sequence_numbers_(0), num_lost_sequence_numbers_(0) {} |
asapersson@webrtc.org | 823c9b8 | 2015-01-08 07:50:56 +0000 | [diff] [blame] | 31 | |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 32 | ReportBlockStats::~ReportBlockStats() {} |
| 33 | |
Niels Möller | 77d3efc | 2019-08-01 15:09:51 +0200 | [diff] [blame] | 34 | void 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 = |
srte | 3e69e5c | 2017-08-09 06:13:45 -0700 | [diff] [blame] | 38 | rtcp_stats.extended_highest_sequence_number; |
Niels Möller | 77d3efc | 2019-08-01 15:09:51 +0200 | [diff] [blame] | 39 | StoreAndAddPacketIncrement(ssrc, report); |
asapersson@webrtc.org | 823c9b8 | 2015-01-08 07:50:56 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Niels Möller | 77d3efc | 2019-08-01 15:09:51 +0200 | [diff] [blame] | 42 | void ReportBlockStats::StoreAndAddPacketIncrement(uint32_t ssrc, |
| 43 | const Report& report) { |
asapersson@webrtc.org | 823c9b8 | 2015-01-08 07:50:56 +0000 | [diff] [blame] | 44 | // Get diff with previous report block. |
Niels Möller | 77d3efc | 2019-08-01 15:09:51 +0200 | [diff] [blame] | 45 | 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.org | 823c9b8 | 2015-01-08 07:50:56 +0000 | [diff] [blame] | 50 | if (seq_num_diff >= 0 && cum_loss_diff >= 0) { |
asapersson@webrtc.org | 823c9b8 | 2015-01-08 07:50:56 +0000 | [diff] [blame] | 51 | // 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öller | 77d3efc | 2019-08-01 15:09:51 +0200 | [diff] [blame] | 57 | prev_reports_[ssrc] = report; |
asapersson@webrtc.org | 823c9b8 | 2015-01-08 07:50:56 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | int ReportBlockStats::FractionLostInPercent() const { |
asapersson@webrtc.org | e7358ea | 2015-01-21 09:00:19 +0000 | [diff] [blame] | 61 | if (num_sequence_numbers_ == 0) { |
| 62 | return -1; |
| 63 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 64 | return FractionLost(num_lost_sequence_numbers_, num_sequence_numbers_) * 100 / |
| 65 | 255; |
asapersson@webrtc.org | 823c9b8 | 2015-01-08 07:50:56 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | } // namespace webrtc |