blob: 8e3c5e354795a17a6c9ed32c4e4e9b3343294186 [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
23// Inactivity threshold above which probing is restarted.
24constexpr int kInactivityThresholdMs = 5000;
25
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
isheriffcc5903e2016-10-04 08:29:38 -070035int ComputeDeltaFromBitrate(size_t probe_size, uint32_t bitrate_bps) {
kwibergaf476c72016-11-28 15:21:39 -080036 RTC_CHECK_GT(bitrate_bps, 0);
isheriffcc5903e2016-10-04 08:29:38 -070037 // Compute the time delta needed to send probe_size bytes at bitrate_bps
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000038 // bps. Result is in milliseconds.
isheriffcc5903e2016-10-04 08:29:38 -070039 return static_cast<int>((1000ll * probe_size * 8) / bitrate_bps);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000040}
41} // namespace
42
43BitrateProber::BitrateProber()
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070044 : probing_state_(ProbingState::kDisabled),
isheriffcc5903e2016-10-04 08:29:38 -070045 probe_size_last_sent_(0),
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070046 time_last_probe_sent_ms_(-1),
47 next_cluster_id_(0) {
48 SetEnabled(true);
49}
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000050
51void BitrateProber::SetEnabled(bool enable) {
52 if (enable) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070053 if (probing_state_ == ProbingState::kDisabled) {
54 probing_state_ = ProbingState::kInactive;
55 LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000056 }
57 } else {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070058 probing_state_ = ProbingState::kDisabled;
59 LOG(LS_INFO) << "Bandwidth probing disabled";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000060 }
61}
62
63bool BitrateProber::IsProbing() const {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070064 return probing_state_ == ProbingState::kActive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000065}
66
philipel4a1ec1e2016-08-15 11:51:06 -070067void BitrateProber::OnIncomingPacket(size_t packet_size) {
Peter Boström0453ef82016-02-16 16:23:08 +010068 // Don't initialize probing unless we have something large enough to start
69 // probing.
philipel4a1ec1e2016-08-15 11:51:06 -070070 if (probing_state_ == ProbingState::kInactive &&
philipeleb680ea2016-08-17 11:11:59 +020071 !clusters_.empty() &&
philipel4a1ec1e2016-08-15 11:51:06 -070072 packet_size >= PacedSender::kMinProbePacketSize) {
73 probing_state_ = ProbingState::kActive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000074 }
philipel4a1ec1e2016-08-15 11:51:06 -070075}
76
philipelfd58b612017-01-04 07:05:25 -080077void BitrateProber::CreateProbeCluster(int bitrate_bps) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070078 RTC_DCHECK(probing_state_ != ProbingState::kDisabled);
philipel4a1ec1e2016-08-15 11:51:06 -070079 ProbeCluster cluster;
philipelfd58b612017-01-04 07:05:25 -080080 cluster.min_probes = kMinProbePacketsSent;
81 cluster.min_bytes = bitrate_bps * kMinProbeDurationMs / 8000;
82 cluster.bitrate_bps = bitrate_bps;
philipel4a1ec1e2016-08-15 11:51:06 -070083 cluster.id = next_cluster_id_++;
84 clusters_.push(cluster);
philipelfd58b612017-01-04 07:05:25 -080085
86 LOG(LS_INFO) << "Probe cluster (bitrate:min bytes:min packets): ("
87 << cluster.bitrate_bps << ":" << cluster.min_bytes << ":"
88 << cluster.min_probes << ")";
89 // If we are already probing, continue to do so. Otherwise set it to
90 // kInactive and wait for OnIncomingPacket to start the probing.
philipel4a1ec1e2016-08-15 11:51:06 -070091 if (probing_state_ != ProbingState::kActive)
92 probing_state_ = ProbingState::kInactive;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070093}
94
95void BitrateProber::ResetState() {
96 time_last_probe_sent_ms_ = -1;
isheriffcc5903e2016-10-04 08:29:38 -070097 probe_size_last_sent_ = 0;
philipel4a1ec1e2016-08-15 11:51:06 -070098
99 // Recreate all probing clusters.
100 std::queue<ProbeCluster> clusters;
101 clusters.swap(clusters_);
102 while (!clusters.empty()) {
philipelfd58b612017-01-04 07:05:25 -0800103 CreateProbeCluster(clusters.front().bitrate_bps);
philipel4a1ec1e2016-08-15 11:51:06 -0700104 clusters.pop();
105 }
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700106 // If its enabled, reset to inactive.
107 if (probing_state_ != ProbingState::kDisabled)
108 probing_state_ = ProbingState::kInactive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000109}
110
111int BitrateProber::TimeUntilNextProbe(int64_t now_ms) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700112 // Probing is not active or probing is already complete.
113 if (probing_state_ != ProbingState::kActive || clusters_.empty())
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000114 return -1;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700115 // time_last_probe_sent_ms_ of -1 indicates no probes have yet been sent.
116 int64_t elapsed_time_ms;
117 if (time_last_probe_sent_ms_ == -1) {
118 elapsed_time_ms = 0;
119 } else {
120 elapsed_time_ms = now_ms - time_last_probe_sent_ms_;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000121 }
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700122 // If no probes have been sent for a while, abort current probing and
123 // reset.
124 if (elapsed_time_ms > kInactivityThresholdMs) {
125 ResetState();
Peter Boström0453ef82016-02-16 16:23:08 +0100126 return -1;
127 }
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000128 // We will send the first probe packet immediately if no packet has been
129 // sent before.
130 int time_until_probe_ms = 0;
isheriffcc5903e2016-10-04 08:29:38 -0700131 if (probe_size_last_sent_ != 0 && probing_state_ == ProbingState::kActive) {
philipelfd58b612017-01-04 07:05:25 -0800132 int next_delta_ms = ComputeDeltaFromBitrate(probe_size_last_sent_,
133 clusters_.front().bitrate_bps);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000134 time_until_probe_ms = next_delta_ms - elapsed_time_ms;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000135 // If we have waited more than 3 ms for a new packet to probe with we will
136 // consider this probing session over.
137 const int kMaxProbeDelayMs = 3;
138 if (next_delta_ms < kMinProbeDeltaMs ||
139 time_until_probe_ms < -kMaxProbeDelayMs) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700140 probing_state_ = ProbingState::kSuspended;
141 LOG(LS_INFO) << "Delta too small or missed probing accurately, suspend";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000142 time_until_probe_ms = 0;
143 }
144 }
Stefan Holmer01b48882015-05-05 10:21:24 +0200145 return std::max(time_until_probe_ms, 0);
146}
147
philipeldd324862016-05-06 17:06:14 +0200148int BitrateProber::CurrentClusterId() const {
149 RTC_DCHECK(!clusters_.empty());
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700150 RTC_DCHECK(ProbingState::kActive == probing_state_);
philipeldd324862016-05-06 17:06:14 +0200151 return clusters_.front().id;
152}
153
isheriffcc5903e2016-10-04 08:29:38 -0700154// Probe size is recommended based on the probe bitrate required. We choose
155// a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be
156// feasible.
157size_t BitrateProber::RecommendedMinProbeSize() const {
158 RTC_DCHECK(!clusters_.empty());
philipelfd58b612017-01-04 07:05:25 -0800159 return clusters_.front().bitrate_bps * 2 * kMinProbeDeltaMs / (8 * 1000);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000160}
161
isheriffcc5903e2016-10-04 08:29:38 -0700162void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) {
163 RTC_DCHECK(probing_state_ == ProbingState::kActive);
kwibergaf476c72016-11-28 15:21:39 -0800164 RTC_DCHECK_GT(bytes, 0);
isheriffcc5903e2016-10-04 08:29:38 -0700165 probe_size_last_sent_ = bytes;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700166 time_last_probe_sent_ms_ = now_ms;
philipeldd324862016-05-06 17:06:14 +0200167 if (!clusters_.empty()) {
168 ProbeCluster* cluster = &clusters_.front();
philipelfd58b612017-01-04 07:05:25 -0800169 cluster->sent_bytes += static_cast<int>(bytes);
170 cluster->sent_probes += 1;
171 if (cluster->sent_bytes >= cluster->min_bytes &&
172 cluster->sent_probes >= 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}
179} // namespace webrtc