blob: 4b2a977bbebcdcab302e24c762e679ae0cd87d0e [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 &&
62 packet_size >= PacedSender::kMinProbePacketSize) {
63 probing_state_ = ProbingState::kActive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000064 }
philipel4a1ec1e2016-08-15 11:51:06 -070065}
66
67void BitrateProber::ProbeAtBitrate(uint32_t bitrate_bps, int num_packets) {
68 ProbeCluster cluster;
69 cluster.max_probe_packets = num_packets;
70 cluster.probe_bitrate_bps = bitrate_bps;
71 cluster.id = next_cluster_id_++;
72 clusters_.push(cluster);
73 LOG(LS_INFO) << "Probe cluster (bitrate:packets): ("
74 << cluster.probe_bitrate_bps << ":" << cluster.max_probe_packets
75 << ") ";
76 if (probing_state_ != ProbingState::kActive)
77 probing_state_ = ProbingState::kInactive;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070078}
79
80void BitrateProber::ResetState() {
81 time_last_probe_sent_ms_ = -1;
82 packet_size_last_sent_ = 0;
philipel4a1ec1e2016-08-15 11:51:06 -070083
84 // Recreate all probing clusters.
85 std::queue<ProbeCluster> clusters;
86 clusters.swap(clusters_);
87 while (!clusters.empty()) {
88 ProbeAtBitrate(clusters.front().probe_bitrate_bps,
89 clusters.front().max_probe_packets);
90 clusters.pop();
91 }
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070092 // If its enabled, reset to inactive.
93 if (probing_state_ != ProbingState::kDisabled)
94 probing_state_ = ProbingState::kInactive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000095}
96
97int BitrateProber::TimeUntilNextProbe(int64_t now_ms) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070098 // Probing is not active or probing is already complete.
99 if (probing_state_ != ProbingState::kActive || clusters_.empty())
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000100 return -1;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700101 // time_last_probe_sent_ms_ of -1 indicates no probes have yet been sent.
102 int64_t elapsed_time_ms;
103 if (time_last_probe_sent_ms_ == -1) {
104 elapsed_time_ms = 0;
105 } else {
106 elapsed_time_ms = now_ms - time_last_probe_sent_ms_;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000107 }
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700108 // If no probes have been sent for a while, abort current probing and
109 // reset.
110 if (elapsed_time_ms > kInactivityThresholdMs) {
111 ResetState();
Peter Boström0453ef82016-02-16 16:23:08 +0100112 return -1;
113 }
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000114 // We will send the first probe packet immediately if no packet has been
115 // sent before.
116 int time_until_probe_ms = 0;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700117 if (packet_size_last_sent_ != 0 && probing_state_ == ProbingState::kActive) {
philipeldd324862016-05-06 17:06:14 +0200118 int next_delta_ms = ComputeDeltaFromBitrate(
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700119 packet_size_last_sent_, clusters_.front().probe_bitrate_bps);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000120 time_until_probe_ms = next_delta_ms - elapsed_time_ms;
121 // There is no point in trying to probe with less than 1 ms between packets
122 // as it essentially means trying to probe at infinite bandwidth.
123 const int kMinProbeDeltaMs = 1;
124 // If we have waited more than 3 ms for a new packet to probe with we will
125 // consider this probing session over.
126 const int kMaxProbeDelayMs = 3;
127 if (next_delta_ms < kMinProbeDeltaMs ||
128 time_until_probe_ms < -kMaxProbeDelayMs) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700129 probing_state_ = ProbingState::kSuspended;
130 LOG(LS_INFO) << "Delta too small or missed probing accurately, suspend";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000131 time_until_probe_ms = 0;
132 }
133 }
Stefan Holmer01b48882015-05-05 10:21:24 +0200134 return std::max(time_until_probe_ms, 0);
135}
136
philipeldd324862016-05-06 17:06:14 +0200137int BitrateProber::CurrentClusterId() const {
138 RTC_DCHECK(!clusters_.empty());
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700139 RTC_DCHECK(ProbingState::kActive == probing_state_);
philipeldd324862016-05-06 17:06:14 +0200140 return clusters_.front().id;
141}
142
Stefan Holmer01b48882015-05-05 10:21:24 +0200143size_t BitrateProber::RecommendedPacketSize() const {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700144 return packet_size_last_sent_;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000145}
146
147void BitrateProber::PacketSent(int64_t now_ms, size_t packet_size) {
148 assert(packet_size > 0);
Peter Boström0453ef82016-02-16 16:23:08 +0100149 if (packet_size < PacedSender::kMinProbePacketSize)
150 return;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700151 packet_size_last_sent_ = packet_size;
152 if (probing_state_ != ProbingState::kActive)
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000153 return;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700154 time_last_probe_sent_ms_ = now_ms;
philipeldd324862016-05-06 17:06:14 +0200155 if (!clusters_.empty()) {
156 ProbeCluster* cluster = &clusters_.front();
157 ++cluster->sent_probe_packets;
158 if (cluster->sent_probe_packets == cluster->max_probe_packets)
159 clusters_.pop();
philipel1a93cde2016-06-03 01:41:45 -0700160 if (clusters_.empty())
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700161 probing_state_ = ProbingState::kSuspended;
philipeldd324862016-05-06 17:06:14 +0200162 }
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000163}
164} // namespace webrtc