blob: 4e34aa13c6c57401c1c67affe51db788eadb1c28 [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
14#include <list>
15#include <map>
16#include <memory>
17#include <vector>
18
19#include "webrtc/base/checks.h"
20#include "webrtc/base/constructormagic.h"
21#include "webrtc/base/criticalsection.h"
22#include "webrtc/base/rate_statistics.h"
23#include "webrtc/base/thread_checker.h"
24#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"
29#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
30
31namespace webrtc {
32
33class DelayBasedBwe : public RemoteBitrateEstimator {
34 public:
stefan5e12d362016-07-11 01:44:02 -070035 DelayBasedBwe(RemoteBitrateObserver* observer, Clock* clock);
philipel863a8262016-06-17 09:21:34 -070036 virtual ~DelayBasedBwe() {}
37
38 void IncomingPacketFeedbackVector(
39 const std::vector<PacketInfo>& packet_feedback_vector) override;
40
41 void IncomingPacket(int64_t arrival_time_ms,
42 size_t payload_size,
stefan5e12d362016-07-11 01:44:02 -070043 const RTPHeader& header) override {
44 RTC_NOTREACHED();
45 }
philipel863a8262016-06-17 09:21:34 -070046
47 // This class relies on Process() being called periodically (at least once
48 // every other second) for streams to be timed out properly. Therefore it
49 // shouldn't be detached from the ProcessThread except if it's about to be
50 // deleted.
51 void Process() override;
52 int64_t TimeUntilNextProcess() override;
53 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
54 void RemoveStream(uint32_t ssrc) override;
55 bool LatestEstimate(std::vector<uint32_t>* ssrcs,
56 uint32_t* bitrate_bps) const override;
57 void SetMinBitrate(int min_bitrate_bps) override;
58
59 private:
60 struct Probe {
61 Probe(int64_t send_time_ms,
62 int64_t recv_time_ms,
63 size_t payload_size,
64 int cluster_id)
65 : send_time_ms(send_time_ms),
66 recv_time_ms(recv_time_ms),
67 payload_size(payload_size),
68 cluster_id(cluster_id) {}
69 int64_t send_time_ms;
70 int64_t recv_time_ms;
71 size_t payload_size;
72 int cluster_id;
73 };
74
75 struct Cluster {
76 Cluster()
77 : send_mean_ms(0.0f),
78 recv_mean_ms(0.0f),
79 mean_size(0),
80 count(0),
81 num_above_min_delta(0) {}
82
83 int GetSendBitrateBps() const {
84 RTC_CHECK_GT(send_mean_ms, 0.0f);
85 return mean_size * 8 * 1000 / send_mean_ms;
86 }
87
88 int GetRecvBitrateBps() const {
89 RTC_CHECK_GT(recv_mean_ms, 0.0f);
90 return mean_size * 8 * 1000 / recv_mean_ms;
91 }
92
93 float send_mean_ms;
94 float recv_mean_ms;
95 // TODO(holmer): Add some variance metric as well?
96 size_t mean_size;
97 int count;
98 int num_above_min_delta;
99 };
100
101 typedef std::map<uint32_t, int64_t> Ssrcs;
102 enum class ProbeResult { kBitrateUpdated, kNoUpdate };
103
104 static void AddCluster(std::list<Cluster>* clusters, Cluster* cluster);
105
106 void IncomingPacketInfo(int64_t arrival_time_ms,
107 uint32_t send_time_24bits,
108 size_t payload_size,
109 uint32_t ssrc,
philipel863a8262016-06-17 09:21:34 -0700110 int probe_cluster_id);
111
112 void ComputeClusters(std::list<Cluster>* clusters) const;
113
114 std::list<Cluster>::const_iterator FindBestProbe(
115 const std::list<Cluster>& clusters) const;
116
117 // Returns true if a probe which changed the estimate was detected.
118 ProbeResult ProcessClusters(int64_t now_ms) EXCLUSIVE_LOCKS_REQUIRED(&crit_);
119
120 bool IsBitrateImproving(int probe_bitrate_bps) const
121 EXCLUSIVE_LOCKS_REQUIRED(&crit_);
122
123 void TimeoutStreams(int64_t now_ms) EXCLUSIVE_LOCKS_REQUIRED(&crit_);
124
125 rtc::ThreadChecker network_thread_;
stefan5e12d362016-07-11 01:44:02 -0700126 Clock* const clock_;
philipel863a8262016-06-17 09:21:34 -0700127 RemoteBitrateObserver* const observer_;
128 std::unique_ptr<InterArrival> inter_arrival_;
129 std::unique_ptr<OveruseEstimator> estimator_;
130 OveruseDetector detector_;
131 RateStatistics incoming_bitrate_;
philipel863a8262016-06-17 09:21:34 -0700132 std::list<Probe> probes_;
133 size_t total_probes_received_;
134 int64_t first_packet_time_ms_;
135 int64_t last_update_ms_;
stefan64636dd2016-08-03 00:29:03 -0700136 bool uma_recorded_;
philipel863a8262016-06-17 09:21:34 -0700137
138 rtc::CriticalSection crit_;
139 Ssrcs ssrcs_ GUARDED_BY(&crit_);
140 AimdRateControl remote_rate_ GUARDED_BY(&crit_);
141
142 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(DelayBasedBwe);
143};
144
145} // namespace webrtc
146
147#endif // WEBRTC_MODULES_CONGESTION_CONTROLLER_DELAY_BASED_BWE_H_