blob: 1dc77c5d607c8383d2f21bcb86a6c43df54be5c4 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/pacing/bitrate_prober.h"
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000012
Stefan Holmer01b48882015-05-05 10:21:24 +020013#include <algorithm>
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000014
Elad Alon4a87e1c2017-10-03 16:11:34 +020015#include "logging/rtc_event_log/events/rtc_event_probe_cluster_created.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#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"
Elad Alon4a87e1c2017-10-03 16:11:34 +020020#include "rtc_base/ptr_util.h"
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000021
22namespace webrtc {
23
24namespace {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070025
isheriffcc5903e2016-10-04 08:29:38 -070026// A minimum interval between probes to allow scheduling to be feasible.
27constexpr int kMinProbeDeltaMs = 1;
28
philipelfd58b612017-01-04 07:05:25 -080029// The minimum number probing packets used.
30constexpr int kMinProbePacketsSent = 5;
31
32// The minimum probing duration in ms.
33constexpr int kMinProbeDurationMs = 15;
34
sergeyu6dbbd892017-01-17 15:07:59 -080035// Maximum amount of time each probe can be delayed. Probe cluster is reset and
36// retried from the start when this limit is reached.
37constexpr int kMaxProbeDelayMs = 3;
38
39// Number of times probing is retried before the cluster is dropped.
40constexpr int kMaxRetryAttempts = 3;
41
stefanf00497c2017-01-27 02:27:33 -080042// 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.
45constexpr size_t kMinProbePacketSize = 200;
46
Stefan Holmer0e3213a2017-02-08 15:19:05 +010047constexpr int64_t kProbeClusterTimeoutMs = 5000;
48
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000049} // namespace
50
philipelc3b3f7a2017-03-29 01:23:13 -070051BitrateProber::BitrateProber() : BitrateProber(nullptr) {}
52
53BitrateProber::BitrateProber(RtcEventLog* event_log)
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070054 : probing_state_(ProbingState::kDisabled),
sergeyu6dbbd892017-01-17 15:07:59 -080055 next_probe_time_ms_(-1),
philipelc3b3f7a2017-03-29 01:23:13 -070056 next_cluster_id_(0),
57 event_log_(event_log) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070058 SetEnabled(true);
59}
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000060
61void BitrateProber::SetEnabled(bool enable) {
62 if (enable) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070063 if (probing_state_ == ProbingState::kDisabled) {
64 probing_state_ = ProbingState::kInactive;
Mirko Bonadei675513b2017-11-09 11:09:25 +010065 RTC_LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000066 }
67 } else {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070068 probing_state_ = ProbingState::kDisabled;
Mirko Bonadei675513b2017-11-09 11:09:25 +010069 RTC_LOG(LS_INFO) << "Bandwidth probing disabled";
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000070 }
71}
72
73bool BitrateProber::IsProbing() const {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070074 return probing_state_ == ProbingState::kActive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000075}
76
philipel4a1ec1e2016-08-15 11:51:06 -070077void BitrateProber::OnIncomingPacket(size_t packet_size) {
Peter Boström0453ef82016-02-16 16:23:08 +010078 // Don't initialize probing unless we have something large enough to start
79 // probing.
stefanf00497c2017-01-27 02:27:33 -080080 if (probing_state_ == ProbingState::kInactive && !clusters_.empty() &&
81 packet_size >=
82 std::min<size_t>(RecommendedMinProbeSize(), kMinProbePacketSize)) {
sergeyu6dbbd892017-01-17 15:07:59 -080083 // Send next probe right away.
84 next_probe_time_ms_ = -1;
philipel4a1ec1e2016-08-15 11:51:06 -070085 probing_state_ = ProbingState::kActive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000086 }
philipel4a1ec1e2016-08-15 11:51:06 -070087}
88
Stefan Holmer0e3213a2017-02-08 15:19:05 +010089void BitrateProber::CreateProbeCluster(int bitrate_bps, int64_t now_ms) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070090 RTC_DCHECK(probing_state_ != ProbingState::kDisabled);
philipeld1d247f2017-05-04 08:35:52 -070091 RTC_DCHECK_GT(bitrate_bps, 0);
Stefan Holmer0e3213a2017-02-08 15:19:05 +010092 while (!clusters_.empty() &&
93 now_ms - clusters_.front().time_created_ms > kProbeClusterTimeoutMs) {
94 clusters_.pop();
95 }
96
philipel4a1ec1e2016-08-15 11:51:06 -070097 ProbeCluster cluster;
Stefan Holmer0e3213a2017-02-08 15:19:05 +010098 cluster.time_created_ms = now_ms;
philipelc7bf32a2017-02-17 03:59:43 -080099 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_++;
philipel4a1ec1e2016-08-15 11:51:06 -0700104 clusters_.push(cluster);
philipelc3b3f7a2017-03-29 01:23:13 -0700105 if (event_log_)
Elad Alon4a87e1c2017-10-03 16:11:34 +0200106 event_log_->Log(rtc::MakeUnique<RtcEventProbeClusterCreated>(
philipelc3b3f7a2017-03-29 01:23:13 -0700107 cluster.pace_info.probe_cluster_id, cluster.pace_info.send_bitrate_bps,
108 cluster.pace_info.probe_cluster_min_probes,
Elad Alon4a87e1c2017-10-03 16:11:34 +0200109 cluster.pace_info.probe_cluster_min_bytes));
philipelfd58b612017-01-04 07:05:25 -0800110
Mirko Bonadei675513b2017-11-09 11:09:25 +0100111 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 << ")";
philipelfd58b612017-01-04 07:05:25 -0800115 // If we are already probing, continue to do so. Otherwise set it to
116 // kInactive and wait for OnIncomingPacket to start the probing.
philipel4a1ec1e2016-08-15 11:51:06 -0700117 if (probing_state_ != ProbingState::kActive)
118 probing_state_ = ProbingState::kInactive;
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700119}
120
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100121void BitrateProber::ResetState(int64_t now_ms) {
sergeyu6dbbd892017-01-17 15:07:59 -0800122 RTC_DCHECK(probing_state_ == ProbingState::kActive);
sprangebfbc8e2017-01-10 01:27:28 -0800123
philipel4a1ec1e2016-08-15 11:51:06 -0700124 // Recreate all probing clusters.
125 std::queue<ProbeCluster> clusters;
126 clusters.swap(clusters_);
127 while (!clusters.empty()) {
sergeyu6dbbd892017-01-17 15:07:59 -0800128 if (clusters.front().retries < kMaxRetryAttempts) {
philipelc7bf32a2017-02-17 03:59:43 -0800129 CreateProbeCluster(clusters.front().pace_info.send_bitrate_bps, now_ms);
sergeyu6dbbd892017-01-17 15:07:59 -0800130 clusters_.back().retries = clusters.front().retries + 1;
131 }
philipel4a1ec1e2016-08-15 11:51:06 -0700132 clusters.pop();
133 }
sergeyu6dbbd892017-01-17 15:07:59 -0800134
135 probing_state_ = ProbingState::kInactive;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000136}
137
138int BitrateProber::TimeUntilNextProbe(int64_t now_ms) {
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700139 // Probing is not active or probing is already complete.
140 if (probing_state_ != ProbingState::kActive || clusters_.empty())
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000141 return -1;
sergeyu6dbbd892017-01-17 15:07:59 -0800142
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000143 int time_until_probe_ms = 0;
sergeyu6dbbd892017-01-17 15:07:59 -0800144 if (next_probe_time_ms_ >= 0) {
145 time_until_probe_ms = next_probe_time_ms_ - now_ms;
146 if (time_until_probe_ms < -kMaxProbeDelayMs) {
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100147 ResetState(now_ms);
sergeyu6dbbd892017-01-17 15:07:59 -0800148 return -1;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000149 }
150 }
sergeyu6dbbd892017-01-17 15:07:59 -0800151
Stefan Holmer01b48882015-05-05 10:21:24 +0200152 return std::max(time_until_probe_ms, 0);
153}
154
philipelc7bf32a2017-02-17 03:59:43 -0800155PacedPacketInfo BitrateProber::CurrentCluster() const {
philipeldd324862016-05-06 17:06:14 +0200156 RTC_DCHECK(!clusters_.empty());
philipeld1d247f2017-05-04 08:35:52 -0700157 RTC_DCHECK(probing_state_ == ProbingState::kActive);
philipelc7bf32a2017-02-17 03:59:43 -0800158 return clusters_.front().pace_info;
philipeldd324862016-05-06 17:06:14 +0200159}
160
isheriffcc5903e2016-10-04 08:29:38 -0700161// 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.
164size_t BitrateProber::RecommendedMinProbeSize() const {
165 RTC_DCHECK(!clusters_.empty());
philipelc7bf32a2017-02-17 03:59:43 -0800166 return clusters_.front().pace_info.send_bitrate_bps * 2 * kMinProbeDeltaMs /
167 (8 * 1000);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000168}
169
isheriffcc5903e2016-10-04 08:29:38 -0700170void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) {
171 RTC_DCHECK(probing_state_ == ProbingState::kActive);
kwibergaf476c72016-11-28 15:21:39 -0800172 RTC_DCHECK_GT(bytes, 0);
sergeyu6dbbd892017-01-17 15:07:59 -0800173
philipeldd324862016-05-06 17:06:14 +0200174 if (!clusters_.empty()) {
175 ProbeCluster* cluster = &clusters_.front();
sergeyu6dbbd892017-01-17 15:07:59 -0800176 if (cluster->sent_probes == 0) {
177 RTC_DCHECK_EQ(cluster->time_started_ms, -1);
178 cluster->time_started_ms = now_ms;
179 }
philipelfd58b612017-01-04 07:05:25 -0800180 cluster->sent_bytes += static_cast<int>(bytes);
181 cluster->sent_probes += 1;
philipeld1d247f2017-05-04 08:35:52 -0700182 next_probe_time_ms_ = GetNextProbeTime(*cluster);
philipelc7bf32a2017-02-17 03:59:43 -0800183 if (cluster->sent_bytes >= cluster->pace_info.probe_cluster_min_bytes &&
184 cluster->sent_probes >= cluster->pace_info.probe_cluster_min_probes) {
philipeldd324862016-05-06 17:06:14 +0200185 clusters_.pop();
philipelfd58b612017-01-04 07:05:25 -0800186 }
philipel1a93cde2016-06-03 01:41:45 -0700187 if (clusters_.empty())
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700188 probing_state_ = ProbingState::kSuspended;
philipeldd324862016-05-06 17:06:14 +0200189 }
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000190}
sergeyu6dbbd892017-01-17 15:07:59 -0800191
192int64_t BitrateProber::GetNextProbeTime(const ProbeCluster& cluster) {
philipelc7bf32a2017-02-17 03:59:43 -0800193 RTC_CHECK_GT(cluster.pace_info.send_bitrate_bps, 0);
sergeyu6dbbd892017-01-17 15:07:59 -0800194 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.
philipelc7bf32a2017-02-17 03:59:43 -0800198 int64_t delta_ms =
199 (8000ll * cluster.sent_bytes + cluster.pace_info.send_bitrate_bps / 2) /
200 cluster.pace_info.send_bitrate_bps;
sergeyu6dbbd892017-01-17 15:07:59 -0800201 return cluster.time_started_ms + delta_ms;
202}
203
204
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000205} // namespace webrtc