This is a reland of 739a5b3692880cb6b41ae620fb9e755c39b044b1 Patchset 1 is the original CL, patchset 3 includes a fix Original change's description: > Refactors BitrateProber with unit types and absolute probe time. > > Using unit types improves readability and some conversion in PacedSender > can be removed. > > TimeUntilNextProbe() is replaced by NextProbeTime(), so returning an > absolute time rather than a delta. This fits better with the upcoming > TaskQueue based pacer, and is also what is already stored internally > in BitrateProber. > > Bug: webrtc:10809 > Change-Id: I5a4e289d2b53e99d3c0a2f4b36a966dba759d5cf > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/158743 > Commit-Queue: Erik Språng <sprang@webrtc.org> > Reviewed-by: Sebastian Jansson <srte@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#29670} Bug: webrtc:10809 Change-Id: I033193c78474fdd82c109fdab0a8f09a05f7b30e Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/158841 Commit-Queue: Erik Språng <sprang@webrtc.org> Reviewed-by: Sebastian Jansson <srte@webrtc.org> Cr-Commit-Position: refs/heads/master@{#29688}
203 lines
7.6 KiB
C++
203 lines
7.6 KiB
C++
/*
|
|
* Copyright (c) 2014 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/pacing/bitrate_prober.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include "absl/memory/memory.h"
|
|
#include "api/rtc_event_log/rtc_event.h"
|
|
#include "api/rtc_event_log/rtc_event_log.h"
|
|
#include "logging/rtc_event_log/events/rtc_event_probe_cluster_created.h"
|
|
#include "rtc_base/checks.h"
|
|
#include "rtc_base/logging.h"
|
|
#include "system_wrappers/include/metrics.h"
|
|
|
|
namespace webrtc {
|
|
|
|
namespace {
|
|
// The min probe packet size is scaled with the bitrate we're probing at.
|
|
// This defines the max min probe packet size, meaning that on high bitrates
|
|
// we have a min probe packet size of 200 bytes.
|
|
constexpr size_t kMinProbePacketSize = 200;
|
|
|
|
constexpr TimeDelta kProbeClusterTimeout = TimeDelta::Seconds<5>();
|
|
|
|
} // namespace
|
|
|
|
BitrateProberConfig::BitrateProberConfig(
|
|
const WebRtcKeyValueConfig* key_value_config)
|
|
: min_probe_packets_sent("min_probe_packets_sent", 5),
|
|
min_probe_delta("min_probe_delta", TimeDelta::ms(1)),
|
|
min_probe_duration("min_probe_duration", TimeDelta::ms(15)),
|
|
max_probe_delay("max_probe_delay", TimeDelta::ms(3)) {
|
|
ParseFieldTrial({&min_probe_packets_sent, &min_probe_delta,
|
|
&min_probe_duration, &max_probe_delay},
|
|
key_value_config->Lookup("WebRTC-Bwe-ProbingConfiguration"));
|
|
ParseFieldTrial({&min_probe_packets_sent, &min_probe_delta,
|
|
&min_probe_duration, &max_probe_delay},
|
|
key_value_config->Lookup("WebRTC-Bwe-ProbingBehavior"));
|
|
}
|
|
|
|
BitrateProber::~BitrateProber() {
|
|
RTC_HISTOGRAM_COUNTS_1000("WebRTC.BWE.Probing.TotalProbeClustersRequested",
|
|
total_probe_count_);
|
|
RTC_HISTOGRAM_COUNTS_1000("WebRTC.BWE.Probing.TotalFailedProbeClusters",
|
|
total_failed_probe_count_);
|
|
}
|
|
|
|
BitrateProber::BitrateProber(const WebRtcKeyValueConfig& field_trials)
|
|
: probing_state_(ProbingState::kDisabled),
|
|
next_probe_time_(Timestamp::PlusInfinity()),
|
|
total_probe_count_(0),
|
|
total_failed_probe_count_(0),
|
|
config_(&field_trials) {
|
|
SetEnabled(true);
|
|
}
|
|
|
|
void BitrateProber::SetEnabled(bool enable) {
|
|
if (enable) {
|
|
if (probing_state_ == ProbingState::kDisabled) {
|
|
probing_state_ = ProbingState::kInactive;
|
|
RTC_LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive";
|
|
}
|
|
} else {
|
|
probing_state_ = ProbingState::kDisabled;
|
|
RTC_LOG(LS_INFO) << "Bandwidth probing disabled";
|
|
}
|
|
}
|
|
|
|
bool BitrateProber::IsProbing() const {
|
|
return probing_state_ == ProbingState::kActive;
|
|
}
|
|
|
|
void BitrateProber::OnIncomingPacket(size_t packet_size) {
|
|
// Don't initialize probing unless we have something large enough to start
|
|
// probing.
|
|
if (probing_state_ == ProbingState::kInactive && !clusters_.empty() &&
|
|
packet_size >=
|
|
std::min<size_t>(RecommendedMinProbeSize(), kMinProbePacketSize)) {
|
|
// Send next probe right away.
|
|
next_probe_time_ = Timestamp::MinusInfinity();
|
|
probing_state_ = ProbingState::kActive;
|
|
}
|
|
}
|
|
|
|
void BitrateProber::CreateProbeCluster(DataRate bitrate,
|
|
Timestamp now,
|
|
int cluster_id) {
|
|
RTC_DCHECK(probing_state_ != ProbingState::kDisabled);
|
|
RTC_DCHECK_GT(bitrate, DataRate::Zero());
|
|
|
|
total_probe_count_++;
|
|
while (!clusters_.empty() &&
|
|
now - clusters_.front().created_at > kProbeClusterTimeout) {
|
|
clusters_.pop();
|
|
total_failed_probe_count_++;
|
|
}
|
|
|
|
ProbeCluster cluster;
|
|
cluster.created_at = now;
|
|
cluster.pace_info.probe_cluster_min_probes = config_.min_probe_packets_sent;
|
|
cluster.pace_info.probe_cluster_min_bytes =
|
|
(bitrate * config_.min_probe_duration.Get()).bytes();
|
|
RTC_DCHECK_GE(cluster.pace_info.probe_cluster_min_bytes, 0);
|
|
cluster.pace_info.send_bitrate_bps = bitrate.bps();
|
|
cluster.pace_info.probe_cluster_id = cluster_id;
|
|
clusters_.push(cluster);
|
|
|
|
RTC_LOG(LS_INFO) << "Probe cluster (bitrate:min bytes:min packets): ("
|
|
<< cluster.pace_info.send_bitrate_bps << ":"
|
|
<< cluster.pace_info.probe_cluster_min_bytes << ":"
|
|
<< cluster.pace_info.probe_cluster_min_probes << ")";
|
|
// If we are already probing, continue to do so. Otherwise set it to
|
|
// kInactive and wait for OnIncomingPacket to start the probing.
|
|
if (probing_state_ != ProbingState::kActive)
|
|
probing_state_ = ProbingState::kInactive;
|
|
}
|
|
|
|
Timestamp BitrateProber::NextProbeTime(Timestamp now) const {
|
|
// Probing is not active or probing is already complete.
|
|
if (probing_state_ != ProbingState::kActive || clusters_.empty()) {
|
|
return Timestamp::PlusInfinity();
|
|
}
|
|
|
|
if (next_probe_time_.IsFinite() &&
|
|
now - next_probe_time_ > config_.max_probe_delay.Get()) {
|
|
RTC_DLOG(LS_WARNING) << "Probe delay too high"
|
|
<< " (next_ms:" << next_probe_time_.ms()
|
|
<< ", now_ms: " << now.ms() << ")";
|
|
return Timestamp::PlusInfinity();
|
|
}
|
|
|
|
return next_probe_time_;
|
|
}
|
|
|
|
PacedPacketInfo BitrateProber::CurrentCluster() const {
|
|
RTC_DCHECK(!clusters_.empty());
|
|
RTC_DCHECK(probing_state_ == ProbingState::kActive);
|
|
PacedPacketInfo info = clusters_.front().pace_info;
|
|
info.probe_cluster_bytes_sent = clusters_.front().sent_bytes;
|
|
return info;
|
|
}
|
|
|
|
// Probe size is recommended based on the probe bitrate required. We choose
|
|
// a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be
|
|
// feasible.
|
|
size_t BitrateProber::RecommendedMinProbeSize() const {
|
|
RTC_DCHECK(!clusters_.empty());
|
|
return clusters_.front().pace_info.send_bitrate_bps * 2 *
|
|
config_.min_probe_delta->ms() / (8 * 1000);
|
|
}
|
|
|
|
void BitrateProber::ProbeSent(Timestamp now, size_t bytes) {
|
|
RTC_DCHECK(probing_state_ == ProbingState::kActive);
|
|
RTC_DCHECK_GT(bytes, 0);
|
|
|
|
if (!clusters_.empty()) {
|
|
ProbeCluster* cluster = &clusters_.front();
|
|
if (cluster->sent_probes == 0) {
|
|
RTC_DCHECK(cluster->started_at.IsInfinite());
|
|
cluster->started_at = now;
|
|
}
|
|
cluster->sent_bytes += static_cast<int>(bytes);
|
|
cluster->sent_probes += 1;
|
|
next_probe_time_ = CalculateNextProbeTime(*cluster);
|
|
if (cluster->sent_bytes >= cluster->pace_info.probe_cluster_min_bytes &&
|
|
cluster->sent_probes >= cluster->pace_info.probe_cluster_min_probes) {
|
|
RTC_HISTOGRAM_COUNTS_100000("WebRTC.BWE.Probing.ProbeClusterSizeInBytes",
|
|
cluster->sent_bytes);
|
|
RTC_HISTOGRAM_COUNTS_100("WebRTC.BWE.Probing.ProbesPerCluster",
|
|
cluster->sent_probes);
|
|
RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.Probing.TimePerProbeCluster",
|
|
(now - cluster->started_at).ms());
|
|
|
|
clusters_.pop();
|
|
}
|
|
if (clusters_.empty())
|
|
probing_state_ = ProbingState::kSuspended;
|
|
}
|
|
}
|
|
|
|
Timestamp BitrateProber::CalculateNextProbeTime(
|
|
const ProbeCluster& cluster) const {
|
|
RTC_CHECK_GT(cluster.pace_info.send_bitrate_bps, 0);
|
|
RTC_CHECK(cluster.started_at.IsFinite());
|
|
|
|
// Compute the time delta from the cluster start to ensure probe bitrate stays
|
|
// close to the target bitrate. Result is in milliseconds.
|
|
DataSize sent_bytes = DataSize::bytes(cluster.sent_bytes);
|
|
DataRate send_bitrate = DataRate::bps(cluster.pace_info.send_bitrate_bps);
|
|
TimeDelta delta = sent_bytes / send_bitrate;
|
|
return cluster.started_at + delta;
|
|
}
|
|
|
|
} // namespace webrtc
|