Elad Alon | d95b0a2 | 2018-11-09 15:38:54 | [diff] [blame] | 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 | |
Björn Terelius | ac4e0b6 | 2024-05-28 12:17:46 | [diff] [blame^] | 13 | #include <cstdint> |
| 14 | |
Elad Alon | d95b0a2 | 2018-11-09 15:38:54 | [diff] [blame] | 15 | #include "rtc_base/checks.h" |
| 16 | |
| 17 | namespace webrtc { |
| 18 | namespace { |
| 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.) |
| 23 | constexpr uint32_t kPacketLossFractionRange = (1 << 14) - 1; // 0x3fff |
| 24 | constexpr float kPacketLossFractionRangeFloat = |
| 25 | static_cast<float>(kPacketLossFractionRange); |
| 26 | } // namespace |
| 27 | |
| 28 | uint32_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 | |
| 34 | bool 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 |