1 /*
2 * Copyright (c) 2021 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 #ifndef LOGGING_RTC_EVENT_LOG_EVENTS_RTC_EVENT_FIELD_EXTRACTION_H_
12 #define LOGGING_RTC_EVENT_LOG_EVENTS_RTC_EVENT_FIELD_EXTRACTION_H_
13
14 #include <string>
15 #include <vector>
16
17 #include "absl/types/optional.h"
18 #include "api/array_view.h"
19 #include "api/rtc_event_log/rtc_event.h"
20 #include "api/units/timestamp.h"
21 #include "logging/rtc_event_log/encoder/rtc_event_log_encoder_common.h"
22 #include "rtc_base/logging.h"
23
24 namespace webrtc_event_logging {
25 uint8_t UnsignedBitWidth(uint64_t max_magnitude);
26 uint8_t SignedBitWidth(uint64_t max_pos_magnitude, uint64_t max_neg_magnitude);
27 uint64_t MaxUnsignedValueOfBitWidth(uint64_t bit_width);
28 uint64_t UnsignedDelta(uint64_t previous, uint64_t current, uint64_t bit_mask);
29 } // namespace webrtc_event_logging
30
31 namespace webrtc {
32 template <typename T, std::enable_if_t<std::is_signed<T>::value, bool> = true>
EncodeAsUnsigned(T value)33 uint64_t EncodeAsUnsigned(T value) {
34 return webrtc_event_logging::ToUnsigned(value);
35 }
36
37 template <typename T, std::enable_if_t<std::is_unsigned<T>::value, bool> = true>
EncodeAsUnsigned(T value)38 uint64_t EncodeAsUnsigned(T value) {
39 return static_cast<uint64_t>(value);
40 }
41
42 template <typename T, std::enable_if_t<std::is_signed<T>::value, bool> = true>
DecodeFromUnsignedToType(uint64_t value)43 T DecodeFromUnsignedToType(uint64_t value) {
44 T signed_value = 0;
45 bool success = webrtc_event_logging::ToSigned<T>(value, &signed_value);
46 if (!success) {
47 RTC_LOG(LS_ERROR) << "Failed to convert " << value << "to signed type.";
48 // TODO(terelius): Propagate error?
49 }
50 return signed_value;
51 }
52
53 template <typename T, std::enable_if_t<std::is_unsigned<T>::value, bool> = true>
DecodeFromUnsignedToType(uint64_t value)54 T DecodeFromUnsignedToType(uint64_t value) {
55 // TODO(terelius): Check range?
56 return static_cast<T>(value);
57 }
58
59 // RtcEventLogEnum<T> defines a mapping between an enum T
60 // and the event log encodings. To log a new enum type T,
61 // specialize RtcEventLogEnum<T> and add static methods
62 // static uint64_t Encode(T x) {}
63 // static RtcEventLogParseStatusOr<T> Decode(uint64_t x) {}
64 template <typename T>
65 class RtcEventLogEnum {
66 static_assert(sizeof(T) != sizeof(T),
67 "Missing specialisation of RtcEventLogEnum for type");
68 };
69
70 // Represents a vector<optional<uint64_t>> optional_values
71 // as a bit-vector `position_mask` which identifies the positions
72 // of existing values, and a (potentially shorter)
73 // `vector<uint64_t> values` containing the actual values.
74 // The bit vector is constructed such that position_mask[i]
75 // is true iff optional_values[i] has a value, and `values.size()`
76 // is equal to the number of set bits in `position_mask`.
77 struct ValuesWithPositions {
78 std::vector<bool> position_mask;
79 std::vector<uint64_t> values;
80 };
81
82 } // namespace webrtc
83
84 #endif // LOGGING_RTC_EVENT_LOG_EVENTS_RTC_EVENT_FIELD_EXTRACTION_H_
85