blob: 54900997e48ae7f747115533d3e2a2fa99b0be98 [file] [log] [blame]
Elad Alond95b0a22018-11-09 15:38:541/*
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
Björn Tereliusac4e0b62024-05-28 12:17:4613#include <cstdint>
14
Elad Alond95b0a22018-11-09 15:38:5415#include "rtc_base/checks.h"
16
17namespace webrtc {
18namespace {
19// We use 0x3fff because that gives decent precision (compared to the underlying
20// measurement producing the packet loss fraction) on the one hand, while
21// allowing us to use no more than 2 bytes in varint form on the other hand.
22// (We might also fixed-size encode using at most 14 bits.)
23constexpr uint32_t kPacketLossFractionRange = (1 << 14) - 1; // 0x3fff
24constexpr float kPacketLossFractionRangeFloat =
25 static_cast<float>(kPacketLossFractionRange);
26} // namespace
27
28uint32_t ConvertPacketLossFractionToProtoFormat(float packet_loss_fraction) {
29 RTC_DCHECK_GE(packet_loss_fraction, 0);
30 RTC_DCHECK_LE(packet_loss_fraction, 1);
31 return static_cast<uint32_t>(packet_loss_fraction * kPacketLossFractionRange);
32}
33
34bool ParsePacketLossFractionFromProtoFormat(uint32_t proto_packet_loss_fraction,
35 float* output) {
36 if (proto_packet_loss_fraction >= kPacketLossFractionRange) {
37 return false;
38 }
39 *output = proto_packet_loss_fraction / kPacketLossFractionRangeFloat;
40 return true;
41}
42} // namespace webrtc