blob: c1c68c28af33266f1afaa3e86a303931cdb845be [file] [log] [blame]
philipel863a8262016-06-17 09:21:34 -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#ifndef WEBRTC_MODULES_CONGESTION_CONTROLLER_DELAY_BASED_BWE_H_
12#define WEBRTC_MODULES_CONGESTION_CONTROLLER_DELAY_BASED_BWE_H_
13
philipel863a8262016-06-17 09:21:34 -070014#include <memory>
Stefan Holmer492ee282016-10-27 17:19:20 +020015#include <utility>
philipel863a8262016-06-17 09:21:34 -070016#include <vector>
17
18#include "webrtc/base/checks.h"
19#include "webrtc/base/constructormagic.h"
philipel863a8262016-06-17 09:21:34 -070020#include "webrtc/base/thread_checker.h"
terelius5a388362016-12-09 05:50:01 -080021#include "webrtc/modules/congestion_controller/median_slope_estimator.h"
philipel7522a282016-08-16 10:59:36 +020022#include "webrtc/modules/congestion_controller/probe_bitrate_estimator.h"
tereliusafaef8b2016-11-17 03:48:18 -080023#include "webrtc/modules/congestion_controller/trendline_estimator.h"
philipel863a8262016-06-17 09:21:34 -070024#include "webrtc/modules/remote_bitrate_estimator/aimd_rate_control.h"
25#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
26#include "webrtc/modules/remote_bitrate_estimator/inter_arrival.h"
27#include "webrtc/modules/remote_bitrate_estimator/overuse_detector.h"
28#include "webrtc/modules/remote_bitrate_estimator/overuse_estimator.h"
philipel863a8262016-06-17 09:21:34 -070029
30namespace webrtc {
31
terelius0baf55d2017-02-17 03:38:28 -080032class RtcEventLog;
33
Stefan Holmer280de9e2016-09-30 10:06:51 +020034class DelayBasedBwe {
philipel863a8262016-06-17 09:21:34 -070035 public:
Stefan Holmer280de9e2016-09-30 10:06:51 +020036 static const int64_t kStreamTimeOutMs = 2000;
37
38 struct Result {
39 Result() : updated(false), probe(false), target_bitrate_bps(0) {}
40 Result(bool probe, uint32_t target_bitrate_bps)
41 : updated(true), probe(probe), target_bitrate_bps(target_bitrate_bps) {}
42 bool updated;
43 bool probe;
44 uint32_t target_bitrate_bps;
45 };
46
elad.alon61ce37e2017-03-09 07:09:31 -080047 DelayBasedBwe(RtcEventLog* event_log, const Clock* clock);
philipel863a8262016-06-17 09:21:34 -070048 virtual ~DelayBasedBwe() {}
49
Stefan Holmer280de9e2016-09-30 10:06:51 +020050 Result IncomingPacketFeedbackVector(
elad.alonf9490002017-03-06 05:32:21 -080051 const std::vector<PacketFeedback>& packet_feedback_vector);
Stefan Holmer280de9e2016-09-30 10:06:51 +020052 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms);
philipel7522a282016-08-16 10:59:36 +020053 bool LatestEstimate(std::vector<uint32_t>* ssrcs,
Stefan Holmer280de9e2016-09-30 10:06:51 +020054 uint32_t* bitrate_bps) const;
stefan5a2c5062017-01-27 06:43:18 -080055 void SetStartBitrate(int start_bitrate_bps);
Stefan Holmer280de9e2016-09-30 10:06:51 +020056 void SetMinBitrate(int min_bitrate_bps);
terelius67370452017-04-19 09:15:04 -070057 int64_t GetExpectedBwePeriodMs() const;
philipel863a8262016-06-17 09:21:34 -070058
philipel863a8262016-06-17 09:21:34 -070059 private:
Stefan Holmer492ee282016-10-27 17:19:20 +020060 // Computes a bayesian estimate of the throughput given acks containing
61 // the arrival time and payload size. Samples which are far from the current
62 // estimate or are based on few packets are given a smaller weight, as they
63 // are considered to be more likely to have been caused by, e.g., delay spikes
64 // unrelated to congestion.
65 class BitrateEstimator {
66 public:
67 BitrateEstimator();
68 void Update(int64_t now_ms, int bytes);
69 rtc::Optional<uint32_t> bitrate_bps() const;
70
71 private:
72 float UpdateWindow(int64_t now_ms, int bytes, int rate_window_ms);
73 int sum_;
74 int64_t current_win_ms_;
75 int64_t prev_time_ms_;
76 float bitrate_estimate_;
77 float bitrate_estimate_var_;
Stefan Holmer492ee282016-10-27 17:19:20 +020078 };
79
elad.alonf9490002017-03-06 05:32:21 -080080 Result IncomingPacketFeedback(const PacketFeedback& packet_feedback);
stefane3a55672017-02-13 09:08:22 -080081 Result OnLongFeedbackDelay(int64_t arrival_time_ms);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070082 // Updates the current remote rate estimate and returns true if a valid
83 // estimate exists.
84 bool UpdateEstimate(int64_t packet_arrival_time_ms,
85 int64_t now_ms,
Stefan Holmer492ee282016-10-27 17:19:20 +020086 rtc::Optional<uint32_t> acked_bitrate_bps,
Stefan Holmer280de9e2016-09-30 10:06:51 +020087 uint32_t* target_bitrate_bps);
philipel863a8262016-06-17 09:21:34 -070088
89 rtc::ThreadChecker network_thread_;
terelius0baf55d2017-02-17 03:38:28 -080090 RtcEventLog* const event_log_;
elad.alon61ce37e2017-03-09 07:09:31 -080091 const Clock* const clock_;
philipel863a8262016-06-17 09:21:34 -070092 std::unique_ptr<InterArrival> inter_arrival_;
tereliusafaef8b2016-11-17 03:48:18 -080093 std::unique_ptr<TrendlineEstimator> trendline_estimator_;
philipel863a8262016-06-17 09:21:34 -070094 OveruseDetector detector_;
Stefan Holmer492ee282016-10-27 17:19:20 +020095 BitrateEstimator receiver_incoming_bitrate_;
philipel863a8262016-06-17 09:21:34 -070096 int64_t last_update_ms_;
philipel7522a282016-08-16 10:59:36 +020097 int64_t last_seen_packet_ms_;
stefan64636dd2016-08-03 00:29:03 -070098 bool uma_recorded_;
terelius6ed592d2016-10-18 05:55:30 -070099 AimdRateControl rate_control_;
Stefan Holmer280de9e2016-09-30 10:06:51 +0200100 ProbeBitrateEstimator probe_bitrate_estimator_;
tereliusafaef8b2016-11-17 03:48:18 -0800101 size_t trendline_window_size_;
102 double trendline_smoothing_coeff_;
103 double trendline_threshold_gain_;
stefane3a55672017-02-13 09:08:22 -0800104 int consecutive_delayed_feedbacks_;
terelius0baf55d2017-02-17 03:38:28 -0800105 uint32_t last_logged_bitrate_;
106 BandwidthUsage last_logged_state_;
philipel863a8262016-06-17 09:21:34 -0700107
108 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(DelayBasedBwe);
109};
110
111} // namespace webrtc
112
113#endif // WEBRTC_MODULES_CONGESTION_CONTROLLER_DELAY_BASED_BWE_H_