blob: b016faba2ee3f987951452faa1ea79048de15678 [file] [log] [blame]
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +00001/*
2 * Copyright (c) 2012 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 * FEC and NACK added bitrate is handled outside class
11 */
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#ifndef MODULES_BITRATE_CONTROLLER_SEND_SIDE_BANDWIDTH_ESTIMATION_H_
14#define MODULES_BITRATE_CONTROLLER_SEND_SIDE_BANDWIDTH_ESTIMATION_H_
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000015
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <stdint.h>
andresp@webrtc.org44caf012014-03-26 21:00:21 +000017#include <deque>
jbauchf91e6d02016-01-24 23:05:21 -080018#include <utility>
19#include <vector>
andresp@webrtc.org44caf012014-03-26 21:00:21 +000020
Sebastian Jansson7c1744d2018-10-08 11:00:50 +020021#include "absl/types/optional.h"
Yves Gerey988cc082018-10-23 12:03:01 +020022#include "api/transport/network_types.h"
23#include "api/units/data_rate.h"
24#include "api/units/time_delta.h"
25#include "api/units/timestamp.h"
Christoffer Rodbro3a837482018-11-19 15:30:23 +010026#include "modules/bitrate_controller/loss_based_bandwidth_estimation.h"
27#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Sebastian Jansson2e068e82018-10-08 12:49:53 +020028#include "rtc_base/experiments/field_trial_parser.h"
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000029
30namespace webrtc {
terelius006d93d2015-11-05 12:02:15 -080031
32class RtcEventLog;
33
Sebastian Jansson57f3ad02018-11-23 14:49:18 +010034class LinkCapacityTracker {
35 public:
36 LinkCapacityTracker();
37 ~LinkCapacityTracker();
38 void OnOveruse(DataRate acknowledged_rate, Timestamp at_time);
39 void OnStartingRate(DataRate start_rate);
40 void OnRateUpdate(DataRate acknowledged, Timestamp at_time);
41 void OnRttBackoff(DataRate backoff_rate, Timestamp at_time);
42 DataRate estimate() const;
43
44 private:
45 FieldTrialParameter<TimeDelta> tracking_rate;
46 double capacity_estimate_bps_ = 0;
47 Timestamp last_link_capacity_update_ = Timestamp::MinusInfinity();
48};
49
Sebastian Jansson24643482018-11-14 14:19:45 +010050class RttBasedBackoff {
51 public:
52 RttBasedBackoff();
53 ~RttBasedBackoff();
54 void OnRouteChange();
55 void UpdatePropagationRtt(Timestamp at_time, TimeDelta propagation_rtt);
56 TimeDelta RttLowerBound(Timestamp at_time) const;
57
58 FieldTrialParameter<TimeDelta> rtt_limit_;
59 FieldTrialParameter<double> drop_fraction_;
60 FieldTrialParameter<TimeDelta> drop_interval_;
61 FieldTrialFlag persist_on_route_change_;
62
63 public:
64 Timestamp last_propagation_rtt_update_;
65 TimeDelta last_propagation_rtt_;
Sebastian Jansson2e068e82018-10-08 12:49:53 +020066};
67
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000068class SendSideBandwidthEstimation {
69 public:
ivoc14d5dbe2016-07-04 07:06:55 -070070 SendSideBandwidthEstimation() = delete;
71 explicit SendSideBandwidthEstimation(RtcEventLog* event_log);
Sebastian Jansson2e068e82018-10-08 12:49:53 +020072 ~SendSideBandwidthEstimation();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000073
Sebastian Jansson24643482018-11-14 14:19:45 +010074 void OnRouteChange();
Stefan Holmere5904162015-03-26 11:11:06 +010075 void CurrentEstimate(int* bitrate, uint8_t* loss, int64_t* rtt) const;
Sebastian Jansson57f3ad02018-11-23 14:49:18 +010076 DataRate GetEstimatedLinkCapacity() const;
andresp@webrtc.org44caf012014-03-26 21:00:21 +000077 // Call periodically to update estimate.
Sebastian Jansson7c1744d2018-10-08 11:00:50 +020078 void UpdateEstimate(Timestamp at_time);
Sebastian Jansson2e068e82018-10-08 12:49:53 +020079 void OnSentPacket(SentPacket sent_packet);
Sebastian Jansson24643482018-11-14 14:19:45 +010080 void UpdatePropagationRtt(Timestamp at_time, TimeDelta propagation_rtt);
andresp@webrtc.org44caf012014-03-26 21:00:21 +000081
andresp@webrtc.org07bc7342014-03-21 16:51:01 +000082 // Call when we receive a RTCP message with TMMBR or REMB.
Sebastian Jansson7c1744d2018-10-08 11:00:50 +020083 void UpdateReceiverEstimate(Timestamp at_time, DataRate bandwidth);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000084
stefan32f81542016-01-20 07:13:58 -080085 // Call when a new delay-based estimate is available.
Sebastian Jansson7c1744d2018-10-08 11:00:50 +020086 void UpdateDelayBasedEstimate(Timestamp at_time, DataRate bitrate);
stefan32f81542016-01-20 07:13:58 -080087
andresp@webrtc.org07bc7342014-03-21 16:51:01 +000088 // Call when we receive a RTCP message with a ReceiveBlock.
89 void UpdateReceiverBlock(uint8_t fraction_loss,
Sebastian Jansson7c1744d2018-10-08 11:00:50 +020090 TimeDelta rtt_ms,
andresp@webrtc.org07bc7342014-03-21 16:51:01 +000091 int number_of_packets,
Sebastian Jansson7c1744d2018-10-08 11:00:50 +020092 Timestamp at_time);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +000093
Sebastian Jansson439f0bc2018-02-20 10:46:39 +010094 // Call when we receive a RTCP message with a ReceiveBlock.
95 void UpdatePacketsLost(int packets_lost,
96 int number_of_packets,
Sebastian Jansson7c1744d2018-10-08 11:00:50 +020097 Timestamp at_time);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +010098
99 // Call when we receive a RTCP message with a ReceiveBlock.
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200100 void UpdateRtt(TimeDelta rtt, Timestamp at_time);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100101
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200102 void SetBitrates(absl::optional<DataRate> send_bitrate,
103 DataRate min_bitrate,
104 DataRate max_bitrate,
105 Timestamp at_time);
106 void SetSendBitrate(DataRate bitrate, Timestamp at_time);
107 void SetMinMaxBitrate(DataRate min_bitrate, DataRate max_bitrate);
Stefan Holmere5904162015-03-26 11:11:06 +0100108 int GetMinBitrate() const;
Sebastian Jansson57f3ad02018-11-23 14:49:18 +0100109 void SetAcknowledgedRate(absl::optional<DataRate> acknowledged_rate,
110 Timestamp at_time);
111 void IncomingPacketFeedbackVector(const TransportPacketsFeedback& report);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000112
113 private:
stefan@webrtc.orgdb262472014-11-04 19:32:10 +0000114 enum UmaState { kNoUpdate, kFirstDone, kDone };
115
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200116 bool IsInStartPhase(Timestamp at_time) const;
stefan@webrtc.org548b2282014-11-03 14:42:43 +0000117
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200118 void UpdateUmaStatsPacketsLost(Timestamp at_time, int packets_lost);
stefan@webrtc.orgdb262472014-11-04 19:32:10 +0000119
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000120 // Updates history of min bitrates.
121 // After this method returns min_bitrate_history_.front().second contains the
122 // min bitrate used during last kBweIncreaseIntervalMs.
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200123 void UpdateMinHistory(Timestamp at_time);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000124
Christoffer Rodbro3a837482018-11-19 15:30:23 +0100125 DataRate MaybeRampupOrBackoff(DataRate new_bitrate, Timestamp at_time);
126
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200127 // Cap |bitrate| to [min_bitrate_configured_, max_bitrate_configured_] and
128 // set |current_bitrate_| to the capped value and updates the event log.
129 void CapBitrateToThresholds(Timestamp at_time, DataRate bitrate);
philipel1b965312017-04-18 06:55:32 -0700130
Sebastian Jansson24643482018-11-14 14:19:45 +0100131 RttBasedBackoff rtt_backoff_;
Sebastian Jansson57f3ad02018-11-23 14:49:18 +0100132 LinkCapacityTracker link_capacity_;
Sebastian Jansson2e068e82018-10-08 12:49:53 +0200133
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200134 std::deque<std::pair<Timestamp, DataRate> > min_bitrate_history_;
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000135
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000136 // incoming filters
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100137 int lost_packets_since_last_loss_update_;
pbosb7edb882015-10-22 08:52:20 -0700138 int expected_packets_since_last_loss_update_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000139
Sebastian Jansson57f3ad02018-11-23 14:49:18 +0100140 absl::optional<DataRate> acknowledged_rate_;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200141 DataRate current_bitrate_;
142 DataRate min_bitrate_configured_;
143 DataRate max_bitrate_configured_;
144 Timestamp last_low_bitrate_log_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000145
pbosb7edb882015-10-22 08:52:20 -0700146 bool has_decreased_since_last_fraction_loss_;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200147 Timestamp last_loss_feedback_;
148 Timestamp last_loss_packet_report_;
149 Timestamp last_timeout_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000150 uint8_t last_fraction_loss_;
stefan3821ff82016-09-04 05:07:26 -0700151 uint8_t last_logged_fraction_loss_;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200152 TimeDelta last_round_trip_time_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000153
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200154 DataRate bwe_incoming_;
155 DataRate delay_based_bitrate_;
156 Timestamp time_last_decrease_;
157 Timestamp first_report_time_;
stefan@webrtc.org548b2282014-11-03 14:42:43 +0000158 int initially_lost_packets_;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200159 DataRate bitrate_at_2_seconds_;
stefan@webrtc.orgdb262472014-11-04 19:32:10 +0000160 UmaState uma_update_state_;
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100161 UmaState uma_rtt_state_;
stefan@webrtc.org474e36e2015-01-19 15:44:47 +0000162 std::vector<bool> rampup_uma_stats_updated_;
terelius006d93d2015-11-05 12:02:15 -0800163 RtcEventLog* event_log_;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200164 Timestamp last_rtc_event_log_;
Stefan Holmer52200d02016-09-20 14:14:23 +0200165 bool in_timeout_experiment_;
Stefan Holmer9c79ed92017-03-31 15:53:27 +0200166 float low_loss_threshold_;
167 float high_loss_threshold_;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200168 DataRate bitrate_threshold_;
Christoffer Rodbro3a837482018-11-19 15:30:23 +0100169 LossBasedBandwidthEstimation loss_based_bandwidth_estimation_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000170};
171} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200172#endif // MODULES_BITRATE_CONTROLLER_SEND_SIDE_BANDWIDTH_ESTIMATION_H_