1 /*
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 "modules/pacing/bitrate_prober.h"
12 
13 #include <algorithm>
14 
15 #include "logging/rtc_event_log/events/rtc_event_probe_cluster_created.h"
16 #include "logging/rtc_event_log/rtc_event_log.h"
17 #include "modules/pacing/paced_sender.h"
18 #include "rtc_base/checks.h"
19 #include "rtc_base/logging.h"
20 #include "rtc_base/ptr_util.h"
21 
22 namespace webrtc {
23 
24 namespace {
25 
26 // A minimum interval between probes to allow scheduling to be feasible.
27 constexpr int kMinProbeDeltaMs = 1;
28 
29 // The minimum number probing packets used.
30 constexpr int kMinProbePacketsSent = 5;
31 
32 // The minimum probing duration in ms.
33 constexpr int kMinProbeDurationMs = 15;
34 
35 // Maximum amount of time each probe can be delayed. Probe cluster is reset and
36 // retried from the start when this limit is reached.
37 constexpr int kMaxProbeDelayMs = 3;
38 
39 // Number of times probing is retried before the cluster is dropped.
40 constexpr int kMaxRetryAttempts = 3;
41 
42 // The min probe packet size is scaled with the bitrate we're probing at.
43 // This defines the max min probe packet size, meaning that on high bitrates
44 // we have a min probe packet size of 200 bytes.
45 constexpr size_t kMinProbePacketSize = 200;
46 
47 constexpr int64_t kProbeClusterTimeoutMs = 5000;
48 
49 }  // namespace
50 
BitrateProber()51 BitrateProber::BitrateProber() : BitrateProber(nullptr) {}
52 
BitrateProber(RtcEventLog * event_log)53 BitrateProber::BitrateProber(RtcEventLog* event_log)
54     : probing_state_(ProbingState::kDisabled),
55       next_probe_time_ms_(-1),
56       next_cluster_id_(0),
57       event_log_(event_log) {
58   SetEnabled(true);
59 }
60 
SetEnabled(bool enable)61 void BitrateProber::SetEnabled(bool enable) {
62   if (enable) {
63     if (probing_state_ == ProbingState::kDisabled) {
64       probing_state_ = ProbingState::kInactive;
65       RTC_LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive";
66     }
67   } else {
68     probing_state_ = ProbingState::kDisabled;
69     RTC_LOG(LS_INFO) << "Bandwidth probing disabled";
70   }
71 }
72 
IsProbing() const73 bool BitrateProber::IsProbing() const {
74   return probing_state_ == ProbingState::kActive;
75 }
76 
OnIncomingPacket(size_t packet_size)77 void BitrateProber::OnIncomingPacket(size_t packet_size) {
78   // Don't initialize probing unless we have something large enough to start
79   // probing.
80   if (probing_state_ == ProbingState::kInactive && !clusters_.empty() &&
81       packet_size >=
82           std::min<size_t>(RecommendedMinProbeSize(), kMinProbePacketSize)) {
83     // Send next probe right away.
84     next_probe_time_ms_ = -1;
85     probing_state_ = ProbingState::kActive;
86   }
87 }
88 
CreateProbeCluster(int bitrate_bps,int64_t now_ms)89 void BitrateProber::CreateProbeCluster(int bitrate_bps, int64_t now_ms) {
90   RTC_DCHECK(probing_state_ != ProbingState::kDisabled);
91   RTC_DCHECK_GT(bitrate_bps, 0);
92   while (!clusters_.empty() &&
93          now_ms - clusters_.front().time_created_ms > kProbeClusterTimeoutMs) {
94     clusters_.pop();
95   }
96 
97   ProbeCluster cluster;
98   cluster.time_created_ms = now_ms;
99   cluster.pace_info.probe_cluster_min_probes = kMinProbePacketsSent;
100   cluster.pace_info.probe_cluster_min_bytes =
101       bitrate_bps * kMinProbeDurationMs / 8000;
102   cluster.pace_info.send_bitrate_bps = bitrate_bps;
103   cluster.pace_info.probe_cluster_id = next_cluster_id_++;
104   clusters_.push(cluster);
105   if (event_log_)
106     event_log_->Log(rtc::MakeUnique<RtcEventProbeClusterCreated>(
107         cluster.pace_info.probe_cluster_id, cluster.pace_info.send_bitrate_bps,
108         cluster.pace_info.probe_cluster_min_probes,
109         cluster.pace_info.probe_cluster_min_bytes));
110 
111   RTC_LOG(LS_INFO) << "Probe cluster (bitrate:min bytes:min packets): ("
112                    << cluster.pace_info.send_bitrate_bps << ":"
113                    << cluster.pace_info.probe_cluster_min_bytes << ":"
114                    << cluster.pace_info.probe_cluster_min_probes << ")";
115   // If we are already probing, continue to do so. Otherwise set it to
116   // kInactive and wait for OnIncomingPacket to start the probing.
117   if (probing_state_ != ProbingState::kActive)
118     probing_state_ = ProbingState::kInactive;
119 }
120 
ResetState(int64_t now_ms)121 void BitrateProber::ResetState(int64_t now_ms) {
122   RTC_DCHECK(probing_state_ == ProbingState::kActive);
123 
124   // Recreate all probing clusters.
125   std::queue<ProbeCluster> clusters;
126   clusters.swap(clusters_);
127   while (!clusters.empty()) {
128     if (clusters.front().retries < kMaxRetryAttempts) {
129       CreateProbeCluster(clusters.front().pace_info.send_bitrate_bps, now_ms);
130       clusters_.back().retries = clusters.front().retries + 1;
131     }
132     clusters.pop();
133   }
134 
135   probing_state_ = ProbingState::kInactive;
136 }
137 
TimeUntilNextProbe(int64_t now_ms)138 int BitrateProber::TimeUntilNextProbe(int64_t now_ms) {
139   // Probing is not active or probing is already complete.
140   if (probing_state_ != ProbingState::kActive || clusters_.empty())
141     return -1;
142 
143   int time_until_probe_ms = 0;
144   if (next_probe_time_ms_ >= 0) {
145     time_until_probe_ms = next_probe_time_ms_ - now_ms;
146     if (time_until_probe_ms < -kMaxProbeDelayMs) {
147       ResetState(now_ms);
148       return -1;
149     }
150   }
151 
152   return std::max(time_until_probe_ms, 0);
153 }
154 
CurrentCluster() const155 PacedPacketInfo BitrateProber::CurrentCluster() const {
156   RTC_DCHECK(!clusters_.empty());
157   RTC_DCHECK(probing_state_ == ProbingState::kActive);
158   return clusters_.front().pace_info;
159 }
160 
161 // Probe size is recommended based on the probe bitrate required. We choose
162 // a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be
163 // feasible.
RecommendedMinProbeSize() const164 size_t BitrateProber::RecommendedMinProbeSize() const {
165   RTC_DCHECK(!clusters_.empty());
166   return clusters_.front().pace_info.send_bitrate_bps * 2 * kMinProbeDeltaMs /
167          (8 * 1000);
168 }
169 
ProbeSent(int64_t now_ms,size_t bytes)170 void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) {
171   RTC_DCHECK(probing_state_ == ProbingState::kActive);
172   RTC_DCHECK_GT(bytes, 0);
173 
174   if (!clusters_.empty()) {
175     ProbeCluster* cluster = &clusters_.front();
176     if (cluster->sent_probes == 0) {
177       RTC_DCHECK_EQ(cluster->time_started_ms, -1);
178       cluster->time_started_ms = now_ms;
179     }
180     cluster->sent_bytes += static_cast<int>(bytes);
181     cluster->sent_probes += 1;
182     next_probe_time_ms_ = GetNextProbeTime(*cluster);
183     if (cluster->sent_bytes >= cluster->pace_info.probe_cluster_min_bytes &&
184         cluster->sent_probes >= cluster->pace_info.probe_cluster_min_probes) {
185       clusters_.pop();
186     }
187     if (clusters_.empty())
188       probing_state_ = ProbingState::kSuspended;
189   }
190 }
191 
GetNextProbeTime(const ProbeCluster & cluster)192 int64_t BitrateProber::GetNextProbeTime(const ProbeCluster& cluster) {
193   RTC_CHECK_GT(cluster.pace_info.send_bitrate_bps, 0);
194   RTC_CHECK_GE(cluster.time_started_ms, 0);
195 
196   // Compute the time delta from the cluster start to ensure probe bitrate stays
197   // close to the target bitrate. Result is in milliseconds.
198   int64_t delta_ms =
199       (8000ll * cluster.sent_bytes + cluster.pace_info.send_bitrate_bps / 2) /
200       cluster.pace_info.send_bitrate_bps;
201   return cluster.time_started_ms + delta_ms;
202 }
203 
204 
205 }  // namespace webrtc
206