1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/*
* Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/rtp_rtcp/source/rtcp_packet/remote_estimate.h"
#include <cstddef>
#include <cstdint>
#include <functional>
#include <utility>
#include <vector>
#include "api/array_view.h"
#include "api/transport/network_types.h"
#include "api/units/data_rate.h"
#include "modules/rtp_rtcp/source/byte_io.h"
#include "modules/rtp_rtcp/source/rtcp_packet/app.h"
#include "rtc_base/buffer.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
namespace webrtc {
namespace rtcp {
namespace {
static constexpr int kFieldValueSize = 3;
static constexpr int kFieldSize = 1 + kFieldValueSize;
static constexpr DataRate kDataRateResolution = DataRate::KilobitsPerSec(1);
constexpr int64_t kMaxEncoded = (1 << (kFieldValueSize * 8)) - 1;
class DataRateSerializer {
public:
DataRateSerializer(
uint8_t id,
std::function<DataRate*(NetworkStateEstimate*)> field_getter)
: id_(id), field_getter_(field_getter) {}
uint8_t id() const { return id_; }
void Read(const uint8_t* src, NetworkStateEstimate* target) const {
int64_t scaled = ByteReader<uint32_t, kFieldValueSize>::ReadBigEndian(src);
if (scaled == kMaxEncoded) {
*field_getter_(target) = DataRate::PlusInfinity();
} else {
*field_getter_(target) = kDataRateResolution * scaled;
}
}
bool Write(const NetworkStateEstimate& src, uint8_t* target) const {
auto value = *field_getter_(const_cast<NetworkStateEstimate*>(&src));
if (value.IsMinusInfinity()) {
RTC_LOG(LS_WARNING) << "Trying to serialize MinusInfinity";
return false;
}
ByteWriter<uint8_t>::WriteBigEndian(target++, id_);
int64_t scaled;
if (value.IsPlusInfinity()) {
scaled = kMaxEncoded;
} else {
scaled = value / kDataRateResolution;
if (scaled >= kMaxEncoded) {
scaled = kMaxEncoded;
RTC_LOG(LS_WARNING) << ToString(value) << " is larger than max ("
<< ToString(kMaxEncoded * kDataRateResolution)
<< "), encoded as PlusInfinity.";
}
}
ByteWriter<uint32_t, kFieldValueSize>::WriteBigEndian(target, scaled);
return true;
}
private:
const uint8_t id_;
const std::function<DataRate*(NetworkStateEstimate*)> field_getter_;
};
class RemoteEstimateSerializerImpl : public RemoteEstimateSerializer {
public:
explicit RemoteEstimateSerializerImpl(std::vector<DataRateSerializer> fields)
: fields_(fields) {}
Buffer Serialize(const NetworkStateEstimate& src) const override {
size_t max_size = fields_.size() * kFieldSize;
size_t size = 0;
Buffer buf(max_size);
for (const auto& field : fields_) {
if (field.Write(src, buf.data() + size)) {
size += kFieldSize;
}
}
buf.SetSize(size);
return buf;
}
bool Parse(ArrayView<const uint8_t> src,
NetworkStateEstimate* target) const override {
if (src.size() % kFieldSize != 0)
return false;
RTC_DCHECK_EQ(src.size() % kFieldSize, 0);
for (const uint8_t* data_ptr = src.data(); data_ptr < src.end();
data_ptr += kFieldSize) {
uint8_t field_id = ByteReader<uint8_t>::ReadBigEndian(data_ptr);
for (const auto& field : fields_) {
if (field.id() == field_id) {
field.Read(data_ptr + 1, target);
break;
}
}
}
return true;
}
private:
const std::vector<DataRateSerializer> fields_;
};
} // namespace
const RemoteEstimateSerializer* GetRemoteEstimateSerializer() {
using E = NetworkStateEstimate;
static auto* serializer = new RemoteEstimateSerializerImpl({
{1, [](E* e) { return &e->link_capacity_lower; }},
{2, [](E* e) { return &e->link_capacity_upper; }},
});
return serializer;
}
RemoteEstimate::RemoteEstimate() : serializer_(GetRemoteEstimateSerializer()) {
SetSubType(kSubType);
SetName(kName);
SetSenderSsrc(0);
}
RemoteEstimate::RemoteEstimate(App&& app)
: App(std::move(app)), serializer_(GetRemoteEstimateSerializer()) {}
bool RemoteEstimate::ParseData() {
return serializer_->Parse({data(), data_size()}, &estimate_);
}
void RemoteEstimate::SetEstimate(NetworkStateEstimate estimate) {
estimate_ = estimate;
auto buf = serializer_->Serialize(estimate);
SetData(buf.data(), buf.size());
}
} // namespace rtcp
} // namespace webrtc
|