blob: e91e06efd555c9f8a0e9e852ad4979ee9088e1cc [file] [log] [blame]
Irfan Sheriffb2540bb2016-09-12 12:28:54 -07001/*
2 * Copyright (c) 2016 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/congestion_controller/probe_controller.h"
12
13#include <initializer_list>
14
15#include "webrtc/base/logging.h"
16
17namespace webrtc {
18
19namespace {
20
21// Number of deltas between probes per cluster. On the very first cluster,
22// we will need kProbeDeltasPerCluster + 1 probes, but on a cluster following
23// another, we need kProbeDeltasPerCluster probes.
24constexpr int kProbeDeltasPerCluster = 5;
25
26// Maximum waiting time from the time of initiating probing to getting
27// the measured results back.
28constexpr int64_t kMaxWaitingTimeForProbingResultMs = 1000;
29
30// Value of |min_bitrate_to_probe_further_bps_| that indicates
31// further probing is disabled.
32constexpr int kExponentialProbingDisabled = 0;
33
34} // namespace
35
36ProbeController::ProbeController(PacedSender* pacer, Clock* clock)
37 : pacer_(pacer),
38 clock_(clock),
39 state_(State::kInit),
40 min_bitrate_to_probe_further_bps_(kExponentialProbingDisabled),
41 time_last_probing_initiated_ms_(0),
42 estimated_bitrate_bps_(0),
43 max_bitrate_bps_(0) {}
44
45void ProbeController::SetBitrates(int min_bitrate_bps,
46 int start_bitrate_bps,
47 int max_bitrate_bps) {
48 rtc::CritScope cs(&critsect_);
49 if (state_ == State::kInit) {
50 // When probing at 1.8 Mbps ( 6x 300), this represents a threshold of
51 // 1.2 Mbps to continue probing.
52 InitiateProbing({3 * start_bitrate_bps, 6 * start_bitrate_bps},
53 4 * start_bitrate_bps);
54 }
55
56 // Only do probing if:
Irfan Sheriff9b7b7532016-09-16 11:30:44 -070057 // we are mid-call, which we consider to be if
58 // exponential probing is not active and
59 // |estimated_bitrate_bps_| is valid (> 0) and
60 // the current bitrate is lower than the new |max_bitrate_bps|, and
61 // we actually want to increase the |max_bitrate_bps_|.
62 if (state_ != State::kWaitingForProbingResult &&
63 estimated_bitrate_bps_ != 0 && estimated_bitrate_bps_ < max_bitrate_bps &&
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070064 max_bitrate_bps > max_bitrate_bps_) {
65 InitiateProbing({max_bitrate_bps}, kExponentialProbingDisabled);
66 }
67 max_bitrate_bps_ = max_bitrate_bps;
68}
69
70void ProbeController::SetEstimatedBitrate(int bitrate_bps) {
71 rtc::CritScope cs(&critsect_);
72 if (state_ == State::kWaitingForProbingResult) {
73 if ((clock_->TimeInMilliseconds() - time_last_probing_initiated_ms_) >
74 kMaxWaitingTimeForProbingResultMs) {
75 LOG(LS_INFO) << "kWaitingForProbingResult: timeout";
76 state_ = State::kProbingComplete;
77 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
78 } else {
79 // Continue probing if probing results indicate channel has greater
80 // capacity.
81 LOG(LS_INFO) << "Measured bitrate: " << bitrate_bps
82 << " Minimum to probe further: "
83 << min_bitrate_to_probe_further_bps_;
84 if (min_bitrate_to_probe_further_bps_ != kExponentialProbingDisabled &&
85 bitrate_bps > min_bitrate_to_probe_further_bps_) {
86 // Double the probing bitrate and expect a minimum of 25% gain to
87 // continue probing.
88 InitiateProbing({2 * bitrate_bps}, 1.25 * bitrate_bps);
89 }
90 }
91 }
92 estimated_bitrate_bps_ = bitrate_bps;
93}
94
95void ProbeController::InitiateProbing(
96 std::initializer_list<int> bitrates_to_probe,
97 int min_bitrate_to_probe_further_bps) {
98 bool first_cluster = true;
99 for (int bitrate : bitrates_to_probe) {
100 if (first_cluster) {
101 pacer_->CreateProbeCluster(bitrate, kProbeDeltasPerCluster + 1);
102 first_cluster = false;
103 } else {
104 pacer_->CreateProbeCluster(bitrate, kProbeDeltasPerCluster);
105 }
106 }
107 min_bitrate_to_probe_further_bps_ = min_bitrate_to_probe_further_bps;
108 time_last_probing_initiated_ms_ = clock_->TimeInMilliseconds();
109 if (min_bitrate_to_probe_further_bps == kExponentialProbingDisabled)
110 state_ = State::kProbingComplete;
111 else
112 state_ = State::kWaitingForProbingResult;
113}
114
115} // namespace webrtc