blob: 8dc89e4a86cb7284104e2b8ad55448f7cae42286 [file] [log] [blame]
stefan@webrtc.org82462aa2014-10-23 11:57:05 +00001/*
2 * Copyright (c) 2014 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/pacing/bitrate_prober.h"
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000012
Stefan Holmer01b48882015-05-05 10:21:24 +020013#include <algorithm>
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000014
Karl Wiberg918f50c2018-07-05 11:40:33 +020015#include "absl/memory/memory.h"
Danil Chapovalov16cb1f62019-09-05 11:29:59 +020016#include "api/rtc_event_log/rtc_event.h"
Danil Chapovalov83bbe912019-08-07 12:24:53 +020017#include "api/rtc_event_log/rtc_event_log.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020018#include "logging/rtc_event_log/events/rtc_event_probe_cluster_created.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
20#include "rtc_base/logging.h"
Jonas Olsson8f433842019-03-25 10:10:31 +010021#include "system_wrappers/include/metrics.h"
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000022
23namespace webrtc {
24
25namespace {
stefanf00497c2017-01-27 02:27:33 -080026// The min probe packet size is scaled with the bitrate we're probing at.
27// This defines the max min probe packet size, meaning that on high bitrates
28// we have a min probe packet size of 200 bytes.
29constexpr size_t kMinProbePacketSize = 200;
30
Erik Språngb210eeb2019-11-05 11:21:48 +010031constexpr TimeDelta kProbeClusterTimeout = TimeDelta::Seconds<5>();
Stefan Holmer0e3213a2017-02-08 15:19:05 +010032
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000033} // namespace
34
Jonas Olsson24923e82019-03-27 14:19:04 +010035BitrateProberConfig::BitrateProberConfig(
36 const WebRtcKeyValueConfig* key_value_config)
37 : min_probe_packets_sent("min_probe_packets_sent", 5),
38 min_probe_delta("min_probe_delta", TimeDelta::ms(1)),
39 min_probe_duration("min_probe_duration", TimeDelta::ms(15)),
40 max_probe_delay("max_probe_delay", TimeDelta::ms(3)) {
41 ParseFieldTrial({&min_probe_packets_sent, &min_probe_delta,
42 &min_probe_duration, &max_probe_delay},
43 key_value_config->Lookup("WebRTC-Bwe-ProbingConfiguration"));
Jonas Olsson2e8d78c2019-05-27 13:31:45 +020044 ParseFieldTrial({&min_probe_packets_sent, &min_probe_delta,
45 &min_probe_duration, &max_probe_delay},
46 key_value_config->Lookup("WebRTC-Bwe-ProbingBehavior"));
Jonas Olsson24923e82019-03-27 14:19:04 +010047}
philipelc3b3f7a2017-03-29 01:23:13 -070048
Jonas Olsson8f433842019-03-25 10:10:31 +010049BitrateProber::~BitrateProber() {
50 RTC_HISTOGRAM_COUNTS_1000("WebRTC.BWE.Probing.TotalProbeClustersRequested",
51 total_probe_count_);
52 RTC_HISTOGRAM_COUNTS_1000("WebRTC.BWE.Probing.TotalFailedProbeClusters",
53 total_failed_probe_count_);
54}
Mirko Bonadeib471c902018-07-18 14:11:27 +020055
Jonas Olsson24923e82019-03-27 14:19:04 +010056BitrateProber::BitrateProber(const WebRtcKeyValueConfig& field_trials)
Jonas Olsson8f433842019-03-25 10:10:31 +010057 : probing_state_(ProbingState::kDisabled),
Erik Språngb210eeb2019-11-05 11:21:48 +010058 next_probe_time_(Timestamp::PlusInfinity()),
Jonas Olsson8f433842019-03-25 10:10:31 +010059 total_probe_count_(0),
Jonas Olsson24923e82019-03-27 14:19:04 +010060 total_failed_probe_count_(0),
61 config_(&field_trials) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070062 SetEnabled(true);
63}
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000064
65void BitrateProber::SetEnabled(bool enable) {
66 if (enable) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070067 if (probing_state_ == ProbingState::kDisabled) {
68 probing_state_ = ProbingState::kInactive;
Mirko Bonadei675513b2017-11-09 11:09:25 +010069 RTC_LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000070 }
71 } else {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070072 probing_state_ = ProbingState::kDisabled;
Mirko Bonadei675513b2017-11-09 11:09:25 +010073 RTC_LOG(LS_INFO) << "Bandwidth probing disabled";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000074 }
75}
76
77bool BitrateProber::IsProbing() const {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070078 return probing_state_ == ProbingState::kActive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000079}
80
philipel4a1ec1e2016-08-15 11:51:06 -070081void BitrateProber::OnIncomingPacket(size_t packet_size) {
Peter Boström0453ef82016-02-16 16:23:08 +010082 // Don't initialize probing unless we have something large enough to start
83 // probing.
stefanf00497c2017-01-27 02:27:33 -080084 if (probing_state_ == ProbingState::kInactive && !clusters_.empty() &&
85 packet_size >=
86 std::min<size_t>(RecommendedMinProbeSize(), kMinProbePacketSize)) {
sergeyu6dbbd892017-01-17 15:07:59 -080087 // Send next probe right away.
Erik Språngb210eeb2019-11-05 11:21:48 +010088 next_probe_time_ = Timestamp::MinusInfinity();
philipel4a1ec1e2016-08-15 11:51:06 -070089 probing_state_ = ProbingState::kActive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000090 }
philipel4a1ec1e2016-08-15 11:51:06 -070091}
92
Erik Språngb210eeb2019-11-05 11:21:48 +010093void BitrateProber::CreateProbeCluster(DataRate bitrate,
94 Timestamp now,
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -080095 int cluster_id) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070096 RTC_DCHECK(probing_state_ != ProbingState::kDisabled);
Erik Språngb210eeb2019-11-05 11:21:48 +010097 RTC_DCHECK_GT(bitrate, DataRate::Zero());
Jonas Olsson8f433842019-03-25 10:10:31 +010098
99 total_probe_count_++;
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100100 while (!clusters_.empty() &&
Erik Språngb210eeb2019-11-05 11:21:48 +0100101 now - clusters_.front().created_at > kProbeClusterTimeout) {
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100102 clusters_.pop();
Jonas Olsson8f433842019-03-25 10:10:31 +0100103 total_failed_probe_count_++;
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100104 }
105
philipel4a1ec1e2016-08-15 11:51:06 -0700106 ProbeCluster cluster;
Erik Språngb210eeb2019-11-05 11:21:48 +0100107 cluster.created_at = now;
Jonas Olsson24923e82019-03-27 14:19:04 +0100108 cluster.pace_info.probe_cluster_min_probes = config_.min_probe_packets_sent;
109 cluster.pace_info.probe_cluster_min_bytes =
Erik Språngb210eeb2019-11-05 11:21:48 +0100110 (bitrate * config_.min_probe_duration.Get()).bytes();
Johannes Kron85846672018-11-09 12:39:38 +0100111 RTC_DCHECK_GE(cluster.pace_info.probe_cluster_min_bytes, 0);
Erik Språngb210eeb2019-11-05 11:21:48 +0100112 cluster.pace_info.send_bitrate_bps = bitrate.bps();
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -0800113 cluster.pace_info.probe_cluster_id = cluster_id;
philipel4a1ec1e2016-08-15 11:51:06 -0700114 clusters_.push(cluster);
philipelfd58b612017-01-04 07:05:25 -0800115
Mirko Bonadei675513b2017-11-09 11:09:25 +0100116 RTC_LOG(LS_INFO) << "Probe cluster (bitrate:min bytes:min packets): ("
117 << cluster.pace_info.send_bitrate_bps << ":"
118 << cluster.pace_info.probe_cluster_min_bytes << ":"
119 << cluster.pace_info.probe_cluster_min_probes << ")";
philipelfd58b612017-01-04 07:05:25 -0800120 // If we are already probing, continue to do so. Otherwise set it to
121 // kInactive and wait for OnIncomingPacket to start the probing.
philipel4a1ec1e2016-08-15 11:51:06 -0700122 if (probing_state_ != ProbingState::kActive)
123 probing_state_ = ProbingState::kInactive;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700124}
125
Erik Språngb210eeb2019-11-05 11:21:48 +0100126Timestamp BitrateProber::NextProbeTime(Timestamp now) const {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700127 // Probing is not active or probing is already complete.
Erik Språngb210eeb2019-11-05 11:21:48 +0100128 if (probing_state_ != ProbingState::kActive || clusters_.empty()) {
129 return Timestamp::PlusInfinity();
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000130 }
sergeyu6dbbd892017-01-17 15:07:59 -0800131
Erik Språngb210eeb2019-11-05 11:21:48 +0100132 if (next_probe_time_.IsFinite() &&
133 now - next_probe_time_ > config_.max_probe_delay.Get()) {
134 RTC_DLOG(LS_WARNING) << "Probe delay too high"
135 << " (next_ms:" << next_probe_time_.ms()
136 << ", now_ms: " << now.ms() << ")";
137 return Timestamp::PlusInfinity();
138 }
139
140 return next_probe_time_;
Stefan Holmer01b48882015-05-05 10:21:24 +0200141}
142
philipelc7bf32a2017-02-17 03:59:43 -0800143PacedPacketInfo BitrateProber::CurrentCluster() const {
philipeldd324862016-05-06 17:06:14 +0200144 RTC_DCHECK(!clusters_.empty());
philipeld1d247f2017-05-04 08:35:52 -0700145 RTC_DCHECK(probing_state_ == ProbingState::kActive);
Erik Språng78c82a42019-10-03 18:46:04 +0200146 PacedPacketInfo info = clusters_.front().pace_info;
147 info.probe_cluster_bytes_sent = clusters_.front().sent_bytes;
148 return info;
philipeldd324862016-05-06 17:06:14 +0200149}
150
isheriffcc5903e2016-10-04 08:29:38 -0700151// Probe size is recommended based on the probe bitrate required. We choose
152// a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be
153// feasible.
154size_t BitrateProber::RecommendedMinProbeSize() const {
155 RTC_DCHECK(!clusters_.empty());
Jonas Olsson24923e82019-03-27 14:19:04 +0100156 return clusters_.front().pace_info.send_bitrate_bps * 2 *
157 config_.min_probe_delta->ms() / (8 * 1000);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000158}
159
Erik Språngb210eeb2019-11-05 11:21:48 +0100160void BitrateProber::ProbeSent(Timestamp now, size_t bytes) {
isheriffcc5903e2016-10-04 08:29:38 -0700161 RTC_DCHECK(probing_state_ == ProbingState::kActive);
kwibergaf476c72016-11-28 15:21:39 -0800162 RTC_DCHECK_GT(bytes, 0);
sergeyu6dbbd892017-01-17 15:07:59 -0800163
philipeldd324862016-05-06 17:06:14 +0200164 if (!clusters_.empty()) {
165 ProbeCluster* cluster = &clusters_.front();
sergeyu6dbbd892017-01-17 15:07:59 -0800166 if (cluster->sent_probes == 0) {
Erik Språngb210eeb2019-11-05 11:21:48 +0100167 RTC_DCHECK(cluster->started_at.IsInfinite());
168 cluster->started_at = now;
sergeyu6dbbd892017-01-17 15:07:59 -0800169 }
philipelfd58b612017-01-04 07:05:25 -0800170 cluster->sent_bytes += static_cast<int>(bytes);
171 cluster->sent_probes += 1;
Erik Språngb210eeb2019-11-05 11:21:48 +0100172 next_probe_time_ = CalculateNextProbeTime(*cluster);
philipelc7bf32a2017-02-17 03:59:43 -0800173 if (cluster->sent_bytes >= cluster->pace_info.probe_cluster_min_bytes &&
174 cluster->sent_probes >= cluster->pace_info.probe_cluster_min_probes) {
Jonas Olsson8f433842019-03-25 10:10:31 +0100175 RTC_HISTOGRAM_COUNTS_100000("WebRTC.BWE.Probing.ProbeClusterSizeInBytes",
176 cluster->sent_bytes);
177 RTC_HISTOGRAM_COUNTS_100("WebRTC.BWE.Probing.ProbesPerCluster",
178 cluster->sent_probes);
179 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.Probing.TimePerProbeCluster",
Erik Språngb210eeb2019-11-05 11:21:48 +0100180 (now - cluster->started_at).ms());
Jonas Olsson8f433842019-03-25 10:10:31 +0100181
philipeldd324862016-05-06 17:06:14 +0200182 clusters_.pop();
philipelfd58b612017-01-04 07:05:25 -0800183 }
philipel1a93cde2016-06-03 01:41:45 -0700184 if (clusters_.empty())
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700185 probing_state_ = ProbingState::kSuspended;
philipeldd324862016-05-06 17:06:14 +0200186 }
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000187}
sergeyu6dbbd892017-01-17 15:07:59 -0800188
Erik Språngb210eeb2019-11-05 11:21:48 +0100189Timestamp BitrateProber::CalculateNextProbeTime(
190 const ProbeCluster& cluster) const {
philipelc7bf32a2017-02-17 03:59:43 -0800191 RTC_CHECK_GT(cluster.pace_info.send_bitrate_bps, 0);
Erik Språngb210eeb2019-11-05 11:21:48 +0100192 RTC_CHECK(cluster.started_at.IsFinite());
sergeyu6dbbd892017-01-17 15:07:59 -0800193
194 // Compute the time delta from the cluster start to ensure probe bitrate stays
195 // close to the target bitrate. Result is in milliseconds.
Erik Språngb210eeb2019-11-05 11:21:48 +0100196 DataSize sent_bytes = DataSize::bytes(cluster.sent_bytes);
197 DataRate send_bitrate = DataRate::bps(cluster.pace_info.send_bitrate_bps);
198 TimeDelta delta = sent_bytes / send_bitrate;
199 return cluster.started_at + delta;
sergeyu6dbbd892017-01-17 15:07:59 -0800200}
201
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000202} // namespace webrtc