blob: 56214de3e5f4773285d80f4ba0c55ac28134cb30 [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:
35 explicit DelayBasedBwe(RemoteBitrateObserver* observer);
36 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,
pbos2169d8b2016-06-20 11:53:02 -070043 const RTPHeader& header) override;
philipel863a8262016-06-17 09:21:34 -070044
45 void IncomingPacket(int64_t arrival_time_ms,
46 size_t payload_size,
47 const RTPHeader& header,
philipel863a8262016-06-17 09:21:34 -070048 int probe_cluster_id);
49
50 // This class relies on Process() being called periodically (at least once
51 // every other second) for streams to be timed out properly. Therefore it
52 // shouldn't be detached from the ProcessThread except if it's about to be
53 // deleted.
54 void Process() override;
55 int64_t TimeUntilNextProcess() override;
56 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
57 void RemoveStream(uint32_t ssrc) override;
58 bool LatestEstimate(std::vector<uint32_t>* ssrcs,
59 uint32_t* bitrate_bps) const override;
60 void SetMinBitrate(int min_bitrate_bps) override;
61
62 private:
63 struct Probe {
64 Probe(int64_t send_time_ms,
65 int64_t recv_time_ms,
66 size_t payload_size,
67 int cluster_id)
68 : send_time_ms(send_time_ms),
69 recv_time_ms(recv_time_ms),
70 payload_size(payload_size),
71 cluster_id(cluster_id) {}
72 int64_t send_time_ms;
73 int64_t recv_time_ms;
74 size_t payload_size;
75 int cluster_id;
76 };
77
78 struct Cluster {
79 Cluster()
80 : send_mean_ms(0.0f),
81 recv_mean_ms(0.0f),
82 mean_size(0),
83 count(0),
84 num_above_min_delta(0) {}
85
86 int GetSendBitrateBps() const {
87 RTC_CHECK_GT(send_mean_ms, 0.0f);
88 return mean_size * 8 * 1000 / send_mean_ms;
89 }
90
91 int GetRecvBitrateBps() const {
92 RTC_CHECK_GT(recv_mean_ms, 0.0f);
93 return mean_size * 8 * 1000 / recv_mean_ms;
94 }
95
96 float send_mean_ms;
97 float recv_mean_ms;
98 // TODO(holmer): Add some variance metric as well?
99 size_t mean_size;
100 int count;
101 int num_above_min_delta;
102 };
103
104 typedef std::map<uint32_t, int64_t> Ssrcs;
105 enum class ProbeResult { kBitrateUpdated, kNoUpdate };
106
107 static void AddCluster(std::list<Cluster>* clusters, Cluster* cluster);
108
109 void IncomingPacketInfo(int64_t arrival_time_ms,
110 uint32_t send_time_24bits,
111 size_t payload_size,
112 uint32_t ssrc,
philipel863a8262016-06-17 09:21:34 -0700113 int probe_cluster_id);
114
115 void ComputeClusters(std::list<Cluster>* clusters) const;
116
117 std::list<Cluster>::const_iterator FindBestProbe(
118 const std::list<Cluster>& clusters) const;
119
120 // Returns true if a probe which changed the estimate was detected.
121 ProbeResult ProcessClusters(int64_t now_ms) EXCLUSIVE_LOCKS_REQUIRED(&crit_);
122
123 bool IsBitrateImproving(int probe_bitrate_bps) const
124 EXCLUSIVE_LOCKS_REQUIRED(&crit_);
125
126 void TimeoutStreams(int64_t now_ms) EXCLUSIVE_LOCKS_REQUIRED(&crit_);
127
128 rtc::ThreadChecker network_thread_;
129 RemoteBitrateObserver* const observer_;
130 std::unique_ptr<InterArrival> inter_arrival_;
131 std::unique_ptr<OveruseEstimator> estimator_;
132 OveruseDetector detector_;
133 RateStatistics incoming_bitrate_;
134 std::vector<int> recent_propagation_delta_ms_;
135 std::vector<int64_t> recent_update_time_ms_;
136 std::list<Probe> probes_;
137 size_t total_probes_received_;
138 int64_t first_packet_time_ms_;
139 int64_t last_update_ms_;
140
141 rtc::CriticalSection crit_;
142 Ssrcs ssrcs_ GUARDED_BY(&crit_);
143 AimdRateControl remote_rate_ GUARDED_BY(&crit_);
144
145 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(DelayBasedBwe);
146};
147
148} // namespace webrtc
149
150#endif // WEBRTC_MODULES_CONGESTION_CONTROLLER_DELAY_BASED_BWE_H_