1 /*
2  *  Copyright (c) 2015 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 MODULES_REMOTE_BITRATE_ESTIMATOR_REMOTE_BITRATE_ESTIMATOR_ABS_SEND_TIME_H_
12 #define MODULES_REMOTE_BITRATE_ESTIMATOR_REMOTE_BITRATE_ESTIMATOR_ABS_SEND_TIME_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <list>
18 #include <map>
19 #include <memory>
20 #include <vector>
21 
22 #include "api/rtp_headers.h"
23 #include "api/transport/field_trial_based_config.h"
24 #include "api/units/data_rate.h"
25 #include "api/units/data_size.h"
26 #include "api/units/time_delta.h"
27 #include "api/units/timestamp.h"
28 #include "modules/remote_bitrate_estimator/aimd_rate_control.h"
29 #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
30 #include "modules/remote_bitrate_estimator/inter_arrival.h"
31 #include "modules/remote_bitrate_estimator/overuse_detector.h"
32 #include "modules/remote_bitrate_estimator/overuse_estimator.h"
33 #include "rtc_base/checks.h"
34 #include "rtc_base/race_checker.h"
35 #include "rtc_base/rate_statistics.h"
36 #include "rtc_base/synchronization/mutex.h"
37 #include "rtc_base/thread_annotations.h"
38 #include "system_wrappers/include/clock.h"
39 
40 namespace webrtc {
41 
42 class RemoteBitrateEstimatorAbsSendTime : public RemoteBitrateEstimator {
43  public:
44   RemoteBitrateEstimatorAbsSendTime(RemoteBitrateObserver* observer,
45                                     Clock* clock);
46 
47   RemoteBitrateEstimatorAbsSendTime() = delete;
48   RemoteBitrateEstimatorAbsSendTime(const RemoteBitrateEstimatorAbsSendTime&) =
49       delete;
50   RemoteBitrateEstimatorAbsSendTime& operator=(
51       const RemoteBitrateEstimatorAbsSendTime&) = delete;
52 
53   ~RemoteBitrateEstimatorAbsSendTime() override;
54 
55   void IncomingPacket(int64_t arrival_time_ms,
56                       size_t payload_size,
57                       const RTPHeader& header) override;
58   TimeDelta Process() override;
59   void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
60   void RemoveStream(uint32_t ssrc) override;
61   DataRate LatestEstimate() const override;
62 
63  private:
64   struct Probe {
ProbeProbe65     Probe(Timestamp send_time, Timestamp recv_time, DataSize payload_size)
66         : send_time(send_time),
67           recv_time(recv_time),
68           payload_size(payload_size) {}
69 
70     Timestamp send_time;
71     Timestamp recv_time;
72     DataSize payload_size;
73   };
74 
75   struct Cluster {
SendBitrateCluster76     DataRate SendBitrate() const { return mean_size / send_mean; }
RecvBitrateCluster77     DataRate RecvBitrate() const { return mean_size / recv_mean; }
78 
79     TimeDelta send_mean = TimeDelta::Zero();
80     TimeDelta recv_mean = TimeDelta::Zero();
81     // TODO(holmer): Add some variance metric as well?
82     DataSize mean_size = DataSize::Zero();
83     int count = 0;
84     int num_above_min_delta = 0;
85   };
86 
87   enum class ProbeResult { kBitrateUpdated, kNoUpdate };
88 
89   static bool IsWithinClusterBounds(TimeDelta send_delta,
90                                     const Cluster& cluster_aggregate);
91 
92   static void MaybeAddCluster(const Cluster& cluster_aggregate,
93                               std::list<Cluster>& clusters);
94 
95   void IncomingPacketInfo(Timestamp arrival_time,
96                           uint32_t send_time_24bits,
97                           DataSize payload_size,
98                           uint32_t ssrc);
99 
100   std::list<Cluster> ComputeClusters() const;
101 
102   const Cluster* FindBestProbe(const std::list<Cluster>& clusters) const;
103 
104   // Returns true if a probe which changed the estimate was detected.
105   ProbeResult ProcessClusters(Timestamp now)
106       RTC_EXCLUSIVE_LOCKS_REQUIRED(&mutex_);
107 
108   bool IsBitrateImproving(DataRate probe_bitrate) const
109       RTC_EXCLUSIVE_LOCKS_REQUIRED(&mutex_);
110 
111   void TimeoutStreams(Timestamp now) RTC_EXCLUSIVE_LOCKS_REQUIRED(&mutex_);
112 
113   rtc::RaceChecker network_race_;
114   Clock* const clock_;
115   const FieldTrialBasedConfig field_trials_;
116   RemoteBitrateObserver* const observer_;
117   std::unique_ptr<InterArrival> inter_arrival_;
118   std::unique_ptr<OveruseEstimator> estimator_;
119   OveruseDetector detector_;
120   RateStatistics incoming_bitrate_{kBitrateWindowMs, 8000};
121   bool incoming_bitrate_initialized_ = false;
122   std::list<Probe> probes_;
123   size_t total_probes_received_ = 0;
124   Timestamp first_packet_time_ = Timestamp::MinusInfinity();
125   Timestamp last_update_ = Timestamp::MinusInfinity();
126   bool uma_recorded_ = false;
127 
128   mutable Mutex mutex_;
129   std::map<uint32_t, Timestamp> ssrcs_ RTC_GUARDED_BY(&mutex_);
130   AimdRateControl remote_rate_ RTC_GUARDED_BY(&mutex_);
131 };
132 
133 }  // namespace webrtc
134 
135 #endif  // MODULES_REMOTE_BITRATE_ESTIMATOR_REMOTE_BITRATE_ESTIMATOR_ABS_SEND_TIME_H_
136