blob: e2cea7330589729552923e6cbfb7e343ef569e12 [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) {
philipel4a1ec1e2016-08-15 11:51:06 -070069 ProbeCluster cluster;
70 cluster.max_probe_packets = num_packets;
71 cluster.probe_bitrate_bps = bitrate_bps;
72 cluster.id = next_cluster_id_++;
73 clusters_.push(cluster);
74 LOG(LS_INFO) << "Probe cluster (bitrate:packets): ("
75 << cluster.probe_bitrate_bps << ":" << cluster.max_probe_packets
76 << ") ";
77 if (probing_state_ != ProbingState::kActive)
78 probing_state_ = ProbingState::kInactive;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070079}
80
81void BitrateProber::ResetState() {
82 time_last_probe_sent_ms_ = -1;
83 packet_size_last_sent_ = 0;
philipel4a1ec1e2016-08-15 11:51:06 -070084
85 // Recreate all probing clusters.
86 std::queue<ProbeCluster> clusters;
87 clusters.swap(clusters_);
88 while (!clusters.empty()) {
philipeleb680ea2016-08-17 11:11:59 +020089 CreateProbeCluster(clusters.front().probe_bitrate_bps,
90 clusters.front().max_probe_packets);
philipel4a1ec1e2016-08-15 11:51:06 -070091 clusters.pop();
92 }
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070093 // If its enabled, reset to inactive.
94 if (probing_state_ != ProbingState::kDisabled)
95 probing_state_ = ProbingState::kInactive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000096}
97
98int BitrateProber::TimeUntilNextProbe(int64_t now_ms) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070099 // Probing is not active or probing is already complete.
100 if (probing_state_ != ProbingState::kActive || clusters_.empty())
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000101 return -1;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700102 // time_last_probe_sent_ms_ of -1 indicates no probes have yet been sent.
103 int64_t elapsed_time_ms;
104 if (time_last_probe_sent_ms_ == -1) {
105 elapsed_time_ms = 0;
106 } else {
107 elapsed_time_ms = now_ms - time_last_probe_sent_ms_;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000108 }
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700109 // If no probes have been sent for a while, abort current probing and
110 // reset.
111 if (elapsed_time_ms > kInactivityThresholdMs) {
112 ResetState();
Peter Boström0453ef82016-02-16 16:23:08 +0100113 return -1;
114 }
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000115 // We will send the first probe packet immediately if no packet has been
116 // sent before.
117 int time_until_probe_ms = 0;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700118 if (packet_size_last_sent_ != 0 && probing_state_ == ProbingState::kActive) {
philipeldd324862016-05-06 17:06:14 +0200119 int next_delta_ms = ComputeDeltaFromBitrate(
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700120 packet_size_last_sent_, clusters_.front().probe_bitrate_bps);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000121 time_until_probe_ms = next_delta_ms - elapsed_time_ms;
122 // There is no point in trying to probe with less than 1 ms between packets
123 // as it essentially means trying to probe at infinite bandwidth.
124 const int kMinProbeDeltaMs = 1;
125 // If we have waited more than 3 ms for a new packet to probe with we will
126 // consider this probing session over.
127 const int kMaxProbeDelayMs = 3;
128 if (next_delta_ms < kMinProbeDeltaMs ||
129 time_until_probe_ms < -kMaxProbeDelayMs) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700130 probing_state_ = ProbingState::kSuspended;
131 LOG(LS_INFO) << "Delta too small or missed probing accurately, suspend";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000132 time_until_probe_ms = 0;
133 }
134 }
Stefan Holmer01b48882015-05-05 10:21:24 +0200135 return std::max(time_until_probe_ms, 0);
136}
137
philipeldd324862016-05-06 17:06:14 +0200138int BitrateProber::CurrentClusterId() const {
139 RTC_DCHECK(!clusters_.empty());
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700140 RTC_DCHECK(ProbingState::kActive == probing_state_);
philipeldd324862016-05-06 17:06:14 +0200141 return clusters_.front().id;
142}
143
Stefan Holmer01b48882015-05-05 10:21:24 +0200144size_t BitrateProber::RecommendedPacketSize() const {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700145 return packet_size_last_sent_;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000146}
147
148void BitrateProber::PacketSent(int64_t now_ms, size_t packet_size) {
149 assert(packet_size > 0);
Peter Boström0453ef82016-02-16 16:23:08 +0100150 if (packet_size < PacedSender::kMinProbePacketSize)
151 return;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700152 packet_size_last_sent_ = packet_size;
153 if (probing_state_ != ProbingState::kActive)
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000154 return;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700155 time_last_probe_sent_ms_ = now_ms;
philipeldd324862016-05-06 17:06:14 +0200156 if (!clusters_.empty()) {
157 ProbeCluster* cluster = &clusters_.front();
158 ++cluster->sent_probe_packets;
159 if (cluster->sent_probe_packets == cluster->max_probe_packets)
160 clusters_.pop();
philipel1a93cde2016-06-03 01:41:45 -0700161 if (clusters_.empty())
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700162 probing_state_ = ProbingState::kSuspended;
philipeldd324862016-05-06 17:06:14 +0200163 }
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000164}
165} // namespace webrtc