blob: 39fe0f4fd286031bcf828d929c05c2741fdff6e9 [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;
philipelfd58b612017-01-04 07:05:25 -080091 cluster.min_probes = kMinProbePacketsSent;
92 cluster.min_bytes = bitrate_bps * kMinProbeDurationMs / 8000;
93 cluster.bitrate_bps = bitrate_bps;
Stefan Holmer0e3213a2017-02-08 15:19:05 +010094 cluster.time_created_ms = now_ms;
philipel4a1ec1e2016-08-15 11:51:06 -070095 cluster.id = next_cluster_id_++;
96 clusters_.push(cluster);
philipelfd58b612017-01-04 07:05:25 -080097
98 LOG(LS_INFO) << "Probe cluster (bitrate:min bytes:min packets): ("
99 << cluster.bitrate_bps << ":" << cluster.min_bytes << ":"
100 << cluster.min_probes << ")";
101 // If we are already probing, continue to do so. Otherwise set it to
102 // kInactive and wait for OnIncomingPacket to start the probing.
philipel4a1ec1e2016-08-15 11:51:06 -0700103 if (probing_state_ != ProbingState::kActive)
104 probing_state_ = ProbingState::kInactive;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700105}
106
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100107void BitrateProber::ResetState(int64_t now_ms) {
sergeyu6dbbd892017-01-17 15:07:59 -0800108 RTC_DCHECK(probing_state_ == ProbingState::kActive);
sprangebfbc8e2017-01-10 01:27:28 -0800109
philipel4a1ec1e2016-08-15 11:51:06 -0700110 // Recreate all probing clusters.
111 std::queue<ProbeCluster> clusters;
112 clusters.swap(clusters_);
113 while (!clusters.empty()) {
sergeyu6dbbd892017-01-17 15:07:59 -0800114 if (clusters.front().retries < kMaxRetryAttempts) {
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100115 CreateProbeCluster(clusters.front().bitrate_bps, now_ms);
sergeyu6dbbd892017-01-17 15:07:59 -0800116 clusters_.back().retries = clusters.front().retries + 1;
117 }
philipel4a1ec1e2016-08-15 11:51:06 -0700118 clusters.pop();
119 }
sergeyu6dbbd892017-01-17 15:07:59 -0800120
121 probing_state_ = ProbingState::kInactive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000122}
123
124int BitrateProber::TimeUntilNextProbe(int64_t now_ms) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700125 // Probing is not active or probing is already complete.
126 if (probing_state_ != ProbingState::kActive || clusters_.empty())
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000127 return -1;
sergeyu6dbbd892017-01-17 15:07:59 -0800128
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000129 int time_until_probe_ms = 0;
sergeyu6dbbd892017-01-17 15:07:59 -0800130 if (next_probe_time_ms_ >= 0) {
131 time_until_probe_ms = next_probe_time_ms_ - now_ms;
132 if (time_until_probe_ms < -kMaxProbeDelayMs) {
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100133 ResetState(now_ms);
sergeyu6dbbd892017-01-17 15:07:59 -0800134 return -1;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000135 }
136 }
sergeyu6dbbd892017-01-17 15:07:59 -0800137
Stefan Holmer01b48882015-05-05 10:21:24 +0200138 return std::max(time_until_probe_ms, 0);
139}
140
philipeldd324862016-05-06 17:06:14 +0200141int BitrateProber::CurrentClusterId() const {
142 RTC_DCHECK(!clusters_.empty());
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700143 RTC_DCHECK(ProbingState::kActive == probing_state_);
philipeldd324862016-05-06 17:06:14 +0200144 return clusters_.front().id;
145}
146
isheriffcc5903e2016-10-04 08:29:38 -0700147// Probe size is recommended based on the probe bitrate required. We choose
148// a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be
149// feasible.
150size_t BitrateProber::RecommendedMinProbeSize() const {
151 RTC_DCHECK(!clusters_.empty());
stefan7f08e822017-01-31 07:49:28 -0800152 return clusters_.front().bitrate_bps * 2 * kMinProbeDeltaMs / (8 * 1000);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000153}
154
isheriffcc5903e2016-10-04 08:29:38 -0700155void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) {
156 RTC_DCHECK(probing_state_ == ProbingState::kActive);
kwibergaf476c72016-11-28 15:21:39 -0800157 RTC_DCHECK_GT(bytes, 0);
sergeyu6dbbd892017-01-17 15:07:59 -0800158
philipeldd324862016-05-06 17:06:14 +0200159 if (!clusters_.empty()) {
160 ProbeCluster* cluster = &clusters_.front();
sergeyu6dbbd892017-01-17 15:07:59 -0800161 if (cluster->sent_probes == 0) {
162 RTC_DCHECK_EQ(cluster->time_started_ms, -1);
163 cluster->time_started_ms = now_ms;
164 }
philipelfd58b612017-01-04 07:05:25 -0800165 cluster->sent_bytes += static_cast<int>(bytes);
166 cluster->sent_probes += 1;
sergeyu6dbbd892017-01-17 15:07:59 -0800167 next_probe_time_ms_ = GetNextProbeTime(clusters_.front());
philipelfd58b612017-01-04 07:05:25 -0800168 if (cluster->sent_bytes >= cluster->min_bytes &&
169 cluster->sent_probes >= cluster->min_probes) {
philipeldd324862016-05-06 17:06:14 +0200170 clusters_.pop();
philipelfd58b612017-01-04 07:05:25 -0800171 }
philipel1a93cde2016-06-03 01:41:45 -0700172 if (clusters_.empty())
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700173 probing_state_ = ProbingState::kSuspended;
philipeldd324862016-05-06 17:06:14 +0200174 }
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000175}
sergeyu6dbbd892017-01-17 15:07:59 -0800176
177int64_t BitrateProber::GetNextProbeTime(const ProbeCluster& cluster) {
178 RTC_CHECK_GT(cluster.bitrate_bps, 0);
179 RTC_CHECK_GE(cluster.time_started_ms, 0);
180
181 // Compute the time delta from the cluster start to ensure probe bitrate stays
182 // close to the target bitrate. Result is in milliseconds.
183 int64_t delta_ms = (8000ll * cluster.sent_bytes + cluster.bitrate_bps / 2) /
184 cluster.bitrate_bps;
185 return cluster.time_started_ms + delta_ms;
186}
187
188
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000189} // namespace webrtc