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)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)) / 24 num_sequence_numbers; 25 } 26 } // namespace 27 28 // Helper class for rtcp statistics. ReportBlockStats()29ReportBlockStats::ReportBlockStats() 30 : num_sequence_numbers_(0), num_lost_sequence_numbers_(0) {} 31 ~ReportBlockStats()32ReportBlockStats::~ReportBlockStats() {} 33 Store(uint32_t ssrc,const RtcpStatistics & rtcp_stats)34void 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 = 38 rtcp_stats.extended_highest_sequence_number; 39 StoreAndAddPacketIncrement(ssrc, report); 40 } 41 StoreAndAddPacketIncrement(uint32_t ssrc,const Report & report)42void ReportBlockStats::StoreAndAddPacketIncrement(uint32_t ssrc, 43 const Report& report) { 44 // Get diff with previous report block. 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; 50 if (seq_num_diff >= 0 && cum_loss_diff >= 0) { 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. 57 prev_reports_[ssrc] = report; 58 } 59 FractionLostInPercent() const60int ReportBlockStats::FractionLostInPercent() const { 61 if (num_sequence_numbers_ == 0) { 62 return -1; 63 } 64 return FractionLost(num_lost_sequence_numbers_, num_sequence_numbers_) * 100 / 65 255; 66 } 67 68 } // namespace webrtc 69