blob: fde2f655520e195cdb663244e27d5bc093234f49 [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
29int ComputeDeltaFromBitrate(size_t probe_size, uint32_t bitrate_bps) {
philipel4a1ec1e2016-08-15 11:51:06 -070030 RTC_CHECK_GT(bitrate_bps, 0u);
isheriffcc5903e2016-10-04 08:29:38 -070031 // Compute the time delta needed to send probe_size bytes at bitrate_bps
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000032 // bps. Result is in milliseconds.
isheriffcc5903e2016-10-04 08:29:38 -070033 return static_cast<int>((1000ll * probe_size * 8) / bitrate_bps);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000034}
35} // namespace
36
37BitrateProber::BitrateProber()
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070038 : probing_state_(ProbingState::kDisabled),
isheriffcc5903e2016-10-04 08:29:38 -070039 probe_size_last_sent_(0),
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070040 time_last_probe_sent_ms_(-1),
41 next_cluster_id_(0) {
42 SetEnabled(true);
43}
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000044
45void BitrateProber::SetEnabled(bool enable) {
46 if (enable) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070047 if (probing_state_ == ProbingState::kDisabled) {
48 probing_state_ = ProbingState::kInactive;
49 LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000050 }
51 } else {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070052 probing_state_ = ProbingState::kDisabled;
53 LOG(LS_INFO) << "Bandwidth probing disabled";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000054 }
55}
56
57bool BitrateProber::IsProbing() const {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070058 return probing_state_ == ProbingState::kActive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000059}
60
philipel4a1ec1e2016-08-15 11:51:06 -070061void BitrateProber::OnIncomingPacket(size_t packet_size) {
Peter Boström0453ef82016-02-16 16:23:08 +010062 // Don't initialize probing unless we have something large enough to start
63 // probing.
philipel4a1ec1e2016-08-15 11:51:06 -070064 if (probing_state_ == ProbingState::kInactive &&
philipeleb680ea2016-08-17 11:11:59 +020065 !clusters_.empty() &&
philipel4a1ec1e2016-08-15 11:51:06 -070066 packet_size >= PacedSender::kMinProbePacketSize) {
67 probing_state_ = ProbingState::kActive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000068 }
philipel4a1ec1e2016-08-15 11:51:06 -070069}
70
isheriffcc5903e2016-10-04 08:29:38 -070071void BitrateProber::CreateProbeCluster(int bitrate_bps, int num_probes) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070072 RTC_DCHECK(probing_state_ != ProbingState::kDisabled);
philipel4a1ec1e2016-08-15 11:51:06 -070073 ProbeCluster cluster;
isheriffcc5903e2016-10-04 08:29:38 -070074 cluster.max_probes = num_probes;
philipel4a1ec1e2016-08-15 11:51:06 -070075 cluster.probe_bitrate_bps = bitrate_bps;
76 cluster.id = next_cluster_id_++;
77 clusters_.push(cluster);
isheriffcc5903e2016-10-04 08:29:38 -070078 LOG(LS_INFO) << "Probe cluster (bitrate:probes): ("
79 << cluster.probe_bitrate_bps << ":" << cluster.max_probes
philipel4a1ec1e2016-08-15 11:51:06 -070080 << ") ";
81 if (probing_state_ != ProbingState::kActive)
82 probing_state_ = ProbingState::kInactive;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070083}
84
85void BitrateProber::ResetState() {
86 time_last_probe_sent_ms_ = -1;
isheriffcc5903e2016-10-04 08:29:38 -070087 probe_size_last_sent_ = 0;
philipel4a1ec1e2016-08-15 11:51:06 -070088
89 // Recreate all probing clusters.
90 std::queue<ProbeCluster> clusters;
91 clusters.swap(clusters_);
92 while (!clusters.empty()) {
philipeleb680ea2016-08-17 11:11:59 +020093 CreateProbeCluster(clusters.front().probe_bitrate_bps,
isheriffcc5903e2016-10-04 08:29:38 -070094 clusters.front().max_probes);
philipel4a1ec1e2016-08-15 11:51:06 -070095 clusters.pop();
96 }
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070097 // If its enabled, reset to inactive.
98 if (probing_state_ != ProbingState::kDisabled)
99 probing_state_ = ProbingState::kInactive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000100}
101
102int BitrateProber::TimeUntilNextProbe(int64_t now_ms) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700103 // Probing is not active or probing is already complete.
104 if (probing_state_ != ProbingState::kActive || clusters_.empty())
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000105 return -1;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700106 // time_last_probe_sent_ms_ of -1 indicates no probes have yet been sent.
107 int64_t elapsed_time_ms;
108 if (time_last_probe_sent_ms_ == -1) {
109 elapsed_time_ms = 0;
110 } else {
111 elapsed_time_ms = now_ms - time_last_probe_sent_ms_;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000112 }
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700113 // If no probes have been sent for a while, abort current probing and
114 // reset.
115 if (elapsed_time_ms > kInactivityThresholdMs) {
116 ResetState();
Peter Boström0453ef82016-02-16 16:23:08 +0100117 return -1;
118 }
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000119 // We will send the first probe packet immediately if no packet has been
120 // sent before.
121 int time_until_probe_ms = 0;
isheriffcc5903e2016-10-04 08:29:38 -0700122 if (probe_size_last_sent_ != 0 && probing_state_ == ProbingState::kActive) {
philipeldd324862016-05-06 17:06:14 +0200123 int next_delta_ms = ComputeDeltaFromBitrate(
isheriffcc5903e2016-10-04 08:29:38 -0700124 probe_size_last_sent_, clusters_.front().probe_bitrate_bps);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000125 time_until_probe_ms = next_delta_ms - elapsed_time_ms;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000126 // If we have waited more than 3 ms for a new packet to probe with we will
127 // consider this probing session over.
128 const int kMaxProbeDelayMs = 3;
129 if (next_delta_ms < kMinProbeDeltaMs ||
130 time_until_probe_ms < -kMaxProbeDelayMs) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700131 probing_state_ = ProbingState::kSuspended;
132 LOG(LS_INFO) << "Delta too small or missed probing accurately, suspend";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000133 time_until_probe_ms = 0;
134 }
135 }
Stefan Holmer01b48882015-05-05 10:21:24 +0200136 return std::max(time_until_probe_ms, 0);
137}
138
philipeldd324862016-05-06 17:06:14 +0200139int BitrateProber::CurrentClusterId() const {
140 RTC_DCHECK(!clusters_.empty());
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700141 RTC_DCHECK(ProbingState::kActive == probing_state_);
philipeldd324862016-05-06 17:06:14 +0200142 return clusters_.front().id;
143}
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());
150 return clusters_.front().probe_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);
156 RTC_DCHECK_GT(bytes, 0u);
157 probe_size_last_sent_ = bytes;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700158 time_last_probe_sent_ms_ = now_ms;
philipeldd324862016-05-06 17:06:14 +0200159 if (!clusters_.empty()) {
160 ProbeCluster* cluster = &clusters_.front();
isheriffcc5903e2016-10-04 08:29:38 -0700161 ++cluster->sent_probes;
162 if (cluster->sent_probes == cluster->max_probes)
philipeldd324862016-05-06 17:06:14 +0200163 clusters_.pop();
philipel1a93cde2016-06-03 01:41:45 -0700164 if (clusters_.empty())
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700165 probing_state_ = ProbingState::kSuspended;
philipeldd324862016-05-06 17:06:14 +0200166 }
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000167}
168} // namespace webrtc