xref: /aosp_15_r20/external/webrtc/logging/rtc_event_log/encoder/rtc_event_log_encoder_common.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2018 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 "logging/rtc_event_log/encoder/rtc_event_log_encoder_common.h"
12 
13 #include "rtc_base/checks.h"
14 
15 namespace webrtc {
16 namespace {
17 // We use 0x3fff because that gives decent precision (compared to the underlying
18 // measurement producing the packet loss fraction) on the one hand, while
19 // allowing us to use no more than 2 bytes in varint form on the other hand.
20 // (We might also fixed-size encode using at most 14 bits.)
21 constexpr uint32_t kPacketLossFractionRange = (1 << 14) - 1;  // 0x3fff
22 constexpr float kPacketLossFractionRangeFloat =
23     static_cast<float>(kPacketLossFractionRange);
24 }  // namespace
25 
ConvertPacketLossFractionToProtoFormat(float packet_loss_fraction)26 uint32_t ConvertPacketLossFractionToProtoFormat(float packet_loss_fraction) {
27   RTC_DCHECK_GE(packet_loss_fraction, 0);
28   RTC_DCHECK_LE(packet_loss_fraction, 1);
29   return static_cast<uint32_t>(packet_loss_fraction * kPacketLossFractionRange);
30 }
31 
ParsePacketLossFractionFromProtoFormat(uint32_t proto_packet_loss_fraction,float * output)32 bool ParsePacketLossFractionFromProtoFormat(uint32_t proto_packet_loss_fraction,
33                                             float* output) {
34   if (proto_packet_loss_fraction >= kPacketLossFractionRange) {
35     return false;
36   }
37   *output = proto_packet_loss_fraction / kPacketLossFractionRangeFloat;
38   return true;
39 }
40 }  // namespace webrtc
41