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 "modules/remote_bitrate_estimator/aimd_rate_control.h"
25 #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
26 #include "modules/remote_bitrate_estimator/inter_arrival.h"
27 #include "modules/remote_bitrate_estimator/overuse_detector.h"
28 #include "modules/remote_bitrate_estimator/overuse_estimator.h"
29 #include "rtc_base/checks.h"
30 #include "rtc_base/constructor_magic.h"
31 #include "rtc_base/race_checker.h"
32 #include "rtc_base/rate_statistics.h"
33 #include "rtc_base/synchronization/mutex.h"
34 #include "rtc_base/thread_annotations.h"
35 #include "system_wrappers/include/clock.h"
36 
37 namespace webrtc {
38 
39 struct Probe {
ProbeProbe40   Probe(int64_t send_time_ms, int64_t recv_time_ms, size_t payload_size)
41       : send_time_ms(send_time_ms),
42         recv_time_ms(recv_time_ms),
43         payload_size(payload_size) {}
44   int64_t send_time_ms;
45   int64_t recv_time_ms;
46   size_t payload_size;
47 };
48 
49 struct Cluster {
ClusterCluster50   Cluster()
51       : send_mean_ms(0.0f),
52         recv_mean_ms(0.0f),
53         mean_size(0),
54         count(0),
55         num_above_min_delta(0) {}
56 
GetSendBitrateBpsCluster57   int GetSendBitrateBps() const {
58     RTC_CHECK_GT(send_mean_ms, 0.0f);
59     return mean_size * 8 * 1000 / send_mean_ms;
60   }
61 
GetRecvBitrateBpsCluster62   int GetRecvBitrateBps() const {
63     RTC_CHECK_GT(recv_mean_ms, 0.0f);
64     return mean_size * 8 * 1000 / recv_mean_ms;
65   }
66 
67   float send_mean_ms;
68   float recv_mean_ms;
69   // TODO(holmer): Add some variance metric as well?
70   size_t mean_size;
71   int count;
72   int num_above_min_delta;
73 };
74 
75 class RemoteBitrateEstimatorAbsSendTime : public RemoteBitrateEstimator {
76  public:
77   RemoteBitrateEstimatorAbsSendTime(RemoteBitrateObserver* observer,
78                                     Clock* clock);
79   ~RemoteBitrateEstimatorAbsSendTime() override;
80 
81   void IncomingPacket(int64_t arrival_time_ms,
82                       size_t payload_size,
83                       const RTPHeader& header) override;
84   // This class relies on Process() being called periodically (at least once
85   // every other second) for streams to be timed out properly. Therefore it
86   // shouldn't be detached from the ProcessThread except if it's about to be
87   // deleted.
88   void Process() override;
89   int64_t TimeUntilNextProcess() override;
90   void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
91   void RemoveStream(uint32_t ssrc) override;
92   bool LatestEstimate(std::vector<uint32_t>* ssrcs,
93                       uint32_t* bitrate_bps) const override;
94   void SetMinBitrate(int min_bitrate_bps) override;
95 
96  private:
97   typedef std::map<uint32_t, int64_t> Ssrcs;
98   enum class ProbeResult { kBitrateUpdated, kNoUpdate };
99 
100   static bool IsWithinClusterBounds(int send_delta_ms,
101                                     const Cluster& cluster_aggregate);
102 
103   static void AddCluster(std::list<Cluster>* clusters, Cluster* cluster);
104 
105   void IncomingPacketInfo(int64_t arrival_time_ms,
106                           uint32_t send_time_24bits,
107                           size_t payload_size,
108                           uint32_t ssrc);
109 
110   void ComputeClusters(std::list<Cluster>* clusters) const;
111 
112   std::list<Cluster>::const_iterator FindBestProbe(
113       const std::list<Cluster>& clusters) const;
114 
115   // Returns true if a probe which changed the estimate was detected.
116   ProbeResult ProcessClusters(int64_t now_ms)
117       RTC_EXCLUSIVE_LOCKS_REQUIRED(&mutex_);
118 
119   bool IsBitrateImproving(int probe_bitrate_bps) const
120       RTC_EXCLUSIVE_LOCKS_REQUIRED(&mutex_);
121 
122   void TimeoutStreams(int64_t now_ms) RTC_EXCLUSIVE_LOCKS_REQUIRED(&mutex_);
123 
124   rtc::RaceChecker network_race_;
125   Clock* const clock_;
126   const FieldTrialBasedConfig field_trials_;
127   RemoteBitrateObserver* const observer_;
128   std::unique_ptr<InterArrival> inter_arrival_;
129   std::unique_ptr<OveruseEstimator> estimator_;
130   OveruseDetector detector_;
131   RateStatistics incoming_bitrate_;
132   bool incoming_bitrate_initialized_;
133   std::vector<int> recent_propagation_delta_ms_;
134   std::vector<int64_t> recent_update_time_ms_;
135   std::list<Probe> probes_;
136   size_t total_probes_received_;
137   int64_t first_packet_time_ms_;
138   int64_t last_update_ms_;
139   bool uma_recorded_;
140 
141   mutable Mutex mutex_;
142   Ssrcs ssrcs_ RTC_GUARDED_BY(&mutex_);
143   AimdRateControl remote_rate_ RTC_GUARDED_BY(&mutex_);
144 
145   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RemoteBitrateEstimatorAbsSendTime);
146 };
147 
148 }  // namespace webrtc
149 
150 #endif  // MODULES_REMOTE_BITRATE_ESTIMATOR_REMOTE_BITRATE_ESTIMATOR_ABS_SEND_TIME_H_
151