2014-10-23 11:57:05 +00:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/pacing/bitrate_prober.h"
|
2014-10-23 11:57:05 +00:00
|
|
|
|
2015-05-05 10:21:24 +02:00
|
|
|
#include <algorithm>
|
2014-10-23 11:57:05 +00:00
|
|
|
|
Use absl::make_unique and absl::WrapUnique directly
Instead of going through our wrappers in ptr_util.h.
This CL was generated by the following script:
git grep -l ptr_util | xargs perl -pi -e 's,#include "rtc_base/ptr_util.h",#include "absl/memory/memory.h",'
git grep -l MakeUnique | xargs perl -pi -e 's,\b(rtc::)?MakeUnique\b,absl::make_unique,g'
git grep -l WrapUnique | xargs perl -pi -e 's,\b(rtc::)?WrapUnique\b,absl::WrapUnique,g'
git checkout -- rtc_base/ptr_util{.h,_unittest.cc}
git cl format
Followed by manually adding dependencies on
//third_party/abseil-cpp/absl/memory until `gn check` stopped
complaining.
Bug: webrtc:9473
Change-Id: I89ccd363f070479b8c431eb2c3d404a46eaacc1c
Reviewed-on: https://webrtc-review.googlesource.com/86600
Commit-Queue: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23850}
2018-07-05 11:40:33 +02:00
|
|
|
#include "absl/memory/memory.h"
|
2018-10-23 12:03:01 +02:00
|
|
|
#include "logging/rtc_event_log/events/rtc_event.h"
|
2017-10-03 16:11:34 +02:00
|
|
|
#include "logging/rtc_event_log/events/rtc_event_probe_cluster_created.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "logging/rtc_event_log/rtc_event_log.h"
|
|
|
|
|
#include "rtc_base/checks.h"
|
|
|
|
|
#include "rtc_base/logging.h"
|
2014-10-23 11:57:05 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
namespace {
|
2016-08-02 12:57:37 -07:00
|
|
|
|
2016-10-04 08:29:38 -07:00
|
|
|
// A minimum interval between probes to allow scheduling to be feasible.
|
|
|
|
|
constexpr int kMinProbeDeltaMs = 1;
|
|
|
|
|
|
2017-01-04 07:05:25 -08:00
|
|
|
// The minimum number probing packets used.
|
|
|
|
|
constexpr int kMinProbePacketsSent = 5;
|
|
|
|
|
|
|
|
|
|
// The minimum probing duration in ms.
|
|
|
|
|
constexpr int kMinProbeDurationMs = 15;
|
|
|
|
|
|
2017-01-17 15:07:59 -08:00
|
|
|
// Maximum amount of time each probe can be delayed. Probe cluster is reset and
|
|
|
|
|
// retried from the start when this limit is reached.
|
|
|
|
|
constexpr int kMaxProbeDelayMs = 3;
|
|
|
|
|
|
2017-01-27 02:27:33 -08:00
|
|
|
// 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;
|
|
|
|
|
|
2017-02-08 15:19:05 +01:00
|
|
|
constexpr int64_t kProbeClusterTimeoutMs = 5000;
|
|
|
|
|
|
2014-10-23 11:57:05 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
2017-03-29 01:23:13 -07:00
|
|
|
BitrateProber::BitrateProber() : BitrateProber(nullptr) {}
|
|
|
|
|
|
2018-07-18 14:11:27 +02:00
|
|
|
BitrateProber::~BitrateProber() = default;
|
|
|
|
|
|
2019-02-15 07:38:04 -08:00
|
|
|
// TODO(psla): Remove this constructor in a follow up change.
|
2017-03-29 01:23:13 -07:00
|
|
|
BitrateProber::BitrateProber(RtcEventLog* event_log)
|
2019-02-15 07:38:04 -08:00
|
|
|
: probing_state_(ProbingState::kDisabled), next_probe_time_ms_(-1) {
|
2016-08-02 12:57:37 -07:00
|
|
|
SetEnabled(true);
|
|
|
|
|
}
|
2014-10-23 11:57:05 +00:00
|
|
|
|
|
|
|
|
void BitrateProber::SetEnabled(bool enable) {
|
|
|
|
|
if (enable) {
|
2016-08-02 12:57:37 -07:00
|
|
|
if (probing_state_ == ProbingState::kDisabled) {
|
|
|
|
|
probing_state_ = ProbingState::kInactive;
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive";
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2016-08-02 12:57:37 -07:00
|
|
|
probing_state_ = ProbingState::kDisabled;
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_INFO) << "Bandwidth probing disabled";
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BitrateProber::IsProbing() const {
|
2016-08-02 12:57:37 -07:00
|
|
|
return probing_state_ == ProbingState::kActive;
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-15 11:51:06 -07:00
|
|
|
void BitrateProber::OnIncomingPacket(size_t packet_size) {
|
2016-02-16 16:23:08 +01:00
|
|
|
// Don't initialize probing unless we have something large enough to start
|
|
|
|
|
// probing.
|
2017-01-27 02:27:33 -08:00
|
|
|
if (probing_state_ == ProbingState::kInactive && !clusters_.empty() &&
|
|
|
|
|
packet_size >=
|
|
|
|
|
std::min<size_t>(RecommendedMinProbeSize(), kMinProbePacketSize)) {
|
2017-01-17 15:07:59 -08:00
|
|
|
// Send next probe right away.
|
|
|
|
|
next_probe_time_ms_ = -1;
|
2016-08-15 11:51:06 -07:00
|
|
|
probing_state_ = ProbingState::kActive;
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
2016-08-15 11:51:06 -07:00
|
|
|
}
|
|
|
|
|
|
2019-02-15 07:38:04 -08:00
|
|
|
void BitrateProber::CreateProbeCluster(int bitrate_bps,
|
|
|
|
|
int64_t now_ms,
|
|
|
|
|
int cluster_id) {
|
2016-09-12 12:28:54 -07:00
|
|
|
RTC_DCHECK(probing_state_ != ProbingState::kDisabled);
|
2017-05-04 08:35:52 -07:00
|
|
|
RTC_DCHECK_GT(bitrate_bps, 0);
|
2017-02-08 15:19:05 +01:00
|
|
|
while (!clusters_.empty() &&
|
|
|
|
|
now_ms - clusters_.front().time_created_ms > kProbeClusterTimeoutMs) {
|
|
|
|
|
clusters_.pop();
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-15 11:51:06 -07:00
|
|
|
ProbeCluster cluster;
|
2017-02-08 15:19:05 +01:00
|
|
|
cluster.time_created_ms = now_ms;
|
2017-02-17 03:59:43 -08:00
|
|
|
cluster.pace_info.probe_cluster_min_probes = kMinProbePacketsSent;
|
2018-11-09 12:39:38 +01:00
|
|
|
cluster.pace_info.probe_cluster_min_bytes = static_cast<int32_t>(
|
|
|
|
|
static_cast<int64_t>(bitrate_bps) * kMinProbeDurationMs / 8000);
|
|
|
|
|
RTC_DCHECK_GE(cluster.pace_info.probe_cluster_min_bytes, 0);
|
2017-02-17 03:59:43 -08:00
|
|
|
cluster.pace_info.send_bitrate_bps = bitrate_bps;
|
2019-02-15 07:38:04 -08:00
|
|
|
cluster.pace_info.probe_cluster_id = cluster_id;
|
2016-08-15 11:51:06 -07:00
|
|
|
clusters_.push(cluster);
|
2017-01-04 07:05:25 -08:00
|
|
|
|
2017-11-09 11:09:25 +01:00
|
|
|
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 << ")";
|
2017-01-04 07:05:25 -08:00
|
|
|
// If we are already probing, continue to do so. Otherwise set it to
|
|
|
|
|
// kInactive and wait for OnIncomingPacket to start the probing.
|
2016-08-15 11:51:06 -07:00
|
|
|
if (probing_state_ != ProbingState::kActive)
|
|
|
|
|
probing_state_ = ProbingState::kInactive;
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-02 12:57:37 -07:00
|
|
|
int BitrateProber::TimeUntilNextProbe(int64_t now_ms) {
|
|
|
|
|
// Probing is not active or probing is already complete.
|
|
|
|
|
if (probing_state_ != ProbingState::kActive || clusters_.empty())
|
2015-02-16 15:47:51 +00:00
|
|
|
return -1;
|
2017-01-17 15:07:59 -08:00
|
|
|
|
2014-10-23 11:57:05 +00:00
|
|
|
int time_until_probe_ms = 0;
|
2017-01-17 15:07:59 -08:00
|
|
|
if (next_probe_time_ms_ >= 0) {
|
|
|
|
|
time_until_probe_ms = next_probe_time_ms_ - now_ms;
|
|
|
|
|
if (time_until_probe_ms < -kMaxProbeDelayMs) {
|
2018-10-24 10:17:52 -07:00
|
|
|
RTC_DLOG(LS_WARNING) << "Probe delay too high"
|
|
|
|
|
<< " (next_ms:" << next_probe_time_ms_
|
|
|
|
|
<< ", now_ms: " << now_ms << ")";
|
2017-01-17 15:07:59 -08:00
|
|
|
return -1;
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
2017-01-17 15:07:59 -08:00
|
|
|
|
2015-05-05 10:21:24 +02:00
|
|
|
return std::max(time_until_probe_ms, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 03:59:43 -08:00
|
|
|
PacedPacketInfo BitrateProber::CurrentCluster() const {
|
2016-05-06 17:06:14 +02:00
|
|
|
RTC_DCHECK(!clusters_.empty());
|
2017-05-04 08:35:52 -07:00
|
|
|
RTC_DCHECK(probing_state_ == ProbingState::kActive);
|
2017-02-17 03:59:43 -08:00
|
|
|
return clusters_.front().pace_info;
|
2016-05-06 17:06:14 +02:00
|
|
|
}
|
|
|
|
|
|
2016-10-04 08:29:38 -07:00
|
|
|
// 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());
|
2017-02-17 03:59:43 -08:00
|
|
|
return clusters_.front().pace_info.send_bitrate_bps * 2 * kMinProbeDeltaMs /
|
|
|
|
|
(8 * 1000);
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-04 08:29:38 -07:00
|
|
|
void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) {
|
|
|
|
|
RTC_DCHECK(probing_state_ == ProbingState::kActive);
|
2016-11-28 15:21:39 -08:00
|
|
|
RTC_DCHECK_GT(bytes, 0);
|
2017-01-17 15:07:59 -08:00
|
|
|
|
2016-05-06 17:06:14 +02:00
|
|
|
if (!clusters_.empty()) {
|
|
|
|
|
ProbeCluster* cluster = &clusters_.front();
|
2017-01-17 15:07:59 -08:00
|
|
|
if (cluster->sent_probes == 0) {
|
|
|
|
|
RTC_DCHECK_EQ(cluster->time_started_ms, -1);
|
|
|
|
|
cluster->time_started_ms = now_ms;
|
|
|
|
|
}
|
2017-01-04 07:05:25 -08:00
|
|
|
cluster->sent_bytes += static_cast<int>(bytes);
|
|
|
|
|
cluster->sent_probes += 1;
|
2017-05-04 08:35:52 -07:00
|
|
|
next_probe_time_ms_ = GetNextProbeTime(*cluster);
|
2017-02-17 03:59:43 -08:00
|
|
|
if (cluster->sent_bytes >= cluster->pace_info.probe_cluster_min_bytes &&
|
|
|
|
|
cluster->sent_probes >= cluster->pace_info.probe_cluster_min_probes) {
|
2016-05-06 17:06:14 +02:00
|
|
|
clusters_.pop();
|
2017-01-04 07:05:25 -08:00
|
|
|
}
|
2016-06-03 01:41:45 -07:00
|
|
|
if (clusters_.empty())
|
2016-08-02 12:57:37 -07:00
|
|
|
probing_state_ = ProbingState::kSuspended;
|
2016-05-06 17:06:14 +02:00
|
|
|
}
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
2017-01-17 15:07:59 -08:00
|
|
|
|
|
|
|
|
int64_t BitrateProber::GetNextProbeTime(const ProbeCluster& cluster) {
|
2017-02-17 03:59:43 -08:00
|
|
|
RTC_CHECK_GT(cluster.pace_info.send_bitrate_bps, 0);
|
2017-01-17 15:07:59 -08:00
|
|
|
RTC_CHECK_GE(cluster.time_started_ms, 0);
|
|
|
|
|
|
|
|
|
|
// Compute the time delta from the cluster start to ensure probe bitrate stays
|
|
|
|
|
// close to the target bitrate. Result is in milliseconds.
|
2017-02-17 03:59:43 -08:00
|
|
|
int64_t delta_ms =
|
|
|
|
|
(8000ll * cluster.sent_bytes + cluster.pace_info.send_bitrate_bps / 2) /
|
|
|
|
|
cluster.pace_info.send_bitrate_bps;
|
2017-01-17 15:07:59 -08:00
|
|
|
return cluster.time_started_ms + delta_ms;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-23 11:57:05 +00:00
|
|
|
} // namespace webrtc
|