blob: dd9ee3325758d93ee9e930a3ce97deb3ff6dd8b8 [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
11#include "webrtc/modules/pacing/bitrate_prober.h"
12
Stefan Holmer01b48882015-05-05 10:21:24 +020013#include <algorithm>
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000014
philipeldd324862016-05-06 17:06:14 +020015#include "webrtc/base/checks.h"
Peter Boström7c704b82015-12-04 16:13:05 +010016#include "webrtc/base/logging.h"
Henrik Kjellander0b9e29c2015-11-16 11:12:24 +010017#include "webrtc/modules/pacing/paced_sender.h"
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000018
19namespace webrtc {
20
21namespace {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070022
isheriffcc5903e2016-10-04 08:29:38 -070023// A minimum interval between probes to allow scheduling to be feasible.
24constexpr int kMinProbeDeltaMs = 1;
25
philipelfd58b612017-01-04 07:05:25 -080026// The minimum number probing packets used.
27constexpr int kMinProbePacketsSent = 5;
28
29// The minimum probing duration in ms.
30constexpr int kMinProbeDurationMs = 15;
31
sergeyu6dbbd892017-01-17 15:07:59 -080032// Maximum amount of time each probe can be delayed. Probe cluster is reset and
33// retried from the start when this limit is reached.
34constexpr int kMaxProbeDelayMs = 3;
35
36// Number of times probing is retried before the cluster is dropped.
37constexpr int kMaxRetryAttempts = 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
48BitrateProber::BitrateProber()
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070049 : probing_state_(ProbingState::kDisabled),
sergeyu6dbbd892017-01-17 15:07:59 -080050 next_probe_time_ms_(-1),
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070051 next_cluster_id_(0) {
52 SetEnabled(true);
53}
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000054
55void BitrateProber::SetEnabled(bool enable) {
56 if (enable) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070057 if (probing_state_ == ProbingState::kDisabled) {
58 probing_state_ = ProbingState::kInactive;
59 LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000060 }
61 } else {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070062 probing_state_ = ProbingState::kDisabled;
63 LOG(LS_INFO) << "Bandwidth probing disabled";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000064 }
65}
66
67bool BitrateProber::IsProbing() const {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070068 return probing_state_ == ProbingState::kActive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000069}
70
philipel4a1ec1e2016-08-15 11:51:06 -070071void BitrateProber::OnIncomingPacket(size_t packet_size) {
Peter Boström0453ef82016-02-16 16:23:08 +010072 // Don't initialize probing unless we have something large enough to start
73 // probing.
stefanf00497c2017-01-27 02:27:33 -080074 if (probing_state_ == ProbingState::kInactive && !clusters_.empty() &&
75 packet_size >=
76 std::min<size_t>(RecommendedMinProbeSize(), kMinProbePacketSize)) {
sergeyu6dbbd892017-01-17 15:07:59 -080077 // Send next probe right away.
78 next_probe_time_ms_ = -1;
philipel4a1ec1e2016-08-15 11:51:06 -070079 probing_state_ = ProbingState::kActive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000080 }
philipel4a1ec1e2016-08-15 11:51:06 -070081}
82
Stefan Holmer0e3213a2017-02-08 15:19:05 +010083void BitrateProber::CreateProbeCluster(int bitrate_bps, int64_t now_ms) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070084 RTC_DCHECK(probing_state_ != ProbingState::kDisabled);
Stefan Holmer0e3213a2017-02-08 15:19:05 +010085 while (!clusters_.empty() &&
86 now_ms - clusters_.front().time_created_ms > kProbeClusterTimeoutMs) {
87 clusters_.pop();
88 }
89
philipel4a1ec1e2016-08-15 11:51:06 -070090 ProbeCluster cluster;
Stefan Holmer0e3213a2017-02-08 15:19:05 +010091 cluster.time_created_ms = now_ms;
philipelc7bf32a2017-02-17 03:59:43 -080092 cluster.pace_info.probe_cluster_min_probes = kMinProbePacketsSent;
93 cluster.pace_info.probe_cluster_min_bytes =
94 bitrate_bps * kMinProbeDurationMs / 8000;
95 cluster.pace_info.send_bitrate_bps = bitrate_bps;
96 cluster.pace_info.probe_cluster_id = next_cluster_id_++;
philipel4a1ec1e2016-08-15 11:51:06 -070097 clusters_.push(cluster);
philipelfd58b612017-01-04 07:05:25 -080098
99 LOG(LS_INFO) << "Probe cluster (bitrate:min bytes:min packets): ("
philipelc7bf32a2017-02-17 03:59:43 -0800100 << cluster.pace_info.send_bitrate_bps << ":"
101 << cluster.pace_info.probe_cluster_min_bytes << ":"
102 << cluster.pace_info.probe_cluster_min_probes << ")";
philipelfd58b612017-01-04 07:05:25 -0800103 // If we are already probing, continue to do so. Otherwise set it to
104 // kInactive and wait for OnIncomingPacket to start the probing.
philipel4a1ec1e2016-08-15 11:51:06 -0700105 if (probing_state_ != ProbingState::kActive)
106 probing_state_ = ProbingState::kInactive;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700107}
108
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100109void BitrateProber::ResetState(int64_t now_ms) {
sergeyu6dbbd892017-01-17 15:07:59 -0800110 RTC_DCHECK(probing_state_ == ProbingState::kActive);
sprangebfbc8e2017-01-10 01:27:28 -0800111
philipel4a1ec1e2016-08-15 11:51:06 -0700112 // Recreate all probing clusters.
113 std::queue<ProbeCluster> clusters;
114 clusters.swap(clusters_);
115 while (!clusters.empty()) {
sergeyu6dbbd892017-01-17 15:07:59 -0800116 if (clusters.front().retries < kMaxRetryAttempts) {
philipelc7bf32a2017-02-17 03:59:43 -0800117 CreateProbeCluster(clusters.front().pace_info.send_bitrate_bps, now_ms);
sergeyu6dbbd892017-01-17 15:07:59 -0800118 clusters_.back().retries = clusters.front().retries + 1;
119 }
philipel4a1ec1e2016-08-15 11:51:06 -0700120 clusters.pop();
121 }
sergeyu6dbbd892017-01-17 15:07:59 -0800122
123 probing_state_ = ProbingState::kInactive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000124}
125
126int BitrateProber::TimeUntilNextProbe(int64_t now_ms) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700127 // Probing is not active or probing is already complete.
128 if (probing_state_ != ProbingState::kActive || clusters_.empty())
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000129 return -1;
sergeyu6dbbd892017-01-17 15:07:59 -0800130
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000131 int time_until_probe_ms = 0;
sergeyu6dbbd892017-01-17 15:07:59 -0800132 if (next_probe_time_ms_ >= 0) {
133 time_until_probe_ms = next_probe_time_ms_ - now_ms;
134 if (time_until_probe_ms < -kMaxProbeDelayMs) {
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100135 ResetState(now_ms);
sergeyu6dbbd892017-01-17 15:07:59 -0800136 return -1;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000137 }
138 }
sergeyu6dbbd892017-01-17 15:07:59 -0800139
Stefan Holmer01b48882015-05-05 10:21:24 +0200140 return std::max(time_until_probe_ms, 0);
141}
142
philipelc7bf32a2017-02-17 03:59:43 -0800143PacedPacketInfo BitrateProber::CurrentCluster() const {
philipeldd324862016-05-06 17:06:14 +0200144 RTC_DCHECK(!clusters_.empty());
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700145 RTC_DCHECK(ProbingState::kActive == probing_state_);
philipelc7bf32a2017-02-17 03:59:43 -0800146 return clusters_.front().pace_info;
philipeldd324862016-05-06 17:06:14 +0200147}
148
isheriffcc5903e2016-10-04 08:29:38 -0700149// Probe size is recommended based on the probe bitrate required. We choose
150// a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be
151// feasible.
152size_t BitrateProber::RecommendedMinProbeSize() const {
153 RTC_DCHECK(!clusters_.empty());
philipelc7bf32a2017-02-17 03:59:43 -0800154 return clusters_.front().pace_info.send_bitrate_bps * 2 * kMinProbeDeltaMs /
155 (8 * 1000);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000156}
157
isheriffcc5903e2016-10-04 08:29:38 -0700158void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) {
159 RTC_DCHECK(probing_state_ == ProbingState::kActive);
kwibergaf476c72016-11-28 15:21:39 -0800160 RTC_DCHECK_GT(bytes, 0);
sergeyu6dbbd892017-01-17 15:07:59 -0800161
philipeldd324862016-05-06 17:06:14 +0200162 if (!clusters_.empty()) {
163 ProbeCluster* cluster = &clusters_.front();
sergeyu6dbbd892017-01-17 15:07:59 -0800164 if (cluster->sent_probes == 0) {
165 RTC_DCHECK_EQ(cluster->time_started_ms, -1);
166 cluster->time_started_ms = now_ms;
167 }
philipelfd58b612017-01-04 07:05:25 -0800168 cluster->sent_bytes += static_cast<int>(bytes);
169 cluster->sent_probes += 1;
sergeyu6dbbd892017-01-17 15:07:59 -0800170 next_probe_time_ms_ = GetNextProbeTime(clusters_.front());
philipelc7bf32a2017-02-17 03:59:43 -0800171 if (cluster->sent_bytes >= cluster->pace_info.probe_cluster_min_bytes &&
172 cluster->sent_probes >= cluster->pace_info.probe_cluster_min_probes) {
philipeldd324862016-05-06 17:06:14 +0200173 clusters_.pop();
philipelfd58b612017-01-04 07:05:25 -0800174 }
philipel1a93cde2016-06-03 01:41:45 -0700175 if (clusters_.empty())
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700176 probing_state_ = ProbingState::kSuspended;
philipeldd324862016-05-06 17:06:14 +0200177 }
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000178}
sergeyu6dbbd892017-01-17 15:07:59 -0800179
180int64_t BitrateProber::GetNextProbeTime(const ProbeCluster& cluster) {
philipelc7bf32a2017-02-17 03:59:43 -0800181 RTC_CHECK_GT(cluster.pace_info.send_bitrate_bps, 0);
sergeyu6dbbd892017-01-17 15:07:59 -0800182 RTC_CHECK_GE(cluster.time_started_ms, 0);
183
184 // Compute the time delta from the cluster start to ensure probe bitrate stays
185 // close to the target bitrate. Result is in milliseconds.
philipelc7bf32a2017-02-17 03:59:43 -0800186 int64_t delta_ms =
187 (8000ll * cluster.sent_bytes + cluster.pace_info.send_bitrate_bps / 2) /
188 cluster.pace_info.send_bitrate_bps;
sergeyu6dbbd892017-01-17 15:07:59 -0800189 return cluster.time_started_ms + delta_ms;
190}
191
192
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000193} // namespace webrtc