blob: f3795645fa74b6a712d21d28adb436e41bff2cf5 [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"
Yves Gerey988cc082018-10-23 12:03:01 +020016#include "logging/rtc_event_log/events/rtc_event.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020017#include "logging/rtc_event_log/events/rtc_event_probe_cluster_created.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "logging/rtc_event_log/rtc_event_log.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
20#include "rtc_base/logging.h"
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000021
22namespace webrtc {
23
24namespace {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070025
isheriffcc5903e2016-10-04 08:29:38 -070026// A minimum interval between probes to allow scheduling to be feasible.
27constexpr int kMinProbeDeltaMs = 1;
28
philipelfd58b612017-01-04 07:05:25 -080029// The minimum number probing packets used.
30constexpr int kMinProbePacketsSent = 5;
31
32// The minimum probing duration in ms.
33constexpr int kMinProbeDurationMs = 15;
34
sergeyu6dbbd892017-01-17 15:07:59 -080035// Maximum amount of time each probe can be delayed. Probe cluster is reset and
36// retried from the start when this limit is reached.
37constexpr int kMaxProbeDelayMs = 3;
38
stefanf00497c2017-01-27 02:27:33 -080039// The min probe packet size is scaled with the bitrate we're probing at.
40// This defines the max min probe packet size, meaning that on high bitrates
41// we have a min probe packet size of 200 bytes.
42constexpr size_t kMinProbePacketSize = 200;
43
Stefan Holmer0e3213a2017-02-08 15:19:05 +010044constexpr int64_t kProbeClusterTimeoutMs = 5000;
45
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000046} // namespace
47
philipelc3b3f7a2017-03-29 01:23:13 -070048BitrateProber::BitrateProber() : BitrateProber(nullptr) {}
49
Mirko Bonadeib471c902018-07-18 14:11:27 +020050BitrateProber::~BitrateProber() = default;
51
philipelc3b3f7a2017-03-29 01:23:13 -070052BitrateProber::BitrateProber(RtcEventLog* event_log)
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070053 : probing_state_(ProbingState::kDisabled),
sergeyu6dbbd892017-01-17 15:07:59 -080054 next_probe_time_ms_(-1),
philipelc3b3f7a2017-03-29 01:23:13 -070055 next_cluster_id_(0),
56 event_log_(event_log) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070057 SetEnabled(true);
58}
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000059
60void BitrateProber::SetEnabled(bool enable) {
61 if (enable) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070062 if (probing_state_ == ProbingState::kDisabled) {
63 probing_state_ = ProbingState::kInactive;
Mirko Bonadei675513b2017-11-09 11:09:25 +010064 RTC_LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000065 }
66 } else {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070067 probing_state_ = ProbingState::kDisabled;
Mirko Bonadei675513b2017-11-09 11:09:25 +010068 RTC_LOG(LS_INFO) << "Bandwidth probing disabled";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000069 }
70}
71
72bool BitrateProber::IsProbing() const {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070073 return probing_state_ == ProbingState::kActive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000074}
75
philipel4a1ec1e2016-08-15 11:51:06 -070076void BitrateProber::OnIncomingPacket(size_t packet_size) {
Peter Boström0453ef82016-02-16 16:23:08 +010077 // Don't initialize probing unless we have something large enough to start
78 // probing.
stefanf00497c2017-01-27 02:27:33 -080079 if (probing_state_ == ProbingState::kInactive && !clusters_.empty() &&
80 packet_size >=
81 std::min<size_t>(RecommendedMinProbeSize(), kMinProbePacketSize)) {
sergeyu6dbbd892017-01-17 15:07:59 -080082 // Send next probe right away.
83 next_probe_time_ms_ = -1;
philipel4a1ec1e2016-08-15 11:51:06 -070084 probing_state_ = ProbingState::kActive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000085 }
philipel4a1ec1e2016-08-15 11:51:06 -070086}
87
Stefan Holmer0e3213a2017-02-08 15:19:05 +010088void BitrateProber::CreateProbeCluster(int bitrate_bps, int64_t now_ms) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070089 RTC_DCHECK(probing_state_ != ProbingState::kDisabled);
philipeld1d247f2017-05-04 08:35:52 -070090 RTC_DCHECK_GT(bitrate_bps, 0);
Stefan Holmer0e3213a2017-02-08 15:19:05 +010091 while (!clusters_.empty() &&
92 now_ms - clusters_.front().time_created_ms > kProbeClusterTimeoutMs) {
93 clusters_.pop();
94 }
95
philipel4a1ec1e2016-08-15 11:51:06 -070096 ProbeCluster cluster;
Stefan Holmer0e3213a2017-02-08 15:19:05 +010097 cluster.time_created_ms = now_ms;
philipelc7bf32a2017-02-17 03:59:43 -080098 cluster.pace_info.probe_cluster_min_probes = kMinProbePacketsSent;
99 cluster.pace_info.probe_cluster_min_bytes =
100 bitrate_bps * kMinProbeDurationMs / 8000;
101 cluster.pace_info.send_bitrate_bps = bitrate_bps;
102 cluster.pace_info.probe_cluster_id = next_cluster_id_++;
philipel4a1ec1e2016-08-15 11:51:06 -0700103 clusters_.push(cluster);
philipelc3b3f7a2017-03-29 01:23:13 -0700104 if (event_log_)
Karl Wiberg918f50c2018-07-05 11:40:33 +0200105 event_log_->Log(absl::make_unique<RtcEventProbeClusterCreated>(
philipelc3b3f7a2017-03-29 01:23:13 -0700106 cluster.pace_info.probe_cluster_id, cluster.pace_info.send_bitrate_bps,
107 cluster.pace_info.probe_cluster_min_probes,
Elad Alon4a87e1c2017-10-03 16:11:34 +0200108 cluster.pace_info.probe_cluster_min_bytes));
philipelfd58b612017-01-04 07:05:25 -0800109
Mirko Bonadei675513b2017-11-09 11:09:25 +0100110 RTC_LOG(LS_INFO) << "Probe cluster (bitrate:min bytes:min packets): ("
111 << cluster.pace_info.send_bitrate_bps << ":"
112 << cluster.pace_info.probe_cluster_min_bytes << ":"
113 << cluster.pace_info.probe_cluster_min_probes << ")";
philipelfd58b612017-01-04 07:05:25 -0800114 // If we are already probing, continue to do so. Otherwise set it to
115 // kInactive and wait for OnIncomingPacket to start the probing.
philipel4a1ec1e2016-08-15 11:51:06 -0700116 if (probing_state_ != ProbingState::kActive)
117 probing_state_ = ProbingState::kInactive;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700118}
119
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000120int BitrateProber::TimeUntilNextProbe(int64_t now_ms) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700121 // Probing is not active or probing is already complete.
122 if (probing_state_ != ProbingState::kActive || clusters_.empty())
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000123 return -1;
sergeyu6dbbd892017-01-17 15:07:59 -0800124
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000125 int time_until_probe_ms = 0;
sergeyu6dbbd892017-01-17 15:07:59 -0800126 if (next_probe_time_ms_ >= 0) {
127 time_until_probe_ms = next_probe_time_ms_ - now_ms;
128 if (time_until_probe_ms < -kMaxProbeDelayMs) {
Jamie Walch9c8ae4b2018-10-24 10:17:52 -0700129 RTC_DLOG(LS_WARNING) << "Probe delay too high"
130 << " (next_ms:" << next_probe_time_ms_
131 << ", now_ms: " << now_ms << ")";
sergeyu6dbbd892017-01-17 15:07:59 -0800132 return -1;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000133 }
134 }
sergeyu6dbbd892017-01-17 15:07:59 -0800135
Stefan Holmer01b48882015-05-05 10:21:24 +0200136 return std::max(time_until_probe_ms, 0);
137}
138
philipelc7bf32a2017-02-17 03:59:43 -0800139PacedPacketInfo BitrateProber::CurrentCluster() const {
philipeldd324862016-05-06 17:06:14 +0200140 RTC_DCHECK(!clusters_.empty());
philipeld1d247f2017-05-04 08:35:52 -0700141 RTC_DCHECK(probing_state_ == ProbingState::kActive);
philipelc7bf32a2017-02-17 03:59:43 -0800142 return clusters_.front().pace_info;
philipeldd324862016-05-06 17:06:14 +0200143}
144
isheriffcc5903e2016-10-04 08:29:38 -0700145// Probe size is recommended based on the probe bitrate required. We choose
146// a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be
147// feasible.
148size_t BitrateProber::RecommendedMinProbeSize() const {
149 RTC_DCHECK(!clusters_.empty());
philipelc7bf32a2017-02-17 03:59:43 -0800150 return clusters_.front().pace_info.send_bitrate_bps * 2 * kMinProbeDeltaMs /
151 (8 * 1000);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000152}
153
isheriffcc5903e2016-10-04 08:29:38 -0700154void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) {
155 RTC_DCHECK(probing_state_ == ProbingState::kActive);
kwibergaf476c72016-11-28 15:21:39 -0800156 RTC_DCHECK_GT(bytes, 0);
sergeyu6dbbd892017-01-17 15:07:59 -0800157
philipeldd324862016-05-06 17:06:14 +0200158 if (!clusters_.empty()) {
159 ProbeCluster* cluster = &clusters_.front();
sergeyu6dbbd892017-01-17 15:07:59 -0800160 if (cluster->sent_probes == 0) {
161 RTC_DCHECK_EQ(cluster->time_started_ms, -1);
162 cluster->time_started_ms = now_ms;
163 }
philipelfd58b612017-01-04 07:05:25 -0800164 cluster->sent_bytes += static_cast<int>(bytes);
165 cluster->sent_probes += 1;
philipeld1d247f2017-05-04 08:35:52 -0700166 next_probe_time_ms_ = GetNextProbeTime(*cluster);
philipelc7bf32a2017-02-17 03:59:43 -0800167 if (cluster->sent_bytes >= cluster->pace_info.probe_cluster_min_bytes &&
168 cluster->sent_probes >= cluster->pace_info.probe_cluster_min_probes) {
philipeldd324862016-05-06 17:06:14 +0200169 clusters_.pop();
philipelfd58b612017-01-04 07:05:25 -0800170 }
philipel1a93cde2016-06-03 01:41:45 -0700171 if (clusters_.empty())
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700172 probing_state_ = ProbingState::kSuspended;
philipeldd324862016-05-06 17:06:14 +0200173 }
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000174}
sergeyu6dbbd892017-01-17 15:07:59 -0800175
176int64_t BitrateProber::GetNextProbeTime(const ProbeCluster& cluster) {
philipelc7bf32a2017-02-17 03:59:43 -0800177 RTC_CHECK_GT(cluster.pace_info.send_bitrate_bps, 0);
sergeyu6dbbd892017-01-17 15:07:59 -0800178 RTC_CHECK_GE(cluster.time_started_ms, 0);
179
180 // Compute the time delta from the cluster start to ensure probe bitrate stays
181 // close to the target bitrate. Result is in milliseconds.
philipelc7bf32a2017-02-17 03:59:43 -0800182 int64_t delta_ms =
183 (8000ll * cluster.sent_bytes + cluster.pace_info.send_bitrate_bps / 2) /
184 cluster.pace_info.send_bitrate_bps;
sergeyu6dbbd892017-01-17 15:07:59 -0800185 return cluster.time_started_ms + delta_ms;
186}
187
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000188} // namespace webrtc