blob: 936be3929e99b973971de11c3f09769f9f17029b [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
Per28a44562016-05-04 17:12:51 +020026int ComputeDeltaFromBitrate(size_t packet_size, uint32_t bitrate_bps) {
philipel4a1ec1e2016-08-15 11:51:06 -070027 RTC_CHECK_GT(bitrate_bps, 0u);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000028 // Compute the time delta needed to send packet_size bytes at bitrate_bps
29 // bps. Result is in milliseconds.
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070030 return static_cast<int>((1000ll * packet_size * 8) / bitrate_bps);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000031}
32} // namespace
33
34BitrateProber::BitrateProber()
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070035 : probing_state_(ProbingState::kDisabled),
36 packet_size_last_sent_(0),
37 time_last_probe_sent_ms_(-1),
38 next_cluster_id_(0) {
39 SetEnabled(true);
40}
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000041
42void BitrateProber::SetEnabled(bool enable) {
43 if (enable) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070044 if (probing_state_ == ProbingState::kDisabled) {
45 probing_state_ = ProbingState::kInactive;
46 LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000047 }
48 } else {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070049 probing_state_ = ProbingState::kDisabled;
50 LOG(LS_INFO) << "Bandwidth probing disabled";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000051 }
52}
53
54bool BitrateProber::IsProbing() const {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070055 return probing_state_ == ProbingState::kActive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000056}
57
philipel4a1ec1e2016-08-15 11:51:06 -070058void BitrateProber::OnIncomingPacket(size_t packet_size) {
Peter Boström0453ef82016-02-16 16:23:08 +010059 // Don't initialize probing unless we have something large enough to start
60 // probing.
philipel4a1ec1e2016-08-15 11:51:06 -070061 if (probing_state_ == ProbingState::kInactive &&
philipeleb680ea2016-08-17 11:11:59 +020062 !clusters_.empty() &&
philipel4a1ec1e2016-08-15 11:51:06 -070063 packet_size >= PacedSender::kMinProbePacketSize) {
64 probing_state_ = ProbingState::kActive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000065 }
philipel4a1ec1e2016-08-15 11:51:06 -070066}
67
philipeleb680ea2016-08-17 11:11:59 +020068void BitrateProber::CreateProbeCluster(int bitrate_bps, int num_packets) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070069 RTC_DCHECK(probing_state_ != ProbingState::kDisabled);
philipel4a1ec1e2016-08-15 11:51:06 -070070 ProbeCluster cluster;
71 cluster.max_probe_packets = num_packets;
72 cluster.probe_bitrate_bps = bitrate_bps;
73 cluster.id = next_cluster_id_++;
74 clusters_.push(cluster);
75 LOG(LS_INFO) << "Probe cluster (bitrate:packets): ("
76 << cluster.probe_bitrate_bps << ":" << cluster.max_probe_packets
77 << ") ";
78 if (probing_state_ != ProbingState::kActive)
79 probing_state_ = ProbingState::kInactive;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070080}
81
82void BitrateProber::ResetState() {
83 time_last_probe_sent_ms_ = -1;
84 packet_size_last_sent_ = 0;
philipel4a1ec1e2016-08-15 11:51:06 -070085
86 // Recreate all probing clusters.
87 std::queue<ProbeCluster> clusters;
88 clusters.swap(clusters_);
89 while (!clusters.empty()) {
philipeleb680ea2016-08-17 11:11:59 +020090 CreateProbeCluster(clusters.front().probe_bitrate_bps,
91 clusters.front().max_probe_packets);
philipel4a1ec1e2016-08-15 11:51:06 -070092 clusters.pop();
93 }
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070094 // If its enabled, reset to inactive.
95 if (probing_state_ != ProbingState::kDisabled)
96 probing_state_ = ProbingState::kInactive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000097}
98
99int BitrateProber::TimeUntilNextProbe(int64_t now_ms) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700100 // Probing is not active or probing is already complete.
101 if (probing_state_ != ProbingState::kActive || clusters_.empty())
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000102 return -1;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700103 // time_last_probe_sent_ms_ of -1 indicates no probes have yet been sent.
104 int64_t elapsed_time_ms;
105 if (time_last_probe_sent_ms_ == -1) {
106 elapsed_time_ms = 0;
107 } else {
108 elapsed_time_ms = now_ms - time_last_probe_sent_ms_;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000109 }
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700110 // If no probes have been sent for a while, abort current probing and
111 // reset.
112 if (elapsed_time_ms > kInactivityThresholdMs) {
113 ResetState();
Peter Boström0453ef82016-02-16 16:23:08 +0100114 return -1;
115 }
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000116 // We will send the first probe packet immediately if no packet has been
117 // sent before.
118 int time_until_probe_ms = 0;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700119 if (packet_size_last_sent_ != 0 && probing_state_ == ProbingState::kActive) {
philipeldd324862016-05-06 17:06:14 +0200120 int next_delta_ms = ComputeDeltaFromBitrate(
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700121 packet_size_last_sent_, clusters_.front().probe_bitrate_bps);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000122 time_until_probe_ms = next_delta_ms - elapsed_time_ms;
123 // There is no point in trying to probe with less than 1 ms between packets
124 // as it essentially means trying to probe at infinite bandwidth.
125 const int kMinProbeDeltaMs = 1;
126 // 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
Stefan Holmer01b48882015-05-05 10:21:24 +0200145size_t BitrateProber::RecommendedPacketSize() const {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700146 return packet_size_last_sent_;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000147}
148
149void BitrateProber::PacketSent(int64_t now_ms, size_t packet_size) {
150 assert(packet_size > 0);
Peter Boström0453ef82016-02-16 16:23:08 +0100151 if (packet_size < PacedSender::kMinProbePacketSize)
152 return;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700153 packet_size_last_sent_ = packet_size;
154 if (probing_state_ != ProbingState::kActive)
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000155 return;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700156 time_last_probe_sent_ms_ = now_ms;
philipeldd324862016-05-06 17:06:14 +0200157 if (!clusters_.empty()) {
158 ProbeCluster* cluster = &clusters_.front();
159 ++cluster->sent_probe_packets;
160 if (cluster->sent_probe_packets == cluster->max_probe_packets)
161 clusters_.pop();
philipel1a93cde2016-06-03 01:41:45 -0700162 if (clusters_.empty())
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700163 probing_state_ = ProbingState::kSuspended;
philipeldd324862016-05-06 17:06:14 +0200164 }
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000165}
166} // namespace webrtc