1 /* 2 * Copyright (c) 2017 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_BWE_UPDATE_DELAY_BASED_H_ 12 #define LOGGING_RTC_EVENT_LOG_EVENTS_RTC_EVENT_BWE_UPDATE_DELAY_BASED_H_ 13 14 #include <stdint.h> 15 16 #include <limits> 17 #include <memory> 18 #include <string> 19 #include <vector> 20 21 #include "absl/strings/string_view.h" 22 #include "api/network_state_predictor.h" 23 #include "api/rtc_event_log/rtc_event.h" 24 #include "api/units/timestamp.h" 25 #include "logging/rtc_event_log/events/rtc_event_definition.h" 26 27 namespace webrtc { 28 29 // Separate the event log encoding from the enum values. 30 // As long as the enum values are the same as the encodings, 31 // the two conversion functions can be compiled to (roughly) 32 // a range check each. 33 template <> 34 class RtcEventLogEnum<BandwidthUsage> { 35 static constexpr uint64_t kBwNormal = 0; 36 static constexpr uint64_t kBwUnderusing = 1; 37 static constexpr uint64_t kBwOverusing = 2; 38 39 public: Encode(BandwidthUsage x)40 static uint64_t Encode(BandwidthUsage x) { 41 switch (x) { 42 case BandwidthUsage::kBwNormal: 43 return kBwNormal; 44 case BandwidthUsage::kBwUnderusing: 45 return kBwUnderusing; 46 case BandwidthUsage::kBwOverusing: 47 return kBwOverusing; 48 case BandwidthUsage::kLast: 49 RTC_DCHECK_NOTREACHED(); 50 } 51 RTC_DCHECK_NOTREACHED(); 52 return std::numeric_limits<uint64_t>::max(); 53 } Decode(uint64_t x)54 static RtcEventLogParseStatusOr<BandwidthUsage> Decode(uint64_t x) { 55 switch (x) { 56 case kBwNormal: 57 return BandwidthUsage::kBwNormal; 58 case kBwUnderusing: 59 return BandwidthUsage::kBwUnderusing; 60 case kBwOverusing: 61 return BandwidthUsage::kBwOverusing; 62 } 63 return RtcEventLogParseStatus::Error("Failed to decode BandwidthUsage enum", 64 __FILE__, __LINE__); 65 } 66 }; 67 68 struct LoggedBweDelayBasedUpdate { 69 LoggedBweDelayBasedUpdate() = default; LoggedBweDelayBasedUpdateLoggedBweDelayBasedUpdate70 LoggedBweDelayBasedUpdate(Timestamp timestamp, 71 int32_t bitrate_bps, 72 BandwidthUsage detector_state) 73 : timestamp(timestamp), 74 bitrate_bps(bitrate_bps), 75 detector_state(detector_state) {} 76 log_time_usLoggedBweDelayBasedUpdate77 int64_t log_time_us() const { return timestamp.us(); } log_time_msLoggedBweDelayBasedUpdate78 int64_t log_time_ms() const { return timestamp.ms(); } log_timeLoggedBweDelayBasedUpdate79 Timestamp log_time() const { return timestamp; } 80 81 Timestamp timestamp = Timestamp::MinusInfinity(); 82 int32_t bitrate_bps; 83 BandwidthUsage detector_state; 84 }; 85 86 class RtcEventBweUpdateDelayBased final : public RtcEvent { 87 public: 88 static constexpr Type kType = Type::BweUpdateDelayBased; 89 90 RtcEventBweUpdateDelayBased(int32_t bitrate_bps, 91 BandwidthUsage detector_state); 92 ~RtcEventBweUpdateDelayBased() override; 93 GetType()94 Type GetType() const override { return kType; } IsConfigEvent()95 bool IsConfigEvent() const override { return false; } 96 97 std::unique_ptr<RtcEventBweUpdateDelayBased> Copy() const; 98 bitrate_bps()99 int32_t bitrate_bps() const { return bitrate_bps_; } detector_state()100 BandwidthUsage detector_state() const { return detector_state_; } 101 Encode(rtc::ArrayView<const RtcEvent * > batch)102 static std::string Encode(rtc::ArrayView<const RtcEvent*> batch) { 103 return RtcEventBweUpdateDelayBased::definition_.EncodeBatch(batch); 104 } 105 Parse(absl::string_view encoded_bytes,bool batched,std::vector<LoggedBweDelayBasedUpdate> & output)106 static RtcEventLogParseStatus Parse( 107 absl::string_view encoded_bytes, 108 bool batched, 109 std::vector<LoggedBweDelayBasedUpdate>& output) { 110 return RtcEventBweUpdateDelayBased::definition_.ParseBatch(encoded_bytes, 111 batched, output); 112 } 113 114 private: 115 RtcEventBweUpdateDelayBased(const RtcEventBweUpdateDelayBased& other); 116 117 const int32_t bitrate_bps_; 118 const BandwidthUsage detector_state_; 119 120 static constexpr RtcEventDefinition<RtcEventBweUpdateDelayBased, 121 LoggedBweDelayBasedUpdate, 122 int32_t, 123 BandwidthUsage> 124 definition_{ 125 {"BweDelayBased", RtcEventBweUpdateDelayBased::kType}, 126 {&RtcEventBweUpdateDelayBased::bitrate_bps_, 127 &LoggedBweDelayBasedUpdate::bitrate_bps, 128 {"bitrate_bps", /*id=*/1, FieldType::kVarInt, /*width=*/32}}, 129 {&RtcEventBweUpdateDelayBased::detector_state_, 130 &LoggedBweDelayBasedUpdate::detector_state, 131 {"detector_state", /*id=*/2, FieldType::kVarInt, /*width=*/64}}}; 132 }; 133 134 } // namespace webrtc 135 136 #endif // LOGGING_RTC_EVENT_LOG_EVENTS_RTC_EVENT_BWE_UPDATE_DELAY_BASED_H_ 137